Storybook addon that enables theme switching functionality within the preview environment with support for multiple theming strategies
npx @tessl/cli install tessl/npm-storybook--addon-themes@9.1.0Storybook Addon Themes enables theme switching functionality within the Storybook preview environment. It provides multiple decorators for different theming strategies including CSS classes, data attributes, and JSX providers, making it compatible with popular styling solutions like styled-components, emotion, Material-UI, Tailwind CSS, and Bootstrap.
npm install -D @storybook/addon-themesimport {
withThemeByClassName,
withThemeByDataAttribute,
withThemeFromJSXProvider,
DecoratorHelpers,
type ThemesTypes
} from "@storybook/addon-themes";
// Default export for addon registration
import addonThemes from "@storybook/addon-themes";For CommonJS:
const {
withThemeByClassName,
withThemeByDataAttribute,
withThemeFromJSXProvider,
DecoratorHelpers
} = require("@storybook/addon-themes");
// Default export
const addonThemes = require("@storybook/addon-themes").default;Add the addon to your Storybook configuration:
// .storybook/main.js
export default {
addons: ['@storybook/addon-themes'],
};Configure theme switching in your preview:
// .storybook/preview.js
import { withThemeByClassName } from '@storybook/addon-themes';
export const decorators = [
withThemeByClassName({
themes: {
light: 'light-theme',
dark: 'dark-theme',
},
defaultTheme: 'light',
}),
];The addon is built around several key components:
Theme switching by applying CSS classes to parent elements. Ideal for CSS frameworks and traditional stylesheet-based themes.
function withThemeByClassName<TRenderer extends Renderer = Renderer>(
config: ClassNameStrategyConfiguration
): DecoratorFunction<TRenderer>;
interface ClassNameStrategyConfiguration {
themes: Record<string, string>;
defaultTheme: string;
parentSelector?: string;
}Theme switching by setting data attributes on parent elements. Perfect for CSS custom properties and attribute-based selectors.
function withThemeByDataAttribute<TRenderer extends Renderer = any>(
config: DataAttributeStrategyConfiguration
): DecoratorFunction<TRenderer>;
interface DataAttributeStrategyConfiguration {
themes: Record<string, string>;
defaultTheme: string;
parentSelector?: string;
attributeName?: string;
}Theme switching using JSX provider components and theme objects. Designed for component libraries like styled-components, emotion, and Material-UI.
function withThemeFromJSXProvider<TRenderer extends Renderer = any>(
config: ProviderStrategyConfiguration
): DecoratorFunction<TRenderer>;
interface ProviderStrategyConfiguration {
Provider?: any;
GlobalStyles?: any;
defaultTheme?: string;
themes?: Record<string, any>;
}Utilities for building custom theme decorators when the built-in decorators don't meet specific requirements.
namespace DecoratorHelpers {
function pluckThemeFromContext(context: StoryContext): string;
function initializeThemeState(themeNames: string[], defaultTheme: string): void;
function useThemeParameters(context?: StoryContext): ThemesParameters;
}The package's default export is a function that creates a Storybook addon definition for use with Storybook's internal addon system.
/**
* Creates a Storybook addon definition with theme support
* @returns Storybook addon definition
*/
function default(): PreviewAddon<ThemesTypes>;This export is automatically used by Storybook when the addon is registered in your .storybook/main.js addons array. You typically don't need to import or use this directly unless you're building custom addon integrations.
interface ThemesTypes {
parameters: ThemesParameters;
globals: ThemesGlobals;
}
interface ThemesParameters {
themes?: {
disable?: boolean;
themeOverride?: string;
};
}
interface ThemesGlobals {
theme?: string;
}
interface ThemeAddonState {
themesList: string[];
themeDefault?: string;
}The addon exports several internal constants used for its operation. These are not part of the public API and should not be used directly in consuming code:
PARAM_KEY - Parameter key for theme configurationGLOBAL_KEY - Global key for theme stateADDON_ID - Unique addon identifierTHEME_SWITCHER_ID - Theme switcher component identifierTHEMING_EVENTS - Event constants for theme communicationThese constants are implementation details and may change between releases.