|
| 1 | +# Applying Storybook to Components and Pages |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +StorybookJS is a UI tool for isolating UI components to visually test their styles and states. |
| 6 | + |
| 7 | +This is great for checking the various iterations of a component in a sandbox versus scowering all the pages in a large scale project it is used to verify that the component is rendering properly. |
| 8 | + |
| 9 | +You can also render pages if you need that level of visual testing. |
| 10 | + |
| 11 | +Storybook also gives you a library of addons provided by the team and the community to enhance the testing, including UX testing, A11y compliance, etc. |
| 12 | + |
| 13 | +Check out [Intro to Storybook](https://storybook.js.org/tutorials/intro-to-storybook/) to get an in-depth look on the workflow. |
| 14 | + |
| 15 | +## Spinning up the Storybook server |
| 16 | + |
| 17 | +It's as easy as running `yarn storybook` to boot up a dedicated localhost to see all the components that have stories. |
| 18 | + |
| 19 | +## Setting up a component's stories |
| 20 | + |
| 21 | +A Storybook "story" is an instance of a component in a certain state or with certain paramaters applied to show an alternative version of the component. |
| 22 | + |
| 23 | +Each component will only need one file containing all the stories, and should follow the naming convention of the component. |
| 24 | + |
| 25 | +So for the component `ExpandableCard.tsx`, the stories file will be names `ExpandableCard.stories.tsx`. |
| 26 | + |
| 27 | +The stories file will reside with each component. So the base folder structure in `src` will look like this: |
| 28 | + |
| 29 | +``` |
| 30 | +src/ |
| 31 | +└── components/ |
| 32 | + └── ComponentA/ |
| 33 | + ├── index.tsx |
| 34 | + ├── ComponentA.stories.tsx |
| 35 | + └── // Any other files as applicable (utils, child components, useHook, etc.) |
| 36 | +``` |
| 37 | + |
| 38 | +The initial structure of each story file will look something like this (in typescript): |
| 39 | + |
| 40 | +```tsx |
| 41 | +import ComponentA from "." |
| 42 | + |
| 43 | +export default { |
| 44 | + title: "ComponentA", // Generates the nav structure in the Storybook server |
| 45 | +} as ComponentMeta<typeof ComponentA> |
| 46 | + |
| 47 | +export const Basic = () => <ComponentA /> |
| 48 | +``` |
| 49 | + |
| 50 | +Should the component accept props on all or some renders, a template can be created. |
| 51 | + |
| 52 | +Let's say for a `Button` component with different style variants... |
| 53 | + |
| 54 | +```tsx |
| 55 | +import Button from "." |
| 56 | + |
| 57 | +export default { |
| 58 | + title: "Button", |
| 59 | +} as ComponentMeta<typeof Button> |
| 60 | + |
| 61 | +const Template: ComponentStory<typeof Button> = (args) => ( |
| 62 | + <ComponentA {...args} /> |
| 63 | +) |
| 64 | + |
| 65 | +export const Solid = Template.bind({}) |
| 66 | +Solid.args = { |
| 67 | + variant: "solid", |
| 68 | + children: "A Button", // Assuming the `children` prop takes text content only |
| 69 | +} |
| 70 | + |
| 71 | +export const Outline = Template.bind({}) |
| 72 | +Outline.args = { |
| 73 | + variant: "outline", |
| 74 | + children: "A Button", // Assuming the `children` prop takes text content only |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * For practical purposes, if you are displaying different "variants", |
| 79 | + * they should be shown under one story, so they can be seen side-by-side in the GUI |
| 80 | + * for reviewers to easily compare. |
| 81 | + * This can be done for various sizes or other like alterations |
| 82 | + */ |
| 83 | + |
| 84 | +// Assuming `solid` is the default variant in the Chakra theme config |
| 85 | +export const Variants = () => ( |
| 86 | + <VStack> |
| 87 | + <Button>A Solid Button</Button> |
| 88 | + <Button variant="outline">An Outline Button</Button> |
| 89 | + <Button variant="unstyled">An Unstyled Button</Button> |
| 90 | + </VStack> |
| 91 | +) |
| 92 | +``` |
| 93 | + |
| 94 | +As you go and make adjustments to the component itself or it's variant styles, Storybook will hot reload and those changes will appear in the stories that emphazise them. |
0 commit comments