Newsletter
Join the Community
Subscribe to our newsletter for the latest news and updates
Storybook preset addon to add CSS Modules capabilities
storybook-css-modules is a Storybook preset addon that adds CSS Modules support to Storybook, automatically processing *.modules.css files and exposing their class names as locally scoped JavaScript objects.
storybook-css-modules is a preset addon for Storybook that enables CSS Modules in component stories. It takes .module.css files as input and compiles them so that class names become hashed, locally scoped identifiers that can be imported as properties of a JavaScript object. The addon runs inside Storybook's webpack build and is distributed on npm, created by onWidget and maintained by a community of contributors.
"storybook-css-modules" to the addons array in .storybook/main.js and Storybook wires up css-loader without extra webpack configuration.importLoaders: 1 and localIdentName set to [path][name]__[local]--[hash:base64:5], giving readable yet unique class names.cssModulesLoaderOptions object to the addon in main.js to customize how CSS Modules are processed, for example using css-loader-shorter-classnames for shorter hashes.*.module.css file into a story.Storybook teams that write styles with CSS Modules and want them to work inside stories without manually tweaking webpack. React, Preact, and Vue 3 developers can copy the example projects as a starting point. Component library maintainers who need consistent class-name generation across multiple projects will benefit from the configurable loader options.
cssModulesLoaderOptions to keep production class names short or match an existing naming convention, then reuse the same preset across projects.Install it with npm install -D storybook-css-modules, then register it in .storybook/main.js as a string entry in addons. Finally, import a stylesheet like import styles from './Button.module.css' in any story and use styles.Button as the className prop. The preset configures css-loader for everything ending in .module.css.
storybook-css-modules is free, open-source software released under the MIT license.
Yes, the addon is released under the MIT license and can be used in commercial and open-source projects alike.
Run npm install -D storybook-css-modules, add "storybook-css-modules" to the addons array in .storybook/main.js, then import .module.css files inside your stories and use the exported class names.
Yes, pass a cssModulesLoaderOptions object when registering the addon. The example in the documentation replaces the default localIdentName with a custom getLocalIdent function from css-loader-shorter-classnames.
The repository provides examples for React, Preact, and Vue 3. Because it relies on Storybook's webpack build, the preset works with any Storybook setup that can process css-loader modules.
