Component Story Format 3 is hereAutomate with Chromatic
Storybook Day 2023
Back to integrations
Add your integration
  • ⭐️ Popular
  • 🧩 Essentials
  • 🛠 Code
  • ⚡️ Data & state
  • ✅ Test
  • 💅 Style
  • 🎨 Design
  • ⚙️ Appearance
  • 🗄 Organize
How to install addons Create an addon

Integrate Emotion and Storybook

Emotion is a css-in-js framework to build fast and functional components. This recipe shows you how to get the most out of Emotion in Storybook.

This recipe assumes that you have a React app using Emotion and have just set up Storybook >=6.0 using the getting started guide. Don’t have this? Follow Emotion's installation instructions then run:

How to setup Emotion and Storybook

Emotion is a popular library for building UI components with CSS-in-JS, while Storybook is a tool for creating and testing UI components in isolation. This post will show you how to integrate these two tools to create a powerful and flexible development environment for building user interfaces with Emotion.

This post will explain how to:

  1. 🔌 Setup Global styles
  2. 🧱 Use Emotion in your components
  3. 💅 Use a theme in your stories
  4. 🎨 Switch betweens themes in a click

If you’d like to see the example code of this recipe, check out the example repository on GitHub. Let's get started!

Completed Emotion example with theme switcher

How to setup Global styles

UIs often have a set of global styles that are applied to every component like CSS resets, font-size, font-family, and colors.

In Emotion, use the Global to scope styles globally instead of locally (which is the library's default behavior).

Open .storybook/preview.js and create a Global component which includes a font-family. Then apply it to all stories via a decorator.

If you already have Global in your app, you can import it into .storybook/preview.js instead of creating it anew.

Using Emotion in components

Let’s update some of our example components to use Emotion instead. Open up the Button component in ./src/stories/button.js. and replace it with the following code:

Now the Button component is made with Emotion. In Storybook, you won't notice a visual difference. But if you inspect the DOM, you'll see hashed CSS-in-JS classnames.

Provide a theme for Emotion in Storybook

Switching over to using a theme for emotion in storybook

One of the benefits of Emotion is that you can provide a theme to help you style all of your components in a consistent way. Let's create a new ./src/theme.js and add the following light theme:

To share this theme with the components in Storybook, you'll need a decorator.

Below I created a new file in .storybook called withTheme.decorator.js that will wrap your stories with Emotion's ThemeProvider.

All that is left to do is give this decorator to Storybook. Add the decorator to the decorators array in .storybook/preview.js:

Now, components made with Emotion will get the theme through the theme prop along with the styles inherited from Global. Let's update the example components to use the theme.

Add a theme switcher tool using globalTypes

Dark mode has become an increasingly popular offering on the web. This can be achieved quickly using themes.

Completed Emotion example with theme switcher

Let's add the following dark theme to theme.js

Now, to get the most out of your stories, there should be a way to toggle between themes in a click.

Completed Emotion example with theme switcher

To add the switcher, declare a global type named theme in .storybook/preview.js and give it a list of supported themes to choose from.

This code will create a new toolbar menu to select your desired theme for your stories.

Update withTheme to change themes

The last step to switch between themes is to update the withTheme decorator to change the theme based on the selected value of the theme global variable.

Get involved

Now you're ready to use Emotion with Storybook. 🎉 Check out the example repo for a quick start.

If you use Emotion at work, we'd love your help making an addon that automatically applies the configuration above. Join the maintainers in Discord to get involved, or jump into addon docs.

Join the community
5,898 developers and counting
WhyWhy StorybookComponent-driven UI
Open source software

Maintained by
Special thanks to Netlify and CircleCI