- Notifications
You must be signed in to change notification settings - Fork37
Develop themable components with Emotion/Styled Components/Material-UI with help of Storybook & React Theming
License
react-theming/storybook-addon
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
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
- 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
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});
// .storybook/preview.jsimport{ThemeProvider}from'styled-components';import{addDecorator}from'@storybook/react';import{withThemes}from'@react-theming/storybook-addon';import{theme}from'../src/theme';
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}));
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:
// .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]));
// .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]));
// 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
About
Develop themable components with Emotion/Styled Components/Material-UI with help of Storybook & React Theming
Topics
Resources
License
Code of conduct
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
