Currently, there's an issue using MDX stories with IE11, which doesn't affect the Docs page. It's a known MDX issue, and once it's solved, Storybook's MDX implementation will be updated accordingly.
MDX is a standard file format that combines Markdown with JSX. It means you can use Markdownβs terse syntax (such as # heading) for your documentation, write stories that compile to our component story format, and freely embed JSX component blocks at any point in the file. All at once.
In addition, you can write pure documentation pages in MDX and add them to Storybook alongside your stories.
Let's get started with an example that combines Markdown with a single story:
Checkbox.stories.mdx
And here's how that's rendered in Storybook:
As you can see, a lot is going on here. We're writing Markdown, we're writing JSX, and we're also defining Storybook stories that are drop-in compatible with the entire Storybook ecosystem.
MDX-flavored Component Story Format (CSF) includes a collection of components called "Doc Blocks", that allow Storybook to translate MDX files into Storybook stories. MDX-defined stories are identical to regular Storybook stories, so they can be used with Storybook's entire ecosystem of addons and view layers.
For example, here's the first story from the Checkbox example above, rewritten in CSF:
Checkbox.stories.ts|tsx
There's a one-to-one mapping from the code in MDX to CSF. As a user, this means your existing Storybook knowledge should translate between the two.
Suppose you have an existing story and want to embed it into your docs. Here's how to show a story with ID some--id. Check the browser URL in Storybook v5+ to find a story's ID.
MyComponent.stories.mdx
You can also use the rest of the MDX features in conjunction with embedding. That includes source, preview, and prop tables.
Typically, when you use Storybook MDX, you define stories in the MDX, and documentation is automatically associated with those stories. But what if you want to write Markdown-style documentation without any stories inside?
Suppose you don't define stories in your MDX. In that case, you can write MDX documentation and associate it with an existing story or embed that MDX as its own documentation node in your Storybook's navigation.
If you don't define a Meta, you can write Markdown and associate with an existing story. See "CSF Stories with MDX Docs".
To get a "documentation-only story" in your UI, define a <Meta> as you normally would, but don't define any stories. It will show up in your UI as a documentation node:
When writing your documentation with Storybook and MDX, you get syntax highlighting out of the box for a handful of popular languages (Javascript, Markdown, CSS, HTML, Typescript, GraphQL). For other formats, for instance, SCSS, you'll need to extend the syntax highlighter manually:
MyComponent.stories.mdx
βΉοΈ
For a list of available languages, check react-syntax-highlighter's documentation.
Once you've updated your documentation, you'll see the code block properly highlighted. You can also apply the same principle to other unsupported formats (i.e., diff, hbs).
You can also update your .storybook/preview.js and enable syntax highlighting globally. For example, to add support for SCSS, update your configuration to the following:
.storybook/preview.js
Write your documentation as you usually would, and your existing SCSS code blocks will automatically be highlighted when Storybook reloads. For example:
Unless you use a custom Webpack configuration, all of your MDX files should have the suffix *.stories.mdx. This tells Storybook to apply its special processing to the <Meta> and <Story> elements in the file.
βΉοΈ
Be sure to update .storybook/main.js file to load .stories.mdx stories, as per the addon-docs installation instructions.
Starting with Storybook 6.5, MDX 2 is introduced as an experimental opt-in feature. To enable it, you'll need to take additional steps. Documented below is our recommendation.
Run the following command to add the necessary dependency.
Update your Storybook configuration (in .storybook/main.js|ts) and add the previewMdx2 feature flag as follows: