UNPKG

7.07 kBMarkdownView Raw
1<center>
2 <img src="../docs/media/angular-hero.png" width="100%" />
3</center>
4
5<h1>Storybook Docs for Angular</h1>
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. Storybook Docs for Angular supports [DocsPage](../docs/docspage.md) for auto-generated docs, and [MDX](../docs/mdx.md) for rich long-form docs.
10
11To learn more about Storybook Docs, read the [general documentation](../README.md). To learn the Angular specifics, read on!
12
13- [Installation](#installation)
14- [DocsPage](#docspage)
15- [Props tables](#props-tables)
16- [MDX](#mdx)
17- [IFrame height](#iframe-height)
18- [More resources](#more-resources)
19
20## Installation
21
22First add the package. Make sure that the versions for your `@storybook/*` packages match:
23
24```sh
25yarn add -D @storybook/addon-docs@next
26```
27
28Then add the following to your `.storybook/main.js` exports:
29
30```js
31module.exports = {
32 addons: ['@storybook/addon-docs'],
33};
34```
35
36## DocsPage
37
38When you [install docs](#installation) you should get basic [DocsPage](../docs/docspage.md) documentation automagically for all your stories, available in the `Docs` tab of the Storybook UI.
39
40## Props tables
41
42Getting [Props tables](../docs/props-tables.md) for your components requires a few more steps. Docs for Angular relies on [Compodoc](https://compodoc.app/), the excellent API documentation tool. It supports `inputs`, `outputs`, `properties`, `methods`, `view/content child/children` as first class prop types.
43
44To get this, you'll first need to install Compodoc:
45
46```sh
47yarn add -D @compodoc/compodoc
48```
49
50Then you'll need to configure Compodoc to generate a `documentation.json` file. Adding the following snippet to your `package.json` creates a metadata file `./documentation.json` each time you run storybook:
51
52```json
53{
54 ...
55 "scripts": {
56 "docs:json": "compodoc -p ./tsconfig.json -e json -d .",
57 "storybook": "npm run docs:json && start-storybook -p 6006 -s src/assets",
58 ...
59 },
60}
61```
62
63Unfortunately, it's not currently possible to update this dynamically as you edit your components, but [there's an open issue](https://github.com/storybookjs/storybook/issues/8672) to support this with improvements to Compodoc.
64
65Next, add the following to `.storybook/preview.ts` to load the Compodoc-generated file:
66
67```js
68import { setCompodocJson } from '@storybook/addon-docs/angular';
69import docJson from '../documentation.json';
70setCompodocJson(docJson);
71```
72
73Finally, be sure to fill in the `component` field in your story metadata:
74
75```ts
76import { AppComponent } from './app.component';
77
78export default {
79 title: 'App Component',
80 component: AppComponent,
81};
82```
83
84If you haven't upgraded from `storiesOf`, you can use a parameter to do the same thing:
85
86```ts
87import { storiesOf } from '@storybook/angular';
88import { AppComponent } from './app.component';
89
90storiesOf('App Component', module)
91 .addParameters({ component: AppComponent })
92 .add( ... );
93```
94
95## MDX
96
97[MDX](../docs/mdx.md) is a convenient way to document your components in Markdown and embed documentation components, such as stories and props tables, inline.
98
99Docs has peer dependencies on `react`. If you want to write stories in MDX, you may need to add this dependency as well:
100
101```sh
102yarn add -D react
103```
104
105Then update your `.storybook/main.js` to make sure you load MDX files:
106
107```ts
108module.exports = {
109 stories: ['../src/stories/**/*.stories.@(js|ts|mdx)'],
110};
111```
112
113Finally, you can create MDX files like this:
114
115```md
116import { Meta, Story, ArgsTable } from '@storybook/addon-docs';
117import { AppComponent } from './app.component';
118
119<Meta title='App Component' component={AppComponent} />
120
121# App Component
122
123Some **markdown** description, or whatever you want.
124
125<Story name='basic' height='400px'>{{
126 component: AppComponent,
127 props: {},
128}}</Story>
129
130## ArgsTable
131
132<ArgsTable of={AppComponent} />
133```
134
135Yes, it's redundant to declare `component` twice. [Coming soon](https://github.com/storybookjs/storybook/issues/8673).
136
137Also, to use the `Props` doc block, you need to set up Compodoc, [as described above](#docspage).
138
139When you are using `template`, `moduleMetadata` and/or `addDecorators` with `storiesOf` then you can easily translate your story to MDX, too:
140
141```md
142import { Meta, Story, ArgsTable } from '@storybook/addon-docs';
143import { CheckboxComponent, RadioButtonComponent } from './my-components';
144import { moduleMetadata } from '@storybook/angular';
145
146<Meta title='Checkbox' decorators={[
147 moduleMetadata({
148 declarations: [CheckboxComponent]
149 })
150]} />
151
152# Basic Checkbox
153
154<Story name='basic check' height='400px'>{{
155 template: `
156 <div class="some-wrapper-with-padding">
157 <my-checkbox [checked]="checked">Some Checkbox</my-checkbox>
158 </div>
159 `,
160 props: {
161 checked: true
162 }
163}}</Story>
164
165# Basic Radiobutton
166
167<Story name='basic radio' height='400px'>{{
168 moduleMetadata: {
169 declarations: [RadioButtonComponent]
170 }
171 template: `
172 <div class="some-wrapper-with-padding">
173 <my-radio-btn [checked]="checked">Some Checkbox</my-radio-btn>
174 </div>
175 `,
176 props: {
177 checked: true
178 }
179}}</Story>
180```
181
182## IFrame height
183
184Storybook Docs renders all Angular stories inside IFrames, with a default height of `60px`. You can update this default globally, or modify the IFrame height locally per story in `DocsPage` and `MDX`.
185
186To update the global default, modify `.storybook/preview.ts`:
187
188```ts
189import { addParameters } from '@storybook/angular';
190
191addParameters({ docs: { iframeHeight: 400 } });
192```
193
194For `DocsPage`, you need to update the parameter locally in a story:
195
196```ts
197export const basic = () => ...
198basic.parameters = {
199 docs: { iframeHeight: 400 }
200}
201```
202
203And for `MDX` you can modify it as an attribute on the `Story` element:
204
205```md
206<Story name='basic' height='400px'>{...}</Story>
207```
208
209## Inline Stories
210
211Storybook Docs renders all Angular stories inside IFrames by default. But it is possible to use an inline rendering:
212
213Then update `.storybook/preview.js`:
214
215```js
216import { addParameters } from '@storybook/angular';
217
218addParameters({
219 docs: {
220 inlineStories: true,
221 },
222});
223```
224
225## More resources
226
227Want to learn more? Here are some more articles on Storybook Docs:
228
229- References: [DocsPage](../docs/docspage.md) / [MDX](../docs/mdx.md) / [FAQ](../docs/faq.md) / [Recipes](../docs/recipes.md) / [Theming](../docs/theming.md) / [Props](../docs/props-tables.md)
230- 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)
231- Example: [Storybook Design System](https://github.com/storybookjs/design-system)