Giter Site home page Giter Site logo

storybook-addon-themes's Introduction

Storybook Addon Themes

Greatly inspired by @storybook/addon-backgrounds.

This Storybook Theme Decorator can be used to add a custom HTML class or classes to the the preview in Storybook.

Demo

Compatibility

This version is compatible with storybook version 5.3.x.

Installation

npm i -D storybook-addon-themes

Getting started

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.

Parameters

The themes parameter accept a list of Theme object.

Each Theme is an object with the following properties :

  • name (string): Name of the theme
  • class (string | string[]): HTML class(es) associated with the theme
  • color (string): The color of the badge in the theme selector
  • default (boolean - optionnal): Is the theme selected by default ?

The themes parameter also accept an object with the following properties :

  • list (Theme[] - optionnal): The list of themes
  • disable (boolean - optionnal): Disable the addon for a story

Configuration

Globally

You can configure the themes globally in the storybook preview.js file:

import { addParameters } from '@storybook/react'; // <- or your storybook framework

addParameters({
  themes: [
    { name: 'twitter', class: 'theme-twt', color: '#00aced', default: true },
    { name: 'facebook', class: 'theme-fb', color: '#3b5998' },
  ],
});

See the storybook documentation for more informations.

In story (Component Story Format)

Or configure the themes in your story file like this:

export default {
  component: Button,
  parameters: {
    themes: [
      { name: 'twitter', class: ['theme-twt', 'light-mode'], color: '#00aced' },
      { name: 'facebook', class: ['theme-fb', 'dark-mode'], color: '#3b5998' },
    ]
  },
  title: 'CSF|Button',
};

If you only want to activate the addon or override the themes for a specific story you can write:

export default {
  component: Button,
  title: 'Button',
};

export const withText = () => <Button onClick={action('clicked')}>Hello Button</Button>;
withText.story = {
  parameters: {
    themes: [
      { name: 'twitter', class: ['theme-twt', 'light-mode'], color: '#00aced' },
      { name: 'facebook', class: ['theme-fb', 'dark-mode'], color: '#3b5998' },
    ]
  },
};

In story (StoriesOf API)

Alternatively with the old StoriesOf API:

import { storiesOf } from '@storybook/react'; // <- or your storybook framework

storiesOf('Button', module)
  .addParameters({
    themes: [
      { name: 'twitter', class: 'theme-twt', color: '#00aced', default: true },
      { name: 'facebook', class: 'theme-fb', color: '#3b5998' },
    ],
  })
  .add('with text', () => <button>Click me</button>);

And for a single story:

import { storiesOf } from '@storybook/react';

storiesOf('Button', module)
  .add('with text', () => <button>Click me</button>, {
    themes: [{
      name: 'red', class: 'theme-red', color: 'rgba(255, 0, 0)',
    }]
  });

Usage with decorator

By default the classes will be added to the body element.

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.

Globally

Setup the decorator globally in the preview.js file:

import { addDecorator, addParameters } from '@storybook/react'; // <- or your storybook framework
import { withThemes } from 'storybook-addon-themes/react'; // <- or your storybook framework

addParameters({
  themes: [
    { name: 'twitter', class: 'theme-twt', color: '#00aced', default: true },
    { name: 'facebook', class: 'theme-fb', color: '#3b5998' },
  ],
});
addDecorator(withThemes);

In story (Component Story Format)

Or in your story file (for all stories in that file):

export default {
  component: Button,
  decorators: [ withThemes ],
  parameters: {
    themes: [
      { name: 'twitter', class: ['theme-twt', 'light-mode'], color: '#00aced' },
      { name: 'facebook', class: ['theme-fb', 'dark-mode'], color: '#3b5998' },
    ]
  },
  title: 'CSF|Button',
};

Or just for a specific story:

export const withText = () => <Button onClick={action('clicked')}>Hello Button</Button>;
withText.story = {
  decorators: [ withThemes ],
  parameters: {
    themes: [
      { name: 'twitter', class: ['theme-twt', 'light-mode'], color: '#00aced' },
      { name: 'facebook', class: ['theme-fb', 'dark-mode'], color: '#3b5998' },
    ]
  },
};

In story (StoriesOf API)

And alternatively with the old StoriesOf API:

import { storiesOf } from '@storybook/react'; // <- or your storybook framework
import { withThemes } from 'storybook-addon-themes/react';

storiesOf('Button', module)
  .addDecorator(withThemes)
  .add('with text', () => <button>Click me</button>);

Framework Support Table

React React Native Vue Angular Polymer Mithril HTML Marko Svelte Riot Ember Preact
Usage without decorator + + + + + + + + + + +
Usage with decorator + +

storybook-addon-themes's People

Contributors

tonai avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.