![Create React App Officially Deprecated Amid React 19 Compatibility Issues](https://cdn.sanity.io/images/cgdhsj6q/production/04fa08cf844d798abc0e1a6391c129363cc7e2ab-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Create React App Officially Deprecated Amid React 19 Compatibility Issues
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
storybook-addon-themes
Advanced tools
A storybook addon to switch between different themes for your preview
Greatly inspired by @storybook/addon-backgrounds.
This Storybook Theme Decorator can be used to add a custom HTML class or classes to the preview in Storybook.
This version is compatible with storybook version 6.0.x
.
npm i -D storybook-addon-themes
Then activate the addon by adding it to the storybook main.js
file (located in the Storybook config directory):
module.exports = {
addons: [
// Maybe other addons here...
'storybook-addon-themes'
// Or here...
],
};
See the storybook documentation for more informations.
The themes
parameter accept an array of Theme
object.
Each Theme
is an object with the following properties:
name
(string
): Name of the themeclass
(string | string[]
- optional): HTML class(es) associated with the themecolor
(string
): The color of the badge in the theme selectordefault
[deprecated] (boolean
- optional): Is the theme selected by default?The themes
parameter also accept an object with the following properties:
default
(string
- optional): Name of theme selected by defaultlist
(Theme[]
- required): The list of themesclearable
(boolean
- optional - default is true
): Can the user clear the selected theme ?disable
(boolean
- optional): Disable the addon for a storyDecorator
(Component
- optional): A component to use as the decorator component (see below for more information)onChange
((themeName: Theme) => void
- optional): A callback that will be executed when the theme changestarget
(string
- optional): Target element selected with document.querySelector()
to which classes are applied. Defaults to body
, root
if classes should be applied to documentElement
.You can configure the themes globally in the storybook preview.js
file:
export const parameters = {
themes: {
default: 'twitter',
list: [
{ name: 'twitter', class: 'theme-twt', color: '#00aced' },
{ name: 'facebook', class: 'theme-fb', color: '#3b5998' }
],
},
};
For backward compatibility default
(boolean
) can also be set directly on Theme
object.
This has been deprecated because of the difficulty of changing the default theme due to the need to redefine all Theme
objects.
// deprecated
export const parameters = {
themes: [
{ name: 'twitter', class: 'theme-twt', color: '#00aced', default: true },
{ name: 'facebook', class: 'theme-fb', color: '#3b5998' }
],
};
See the storybook documentation for more informations.
Or configure the themes in your story file like this:
export default {
title: 'CSF|Button',
component: Button,
parameters: {
themes: {
default: 'twitter',
list: [
{ name: 'twitter', class: ['theme-twt', 'light-mode'], color: '#00aced' },
{ name: 'facebook', class: ['theme-fb', 'dark-mode'], color: '#3b5998' },
],
},
},
};
If you only want to activate the addon or override the themes for a specific story you can write:
export default {
title: 'CSF|Button',
component: Button,
};
export const withText = () => <Button onClick={action('clicked')}>Hello Button</Button>;
withText.story = {
parameters: {
themes: {
default: 'twitter',
list: [
{ name: 'twitter', class: ['theme-twt', 'light-mode'], color: '#00aced' },
{ name: 'facebook', class: ['theme-fb', 'dark-mode'], color: '#3b5998' },
],
},
},
};
Alternatively with the old StoriesOf API:
import { storiesOf } from '@storybook/react'; // <- or your storybook framework
storiesOf('StoriesOf|Button', module)
.addParameters({
themes: {
default: 'twitter',
list: [
{ name: 'twitter', class: ['theme-twt', 'light-mode'], color: '#00aced' },
{ name: 'facebook', class: ['theme-fb', 'dark-mode'], color: '#3b5998' },
],
},
})
.add('with text', () => <button>Click me</button>);
And for a single story:
import { storiesOf } from '@storybook/react';
storiesOf('StoriesOf|Button', module)
.add('with text', () => <button>Click me</button>, {
themes: {
list: [
{ name: 'red', class: 'theme-red', color: 'rgba(255, 0, 0)' },
],
},
});
You can also only override a single key on the themes parameter, for instance to set a different default value for a single story:
export default {
title: 'CSF|Button',
component: Button,
};
export const withText = () => <Button onClick={action('clicked')}>Hello Button</Button>;
withText.story = {
parameters: {
themes: {
default: 'facebook',
},
},
};
By default the classes will be added to the body
element or the element configured with target
.
But in this case your theme will not be visible by other addons (like @storybook/addon-storyshots).
To fix this you can add the withThemes
decorator in your stories.
But the decorator method is not available for all frameworks
See here for the list of supported framework.
Setup the decorator globally in the preview.js
file:
import { addDecorator } from '@storybook/react'; // <- or your storybook framework
import { withThemes } from 'storybook-addon-themes/react'; // <- or your storybook framework
addDecorator(withThemes);
export const parameters = {
actions: { argTypesRegex: "^on[A-Z].*" },
themes: {
default: 'twitter',
list: [
{ name: 'twitter', class: ['theme-twt', 'light-mode'], color: '#00aced' },
{ name: 'facebook', class: ['theme-fb', 'dark-mode'], color: '#3b5998' },
],
},
};
Or in your story file (for all stories in that file):
export default {
title: 'CSF|Button',
component: Button,
decorators: [ withThemes ],
parameters: {
themes: {
default: 'twitter',
list: [
{ name: 'twitter', class: ['theme-twt', 'light-mode'], color: '#00aced' },
{ name: 'facebook', class: ['theme-fb', 'dark-mode'], color: '#3b5998' },
],
},
},
};
Or just for a specific story:
export const withText = () => <Button onClick={action('clicked')}>Hello Button</Button>;
withText.story = {
decorators: [ withThemes ],
parameters: {
themes: {
default: 'twitter',
list: [
{ name: 'twitter', class: ['theme-twt', 'light-mode'], color: '#00aced' },
{ name: 'facebook', class: ['theme-fb', 'dark-mode'], color: '#3b5998' },
],
},
},
};
And alternatively with the old StoriesOf API:
import { storiesOf } from '@storybook/react'; // <- or your storybook framework
import { withThemes } from 'storybook-addon-themes/react';
storiesOf('StoriesOf|Button', module)
.addDecorator(withThemes)
.add('with text', () => <button>Click me</button>);
You can provide a component that will be used as decorator using the Decorator
option in the theme
parameter.
The decorator will get the following properties :
theme
: The selected theme or undefined
if none is selected.themes
: The list of themes as provided in the list
option of the theme
parameter.themeClasses
: The formatted theme classes of the selected theme (if the class
option exists on the selected theme).themeName
: The name of the selected theme (equal to none
if none is selected).Don't forget to render the story using the children
prop (React/HTML) or the <slot></slot>
element (Vue/Svelte).
To manage reactivity with the HTML storybook your decorator must return an array containing two elements :
children
).Example of a customized decorator that use a CSS file for changing the theme:
function getOrCreate(id) {
const elementOnDom = document.getElementById(id);
if (elementOnDom) {
return elementOnDom;
}
const element = document.createElement('link');
element.setAttribute('id', id);
element.setAttribute('rel', 'stylesheet');
return element;
}
function Decorator(props) {
const { children } = props;
function setStyles({ theme, themeName }) {
const link = getOrCreate('theme-stylesheet');
if (!theme) {
link.parentNode && link.parentNode.removeChild(link);
} else {
link.href = themeName === 'facebook' ? 'Button-fb.css' : 'Button-twt.css';
children.appendChild(link);
}
}
setStyles(props);
return [children, setStyles];
}
Same example as above for React:
function Decorator(props) {
const { children, themeName } = props;
return (
<>
{children}
{themeName === 'twitter' && <link rel="stylesheet" href="twitter.css"/>}
{themeName === 'facebook' && <link rel="stylesheet" href="facebook.css"/>}
</>
);
};
React | React Native | Vue | Angular | Polymer | Mithril | HTML | Marko | Svelte | Riot | Ember | Preact | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
Usage without decorator | + | + | + | + | + | + | + | + | + | + | + | |
Usage with decorator | + | + | + | + |
FAQs
A storybook addon to switch between different themes for your preview
The npm package storybook-addon-themes receives a total of 24,538 weekly downloads. As such, storybook-addon-themes popularity was classified as popular.
We found that storybook-addon-themes demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.