UNPKG

10.1 kBMarkdownView Raw
1# Storybook Info Addon
2
3Storybook Info Addon will show additional information for your stories in [Storybook](https://storybook.js.org).
4Useful when you want to display usage or other types of documentation alongside your story.
5
6[Framework Support](https://github.com/storybookjs/storybook/blob/master/ADDONS_SUPPORT.md)
7
8![Screenshot](https://raw.githubusercontent.com/storybookjs/storybook/HEAD/addons/info/docs/home-screenshot.png)
9
10## Installation
11
12Install the following npm module:
13
14```sh
15npm i -D @storybook/addon-info
16```
17
18## Basic usage
19
20Then, add `withInfo` as a decorator to your book of stories.
21It is possible to add `info` by default to all or a subsection of stories by using a global or story decorator.
22
23It is important to declare this decorator as **the first decorator**, otherwise it won't work well.
24
25```js
26// Globally in your .storybook/config.js.
27import { addDecorator } from '@storybook/react';
28import { withInfo } from '@storybook/addon-info';
29
30addDecorator(withInfo);
31```
32
33or
34
35```js
36storiesOf('Component', module)
37 .addDecorator(withInfo) // At your stories directly.
38 .add(...);
39```
40
41Then, you can use the `info` parameter to either pass certain options or specific documentation text to your stories.
42A complete list of possible configurations can be found at [in a later section](#setting-global-options).
43This can be done per book of stories:
44
45```js
46import { storiesOf } from '@storybook/react';
47
48import Component from './Component';
49
50storiesOf('Component', module)
51 .addParameters({
52 info: {
53 // Your settings
54 },
55 })
56 .add('with some emoji', () => <Component />);
57```
58
59...or for each story individually:
60
61```js
62import { storiesOf } from '@storybook/react';
63
64import Component from './Component';
65
66storiesOf('Component', module)
67 .add(
68 'with some emoji',
69 () => <Component emoji />,
70 { info: { inline: true, header: false } } // Make your component render inline with the additional info
71 )
72 .add(
73 'with no emoji',
74 () => <Component />,
75 { info: '☹️ no emojis' } // Add additional info text directly
76 );
77```
78
79...or even together:
80
81```js
82import { storiesOf } from '@storybook/react';
83
84import Component from './Component';
85
86storiesOf('Component', module)
87 .addParameters({
88 info: {
89 // Make a default for all stories in this book,
90 inline: true, // where the components are inlined
91 styles: {
92 header: {
93 h1: {
94 color: 'red', // and the headers of the sections are red.
95 },
96 },
97 },
98 },
99 })
100 .add('green version', () => <Component green />, {
101 info: {
102 styles: stylesheet => ({
103 // Setting the style with a function
104 ...stylesheet,
105 header: {
106 ...stylesheet.header,
107 h1: {
108 ...stylesheet.header.h1,
109 color: 'green', // Still inlined but with green headers!
110 },
111 },
112 }),
113 },
114 })
115 .add('something else', () => <Component different />, {
116 info: 'This story has additional text added to the info!', // Still inlined and with red headers!
117 });
118```
119
120It is also possible to disable the `info` addon entirely.
121Depending on the scope at which you want to disable the addon, pass the following parameters object either to an individual story or to an `addParameters` call.
122
123```
124{
125 info: {
126 disable: true
127 }
128}
129```
130
131## Markdown
132
133The `info` addon also supports markdown.
134To use markdown as additional textual documentation for your stories, either pass it directly as a String to the `info` parameters, or use the `text` option.
135
136```js
137storiesOf('Button', module).add('Button Component', () => <Button />, {
138 info: {
139 text: `
140 description or documentation about my component, supports markdown
141
142 ~~~js
143 <Button>Click Here</Button>
144 ~~~
145 `,
146 },
147});
148```
149
150## Setting Global Options
151
152To configure default options for all usage of the info addon, pass a option object along with the decorator in `.storybook/config.js`.
153
154```js
155// config.js
156import { withInfo } from '@storybook/addon-info';
157
158addDecorator(
159 withInfo({
160 header: false, // Global configuration for the info addon across all of your stories.
161 })
162);
163```
164
165Configuration parameters can be set at 3 different locations: passed as default options along the `addDecorator` call, passed as an object of parameters to a book of stories to the `addParameters` call, and passed as direct parameters to each individual story.
166In order, all of them will be combined together, with a later call overriding the previous set configurations on a per-key basis.
167
168## Options and Defaults
169
170```js
171{
172 /**
173 * Text to display with storybook component
174 */
175 text?: string;
176 /**
177 * Displays info inline vs click button to view
178 * @default false
179 */
180 inline: boolean,
181 /**
182 * Toggles display of header with component name and description
183 * @default true
184 */
185 header: boolean,
186 /**
187 * Displays the source of story Component
188 * @default true
189 */
190 source: boolean,
191 /**
192 * Components used in story
193 * Displays Prop Tables with these components
194 * @default []
195 */
196 propTables: Array<React.ComponentType>,
197 /**
198 * Exclude Components from being shown in Prop Tables section
199 * Accepts an array of component classes or functions
200 * @default []
201 */
202 propTablesExclude: Array<React.ComponentType>,
203 /**
204 * Overrides styles of addon. The object should follow this shape:
205 * https://github.com/storybookjs/storybook/blob/master/addons/info/src/components/Story.js#L19.
206 * This prop can also accept a function which has the default stylesheet passed as an argument
207 */
208 styles: Object | Function,
209 /**
210 * Overrides components used to display markdown
211 * @default {}
212 */
213 components: { [key: string]: React.ComponentType },
214 /**
215 * Max props to display per line in source code
216 * @default 3
217 */
218 maxPropsIntoLine: number,
219 /**
220 * Displays the first 10 characters of the prop name
221 * @default 3
222 */
223 maxPropObjectKeys: number,
224 /**
225 * Displays the first 10 items in the default prop array
226 * @default 3
227 */
228 maxPropArrayLength: number,
229 /**
230 * Displays the first 100 characters in the default prop string
231 * @default 50
232 */
233 maxPropStringLength: number,
234 /**
235 * Override the component used to render the props table
236 * @default PropTable
237 */
238 TableComponent: React.ComponentType,
239 /**
240 * Will exclude any respective properties whose name is included in array
241 * @default []
242 */
243 excludedPropTypes: Array<string>,
244}
245```
246
247### Rendering a Custom Table
248
249The `TableComponent` option allows you to define how the prop table should be rendered. Your component will be rendered with the following props.
250
251```js
252 {
253 propDefinitions: Array<{
254 property: string, // The name of the prop
255 propType: Object | string, // The prop type. TODO: info about what this object is...
256 required: boolean, // True if the prop is required
257 description: string, // The description of the prop
258 defaultValue: any // The default value of the prop
259 }>
260 }
261```
262
263Example:
264
265```js
266// button.js
267// @flow
268import React from 'react';
269
270const paddingStyles = {
271 small: '4px 8px',
272 medium: '8px 16px',
273};
274
275const Button = ({
276 size,
277 ...rest
278}: {
279 /** The size of the button */
280 size: 'small' | 'medium',
281}) => {
282 const style = {
283 padding: paddingStyles[size] || '',
284 };
285 return <button style={style} {...rest} />;
286};
287Button.defaultProps = {
288 size: 'medium',
289};
290
291export default Button;
292```
293
294```js
295// stories.js
296import React from 'react';
297
298import { storiesOf } from '@storybook/react';
299import Button from './button';
300
301const Red = props => <span style={{ color: 'red' }} {...props} />;
302
303const TableComponent = ({ propDefinitions }) => {
304 const props = propDefinitions.map(
305 ({ property, propType, required, description, defaultValue }) => {
306 return (
307 <tr key={property}>
308 <td>
309 {property}
310 {required ? <Red>*</Red> : null}
311 </td>
312 <td>{propType.name}</td>
313 <td>{defaultValue}</td>
314 <td>{description}</td>
315 </tr>
316 );
317 }
318 );
319
320 return (
321 <table>
322 <thead>
323 <tr>
324 <th>name</th>
325 <th>type</th>
326 <th>default</th>
327 <th>description</th>
328 </tr>
329 </thead>
330 <tbody>{props}</tbody>
331 </table>
332 );
333};
334
335storiesOf('Button', module).add('with text', () => <Button>Hello Button</Button>, {
336 info: {
337 TableComponent,
338 },
339});
340```
341
342### React Docgen Integration
343
344React Docgen is included as part of the @storybook/react package through the use of `babel-plugin-react-docgen` during babel compile time.
345When rendering a story with a React component commented in this supported format, the Addon Info description will render the comments above the component declaration and the prop table will display the prop's comment in the description column.
346
347```js
348import React from 'react';
349import PropTypes from 'prop-types';
350
351/** Button component description */
352const DocgenButton = ({ disabled, label, style, onClick }) => (
353 <button disabled={disabled} style={style} onClick={onClick}>
354 {label}
355 </button>
356);
357
358DocgenButton.defaultProps = {
359 disabled: false,
360 onClick: () => {},
361 style: {},
362};
363
364DocgenButton.propTypes = {
365 /** Boolean indicating whether the button should render as disabled */
366 disabled: PropTypes.bool,
367 /** button label. */
368 label: PropTypes.string.isRequired,
369 /** onClick handler */
370 onClick: PropTypes.func,
371 /** component styles */
372 style: PropTypes.shape,
373};
374
375export default DocgenButton;
376```
377
378Comments above flow types are also supported. Storybook Info Addon should now render all the correct types for your component if the PropTypes are in the same file as the React component.
379
380## The FAQ
381
382**Components lose their names on static build**
383
384Component names also get minified with other javascript code when building for production.
385When creating components, set the `displayName` static property to show the correct component name on static builds.