From 63f6d9d3f25396212384a936fbdc746e582eb617 Mon Sep 17 00:00:00 2001 From: Maciek Sitkowski <58401630+sitek94@users.noreply.github.com> Date: Thu, 25 Nov 2021 14:32:52 +0100 Subject: [PATCH] feat(storybook): add theme toggle (#44234) --- tools/ui-components/.storybook/preview.js | 34 ++++++++++++++++++++++ tools/ui-components/src/button.stories.tsx | 21 ++----------- tools/ui-components/src/button.types.ts | 1 - tools/ui-components/src/global.css | 2 +- 4 files changed, 37 insertions(+), 21 deletions(-) diff --git a/tools/ui-components/.storybook/preview.js b/tools/ui-components/.storybook/preview.js index 30f63ee296..cb63227087 100644 --- a/tools/ui-components/.storybook/preview.js +++ b/tools/ui-components/.storybook/preview.js @@ -5,5 +5,39 @@ export const parameters = { color: /(background|color)$/i, date: /Date$/ } + }, + backgrounds: { + values: [ + { + name: 'light', + value: '#ffffff' + }, + { + name: 'dark', + value: '#0a0a23' + } + ] } }; + +export const decorators = [renderTheme]; + +/** + * Gets matching theme name for currently selected background and provides it + * to the story. + */ +function renderTheme(Story, context) { + const selectedBackgroundValue = context.globals.backgrounds?.value; + const selectedBackgroundName = parameters.backgrounds.values.find( + bg => bg.value === selectedBackgroundValue + )?.name; + + // There can be no background selected, prevent "undefined" className + const className = selectedBackgroundName || ''; + + return ( +
+ +
+ ); +} diff --git a/tools/ui-components/src/button.stories.tsx b/tools/ui-components/src/button.stories.tsx index 6b885bdeeb..12f8758210 100644 --- a/tools/ui-components/src/button.stories.tsx +++ b/tools/ui-components/src/button.stories.tsx @@ -5,28 +5,11 @@ import { ButtonProps } from './button.types'; const story = { title: 'Example/Button', - component: Button, - argTypes: { - theme: { - options: ['dark', 'light'], - control: { type: 'radio' }, - defaultValue: 'light' - } - } + component: Button }; const Template: Story = args => { - return ( -
-
- ); + return