All Projects → hipstersmoothie → Storybook Dark Mode

hipstersmoothie / Storybook Dark Mode

Licence: mit
A storybook addon that lets your users toggle between dark and light mode.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Storybook Dark Mode

Koa Vue Notes Web
🤓 This is a simple SPA built using Koa as the backend, Vue as the first frontend, and React as the second frontend. Features MySQL integration, user authentication, CRUD note actions, and Vuex store modules.
Stars: ✭ 200 (-8.26%)
Mutual labels:  storybook
Deluminate
An extension for Google Chrome (and Chromium) that inverts the luminance of websites.
Stars: ✭ 211 (-3.21%)
Mutual labels:  dark-theme
Backpack Ui
All the tools you need to build the Lonely Planet UI experience.
Stars: ✭ 216 (-0.92%)
Mutual labels:  storybook
Vuesion
Vuesion is a boilerplate that helps product teams build faster than ever with fewer headaches and modern best practices across engineering & design.
Stars: ✭ 2,510 (+1051.38%)
Mutual labels:  storybook
Gtest Runner
A cross-platform, Qt5 based Graphical User Interface for Google Test unit tests
Stars: ✭ 207 (-5.05%)
Mutual labels:  dark-theme
Addon Smart Knobs
🧠 This Storybook plugin uses @storybook/addon-knobs but creates the knobs automatically based on PropTypes.
Stars: ✭ 215 (-1.38%)
Mutual labels:  storybook
Wandroid
首款适配玩Android、掘金、简书、CSDN、公众号文章黑夜模式,无广告,支持离线阅读,代码图片显示,关注内容本身,阅读体验升级。
Stars: ✭ 199 (-8.72%)
Mutual labels:  dark-theme
Luna
Automatic dark mode for Windows 10
Stars: ✭ 220 (+0.92%)
Mutual labels:  dark-theme
Addon Jsx
This Storybook addon show you the JSX / template of the story
Stars: ✭ 209 (-4.13%)
Mutual labels:  storybook
Bancointer
Redesign of Banco Inter's Internet Banking
Stars: ✭ 216 (-0.92%)
Mutual labels:  dark-theme
Svelte Storybook Tailwind
A starter template for Svelte, TailwindCSS and Storybook. You can easily start your project with this template, instead of wasting time figuring out configurations for each integration.
Stars: ✭ 204 (-6.42%)
Mutual labels:  storybook
Flutter Notes App
A notes taking app written in Flutter
Stars: ✭ 203 (-6.88%)
Mutual labels:  dark-theme
Vscode One Monokai
🎨 Vscode One Monokai theme.
Stars: ✭ 214 (-1.83%)
Mutual labels:  dark-theme
Lerna Yarn Workspaces Monorepo
🐉 A Monorepo with multiple packages and a shared build, test, and release process.
Stars: ✭ 201 (-7.8%)
Mutual labels:  storybook
Govuk React
An implementation of the GOV.UK Design System in React using CSSinJS
Stars: ✭ 219 (+0.46%)
Mutual labels:  storybook
Mojave Dark Rstudio Theme
A Total-IDE Dark RStudio Theme inspired by Apple's dark aestheticcc.
Stars: ✭ 198 (-9.17%)
Mutual labels:  dark-theme
Datweatherdoe
Simple menu bar weather app for macOS
Stars: ✭ 209 (-4.13%)
Mutual labels:  dark-theme
Jxtheme
A powerful and lightweight and customization theme/skin library for iOS 9+ in swift. 主题、换肤、暗黑模式
Stars: ✭ 220 (+0.92%)
Mutual labels:  dark-theme
Edi
Edi - The open source text editor IDE based on AvalonDock and AvalonEdit
Stars: ✭ 220 (+0.92%)
Mutual labels:  dark-theme
Testing React
Testing utilities that allow you to reuse your Storybook stories in your React unit tests!
Stars: ✭ 171 (-21.56%)
Mutual labels:  storybook

storybook-dark-mode

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

Example

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']
};

Configuration

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

import { themes } from '@storybook/theming';

export 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 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 parameters = {
  darkMode: {
    darkClass: 'lights-out',
    lightClass: 'lights-on'
  }
};

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 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 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 provider
import ThemeContext from './theme';

// create a component that uses the dark mode hook
function 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 dark
const 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/addons';
import { addDecorator } from '@storybook/react';

// your theme provider
import ThemeContext from './theme';

// get channel to listen to event emitter
const channel = addons.getChannel();

// create a component that listens for the DARK_MODE event
function 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', setDark);
    return () => channel.off('DARK_MODE', 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>)];

Contributors ✨

Thanks goes to these wonderful people (emoji key):

💬 🎨 🤔 🚇 🚧
Erik Hughes

💻

Adam Jahnke

💻

Carles Núñez

💻

Adam Dierkens

💻

Tobias Skarhed

💻 📖

Fatih Kalifa

💻

Jacob Coughenour

💻

Jeroen Zwartepoorte

📖 💻

Alex Khomenko

💻

Paul Fasola

📖

Pavel Keyzik

📖

David Richolm

📖 💻

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].