Created
August 28, 2018 10:09
-
-
Save hoodwink73/56047ad52e1bba79cfa6aecd31f83024 to your computer and use it in GitHub Desktop.
Switch between emotion themes in Storybook
This file contains hidden or 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
import React, {Fragment} from 'react' | |
import {ThemeProvider} from 'emotion-theming' | |
import {configure, addDecorator} from '@storybook/react' | |
import DarkTheme from '../src/styles/themes/Dark' | |
import LightTheme from '../src/styles/themes/Light' | |
import {withKnobs} from '@storybook/addon-knobs' | |
import {select} from '@storybook/addon-knobs' | |
addDecorator(story => { | |
const themeObjects = { | |
dark: DarkTheme, | |
light: LightTheme, | |
} | |
const chooseTheme = choice => { | |
const _theme = themeObjects[choice.toLowerCase()] | |
if (_theme) { | |
return _theme | |
} | |
return _theme | |
} | |
return ( | |
<div> | |
<ThemeProvider | |
theme={chooseTheme( | |
select( | |
'Choose Theme', | |
['Dark', 'Light'], | |
'Light', | |
), | |
)} | |
> | |
{story()} | |
</ThemeProvider> | |
</div> | |
) | |
}) | |
// the order of this statement is important | |
// we need to add this after the usage of the knobs in the above wrapper decorator | |
// otherwise the knobs would disappear on switching between stories | |
// https://github.com/storybooks/storybook/issues/2706 | |
addDecorator(withKnobs) | |
function loadStories() { | |
require('../src/stories') | |
} | |
configure(loadStories, module) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment