-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mode): support docs view mode (#27)
- Loading branch information
Showing
4 changed files
with
60 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
module.exports = { | ||
addons: ['@storybook/addon-storysource', 'storybook-addon-paddings'], | ||
addons: [ | ||
'@storybook/addon-docs', | ||
'@storybook/addon-storysource', | ||
'storybook-addon-paddings', | ||
], | ||
stories: ['../src/**/*.stories.js'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,66 @@ | ||
import { StoryWrapper, useMemo } from '@storybook/addons'; | ||
import { StoryWrapper, useEffect, useMemo } from '@storybook/addons'; | ||
|
||
import { DEFAULT_PADDING, PARAM_KEY } from './constants'; | ||
import { normalizeValues, getSelectedPadding } from './helpers'; | ||
|
||
const setStyle = (padding = DEFAULT_PADDING) => { | ||
document.body.style.margin = '0'; | ||
document.body.style.padding = padding; | ||
document.body.style.transition = 'padding .3s'; | ||
const setStyle = (selector: string, css: string) => { | ||
const existingStyle = document.getElementById(selector); | ||
|
||
if (existingStyle) { | ||
if (existingStyle.innerHTML !== css) { | ||
existingStyle.innerHTML = css; | ||
} | ||
} else { | ||
const style = document.createElement('style'); | ||
style.setAttribute('id', selector); | ||
style.innerHTML = css; | ||
|
||
document.head.appendChild(style); | ||
} | ||
}; | ||
|
||
export const withPaddings: StoryWrapper = (getStory, context) => { | ||
const { globals, parameters } = context; | ||
const WithPaddings: StoryWrapper = (getStory, context) => { | ||
const { id, globals, parameters, viewMode } = context; | ||
const globalsSelectedPadding = globals[PARAM_KEY]?.value; | ||
const paddingsConfig = parameters[PARAM_KEY]; | ||
const isInDocs = viewMode === 'docs'; | ||
const selector = isInDocs | ||
? `#anchor--${id} .docs-story > div` | ||
: '.sb-show-main'; | ||
|
||
const selectedPadding = useMemo( | ||
() => | ||
getSelectedPadding( | ||
normalizeValues(paddingsConfig), | ||
globalsSelectedPadding, | ||
) ?? DEFAULT_PADDING, | ||
[paddingsConfig, globalsSelectedPadding], | ||
); | ||
|
||
const paddingStyles = useMemo( | ||
() => ` | ||
${selector} { | ||
margin: 0; | ||
padding: ${selectedPadding} !important; | ||
transition: padding .3s; | ||
} | ||
${selector} .innerZoomElementWrapper > div { | ||
border-width: 0 !important; | ||
} | ||
`, | ||
[selector, selectedPadding], | ||
); | ||
|
||
useEffect(() => { | ||
const selectorId = isInDocs | ||
? `addon-paddings-docs-${id}` | ||
: `addon-paddings`; | ||
|
||
// eslint-disable-next-line react-hooks/rules-of-hooks | ||
useMemo(() => { | ||
const selectedPadding = getSelectedPadding( | ||
normalizeValues(paddingsConfig), | ||
globalsSelectedPadding, | ||
); | ||
setStyle(selectedPadding); | ||
}, [paddingsConfig, globalsSelectedPadding]); | ||
setStyle(selectorId, paddingStyles); | ||
}, [id, isInDocs, paddingStyles]); | ||
|
||
return getStory(context); | ||
}; | ||
|
||
export default withPaddings; | ||
export default WithPaddings; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters