DEV Community

SeongKuk Han
SeongKuk Han

Posted on

Next.js using Tailwind with Storybook

Next.js using Tailwind with Storybook

Set up a Next.js project with the following command

if you have your preferred package manager, you can use that.

npx create-next-app@latest --typescript
Enter fullscreen mode Exit fullscreen mode

next js

You can use Tailwind CSS right away if you just select yes when they ask you 'would you like to use Tailwind CSS with this project.'

Now, add storybook into the project.

npx storybook init
Enter fullscreen mode Exit fullscreen mode

When you're done installing Storybook, to use Tailwind CSS in stories directory, we will add a path into content in tailwind.config.ts.

/** @type {import('tailwindcss').Config} */
module.exports = {
  content: [
    "./pages/**/*.{js,ts,jsx,tsx}",
    "./components/**/*.{js,ts,jsx,tsx}",
    "./app/**/*.{js,ts,jsx,tsx}",
    "./stories/**/*.{js,ts,jsx,tsx}", // Here!
  ],
  theme: {
    extend: {
      backgroundImage: {
        "gradient-radial": "radial-gradient(var(--tw-gradient-stops))",
        "gradient-conic":
          "conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))",
      },
    },
  },
  plugins: [],
};

Enter fullscreen mode Exit fullscreen mode

open the Storybook.

npm run storybook
Enter fullscreen mode Exit fullscreen mode

storybook

You will see like this.
Let's make a change to the button component and see if Tailwind CSS works in Storybook.

button source code

I changed the class from storybook-button--primary.
Then, let's see.

tailwind not working in storybook

It doesn't work. To fix this, we need to import the 'globals.css' file in .storybook/preview.ts.

import type { Preview } from "@storybook/react";
import "../app/globals.css"; // Here!

const preview: Preview = {
  parameters: {
    actions: { argTypesRegex: "^on[A-Z].*" },
    controls: {
      matchers: {
        color: /(background|color)$/i,
        date: /Date$/,
      },
    },
  },
};

export default preview;
Enter fullscreen mode Exit fullscreen mode

In my case, the css path is '../app/globals.css', and we will remove the unnecessary style code in globals.css.

@tailwind base;
@tailwind components;
@tailwind utilities;

:root {
  --foreground-rgb: 0, 0, 0;
  --background-start-rgb: 214, 219, 220;
  --background-end-rgb: 255, 255, 255;
}

@media (prefers-color-scheme: dark) {
  :root {
    --foreground-rgb: 255, 255, 255;
    --background-start-rgb: 0, 0, 0;
    --background-end-rgb: 0, 0, 0;
  }
}

/* body {
  color: rgb(var(--foreground-rgb));
  background: linear-gradient(
      to bottom,
      transparent,
      rgb(var(--background-end-rgb))
    )
    rgb(var(--background-start-rgb));
} */
Enter fullscreen mode Exit fullscreen mode

tailwind with storybook works

Done! NextJS does everything for us.

I hope you found it useful.

Happy Coding!

Top comments (5)

Collapse
 
nithishprem profile image
Prathipati Nithish

Thank you!!
You helped me solve an issue in my react-typescript application built using Vite. The issue I was facing was my tailwind classes are not applied to my storybook components.
The missing piece was the following line in .storybook/preview.ts file,

import "../app/globals.css";
Enter fullscreen mode Exit fullscreen mode

After searching through lot of articles finally I landed on your article and solved the issue. In order for tailwind classes to work we need to import the main CSS file with tailwind directives into preview.ts file in storybook folder.

Once again Thanks!

Collapse
 
oscartrooper profile image
Oscar

Thank you very much for this tutorial, I spent several hours researching how to configure storybook with nextjs and many tutorials on the internet mentioned that I had to modify many files and install more libraries, and in the end none of them worked, your tutorial is the simplest and the only functional one, thanks :)

Collapse
 
lico profile image
SeongKuk Han • Edited

I was there too, that's why I wrote this post. I also installed and uninstalled several packages. I'm really glad it helped :) Have a good day!

Collapse
 
marinostbh profile image
Mohamed Amine Terbah

i had an error
node-modules/acron and acron--jsx not found
So i did :
npm i -D @storybook/addon-styling
npm i acorn acorn-jsx
import the global.css file of your tailwind config in .storybook/preview.js => import "../assets/styles/globals.css"
Restart storybook
and it worked

Collapse
 
awaisalwaisy profile image
Alwaisy al-waisy

Thank you. really helpful.