Skip to content

Commit

Permalink
Storybook: Add theme switcher
Browse files Browse the repository at this point in the history
  • Loading branch information
mirka committed Oct 5, 2022
1 parent f4f54c1 commit 67ec72d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
26 changes: 26 additions & 0 deletions storybook/decorators/with-theme.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Internal dependencies
*/
import Theme from '../../packages/components/src/theme';

/**
* A Storybook decorator to show a div before and after the story to check for unwanted margins.
*/

export const WithTheme = ( Story, context ) => {
const themes = {
default: {},
modern: {
accent: '#3858e9',
},
sunrise: {
accent: '#dd823b',
},
};

return (
<Theme { ...themes[ context.globals.componentsTheme ] }>
<Story { ...context } />
</Theme>
);
};
21 changes: 20 additions & 1 deletion storybook/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { WithGlobalCSS } from './decorators/with-global-css';
import { WithMarginChecker } from './decorators/with-margin-checker';
import { WithRTL } from './decorators/with-rtl';
import { WithTheme } from './decorators/with-theme';
import './style.scss';

export const globalTypes = {
Expand All @@ -19,6 +20,19 @@ export const globalTypes = {
],
},
},
componentsTheme: {
name: 'Theme',
description: 'Change the components theme.',
defaultValue: 'default',
toolbar: {
icon: 'paintbrush',
items: [
{ value: 'default', title: 'Default' },
{ value: 'modern', title: 'Modern' },
{ value: 'sunrise', title: 'Sunrise' },
],
},
},
css: {
name: 'Global CSS',
description:
Expand Down Expand Up @@ -51,7 +65,12 @@ export const globalTypes = {
},
};

export const decorators = [ WithGlobalCSS, WithMarginChecker, WithRTL ];
export const decorators = [
WithTheme,
WithGlobalCSS,
WithMarginChecker,
WithRTL,
];

export const parameters = {
controls: {
Expand Down

0 comments on commit 67ec72d

Please sign in to comment.