Skip to content

Commit

Permalink
Storybook: Add theme switcher tool (WordPress#44715)
Browse files Browse the repository at this point in the history
* Storybook: Add theme switcher

* Add comments
  • Loading branch information
mirka authored Oct 13, 2022
1 parent c79bd96 commit 37c0252
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
28 changes: 28 additions & 0 deletions storybook/decorators/with-theme.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* 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: {
// This color was chosen intentionally, because for sufficient contrast,
// the foreground text should be black when this orange is used as a background color.
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. (Work in progress)',
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 37c0252

Please sign in to comment.