Storybook Info Addon
Storybook Info Addon will show additional information for your stories in Storybook.
Useful when you want to display usage or other types of documentation alongside your story.
Framework Support
Installation
Install the following npm module:
npm i -D @storybook/addon-info
Basic usage
Then, add withInfo
as a decorator to your book of stories.
It is possible to add info
by default to all or a subsection of stories by using a global or story decorator.
It is important to declare this decorator as the first decorator, otherwise it won't work well.
import { addDecorator } from '@storybook/react';
import { withInfo } from '@storybook/addon-info';
addDecorator(withInfo);
or
export default {
title: 'Component',
decorators: [withInfo],
};
Then, you can use the info
parameter to either pass certain options or specific documentation text to your stories.
A complete list of possible configurations can be found in a later section.
This can be done per book of stories:
import Component from './Component';
export default {
title: 'Component',
parameters: {
info: {},
},
};
...or for each story individually:
import Component from './Component';
export default {
title: 'Component',
};
export const defaultView = () => <Component />;
defaultView = {
parameters: {
info: { inline: true },
},
};
It is also possible to disable the info
addon entirely.
Depending 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.
info: {
disable: true,
}
Markdown
The info
addon also supports markdown.
To 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.
info: {
text: `
description or documentation about my component, supports markdown
~~~js
<Button>Click Here</Button>
~~~
`,
}
Setting Global Options
To configure default options for all usage of the info addon, pass a option object along with the decorator in .storybook/preview.js
.
import { withInfo } from '@storybook/addon-info';
addDecorator(
withInfo({
header: false,
})
);
Configuration 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.
In order, all of them will be combined together, with a later call overriding the previous set configurations on a per-key basis.
Options and Defaults
{
text?: string;
inline: boolean,
header: boolean,
source: boolean,
propTables: Array<React.ComponentType>,
propTablesExclude: Array<React.ComponentType>,
styles: Object | Function,
components: { [key: string]: React.ComponentType },
maxPropsIntoLine: number,
maxPropObjectKeys: number,
maxPropArrayLength: number,
maxPropStringLength: number,
TableComponent: React.ComponentType,
excludedPropTypes: Array<string>,
}
Rendering a Custom Table
The TableComponent
option allows you to define how the prop table should be rendered. Your component will be rendered with the following props.
{
propDefinitions: Array<{
property: string,
propType: Object | string,
required: boolean,
description: string,
defaultValue: any
}>
}
Example:
import React from 'react';
const paddingStyles = {
small: '4px 8px',
medium: '8px 16px',
};
const Button = ({
size,
...rest
}: {
/** The size of the button */
size: 'small' | 'medium',
}) => {
const style = {
padding: paddingStyles[size] || '',
};
return <button style={style} {...rest} />;
};
Button.defaultProps = {
size: 'medium',
};
export default Button;
import React from 'react';
import { storiesOf } from '@storybook/react';
import Button from './button';
export default {
title: 'Button',
component: Button,
parameters: {
info: TableComponent,
},
};
const Red = props => <span style={{ color: 'red' }} {...props} />;
const TableComponent = ({ propDefinitions }) => {
const props = propDefinitions.map(
({ property, propType, required, description, defaultValue }) => {
return (
<tr key={property}>
<td>
{property}
{required ? <Red>*</Red> : null}
</td>
<td>{propType.name}</td>
<td>{defaultValue}</td>
<td>{description}</td>
</tr>
);
}
);
return (
<table>
<thead>
<tr>
<th>name</th>
<th>type</th>
<th>default</th>
<th>description</th>
</tr>
</thead>
<tbody>{props}</tbody>
</table>
);
};
export const defaultView = () => <Button />;
React Docgen Integration
React Docgen is included as part of the @storybook/react package through the use of babel-plugin-react-docgen
during babel compile time.
When 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.
import React from 'react';
import PropTypes from 'prop-types';
const DocgenButton = ({ disabled, label, style, onClick }) => (
<button disabled={disabled} style={style} onClick={onClick}>
{label}
</button>
);
DocgenButton.defaultProps = {
disabled: false,
onClick: () => {},
style: {},
};
DocgenButton.propTypes = {
disabled: PropTypes.bool,
label: PropTypes.string.isRequired,
onClick: PropTypes.func,
style: PropTypes.shape,
};
export default DocgenButton;
Comments 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.
The FAQ
Components lose their names on static build
Component names also get minified with other javascript code when building for production.
When creating components, set the displayName
static property to show the correct component name on static builds.