feat(storybook): add theme toggle (#44234)
This commit is contained in:
@ -5,5 +5,39 @@ export const parameters = {
|
|||||||
color: /(background|color)$/i,
|
color: /(background|color)$/i,
|
||||||
date: /Date$/
|
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 (
|
||||||
|
<div className={className}>
|
||||||
|
<Story />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
@ -5,28 +5,11 @@ import { ButtonProps } from './button.types';
|
|||||||
|
|
||||||
const story = {
|
const story = {
|
||||||
title: 'Example/Button',
|
title: 'Example/Button',
|
||||||
component: Button,
|
component: Button
|
||||||
argTypes: {
|
|
||||||
theme: {
|
|
||||||
options: ['dark', 'light'],
|
|
||||||
control: { type: 'radio' },
|
|
||||||
defaultValue: 'light'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const Template: Story<ButtonProps> = args => {
|
const Template: Story<ButtonProps> = args => {
|
||||||
return (
|
return <Button {...args} />;
|
||||||
<div
|
|
||||||
className={`flex h-screen justify-center items-center ${
|
|
||||||
args.theme === 'dark'
|
|
||||||
? 'dark bg-dark-theme-background'
|
|
||||||
: 'light bg-light-theme-background'
|
|
||||||
}`}
|
|
||||||
>
|
|
||||||
<Button {...args} />
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const Primary = Template.bind({});
|
export const Primary = Template.bind({});
|
||||||
|
@ -6,5 +6,4 @@ export interface ButtonProps {
|
|||||||
label: string;
|
label: string;
|
||||||
customKey?: string;
|
customKey?: string;
|
||||||
onClick: () => void;
|
onClick: () => void;
|
||||||
theme?: 'light' | 'dark';
|
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
@import 'tailwindcss/base';
|
@import 'tailwindcss/base';
|
||||||
@import 'tailwindcss/components';
|
@import 'tailwindcss/components';
|
||||||
@import 'tailwindcss/utilities';
|
@import 'tailwindcss/utilities';
|
||||||
@import './colors';
|
@import './colors.css';
|
||||||
|
Reference in New Issue
Block a user