code/addons/themes/docs/getting-started/postcss.md
postcssTo get started, install the package as a dev dependency
yarn:
yarn add -D @storybook/addon-themes postcss-dark-theme-class
npm:
npm install -D @storybook/addon-themes postcss-dark-theme-class
pnpm:
pnpm add -D @storybook/addon-themes postcss-dark-theme-class
Now, include the addon in your .storybook/main.js file.
module.exports = {
stories: [
"../stories/**/*.stories.mdx",
"../stories/**/*.stories.@(js|jsx|ts|tsx)",
],
addons: [
"@storybook/addon-essentials",
+ "@storybook/addon-themes"
],
};
prefers-color-scheme mediaCSS has special media at-rule for dark theme: @media (prefers-color-scheme: dark). postcss-dark-theme-class can copy content of those at-rules to .is-dark selector.
Check your project for existing PostCSS config: postcss.config.js in the project root, "postcss" section in package.json or postcss in bundle config.
Add plugin to the list.
module.exports = {
plugins: [
+ require('postcss-dark-theme-class'),
require('autoprefixer')
]
}
Use prefers-color-scheme media in your CSS:
:root {
--text-color: black;
}
@media (prefers-color-scheme: dark) {
html {
--text-color: white;
}
}
To give your stories access to 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;
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: "is-light",
+ dark: "is-dark",
+ },
+ defaultTheme: "light",
+ }),
+ ]
};
export default preview;