Integrate Material UI and Storybook
Material UI is a component library based on Google's Material Design spec. This recipe shows you how to get the most out of Material UI in Storybook.
To get started, you'll need to install
Run the following script to install and register the addon:
Did the configuration script fail?
Under the hood, this runs
npx @storybook/auto-config themes, which should read your project and try to configure your Storybook with the correct decorator. If running that command directly does not solve your problem, please file a bug on the @storybook/auto-config repository so that we can further improve it. To manually add this addon, install it, and then add it to the addons array in your
Material UI depends on two fonts to render as intended, Google’s
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 entry point of your Storybook.
<ThemeProvider />, and your theme(s), then apply them to your stories with the
withThemeFromJSXProvider decorator by adding it to the
When you provide more than one theme, a toolbar menu will appear in the Storybook UI to select your desired theme for your stories.
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.
And now our Button will only take the variant, size, and color props from