-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(storybook): add theme switcher to storybook (#199)
* feat: 🎸 add theme switcher to storybook * feat: 🎸 disable theme switcher by default
- Loading branch information
Showing
14 changed files
with
133 additions
and
11 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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
@import '../../../packages/themes/src/mixins/click.css'; | ||
|
||
.tool { | ||
margin-top: auto; | ||
margin-bottom: auto; | ||
} | ||
|
||
.select { | ||
background: red; | ||
} | ||
|
||
.click { | ||
@mixin theme-click; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import addons, { makeDecorator } from '@storybook/addons'; | ||
import { ADDON_ID } from './register'; | ||
|
||
import styles from './index.css'; | ||
|
||
export default makeDecorator({ | ||
name: 'withThemeSwitcher', | ||
wrapper: (getStory, context) => { | ||
const [theme, setTheme] = useState(); | ||
|
||
const channel = addons.getChannel(); | ||
|
||
useEffect(() => { | ||
channel.on(`${ADDON_ID}/theme`, setTheme); | ||
|
||
return () => { | ||
channel.removeListener(`${ADDON_ID}/theme`, setTheme); | ||
}; | ||
}, []); | ||
|
||
return <div className={styles[theme]}>{getStory(context)}</div>; | ||
}, | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import React, { useCallback, useState } from 'react'; | ||
import addons, { types } from '@storybook/addons'; | ||
import { useChannel, useParameter } from '@storybook/api'; | ||
import { STORY_RENDERED } from '@storybook/core-events'; | ||
import { Form } from '@storybook/components'; | ||
|
||
import './index.css'; | ||
|
||
export const ADDON_ID = 'theme-switcher'; | ||
export const THEMES = ['default', 'click']; | ||
|
||
const Addon = () => { | ||
const [theme, setTheme] = useState('default'); | ||
const { enabled } = useParameter(ADDON_ID, { enabled: false }); | ||
|
||
const emit = useChannel({ | ||
[STORY_RENDERED]: () => emit(`${ADDON_ID}/theme`, theme), | ||
}); | ||
|
||
const handleChange = useCallback(event => { | ||
setTheme(event.target.value); | ||
emit(`${ADDON_ID}/theme`, event.target.value); | ||
}, []); | ||
|
||
return ( | ||
enabled && ( | ||
<div className='tool'> | ||
<Form.Select size={1} onChange={handleChange} className='select'> | ||
{THEMES.map(themeName => ( | ||
<option value={themeName} key={themeName}> | ||
Тема: {themeName} | ||
</option> | ||
))} | ||
</Form.Select> | ||
</div> | ||
) | ||
); | ||
}; | ||
|
||
addons.register(ADDON_ID, () => { | ||
addons.add(ADDON_ID, { | ||
type: types.TOOL, | ||
match: ({ viewMode }) => viewMode === 'story', | ||
render: () => <Addon />, | ||
paramKey: ADDON_ID, | ||
}); | ||
}); |
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
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
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
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
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
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,11 +1,11 @@ | ||
@import './amount/click.css'; | ||
@import './input/click.css'; | ||
@import './form-control/click.css'; | ||
@import './pure-input/click.css'; | ||
@import './button/click.css'; | ||
|
||
@define-mixin theme-click { | ||
@mixin amount-click; | ||
@mixin input-click; | ||
@mixin form-control-click; | ||
@mixin pure-input-click; | ||
@mixin button-click; | ||
} |