Material UI in Storybook
⚠️Update, 2023: A few things have changed since the full release of Storybook 7.0. For a more up-to-date guide to integrating Material UI in Storybook, check out our recipe in the integration catalog.
Material UI (MUI), based on Google’s Material Design language, provides a set of themeable components that you can use to begin building UI right away. You can get started even faster by developing your UI in isolation by building it in Storybook. Here’s how to configure Storybook to load Material UI components and dynamically interact with MUI’s API.
- 📦 Bundle your fonts for fast and consistent rendering
- 🎨 Load your custom theme and add a theme switcher
- ♻️ Reuse Material UI types to auto-generate story controls
Let’s get building
This recipe assumes that you already have a React app using the @mui/material
package set up with Storybook 6.0 or newer. If you don’t have a project ready, clone my example repository to follow along.
Bundle fonts and icons for better perf
Material UI depends on two fonts to render as intended, Google’s Roboto
and Material Icons
. While you can load these fonts directly from the Google Fonts CDN, bundling fonts with Storybook is better for performance.
- 🏎️ Fonts load faster because they are coming from the same place as your app
- ✈️ Font will load offline so you can continue developing your stories anywhere
- 📸 No more inconsistent snapshot tests because fonts load instantly
To get started, install the fonts as dependencies:
Then import the CSS files into .storybook/preview.js
, the entrypoint of your Storybook:
Load custom themes and add a theme switcher
Material UI comes with a default theme out of the box, but you can also create and provide your own themes. Given the popularity of dark mode, you'll likely end with more than one custom theme. Let's look at how you can load custom themes and switch between them with just a click.
For example, take this custom dark mode theme:
To apply the custom theme to our stories, we’ll need to wrap them in Material UI’s ThemeProvider
using a decorator:
Awesome! Now when Storybook is reloaded, you'll see that our withMuiTheme
decorator is providing our custom dark theme.
To automatically catch visual bugs across all themed UI variants, check out Storybook’s Visual Tests addon.
Use globalTypes to add a theme switcher
To take this decorator a step further, let’s add a way to toggle between multiple themes.
To do this, we can declare a global variable named theme in .storybook/preview.js
and give it a list of supported themes to choose from.
Now we can update our decorator to provide the theme selected in our new dropdown.
Now we have a fully functioning theme switcher for our MaterialUI Storybook. If you want to learn more about switchers, check out Yann Braga’s article on adding a theme switcher.
Use Material UI prop types for better controls and docs
Storybook controls give you graphical controls to manipulate a component’s props. They’re handy for finding edge cases of a component and prototyping in the browser.
Usually, you have to manually configure controls. But if you’re using Typescript, you can reuse Material UI’s component prop types to auto generate story controls. As a bonus, this will also automatically populate the prop table in your documentation tab.
Let’s take the following Button component for example.
Here I’m using the label prop as the MuiButton’s
child and passing all other props through. However, when we render this into Storybook, our controls panel only lets us change the label prop that we declared ourselves.
This is because Storybook only adds props to the controls table that are explicitly declared in the component’s prop types or in the Story Args. Let’s update Storybook’s Docgen configuration to bring Material UI‘s Button props into the controls table as well.
We also want to update the parameters in .storybook/preview.js
to show the description and default columns for the controls table.
Lastly, update the ButtonProps
type to extend from Material UI’s Button props to add all of these props to the controls.
Restart your Storybook server so that these config changes take effect. You should now see that Button has controls for all of MuiButton's
props as well.
Choose which controls are visible
Our button now has 27 props, which is perhaps a little much for your use case. To control which props are visible we can use TypeScript’s Pick<type, keys>
and Omit<type, keys>
utilities.
And now our Button will only take the variant, size, and color props from MuiButton.
📣 Shout out to Eric Mudrak’s awesome Storybook with React & TypeScript article that inspired this tip.
Wrapping up
Material UI has a lot to offer—a wide array of components, a powerful theming engine and an icon system. Instead of reinventing the wheel, you can use these building blocks to get going quickly. With a few configuration tweaks, your Storybook can unlock the full potential of Material UI.
Custom Material UI themes can be provided using a Storybook decorator and with an added toolbar item, you can toggle between multiple themes. This makes it easy to switch themes and verify the look and feel of the UI while building the app. Additionally, reuse Material UIs Typescript types to generate dynamic controls and documentation for your stories for free.
If you’re looking for a code example of what I covered here, check out my storybook-mui-example repo on GitHub. If you’re looking for a Storybook addon to handle themes for you, check out the React Theming Addon by awesome community members, Usulpro and Smartlight.
What do you want to see next?
We want to hear from you!
Did you enjoy this recipe? Are there other Storybook integrations that you want to see recipes for?
Tweet at @storybookjs or reach out on the Storybook Discord Server! We can't wait to meet you 🤩!
Stop reinventing the wheel! 🍩 @MaterialUI offers a wide array of themeable and accessible components. Use them to start building UIs immediately. Here are 3 helpful tips to get the most out of MUI with Storybook storybook.js.org/blog/material-… 1/🧵