Storybook for Angular
Automatic setup
You may have tried to use our quick start guide to setup your project for Storybook. If it failed because it couldn’t detect you’re using angular, you could try forcing it to use angular:
Manual setup
If you want to set up Storybook manually for your Angular project, this is the guide for you.
Step 1: Add dependencies
Add @storybook/angular
Add @storybook/angular
to your project. To do that, run:
Add @babel/core, and babel-loader
Make sure that you have @babel/core
, and babel-loader
in your dependencies as well because we list these as a peer dependencies:
Step 2: Add a npm script
Then add the following NPM script to your package.json
in order to start the storybook later in this guide:
Step 3: Create the main file
For a basic Storybook configuration, the only thing you need to do is tell Storybook where to find stories.
To do that, create a file at .storybook/main.js
with the following content:
That will load all the stories underneath your ../src
directory that match the pattern *.stories.[tj]s
. We recommend co-locating your stories with your source files, but you can place them wherever you choose.
Step 4: Storybook TypeScript configuration
@storybook/angular
is using ForkTsCheckerWebpackPlugin to boost the build performance.
This makes it necessary to create a tsconfig.json
file at .storybook/tsconfig.json
with the following content:
Step 5: Write your stories
Now create a ../src/index.stories.js
file, and write your first story like this:
Each story is a single state of your component. In the above case, there are two stories for the demo button component:
Finally: Run your Storybook
Now everything is ready. Run your storybook with:
Storybook should start, on a random open port in dev-mode.
Now you can develop your components and write stories and see the changes in Storybook immediately since it uses Webpack’s hot module reloading.