Tailwindcss One of the best utility-based CSS frameworks I've ever used, period. It...
Tailwindcss
One of the best utility-based CSS frameworks I've ever used, period. It provides all the necessary CSS properties with values and we ourselves have to design. It's much more awesome than Bootstrap or ChakraUI as it doesn't push us to use the component structure.
Because Tailwind is so low-level, it never encourages you to design the same site twice. Even with the same color palette and sizing scale, it's easy to build the same component with a completely different look in the next project.
How to enable class-based dark mode for Next.Js?
In the tailwindcss docs for dark mode, it provides only a basic snippet about what to write on page load for a specific page but it doesn't provide any example or docs for the framework.
To enable class-based dark mode we first have to override the value of the darkMode key from false to 'class' in tailwind.config.js file.
1module.exports ={2darkMode:'class',3// ...4}5
We have to edit the _app.js file to check if dark mode is applied or not before mounting the actual component to the DOM. For that, we will use useEffect from react just before returning the component.
This will add class to html before component mounting.
We also need a theme toggler button. Add our desired toggle button and we have to keep in mind that we need two buttons one for dark and one for light mode
In ThemeSwitch.jsx:
1import{ useEffect, useState }from'react';23constisDark=()=>//Function that will return boolean if any of the condition is satisfied4
Now you can add the theme switch in the navbar of your layout and can change the theme on any page.
Note: Don't forget to add dark:some-value in the class names if you want manual control over CSS.
{
4
useEffect
(
(
)
=>
{
5
if
(
6
localStorage
.
theme
===
'dark'
||
7
(
!
(
'theme'
in
localStorage
)
&&
8
window
.
matchMedia
(
'(prefers-color-scheme: dark)'
)
.
matches
)
9
)
{
10
//check if there is any key for theme in local storage and if the system color theme is dark
11
document
.
documentElement
.
classList
.
remove
(
'light'
)
;
//OPTIONAL - remove light from the html document if any
12
document
.
documentElement
.
classList
.
add
(
'dark'
)
;
// add dark to the <html></html> itself as <html class='dark'></html>
13
}
else
{
14
document
.
documentElement
.
classList
.
remove
(
'dark'
)
;
// remove dark from the html document if any
15
document
.
documentElement
.
classList
.
add
(
'light'
)
;
//OPTIONAL - add light to the <html></html> itself as <html class='light'></html>
16
}
17
}
,
[
]
)
;
18
return
<
Component
{
...
pageProps
}
/>
;
19
}
20
21
export
default
MyApp
;
22
23
(
localStorage
&&
localStorage
.
theme
===
'dark'
)
||
//Condition 1 - has local storage and theme = dark in local storage is found
5
(
!
(
'theme'
in
localStorage
)
&&
6
window
.
matchMedia
(
'(prefers-color-scheme: dark)'
)
.
matches
)
;
//Condition 2 - No theme key in local storage but media color scheme is dark
7
8
const
getTheme
=
(
isDark
)
=>
(
isDark
?
'dark'
:
'light'
)
;
//Function to return 'dark' or 'light' string
9
10
const
ThemeSwitch
=
(
)
=>
{
11
const
[
darkMode
,
setDarkMode
]
=
useState
(
false
)
;
//State for holding theme status
12
13
const
toggleMode
=
(
)
=>
{
//onClick handler for changing theme on button press
14
localStorage
.
theme
=
getTheme
(
!
darkMode
)
;
//setting up local storage theme value
15
if
(
localStorage
.
theme
===
'dark'
)
{
// If theme is 'dark'
16
document
.
documentElement
.
classList
.
remove
(
'light'
)
;
// remove 'light' from html class
17
document
.
documentElement
.
classList
.
add
(
'dark'
)
;
// add 'dark' to html class
18
}
else
{
// if not 'dark'
19
document
.
documentElement
.
classList
.
remove
(
'dark'
)
;
// remove 'dark' from html class
20
document
.
documentElement
.
classList
.
add
(
'light'
)
;
//add 'light' to html class
21
}
22
setDarkMode
(
!
darkMode
)
;
//set dark mode state to opposite of initial value
23
}
;
24
25
useEffect
(
(
)
=>
{
26
setDarkMode
(
isDark
(
)
)
;
//before page mount set the value of dark mode by observing theme in local storage
27
}
,
[
]
)
;
28
29
const
darkModeActive
=
30
process
.
browser
&&
document
.
documentElement
.
classList
.
contains
(
'dark'
)
;
// returns true if its a client and 'dark' is present in html
31
// process.browser is deprecated can be written as typeof window === 'undefined'
32
return
(
33
<
>
34
<
button
className
=
'
w-10 h-10 focus:outline-none
'
onClick
=
{
toggleMode
}
>
35
<
span
className
=
'
sr-only
'
>
Color mode switch button
</
span
>
36
{
darkModeActive
?
(
//switch mode icon according to html class 'dark' or 'light'