storybook-dark-mode Addon | Storybook: Frontend workshop for UI development (2024)

A storybook addons that lets your users toggle between dark and light mode.

storybook-dark-mode Addon | Storybook: Frontend workshop for UI development (1)

Installation

Install the following npm module:

npm i --save-dev storybook-dark-mode

or with yarn:

yarn add -D storybook-dark-mode

Then, add following content to .storybook/main.js

module.exports = { addons: ['storybook-dark-mode']};

Upgrade from earlier version

Change in .storybook/main.js

module.exports = {- addons: ['storybook-dark-mode/register']+ addons: ['storybook-dark-mode']};

Configuration

Configure the dark and light mode by adding the following to your .storybook/preview.js file:

import { themes } from '@storybook/theming';export const parameters = { darkMode: { // Override the default dark theme dark: { ...themes.dark, appBg: 'black' }, // Override the default light theme light: { ...themes.normal, appBg: 'red' } }};

Default Theme

Order of precedence for the initial color scheme:

  1. If the user has previously set a color theme it's used
  2. The value you have configured for current parameter in your storybook
  3. The OS color scheme preference

Once the initial color scheme has been set, subsequent reloads will use this value.To clear the cached color scheme you have to localStorage.clear() in the chrome console.

export const parameters = { darkMode: { // Set the initial theme current: 'light' }};

Dark/Light Class

This plugin will apply a dark and light class name to the manager.This allows you to easily write dark mode aware theme overrides for the storybook UI.

You can override the classNames applied when switching between light and dark mode using the darkClass and lightClass parameters.

export const parameters = { darkMode: { darkClass: 'lights-out', lightClass: 'lights-on' }};

You can also pass an array to apply multiple classes.

export const parameters = { darkMode: { darkClass: ['lights-out', 'foo'], lightClass: ['lights-on', 'bar'] }};

Preview class target

This plugin will apply the dark/light class to the <body> element of the preview iframe. This can be configured with the classTarget parameter.The value will be passed to a querySelector() inside the iframe.

This is useful if the <body> is styled according to a parent's class, in that case it can be set to html.

export const parameters = { darkMode: { classTarget: 'html' }};

Story integration

Preview ClassName

This plugin will apply the darkClass and lightClass classes to the preview iframe if you turn on the stylePreview option.

export const parameters = { darkMode: { stylePreview: true }};

React

If your components use a custom Theme provider, you can integrate it by using the provided hook.

import { useDarkMode } from 'storybook-dark-mode';import { addDecorator } from '@storybook/react';// your theme providerimport ThemeContext from './theme';// create a component that uses the dark mode hookfunction ThemeWrapper(props) { // render your custom theme provider return ( <ThemeContext.Provider value={useDarkMode() ? darkTheme : defaultTheme}> {props.children} </ThemeContext.Provider> );}export const decorators = [renderStory => <ThemeWrapper>{renderStory()}</ThemeWrapper>)];

Theme Knobs

If you want to have you UI's dark mode separate from you components' dark mode, implement this global decorator:

import { themes } from '@storybook/theming';// Add a global decorator that will render a dark background when the// "Color Scheme" knob is set to darkconst knobDecorator = storyFn => { // A knob for color scheme added to every story const colorScheme = select('Color Scheme', ['light', 'dark'], 'light'); // Hook your theme provider with some knobs return React.createElement(ThemeProvider, { // A knob for theme added to every story theme: select('Theme', Object.keys(themes), 'default'), colorScheme, children: [ React.createElement('style', { dangerouslySetInnerHTML: { __html: `html { ${ colorScheme === 'dark' ? 'background-color: rgb(35,35,35);' : '' } }` } }), storyFn() ] });};export const decorators = [knobDecorator];

Events

You can also listen for the DARK_MODE event via the addons channel.

import { addons } from '@storybook/preview-api';import { addDecorator } from '@storybook/react';import { DARK_MODE_EVENT_NAME } from 'storybook-dark-mode';// your theme providerimport ThemeContext from './theme';// get channel to listen to event emitterconst channel = addons.getChannel();// create a component that listens for the DARK_MODE eventfunction ThemeWrapper(props) { // this example uses hook but you can also use class component as well const [isDark, setDark] = useState(false); useEffect(() => { // listen to DARK_MODE event channel.on(DARK_MODE_EVENT_NAME, setDark); return () => channel.off(DARK_MODE_EVENT_NAME, setDark); }, [channel, setDark]); // render your custom theme provider return ( <ThemeContext.Provider value={isDark ? darkTheme : defaultTheme}> {props.children} </ThemeContext.Provider> );}export const decorators = [renderStory => <ThemeWrapper>{renderStory()}</ThemeWrapper>)];

Since in docs mode, Storybook will not display its toolbar,You can also trigger the UPDATE_DARK_MODE event via the addons channel if you want to control that option in docs mode,By editing your .storybook/preview.js.

import React from 'react';import { addons } from '@storybook/preview-api';import { DocsContainer } from '@storybook/addon-docs';import { themes } from '@storybook/theming';import { DARK_MODE_EVENT_NAME, UPDATE_DARK_MODE_EVENT_NAME} from 'storybook-dark-mode';const channel = addons.getChannel();export const parameters = { darkMode: { current: 'light', dark: { ...themes.dark }, light: { ...themes.light } }, docs: { container: props => { const [isDark, setDark] = React.useState(); const onChangeHandler = () => { channel.emit(UPDATE_DARK_MODE_EVENT_NAME); }; React.useEffect(() => { channel.on(DARK_MODE_EVENT_NAME, setDark); return () => channel.removeListener(DARK_MODE_EVENT_NAME, setDark); }, [channel, setDark]); return ( <div> <input type="checkbox" onChange={onChangeHandler} /> <DocsContainer {...props} /> </div> ); } }};

Contributors ✨

Thanks goes to these wonderful people (emoji key):

storybook-dark-mode Addon | Storybook: Frontend workshop for UI development (2)
Andrew Lisowski

πŸ’¬ πŸ’» 🎨 πŸ“– πŸ€” πŸš‡ 🚧 πŸ’‘
storybook-dark-mode Addon | Storybook: Frontend workshop for UI development (3)
Erik Hughes

πŸ’»
storybook-dark-mode Addon | Storybook: Frontend workshop for UI development (4)
Adam Jahnke

πŸ’»
storybook-dark-mode Addon | Storybook: Frontend workshop for UI development (5)
Carles NΓΊΓ±ez

πŸ’»
storybook-dark-mode Addon | Storybook: Frontend workshop for UI development (6)
Adam Dierkens

πŸ’»
storybook-dark-mode Addon | Storybook: Frontend workshop for UI development (7)
Tobias Skarhed

πŸ’» πŸ“–
storybook-dark-mode Addon | Storybook: Frontend workshop for UI development (8)
Fatih Kalifa

πŸ’»
storybook-dark-mode Addon | Storybook: Frontend workshop for UI development (9)
Jacob Coughenour

πŸ’»
storybook-dark-mode Addon | Storybook: Frontend workshop for UI development (10)
Jeroen Zwartepoorte

πŸ“– πŸ’»
storybook-dark-mode Addon | Storybook: Frontend workshop for UI development (11)
Alex Khomenko

πŸ’»
storybook-dark-mode Addon | Storybook: Frontend workshop for UI development (12)
Paul Fasola

πŸ“–
storybook-dark-mode Addon | Storybook: Frontend workshop for UI development (13)
Pavel Keyzik

πŸ“–
storybook-dark-mode Addon | Storybook: Frontend workshop for UI development (14)
David Richolm

πŸ“– πŸ’»
storybook-dark-mode Addon | Storybook: Frontend workshop for UI development (15)
Klaus NygΓ₯rd

πŸ’»
storybook-dark-mode Addon | Storybook: Frontend workshop for UI development (16)
Arturo Silva

πŸ“– πŸ’»
storybook-dark-mode Addon | Storybook: Frontend workshop for UI development (17)
Nikki Pantony

πŸ“– πŸ’»
storybook-dark-mode Addon | Storybook: Frontend workshop for UI development (18)
Ian VanSchooten

πŸ’»
storybook-dark-mode Addon | Storybook: Frontend workshop for UI development (19)
Fabien

πŸ“– πŸ’»
storybook-dark-mode Addon | Storybook: Frontend workshop for UI development (20)
nilscox

πŸ’»
storybook-dark-mode Addon | Storybook: Frontend workshop for UI development (21)
Jack Westbrook

πŸ’»
storybook-dark-mode Addon | Storybook: Frontend workshop for UI development (22)
Ryan McHenry

πŸ“– πŸ’»
storybook-dark-mode Addon | Storybook: Frontend workshop for UI development (23)
Clay Risser

πŸ“– πŸ’»
storybook-dark-mode Addon | Storybook: Frontend workshop for UI development (24)
BeltrΓ‘n Rengifo

πŸ’»
storybook-dark-mode Addon | Storybook: Frontend workshop for UI development (25)
erik-d

πŸ“–
storybook-dark-mode Addon | Storybook: Frontend workshop for UI development (26)
Christopher Dura

πŸ“– πŸ’»
storybook-dark-mode Addon | Storybook: Frontend workshop for UI development (27)
An Dang

πŸ’»
storybook-dark-mode Addon | Storybook: Frontend workshop for UI development (28)
Zeno Jiricek

πŸ’»
storybook-dark-mode Addon | Storybook: Frontend workshop for UI development (29)
Steven Sacks

πŸ“– πŸš‡ πŸ’»
storybook-dark-mode Addon | Storybook: Frontend workshop for UI development (30)
Rohan Poojary

πŸ“– πŸš‡ πŸ’»
storybook-dark-mode Addon | Storybook: Frontend workshop for UI development (31)
Lauri Luotola

πŸ’‘ πŸ’»

This project follows the all-contributors specification. Contributions of any kind welcome!

storybook-dark-mode Addon | Storybook: Frontend workshop for UI development (2024)

FAQs

Does the storybook have Dark Mode? β€Ί

Storybook includes two themes that look good out of the box: "light" and "dark". Unless you've set your preferred color scheme as dark, Storybook will use the light theme as default. Make sure you have installed @storybook/manager-api and @storybook/theming packages.

How do you use Dark Mode in material UI? β€Ί

Enabling Dark Mode using Material UI's ThemeProvider component. The Material UI provides a theme provider component called ThemeProvider that will allow themes to be defined and used in the app. The ThemeProvider tool allows for a dynamic switch from light to dark themes when implementing the Dark mode.

What are storybook addon essentials? β€Ί

Storybook Essentials is a curated collection of addons to bring out the best of Storybook. Each addon is documented and maintained by the core team and will be upgraded alongside Storybook as the platform evolves. We will also do our best to maintain framework support for all of the officially supported frameworks.

What is a storybook in React Native? β€Ί

With Storybook for React Native you can design and develop individual React Native components without running your app. This readme is for the 7.6. 10 version, you can find the 6.5 docs here. If you are migrating from 6.5 to 7.6 you can find the migration guide here.

What is the difference between dark mode and dark reader? β€Ί

Dark Mode extension automatically shifts the colors of your browser content to the warmer end of the color spectrum after dark. Dark Reader extension will put a dark theme on all websites, suitable for those who work at night and dark reader for online news or book reading.

How do I change my book to dark mode? β€Ί

Change the color & brightness
  1. On your Android phone or tablet, open the Google Play Books app .
  2. Open a book.
  3. Tap the top of the screen Display options. Lighting.
  4. Change the color or brightness: To make the screen brighter, drag the brightness scale. To change the color scheme, tap Light, Sepia, or Dark.

What is the best dark color for UI design? β€Ί

Dark grey surfaces also reduce eye strain, as light text on a dark grey surface has less contrast than light text on a black surface. The recommended dark theme surface color is #121212.

What is the best black for UI design? β€Ί

Most designers would think using black would be optimal to achieve strong contrast. However, it's best not to use true black (#000000) for backgrounds or surface colors. Black is best reserved for other UI elements and used sparingly. For example, true black can be used for small UI elements or a surrounding bezel.

How do I make my UI dark theme? β€Ί

Best practices for dark mode UI
  1. Tip 1: Avoid pure white fonts. ...
  2. Tip 2: Tone down vibrant colors. ...
  3. Tip 3: Make smart use of your brand colors. ...
  4. Tip 4: Avoid shadows in dark mode. ...
  5. Tip 5: Utilize light for elevations. ...
  6. Tip 6: Enable users to switch preferences. ...
  7. Tip 7: Maintain accessible contrast ratios.
Jun 2, 2024

Can I use material UI with storybook? β€Ί

Storybook Addon Material-UI. Provides development environment which helps creating Material-UI Components. This is addon for React Storybook which wraps your components into MuiThemeProvider. This accelerates and simplifies the development process for Material-UI based applications.

What is storybook good for? β€Ί

Storybook is a frontend workshop environment tool for UI/UX development that helps bring consistency in designs across multiple components. By developing components that conform to the design standards set by the team, Storybook ensures a consistent and flawless UI.

Why use storybook UI? β€Ί

Storybook is the single source of truth for your UI. Stories index all your components and their various states, making it easy for your team to find and reuse existing UI patterns. Storybook also auto-generates documentation from those stories.

Is storybook good for React? β€Ί

Better component testing: Storybook provides a visual interface for testing React components, which can make it easier to test different scenarios and edge cases. It also allows developers to test components in isolation, making it easier to identify and fix issues.

What is storybook frontend? β€Ί

Storybook is a frontend workshop for building UI components and pages in isolation. Thousands of teams use it for UI development, testing, and documentation.

How do you implement a storybook in React project? β€Ί

To start, open a terminal in a folder of your choice and run the following command to create a new React application. The create-react-app command creates a new folder, react-storybook , and initialises a basic application skeleton. Next, turn this basic React app into a Storybook application.

Which Kindle model has dark mode? β€Ί

If you have a Kindle device launched in 2017 or later, you'll be able to use Dark Mode. This includes the Kindle Oasis 2 and Kindle Paperwhite 10. Older Kindles do not support Dark Mode.

Does Webnovel have a dark mode? β€Ί

Dark Mode,Webnovel UI Upgrade, Total purchases made, etc... Dark Mode for Webnovel.com Adds dark mode theme to Webnovel and Inkstone. Also, a feature that totals transactions over the year.

Is there a dark mode for Wattpad? β€Ί

If you already have Dark Mode enabled on your device, Wattpad will also have Dark Mode enabled when you launch the app. You can set the mode of your reading page independently of your app settings. To change how your reading page is displayed, please check: Reading Settings.

How do you make a story dark? β€Ί

Elements that make a story dark:
  1. Human nature.
  2. Uncomfortable subjects.
  3. Characterisation, especially deep, complex characters.
  4. Fears and insecurities and anxieties.
  5. Any underlying darkness must have meaning.
  6. Intense emotions.
  7. Dark themes.
  8. The real world - it isn't as pleasant as we think.
Sep 17, 2017

Top Articles
Latest Posts
Article information

Author: Carmelo Roob

Last Updated:

Views: 5700

Rating: 4.4 / 5 (45 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Carmelo Roob

Birthday: 1995-01-09

Address: Apt. 915 481 Sipes Cliff, New Gonzalobury, CO 80176

Phone: +6773780339780

Job: Sales Executive

Hobby: Gaming, Jogging, Rugby, Video gaming, Handball, Ice skating, Web surfing

Introduction: My name is Carmelo Roob, I am a modern, handsome, delightful, comfortable, attractive, vast, good person who loves writing and wants to share my knowledge and understanding with you.