Hopp til hovedinnhold

Tips og triks / 5 minutter /

Setting up Storybook 7 with Vite and Tailwind CSS

Setting up Storybook with Tailwind can be quite a challenge, and there are a lot of recipes out there that either don’t work or are outdated. The Storybook web site points out the highlights of using Storybook 7 with Vite:

  • 🏎️ Pre-bundled for performance
  • 🙅 No Webpack and smaller install size
  • 🪄 Zero config
  • 🌐 Vue 2 + 3, Svelte, React, and more

And I managed to easily set this up in three steps:

  1. Create a project with Vite
  2. Add Tailwind
  3. Add Storybook

1 Create the Vite project

A) Create a new project from a terminal

npm create vite@latest
view raw konsoll1.txt hosted with ❤ by GitHub

I opted for a React project with TypeScript

Screenshot from the terminal where I chose React and TypeScript
Screenshot from the terminal where I chose React and TypeScript

B) In the terminal change directory to the project and run npm install - or just npm i

cd my-vite-storybook-tailwind-project/
npm i
view raw konsoll1b.txt hosted with ❤ by GitHub

2 Add Tailwind to the project

A) Install Tailwind

npm install -D tailwindcss postcss autoprefixer
view raw konsoll4.txt hosted with ❤ by GitHub

B) Init the Tailwind config

npx tailwindcss init -p
view raw konsoll5.txt hosted with ❤ by GitHub

C) Edit the config file

Now you have created both a tailwind and a postcss config file. You need to add the right file types that you want to style with Tailwind in the file tailwind.config.cjs:

/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
"./index.html",
"./src/**/*.{js,ts,jsx,tsx}",
],
theme: {
extend: {},
},
plugins: [],
}
The document structure shown in VS Code
The document structure shown in VS Code

In the root folder of your project, you’ll find the Tailwind config file

In the root folder of your project, you’ll find the Tailwind config file

D) Add the Tailwind directives to .src/index.css

@tailwind base;
@tailwind components;
@tailwind utilities;
view raw index.css hosted with ❤ by GitHub
Showing the content of the config file in VS Code
Showing the content of the config file in VS Code

Replace the content of index.css

Replace the content of index.css

E) Check if it’s working like it should

In a terminal, start the dev server

npm run dev
view raw konsoll6.txt hosted with ❤ by GitHub
Showing the terminal window with the server running
Showing the terminal window with the server running

The server is now running, and you can open it in a browser.

The server is now running, and you can open it in a browser.

Add some Tailwind code to .src/App.tsx and to verify that it works as it should.

<h1 className="bg-orange-200 font-bold p-4">Vite + React + Tailwind</h1>
view raw App.tsx hosted with ❤ by GitHub

F) Now Tailwind should be working in your app

The web page with Tailwind styling in the browser
The web page with Tailwind styling in the browser

3 Add Storybook 7

A) Open a new terminal and add Storybook 7 to the project

npx sb@next init --builder=vite
view raw konsoll7.txt hosted with ❤ by GitHub

If you are asked to migrate the project to npm7, answer y.

Screenshot where you are prompted if you want to migrate the project to npm7
Screenshot where you are prompted if you want to migrate the project to npm7

B) Import the tailwind.css file to the .storybook preview.js file

import 'tailwindcss/tailwind.css'
export const parameters = {
actions: { argTypesRegex: "^on[A-Z].*" },
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/,
},
},
}
view raw preview.js hosted with ❤ by GitHub

C) Add some Tailwind classes to one of the stories

For instance you can add a nice orange background to the header element in .src/stories/Header.tsx:

<header className='bg-orange-200'>
view raw Header.tsx hosted with ❤ by GitHub
Screenshot from CS Code showing changes in line 18 of the file
Screenshot from CS Code showing changes in line 18 of the file

Showing the changes to the Header.tsx file

Showing the changes to the Header.tsx file

D) The result in Storybook should be something like this:

Screenshot of Storybook with Tailwind styles
Screenshot of Storybook with Tailwind styles

References and tips

This blog post is based on the following articles:


To get Tailwind suggestions and code completion, I use the Tailwind IntelliSense extension in VS Code. It will also show you the CSS for the different Tailwind classes, which is pretty handy. Happy coding!