code/addons/themes/docs/getting-started/tailwind.md
tailwind.cssTo get started, install the package as a dev dependency.
yarn:
yarn add -D @storybook/addon-themes
npm:
npm install -D @storybook/addon-themes
pnpm:
pnpm add -D @storybook/addon-themes
Now, include the addon in your .storybook/main.js file.
export default {
stories: ['../src/**/*.mdx', '../src/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
addons: [
'@storybook/addon-essentials',
+ '@storybook/addon-themes',
],
};
To give your stories access to Tailwind styles, import them into your .storybook/preview.js file.
import { Preview } from '@storybook/your-renderer';
+import '../src/index.css';
const preview: Preview = {
parameters: { /* ... */ },
};
export default preview;
Tailwind supports light and dark color modes out of the box. These modes can be activated by setting a .dark class on a parent element.
To enable switching between these modes in a click for your stories, use our withThemeByClassName decorator by adding the following code to your .storybook/preview.js file.
-import { Preview } from '@storybook/your-renderer';
+import { Preview, Renderer } from '@storybook/your-renderer';
+import { withThemeByClassName } from '@storybook/addon-themes';
import '../src/index.css';
const preview: Preview = {
parameters: { /* ... */ },
+ decorators: [
+ withThemeByClassName<Renderer>({
+ themes: {
+ light: '',
+ dark: 'dark',
+ },
+ defaultTheme: 'light',
+ }),
+ ]
};
export default preview;
If you've configured Tailwind to toggle themes with a data attribute, use our withThemeByDataAttribute decorator by adding the following code to your .storybook/preview.js file.
-import { Preview } from '@storybook/your-renderer';
+import { Preview, Renderer } from '@storybook/your-renderer';
+import { withThemeByDataAttribute } from '@storybook/addon-themes';
import '../src/index.css';
const preview: Preview = {
parameters: { /* ... */ },
+ decorators: [
+ withThemeByDataAttribute<Renderer>({
+ themes: {
+ light: 'light',
+ dark: 'dark',
+ },
+ defaultTheme: 'light',
+ attributeName: 'data-theme',
+ }),
+ ]
};
export default preview;