{"id":15286555,"url":"https://github.com/react-theming/storybook-addon","last_synced_at":"2025-04-12T23:34:14.054Z","repository":{"id":37812292,"uuid":"73099725","full_name":"react-theming/storybook-addon","owner":"react-theming","description":"Develop themable components with Emotion/Styled Components/Material-UI with help of Storybook \u0026 React Theming","archived":false,"fork":false,"pushed_at":"2023-02-04T04:28:54.000Z","size":11437,"stargazers_count":207,"open_issues_count":3,"forks_count":37,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-21T13:58:19.533Z","etag":null,"topics":["addon","boilerplate","developer-tools","emotion","material-ui","material-ui-react","react","react-storybook","react-storybook-addons","react-theme","react-theming","storybook","styled-components","theme","theming","theming-boilerplate","theming-components"],"latest_commit_sha":null,"homepage":"https://react-theming.github.io/storybook-addon","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/react-theming.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-11-07T16:44:12.000Z","updated_at":"2025-02-20T10:07:15.000Z","dependencies_parsed_at":"2023-02-18T13:46:23.247Z","dependency_job_id":null,"html_url":"https://github.com/react-theming/storybook-addon","commit_stats":null,"previous_names":["sm-react/react-theming"],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/react-theming%2Fstorybook-addon","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/react-theming%2Fstorybook-addon/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/react-theming%2Fstorybook-addon/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/react-theming%2Fstorybook-addon/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/react-theming","download_url":"https://codeload.github.com/react-theming/storybook-addon/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246604612,"owners_count":20804100,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["addon","boilerplate","developer-tools","emotion","material-ui","material-ui-react","react","react-storybook","react-storybook-addons","react-theme","react-theming","storybook","styled-components","theme","theming","theming-boilerplate","theming-components"],"created_at":"2024-09-30T15:16:39.546Z","updated_at":"2025-04-04T02:09:22.984Z","avatar_url":"https://github.com/react-theming.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![npm version](https://badge.fury.io/js/%40react-theming%2Fstorybook-addon.svg)](https://badge.fury.io/js/%40react-theming%2Fstorybook-addon)\n[![Storybook](https://raw.githubusercontent.com/storybookjs/storybook-addon-console/master/docs/storybook.svg?sanitize=true)](https://react-theming.github.io/storybook-addon)\n\n# Storybook Addon @ React Theming\n\nStorybook addon for Styled Components, Emotion, Material-UI and any other theming solution. Allows to develop themed components in isolation.\n\n```shell\nnpm i --save-dev @react-theming/storybook-addon\n```\n[Demo](https://react-theming.github.io/storybook-addon)\n\n![example](https://raw.githubusercontent.com/react-theming/storybook-addon/master/docs/theme-panel.png)\n\n## Features :dizzy:\n\n- Universal - can be used with any styling library\n- Switching between themes from addon panel.\n- Change a color and see how it affects to your components\n- Easily copy-paste paths of nesting theme props into your code\n- Auto changes background\n- Supports dark Storybook theme\n- Keep selected theme on stories updates\n\n\n## Usage\n\nspecify addon in `.storybook/main.js`\n\n```js\n// .storybook/main.js\n\nmodule.exports = {\n  stories: ['../src/**/*.stories.js'],\n  addons: ['@react-theming/storybook-addon'],\n};\n```\n\nor in `.storybook/addons.js` for older versions of Storybook\n\n```js\nimport '@react-theming/storybook-addon/register';\n\n```\n\nThen 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 use **any of theme providers** you're using in your project.\n\n```js\nimport ThemeProvider from 'library-of-your-choice';\nimport { withThemes } from '@react-theming/storybook-addon';\nimport { theme } from '../src/theme';\n\n// create decorator\nconst themingDecorator = withThemes(ThemeProvider, [theme]);\n```\n\nThemeProvider should accept a theme via `theme` props. This is usually the case for the most common styling libraries like Styled Components, Emotion, Material-UI.\n\nIn case of non standard ThemeProvider you can pass `providerFn` function in options:\n\n```js\nconst providerFn = ({ theme, children }) =\u003e {\n  return \u003cThemeProvider theme={muTheme}\u003e{children}\u003c/ThemeProvider\u003e;\n};\n\nconst themingDecorator = withThemes(null, [theme], { providerFn });\n```\n\n## Use your output of the selected value\n\n```js\n// .storybook/preview.js\n\nimport { ThemeProvider } from 'styled-components';\nimport { addDecorator } from '@storybook/react';\nimport { withThemes } from '@react-theming/storybook-addon';\n\nimport { theme } from '../src/theme';\n\n```\n\n### Example getCustomFieldSnippet\n\n```js\nconst selectedValue = {\n  name: \"accent5\",\n  namespace: [\"palette\", \"colors\"],\n  type: \"color\",\n  value: \"#ac924d\"\n}\n\n\nconst getCustomFieldSnippet = selectedValue =\u003e {\n  const { namespace, name } = selectedValue;\n  const path = namespace.join('.');\n  const fullPath = `${path}.${name}`;\n  const themeProp = `\\${({ theme }) =\u003e theme.${fullPath}}`;\n  return themeProp;\n};\n\n// The snippet Func function takes the SelectedValue parameter and returns a string\naddDecorator(withThemes(ThemeProvider, [theme], { getCustomFieldSnippet }));\n\n```\n\n### Example getCustomValueSnippet\n\nBy default, the addon outputs colors in HEX format, if you need some kind of add-in, then pass the colorSnippet parameter.\n\n```js\nconst getCustomValueSnippet = ({value, name, type}) =\u003e {\n  // Here is your code\n  return value\n};\n\n// The colorSnipept function accepts an object consisting of { value : HEX, name: string, type: color}\naddDecorator(withThemes(ThemeProvider, [theme], { getCustomValueSnippet }));\n\n```\n\nBACKGROUND COLOR\n\nThis addon has ability to auto change background color when it detect a dark theme. By default it checks if the theme name contains 'dark'.\n\nYou can customize this behavior by passing `onThemeSwitch` function:\n\n```js\nexport const onThemeSwitch = context =\u003e {\n  const { theme } = context;\n  const background = theme.name === 'Dark theme' ? '#2c2f33' : 'white';\n  const parameters = {\n    backgrounds: {\n      default: background,\n    },\n    // Pass backgrounds: null to disable background switching at all\n  };\n  return {\n    parameters,\n  };\n};\n\nconst themingDecorator = withThemes(null, [theme], { onThemeSwitch });\n```\n\nThis way you can have own checks of what the theme is selected and pass what ever color you need.\n\n!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 passing `backgrounds: null` in parameters.\n\n\nBelow the use cases for most popular styling libraries:\n\n## Using with Emotion\n\n```js\n// .storybook/preview.js\n\nimport { ThemeProvider } from '@emotion/react';\nimport { addDecorator } from '@storybook/react';\nimport { withThemes } from '@react-theming/storybook-addon';\n\nimport { theme } from '../src/theme';\n\n// pass ThemeProvider and array of your themes to decorator\naddDecorator(withThemes(ThemeProvider, [theme]));\n```\n\n\n## 💅 Using with Styled Components\n\n```js\n// .storybook/preview.js\n\nimport { ThemeProvider } from 'styled-components';\nimport { addDecorator } from '@storybook/react';\nimport { withThemes } from '@react-theming/storybook-addon';\n\nimport { theme } from '../src/theme';\n\n// pass ThemeProvider and array of your themes to decorator\naddDecorator(withThemes(ThemeProvider, [theme]));\n```\n\n\n## Using with Material-UI\n\n```js\n// theme.js\nimport { red } from '@material-ui/core/colors';\n\n// A custom theme for this app\nconst theme = {\n  palette: {\n    primary: {\n      main: '#556cd6',\n    },\n    secondary: {\n      main: '#19857b',\n    },\n    error: {\n      main: red.A400,\n    },\n    background: {\n      default: '#fff',\n    },\n  },\n};\n\nexport default theme;\n```\n\n```js\n// .storybook/preview.js\n\nimport { ThemeProvider } from '@material-ui/core';\nimport { createMuiTheme } from '@material-ui/core/styles';\nimport { addDecorator } from '@storybook/react';\nimport { withThemes } from '@react-theming/storybook-addon';\n\nimport theme from '../src/theme';\n\nconst providerFn = ({ theme, children }) =\u003e {\n  const muTheme = createMuiTheme(theme);\n  return \u003cThemeProvider theme={muTheme}\u003e{children}\u003c/ThemeProvider\u003e;\n};\n\n// pass ThemeProvider and array of your themes to decorator\naddDecorator(withThemes(null, [theme], { providerFn }));\n```\n\n```js\n// index.js\n\nimport React from 'react';\nimport ReactDOM from 'react-dom';\nimport { ThemeProvider } from '@material-ui/core/styles';\nimport { createMuiTheme } from '@material-ui/core/styles';\nimport App from './App';\nimport theme from './theme';\n\nReactDOM.render(\n  \u003cThemeProvider theme={createMuiTheme(theme)}\u003e\n    \u003cApp /\u003e\n  \u003c/ThemeProvider\u003e,\n  document.querySelector('#root'),\n);\n\n```\n\nThere is an example app with CRA, Material-UI and Storybook Addon [Demo](https://react-theming.github.io/theming-material-ui/) [Source](https://github.com/react-theming/theming-material-ui)\n\n## Credits\n\n\u003cdiv align=\"left\" style=\"height: 16px;\"\u003eCreated with ❤︎ to \u003cb\u003eReact\u003c/b\u003e and \u003cb\u003eStorybook\u003c/b\u003e by \u003ca\n    href=\"https://twitter.com/UsulPro\"\u003eOleg Proskurin\u003c/a\u003e [\u003ca href=\"https://github.com/react-theming\"\u003eReact Theming\u003c/a\u003e]\n\u003c/div\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freact-theming%2Fstorybook-addon","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Freact-theming%2Fstorybook-addon","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Freact-theming%2Fstorybook-addon/lists"}