Site logo
Sep 16, 20226 mins read

Enable Tailwind Dark Mode in Next.js and Storybook

Nowadays, dark mode on web pages becomes popular because it provides more comfort for some people, including me. Using Tailwind we can configure it easily and choose one of 2 available strategies, the media strategy which leverage preferes-color-scheme CSS media feature and class strategy which use dark class in HTML document. In this article, we will enable Tailwind dark mode in the Next.js project, and also configure Storybook to simulate dark mode in component documentation.

Configure the dark mode

Starting from this step, I assume we already have a project bootstrapped using Next.js and Tailwind. If you need help setting it up, hopefully, this article can be useful. Within this step, we would like to configure the dark mode using class strategy.

Create a color mode switcher component

To make it easier for switching between color modes, let’s create a component for it.

Configure dark mode in Storybook

There are several alternatives to enable dark mode in Storybook but I use storybook-dark-mode addon because my plan is to make the whole parts in Storybook UI changed when the color mode is changed and this addon supports it very well.

Now we have dark mode configured in the web app and Storybook. Read more on Tailwind dark mode documentation for more information about how to use the dark mode. Have a nice day!

software-engineeringjavascript
Setup Local to Develop WordPress Si...
Setting Up Web Project using Next.j...