Skip to content

Commit b13fd11

Browse files
committed
Add theme switching to Storybook.
1 parent 59326e8 commit b13fd11

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

.storybook/decorator-theme.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { upperFirst } from 'lodash';
2+
import React from 'react';
3+
import styled, { ThemeProvider } from 'styled-components';
4+
import theme, { prop } from '../client/theme';
5+
6+
const PreviewArea = styled.div`
7+
background: ${prop('backgroundColor')};
8+
flex-grow: 1;
9+
padding: 2rem;
10+
& > h4 {
11+
margin-top: 0;
12+
color: ${prop('primaryTextColor')};
13+
}
14+
`;
15+
16+
const themeKeys = Object.keys(theme);
17+
18+
export const withThemeProvider = (Story, context) => {
19+
const setting = context.globals.theme;
20+
if (setting === 'all') {
21+
return (
22+
<div style={{ display: 'flex', flexWrap: 'wrap' }}>
23+
{Object.keys(theme).map((themeName) => (
24+
<ThemeProvider theme={theme[themeName]} key={themeName}>
25+
<PreviewArea className={themeName}>
26+
<h4>{upperFirst(themeName)}</h4>
27+
<Story />
28+
</PreviewArea>
29+
</ThemeProvider>
30+
))}
31+
</div>
32+
);
33+
} else {
34+
const themeName = setting;
35+
return (
36+
<ThemeProvider theme={theme[themeName]}>
37+
<PreviewArea className={themeName}>
38+
<Story />
39+
</PreviewArea>
40+
</ThemeProvider>
41+
);
42+
}
43+
};
44+
45+
export const themeToolbarItem = {
46+
description: 'Global theme for components',
47+
defaultValue: 'all',
48+
toolbar: {
49+
title: 'Theme',
50+
icon: 'mirror',
51+
items: [...themeKeys, 'all']
52+
}
53+
};

.storybook/preview.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import React from 'react';
22
import { Provider } from 'react-redux';
33

4-
import ThemeProvider from '../client/modules/App/components/ThemeProvider';
54
import configureStore from '../client/store';
65
import '../client/i18n-test';
7-
import '../client/styles/build/css/main.css'
6+
import '../client/styles/build/css/main.css';
7+
import { withThemeProvider, themeToolbarItem } from './decorator-theme';
88

99
const initialState = window.__INITIAL_STATE__;
1010

@@ -13,10 +13,12 @@ const store = configureStore(initialState);
1313
export const decorators = [
1414
(Story) => (
1515
<Provider store={store}>
16-
<ThemeProvider>
17-
<Story />
18-
</ThemeProvider>
16+
<Story />
1917
</Provider>
2018
),
21-
]
19+
withThemeProvider
20+
];
2221

22+
export const globalTypes = {
23+
theme: themeToolbarItem
24+
};

0 commit comments

Comments
 (0)