Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Develop themable components with Emotion/Styled Components/Material-UI with help of Storybook & React Theming

License

NotificationsYou must be signed in to change notification settings

react-theming/storybook-addon

Repository files navigation

npm versionStorybook

Storybook Addon @ React Theming

Storybook addon for Styled Components, Emotion, Material-UI and any other theming solution. Allows to develop themed components in isolation.

npm i --save-dev @react-theming/storybook-addon

Demo

example

Features 💫

  • Universal - can be used with any styling library
  • Switching between themes from addon panel.
  • Change a color and see how it affects to your components
  • Easily copy-paste paths of nesting theme props into your code
  • Auto changes background
  • Supports dark Storybook theme
  • Keep selected theme on stories updates

Usage

specify addon in.storybook/main.js

// .storybook/main.jsmodule.exports={stories:['../src/**/*.stories.js'],addons:['@react-theming/storybook-addon'],};

or in.storybook/addons.js for older versions of Storybook

import'@react-theming/storybook-addon/register';

Then you'll need to add a decorator with a ThemeProvider of your library. This project is not related to any particular styling solutions, instead, you can useany of theme providers you're using in your project.

importThemeProviderfrom'library-of-your-choice';import{withThemes}from'@react-theming/storybook-addon';import{theme}from'../src/theme';// create decoratorconstthemingDecorator=withThemes(ThemeProvider,[theme]);

ThemeProvider should accept a theme viatheme props. This is usually the case for the most common styling libraries like Styled Components, Emotion, Material-UI.

In case of non standard ThemeProvider you can passproviderFn function in options:

constproviderFn=({ theme, children})=>{return<ThemeProvidertheme={muTheme}>{children}</ThemeProvider>;};constthemingDecorator=withThemes(null,[theme],{ providerFn});

Use your output of the selected value

// .storybook/preview.jsimport{ThemeProvider}from'styled-components';import{addDecorator}from'@storybook/react';import{withThemes}from'@react-theming/storybook-addon';import{theme}from'../src/theme';

Example getCustomFieldSnippet

constselectedValue={name:"accent5",namespace:["palette","colors"],type:"color",value:"#ac924d"}constgetCustomFieldSnippet=selectedValue=>{const{ namespace, name}=selectedValue;constpath=namespace.join('.');constfullPath=`${path}.${name}`;constthemeProp=`\${({ theme }) => theme.${fullPath}}`;returnthemeProp;};// The snippet Func function takes the SelectedValue parameter and returns a stringaddDecorator(withThemes(ThemeProvider,[theme],{ getCustomFieldSnippet}));

Example getCustomValueSnippet

By default, the addon outputs colors in HEX format, if you need some kind of add-in, then pass the colorSnippet parameter.

constgetCustomValueSnippet=({value, name, type})=>{// Here is your codereturnvalue};// The colorSnipept function accepts an object consisting of { value : HEX, name: string, type: color}addDecorator(withThemes(ThemeProvider,[theme],{ getCustomValueSnippet}));

BACKGROUND COLOR

This addon has ability to auto change background color when it detect a dark theme. By default it checks if the theme name contains 'dark'.

You can customize this behavior by passingonThemeSwitch function:

exportconstonThemeSwitch=context=>{const{ theme}=context;constbackground=theme.name==='Dark theme' ?'#2c2f33' :'white';constparameters={backgrounds:{default:background,},// Pass backgrounds: null to disable background switching at all};return{    parameters,};};constthemingDecorator=withThemes(null,[theme],{ onThemeSwitch});

This way you can have own checks of what the theme is selected and pass what ever color you need.

!important: The addon change background color on each theme selecting. In some scenarios you might want to disable this behavior e.g. if you already using addon-backgrounds. You can disable background switching by passingbackgrounds: null in parameters.

Below the use cases for most popular styling libraries:

Using with Emotion

// .storybook/preview.jsimport{ThemeProvider}from'@emotion/react';import{addDecorator}from'@storybook/react';import{withThemes}from'@react-theming/storybook-addon';import{theme}from'../src/theme';// pass ThemeProvider and array of your themes to decoratoraddDecorator(withThemes(ThemeProvider,[theme]));

💅 Using with Styled Components

// .storybook/preview.jsimport{ThemeProvider}from'styled-components';import{addDecorator}from'@storybook/react';import{withThemes}from'@react-theming/storybook-addon';import{theme}from'../src/theme';// pass ThemeProvider and array of your themes to decoratoraddDecorator(withThemes(ThemeProvider,[theme]));

Using with Material-UI

// theme.jsimport{red}from'@material-ui/core/colors';// A custom theme for this appconsttheme={palette:{primary:{main:'#556cd6',},secondary:{main:'#19857b',},error:{main:red.A400,},background:{default:'#fff',},},};exportdefaulttheme;
// .storybook/preview.jsimport{ThemeProvider}from'@material-ui/core';import{createMuiTheme}from'@material-ui/core/styles';import{addDecorator}from'@storybook/react';import{withThemes}from'@react-theming/storybook-addon';importthemefrom'../src/theme';constproviderFn=({ theme, children})=>{constmuTheme=createMuiTheme(theme);return<ThemeProvidertheme={muTheme}>{children}</ThemeProvider>;};// pass ThemeProvider and array of your themes to decoratoraddDecorator(withThemes(null,[theme],{ providerFn}));
// index.jsimportReactfrom'react';importReactDOMfrom'react-dom';import{ThemeProvider}from'@material-ui/core/styles';import{createMuiTheme}from'@material-ui/core/styles';importAppfrom'./App';importthemefrom'./theme';ReactDOM.render(<ThemeProvidertheme={createMuiTheme(theme)}><App/></ThemeProvider>,document.querySelector('#root'),);

There is an example app with CRA, Material-UI and Storybook AddonDemoSource

Credits

Created with ❤︎ toReact andStorybook byOleg Proskurin [React Theming]

Packages

No packages published

Contributors32


[8]ページ先頭

©2009-2025 Movatter.jp