Skip to content

Commit

Permalink
feat: improve theme switcher (#386)
Browse files Browse the repository at this point in the history
  • Loading branch information
reme3d2y committed Nov 27, 2020
1 parent 9ec6d90 commit 9c5c4ec
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
8 changes: 4 additions & 4 deletions .storybook/addons/theme-switcher/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@
font-size: 13px;
}

.click {
body[data-theme='click'] {
@mixin theme-click;
}

.site {
body[data-theme='site'] {
@mixin theme-site;
}

.corp {
body[data-theme='corp'] {
@mixin theme-corp;
}

.mobile {
body[data-theme='mobile'] {
@mixin theme-mobile;
}
16 changes: 8 additions & 8 deletions .storybook/addons/theme-switcher/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import React, { useState, useEffect } from 'react';
import { useEffect, useCallback } from 'react';
import addons, { makeDecorator } from '@storybook/addons';
import { ADDON_ID } from './register';

import styles from './index.css';
import { ADDON_ID, THEME_DATA_ATTR } from './register';

export default makeDecorator({
name: 'withThemeSwitcher',
wrapper: (getStory, context) => {
const [theme, setTheme] = useState();

const channel = addons.getChannel();

const setTheme = useCallback(theme => {
document.body.dataset[THEME_DATA_ATTR] = theme;
}, []);

useEffect(() => {
channel.on(`${ADDON_ID}/theme`, setTheme);

return () => {
channel.removeListener(`${ADDON_ID}/theme`, setTheme);
};
}, []);
}, [channel, setTheme]);

return <div className={styles[theme]}>{getStory(context)}</div>;
return getStory(context);
},
});
17 changes: 13 additions & 4 deletions .storybook/addons/theme-switcher/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,23 @@ import { Form } from '@storybook/components';
import './index.css';

export const ADDON_ID = 'theme-switcher';
export const THEME_DATA_ATTR = 'theme';

const Addon = () => {
const [theme, setTheme] = useState('default');
const [theme, setTheme] = useState(document.body.dataset[THEME_DATA_ATTR] || 'default');
const { themes } = useParameter(ADDON_ID, { themes: [] });

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);
const newTheme = event.target.value;

setTheme(newTheme);
emit(`${ADDON_ID}/theme`, newTheme);

document.body.dataset[THEME_DATA_ATTR] = newTheme;
}, []);

return (
Expand All @@ -27,7 +32,11 @@ const Addon = () => {
<span className='label'>Theme:</span>
<Form.Select size={1} onChange={handleChange} className='select'>
{['default'].concat(themes).map(themeName => (
<option value={themeName} key={themeName}>
<option
value={themeName}
key={themeName}
selected={themeName === theme}
>
{themeName}
</option>
))}
Expand Down

0 comments on commit 9c5c4ec

Please sign in to comment.