UNPKG

10.2 kBMarkdownView Raw
1<center>
2 <img src="https://raw.githubusercontent.com/storybookjs/storybook/main/addons/docs/docs/media/hero.png" width="100%" />
3</center>
4
5# Storybook Docs
6
7> migration guide: This page documents the method to configure storybook introduced recently in 5.3.0, consult the [migration guide](https://github.com/storybookjs/storybook/blob/next/MIGRATION.md) if you want to migrate to this format of configuring storybook.
8
9Storybook Docs transforms your Storybook stories into world-class component documentation.
10
11**DocsPage.** Out of the box, all your stories get a `DocsPage`. `DocsPage` is a zero-config aggregation of your component stories, text descriptions, docgen comments, props tables, and code examples into clean, readable pages.
12
13**MDX.** If you want more control, `MDX` allows you to write long-form markdown documentation and stories in one file. You can also use it to write pure documentation pages and embed them inside your Storybook alongside your stories.
14
15Just like Storybook, Docs supports every major view layer including React, Vue, Angular, HTML, Web components, Svelte, and many more.
16
17Read on to learn more:
18
19- [Storybook Docs](#storybook-docs)
20 - [DocsPage](#docspage)
21 - [MDX](#mdx)
22 - [Framework support](#framework-support)
23 - [Installation](#installation)
24 - [Be sure to check framework specific installation needs](#be-sure-to-check-framework-specific-installation-needs)
25 - [Preset options](#preset-options)
26 - [Manual configuration](#manual-configuration)
27 - [TypeScript configuration](#typescript-configuration)
28 - [More resources](#more-resources)
29
30## DocsPage
31
32When you [install Docs](#installation), every story gets a `DocsPage`. `DocsPage` pulls information from your stories, components, source code, and story metadata to construct a sensible, zero-config default.
33
34Click on the `Docs` tab to see it:
35
36<center>
37 <img src="https://raw.githubusercontent.com/storybookjs/storybook/main/addons/docs/docs/media/docs-tab.png" width="100%" />
38</center>
39
40For more information on how it works, see the [`DocsPage` reference](https://github.com/storybookjs/storybook/tree/next/addons/docs/docs/docspage.md).
41
42## MDX
43
44`MDX` is a syntax for writing long-form documentation and stories side-by-side in the same file. In contrast to `DocsPage`, which provides smart documentation out of the box, `MDX` gives you full control over your component documentation.
45
46Here's an example file:
47
48```md
49import { Meta, Story, Canvas } from '@storybook/addon-docs';
50import { Checkbox } from './Checkbox';
51
52<Meta title="MDX/Checkbox" component={Checkbox} />
53
54# Checkbox
55
56With `MDX` we can define a story for `Checkbox` right in the middle of our
57markdown documentation.
58
59<Canvas>
60 <Story name="all checkboxes">
61 <form>
62 <Checkbox id="Unchecked" label="Unchecked" />
63 <Checkbox id="Checked" label="Checked" checked />
64 <Checkbox appearance="secondary" id="second" label="Secondary" checked />
65 </form>
66 </Story>
67</Canvas>
68```
69
70And here's how that's rendered in Storybook:
71
72<center>
73 <img src="https://raw.githubusercontent.com/storybookjs/storybook/main/addons/docs/docs/media/mdx-simple.png" width="100%" />
74</center>
75
76For more information on `MDX`, see the [`MDX` reference](https://github.com/storybookjs/storybook/tree/next/addons/docs/docs/mdx.md).
77
78## Framework support
79
80Storybook Docs supports all view layers that Storybook supports except for React Native (currently). There are some framework-specific features as well, such as props tables and inline story rendering. The following page captures the current state of support:
81
82[Framework Support](https://storybook.js.org/docs/react/api/frameworks-feature-support)
83
84
85**Note:** `#` = WIP support
86
87Want to add enhanced features to your favorite framework? Check out this [dev guide](https://github.com/storybookjs/storybook/tree/next/addons/docs/docs/multiframework.md)
88
89## Installation
90
91First add the package. Make sure that the versions for your `@storybook/*` packages match:
92
93```sh
94yarn add -D @storybook/addon-docs
95```
96
97Docs has peer dependencies on `react`. If you want to write stories in MDX, you may need to add this dependency as well:
98
99```sh
100yarn add -D react
101```
102
103Then add the following to your `.storybook/main.js`:
104
105```js
106module.exports = {
107 stories: ['../src/**/*.stories.@(js|mdx)'],
108 addons: ['@storybook/addon-docs'],
109};
110```
111
112If using in conjunction with the [storyshots add-on](https://github.com/storybookjs/storybook/blob/next/addons/storyshots/storyshots-core/README.md), you will need to
113configure Jest to transform MDX stories into something Storyshots can understand:
114
115Add the following to your Jest configuration:
116
117```json
118{
119 "transform": {
120 "^.+\\.[tj]sx?$": "babel-jest",
121 "^.+\\.mdx$": "@storybook/addon-docs/jest-transform-mdx"
122 }
123}
124```
125
126### Be sure to check framework specific installation needs
127
128- [React](https://github.com/storybookjs/storybook/tree/next/addons/docs/react) (covered here)
129- [Vue](https://github.com/storybookjs/storybook/tree/next/addons/docs/vue)
130- [Angular](https://github.com/storybookjs/storybook/tree/next/addons/docs/angular)
131- [Ember](https://github.com/storybookjs/storybook/tree/next/addons/docs/ember)
132- [Web Components](https://github.com/storybookjs/storybook/tree/next/addons/docs/web-components)
133- [Common setup (all other frameworks)](https://github.com/storybookjs/storybook/tree/next/addons/docs/common)
134
135## Preset options
136
137The `addon-docs` preset has a few configuration options that can be used to configure its babel/webpack loading behavior. Here's an example of how to use the preset with options:
138
139```js
140module.exports = {
141 addons: [
142 {
143 name: '@storybook/addon-docs',
144 options: {
145 configureJSX: true,
146 babelOptions: {},
147 sourceLoaderOptions: null,
148 transcludeMarkdown: true,
149 },
150 },
151 ],
152};
153```
154
155The `configureJSX` option is useful when you're writing your docs in MDX and your project's babel config isn't already set up to handle JSX files. `babelOptions` is a way to further configure the babel processor when you're using `configureJSX`.
156
157`sourceLoaderOptions` is an object for configuring `@storybook/source-loader`. When set to `null` it tells docs not to run the `source-loader` at all, which can be used as an optimization, or if you're already using `source-loader` in your `main.js`.
158
159The `transcludeMarkdown` option enables mdx files to import `.md` files and render them as a component.
160
161```mdx
162import { Meta } from '@storybook/addon-docs';
163import Changelog from '../CHANGELOG.md';
164
165<Meta title="Changelog" />
166
167<Changelog />
168```
169
170## Manual configuration
171
172We recommend using the preset, which should work out of the box. If you don't want to use the preset, and prefer to configure "the long way" add the following configuration to `.storybook/main.js` (see comments inline for explanation):
173
174```js
175const createCompiler = require('@storybook/addon-docs/mdx-compiler-plugin');
176
177module.exports = {
178 // 1. register the docs panel (as opposed to '@storybook/addon-docs' which
179 // will configure everything with a preset)
180 addons: ['@storybook/addon-docs/register'],
181 // 2. manually configure webpack, since you're not using the preset
182 webpackFinal: async (config) => {
183 config.module.rules.push({
184 // 2a. Load `.stories.mdx` / `.story.mdx` files as CSF and generate
185 // the docs page from the markdown
186 test: /\.(stories|story)\.mdx$/,
187 use: [
188 {
189 // Need to add babel-loader as dependency: `yarn add -D babel-loader`
190 loader: require.resolve('babel-loader'),
191 // may or may not need this line depending on your app's setup
192 options: {
193 plugins: ['@babel/plugin-transform-react-jsx'],
194 },
195 },
196 {
197 loader: '@mdx-js/loader',
198 options: {
199 compilers: [createCompiler({})],
200 },
201 },
202 ],
203 });
204 // 2b. Run `source-loader` on story files to show their source code
205 // automatically in `DocsPage` or the `Source` doc block.
206 config.module.rules.push({
207 test: /\.(stories|story)\.[tj]sx?$/,
208 loader: require.resolve('@storybook/source-loader'),
209 exclude: [/node_modules/],
210 enforce: 'pre',
211 });
212 return config;
213 },
214};
215```
216
217You'll also need to set up the docs parameter in `.storybook/preview.js`. This includes the `DocsPage` for rendering the page, a container, and various configuration options, such as `extractComponentDescription` for manually extracting a component description:
218
219```js
220import { addParameters } from '@storybook/react';
221import { DocsPage, DocsContainer } from '@storybook/addon-docs';
222
223addParameters({
224 docs: {
225 container: DocsContainer,
226 page: DocsPage,
227 },
228});
229```
230
231## TypeScript configuration
232
233As of SB6 [TypeScript is zero-config](https://storybook.js.org/docs/react/configure/typescript) and should work with SB Docs out of the box. For advanced configuration options, refer to the [Props documentation](https://github.com/storybookjs/storybook/tree/next/addons/docs/docs/props-tables.md).
234
235## More resources
236
237Want to learn more? Here are some more articles on Storybook Docs:
238
239- References: [DocsPage](https://github.com/storybookjs/storybook/tree/next/addons/docs/docs/docspage.md) / [MDX](https://github.com/storybookjs/storybook/tree/next/addons/docs/docs/mdx.md) / [FAQ](https://github.com/storybookjs/storybook/tree/next/addons/docs/docs/faq.md) / [Recipes](https://github.com/storybookjs/storybook/tree/next/addons/docs/docs/recipes.md) / [Theming](https://github.com/storybookjs/storybook/tree/next/addons/docs/docs/theming.md) / [Props](https://github.com/storybookjs/storybook/tree/next/addons/docs/docs/props-tables.md)
240- Announcements: [Vision](https://medium.com/storybookjs/storybook-docs-sneak-peak-5be78445094a) / [DocsPage](https://medium.com/storybookjs/storybook-docspage-e185bc3622bf) / [MDX](https://medium.com/storybookjs/rich-docs-with-storybook-mdx-61bc145ae7bc) / [Framework support](https://medium.com/storybookjs/storybook-docs-for-new-frameworks-b1f6090ee0ea)
241- Example: [Storybook Design System](https://github.com/storybookjs/design-system)