Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Jonathan Ruiz
Jonathan Ruiz

Posted on

     

Nextjs + TailwindCSS + Storybook

Create a nextjs project.

UsingJavascript you can use:

npx create-next-app@latest# oryarn create next-app
Enter fullscreen modeExit fullscreen mode

UsingTypescript you can use:

npx create-next-app@latest --typescript# oryarn create next-app --typescript
Enter fullscreen modeExit fullscreen mode

I'll use this options
create-next-app options selected

Install Tailwind CSS with Next.js

Install Tailwind CSS with Next.js - Tailwind CSS

Setting up Tailwind CSS in a Next.js v10+ project.

favicon tailwindcss.com

Trying TailwindCSS and adding a script

I'll try Tailwind CSS modifying the filepages/index.tsx

export default function Home() {  return (    <>      <h1 className='text-red-900 text-9xl'>hola</h1>    </>  )}
Enter fullscreen modeExit fullscreen mode

Now let's add the following script in thepackage.json file

"scripts": {    "watch:css": "npx tailwindcss -i ./styles/globals.css -o ./public/tailwind.css --watch"  }
Enter fullscreen modeExit fullscreen mode

This script will create file with only the clases that we used in our project. Also it will watch pending for new changes in the classes of our project.

Let's run the script
yarn watch:css

File added by the script
File added by the script

Classes used in our project are in that file
classes used in our project

Installing and trying storybook

Before to install storybook we need to rename the.eslintrc.json to.eslintrc to avoid conflict with migrating when storybook is installing.

Now, let's install storybook

npx sb init
Enter fullscreen modeExit fullscreen mode

Storybook installed
Storybook installed
Now try storybook running the script

yarn storybook
Enter fullscreen modeExit fullscreen mode

If show an error in the filetsconfig.json close and open vscode

Configuring storybook with tailwind

Open the file.storybook/preview.js and import the file created by the tailwindcss script

It should looks like this

import('../public/tailwind.css')export const parameters = {  actions: { argTypesRegex: '^on[A-Z].*' },  controls: {    matchers: {      color: /(background|color)$/i,      date: /Date$/    }  }}
Enter fullscreen modeExit fullscreen mode

Now must to tell tailwind to compile the files in the folder stories
Open the filetailwind.config.js and add the path'./stories/*.{js,ts,jsx,tsx}'
It should looks like this

/** @type {import('tailwindcss').Config} */module.exports = {  content: [    './app/**/*.{js,ts,jsx,tsx}',    './pages/**/*.{js,ts,jsx,tsx}',    './components/**/*.{js,ts,jsx,tsx}',    './stories/*.{js,ts,jsx,tsx}'  ],  theme: {    extend: {}  },  plugins: []}
Enter fullscreen modeExit fullscreen mode

Adding a storybook script

"scripts": {  "watch:storybook": "start-storybook dev -p 6006"},
Enter fullscreen modeExit fullscreen mode

Now let's run both script

yarn watch:css
Enter fullscreen modeExit fullscreen mode

Now, open another terminal and run

yarn watch:storybook
Enter fullscreen modeExit fullscreen mode

Creating a component and a story

Let's create a component in the path./stories/AnotherButton.tsx

interface Props {}export function AnotherButton(props: Props) {  return (    <>      <button className='p-8 text-lg text-white rounded-3xl bg-slate-700'>        AnotherButton      </button>    </>  )}
Enter fullscreen modeExit fullscreen mode

Let's create a story for that component in./stories/AnotherButton.stories.tsx

import React from 'react'import { ComponentStory, ComponentMeta } from '@storybook/react'import { AnotherButton } from './AnotherButton'export default {  title: 'tailwind/AnotherButton',  component: AnotherButton} as ComponentMeta<typeof AnotherButton>const Template: ComponentStory<typeof AnotherButton> = (args) => (  <AnotherButton {...args} />)export const Primary = Template.bind({})
Enter fullscreen modeExit fullscreen mode

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
codeofrelevancy profile image
Code of Relevancy
Parimal Nakrani

Great article.
The combination of Nextjs, TailwindCSS and Storybook is truly impressive.. and TypeScript as well..

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Joined

Trending onDEV CommunityHot

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp