Setting up Storybook for Preact with TypeScript
Storybook is a useful tool for visualising what your app’s front-end components will look like across different scenarios.
This guide will cover how you can add Storybook to your TypeScript Preact app.
Creating a Preact app using TypeScript
If you don’t already have a Preact app using TypeScript, you can easily create one using the preact-cli tool:
Remember to cd
into your app’s folder after you’ve created it!
Installing and setting up Storybook
Install Storybook for Preact:
Then create a .storybook/main.js
file, and add the following:
Note that you will have to modify the above code if you are not using
ts-loader
Then you can add a new script to your package.json
file:
(Optional) delete the declaration.d.ts file
If you created your app using the preact-cli tool, I found that deleting the declaration.d.ts
file in your src
folder was required to get Storybook working. I’m not too sure why, but give it a go if things are breaking for you.
Write your first story
Create a file called story.tsx
and import in one of your components:
Now if you run the command:
You will be able to see your storybooks in action.
Happy coding!