From b13fd119112cf73e27024c44ad202f0d73e880b3 Mon Sep 17 00:00:00 2001 From: Linda Paiste Date: Tue, 25 Jul 2023 11:18:45 -0500 Subject: [PATCH] Add theme switching to Storybook. --- .storybook/decorator-theme.js | 53 +++++++++++++++++++++++++++++++++++ .storybook/preview.js | 14 +++++---- 2 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 .storybook/decorator-theme.js diff --git a/.storybook/decorator-theme.js b/.storybook/decorator-theme.js new file mode 100644 index 0000000000..c3a60f8386 --- /dev/null +++ b/.storybook/decorator-theme.js @@ -0,0 +1,53 @@ +import { upperFirst } from 'lodash'; +import React from 'react'; +import styled, { ThemeProvider } from 'styled-components'; +import theme, { prop } from '../client/theme'; + +const PreviewArea = styled.div` + background: ${prop('backgroundColor')}; + flex-grow: 1; + padding: 2rem; + & > h4 { + margin-top: 0; + color: ${prop('primaryTextColor')}; + } +`; + +const themeKeys = Object.keys(theme); + +export const withThemeProvider = (Story, context) => { + const setting = context.globals.theme; + if (setting === 'all') { + return ( +
+ {Object.keys(theme).map((themeName) => ( + + +

{upperFirst(themeName)}

+ +
+
+ ))} +
+ ); + } else { + const themeName = setting; + return ( + + + + + + ); + } +}; + +export const themeToolbarItem = { + description: 'Global theme for components', + defaultValue: 'all', + toolbar: { + title: 'Theme', + icon: 'mirror', + items: [...themeKeys, 'all'] + } +}; diff --git a/.storybook/preview.js b/.storybook/preview.js index 0c8026ae60..7b61f60eb4 100644 --- a/.storybook/preview.js +++ b/.storybook/preview.js @@ -1,10 +1,10 @@ import React from 'react'; import { Provider } from 'react-redux'; -import ThemeProvider from '../client/modules/App/components/ThemeProvider'; import configureStore from '../client/store'; import '../client/i18n-test'; -import '../client/styles/build/css/main.css' +import '../client/styles/build/css/main.css'; +import { withThemeProvider, themeToolbarItem } from './decorator-theme'; const initialState = window.__INITIAL_STATE__; @@ -13,10 +13,12 @@ const store = configureStore(initialState); export const decorators = [ (Story) => ( - - - + ), -] + withThemeProvider +]; +export const globalTypes = { + theme: themeToolbarItem +};