Storybook provides a tool to view, organize, and test React components in isolation.
See Storybook Docs for more detailed information.
React is a component based library where all UI elements in React are either a basic HTML element or a combination of React components. Storybook provides tooling to view React application components in isolation from the React app in a design pattern library. Storybook helps with documentation, design standards, and understanding component functionality.
To use Storybook start by running npm install
. This can be done from the project's root directory with:
$ npm run frontend-install
Storybook comes with a development server to be run locally.
$ npm run storybook
For deployment, Storybook builds to a static site in ./frontend/storybook-static/
by default. In our case, being we are running routing first through our backend application, we set the -o
to publish to /wwwroot/kop
so you can see the kop
output while running the application at /kop/index.html
.
$ npm run build-storybook
Storybook has been deployed to AWS S3 and routed through our dotnet application. All pushed branches will trigger builds and notify Slack when the build has begun.
Note: Stories will be written in the Component Story Format, the newest story format introduced in Storybook 5.2. This format allows stories to be written as ES6 modules.
Stories will be placed in the same folder as the related component. They should have the *.stories.tsx
extension added to the name of the component.
Example Folder Structure:
frontend\src\atoms\forms\input-text.tsx
frontend\src\atoms\forms\input-text.stories.tsx
Every story must have a default
export that contains metadata for the story. The title
field denotes the story hierarchy shown in the Storybook navigation menu. The format for the title should be <Atom Design Building Block> | <Domain> / <Component Name>
. The component
field contains the story's React component.
export default {
title: "Atoms | Forms / Input Text",
component: InputText,
};
Stories should be written as ES6 modules with the const
name representing the name shown in the Storybook navigation. In most cases the name can simply be the name of the component with "Default" added to the end. If various versions of a story need to be shown, the const
name should describe those variations. For example, inputTextDefault
and inputTextKnobs
. Storybook will change the camel case variable name to something that is human readable.
Stories just need to return something that can be rendered to the screen. The example below returns a basic React component. In this case only required props
have been passed into the component with other props
using default or remaining null
.
export const inputTextDefault = () => <InputText placeholder="Placeholder" />;
The Knobs addon has been added to allow interaction with component props
from within Storybook. Knobs adds a "Knobs" tabs inside the Storybook Panel where controls can be added to manipulate the component's props based on factors defined in the story.
Knobs controls are created by passing a "knob" to the component's props from the story. For instance, for the placeholder
prop
shown below the prop
receives the text
knob with a label
and defaultValue
. The label
should be the name of the prop
.
export const inputTextKnobs = () => (
<InputText
disabled={boolean("disabled", false)}
maxLength={number("maxLength", 20)}
placeholder={text("placeholder", "Placeholder")}
/>
);
Storybook will be organized based on Atomic Design principles. React components will be organized as follows:
- Particles - Design tokens including fonts, font sizes, colors, borders, etc. This will likely be handled by the Storybook Design Token Addon.
- Atoms - Foundational building blocks including basic HTML elements.
- Molecules - Simple UI combinations of atoms that function as a unit.
- Organisms - Complex UI combinations of atoms, molecules, or other organisms.
- Templates - Pages layouts with Atoms, Molecules, and Organisms to show content structure.
- Pages - Templates with real content to show design variations and to test the design system.
There are a number of Storybook Addons that could prove to be helpful in the future. They can be added at a later date as we have specific use cases.
- Viewport - For testing responsive designs.
- a11y - Accessibility checker.
- Info - This on is already in use, but could be used in the future for adding additional information to each story.
- Notes - For documenting stories.
- Storybook Design Token - To document design tokens.