A while back I posted Getting started with Storybook. However, like with most things JavaScript, things have changed, and those instructions are now out of date and also, now, much simpler.
I have an existing React web app. so how do I add storybook ?
Note: Do not add storybook from NPM via yarn/npm, instead use the following instructions.
From you web app’s root folder run the following from the root of an existing project
npx storybook init
This will try to detect the framework being use. In the case of React this worked a treat. It will then add a stories folder to your src folder with a bunch of examples. It adds the .storybook folder with the information to tell storybook what files to look for and, ofcourse, add all the package.json dependencies etc.
Now all you need do is run
yarn storybook
So simple.
Here’s a very simple example of a .stories.tsx. I have a Header component which simpler writes a string out for a given date.
import React from 'react'; import { ComponentStory, ComponentMeta } from '@storybook/react'; import { Header } from '../controls/Header'; export default { title: 'Header', component: Header, parameters: { layout: 'fullscreen', }, } as ComponentMeta<typeof Header>; const Template: ComponentStory<typeof Header> = (args) => <Header {...args} />; export const Display = Template.bind({}); Display.args = { endDateTime: new Date(Date.parse("2022-12-31T23:59:00.000Z")) };
The Display export is shown as a view on a component within storybook and we also get the endDateTime editor for trying different inputs out.