π π Simplest way to handle theming in React.js apps
useMode is for applying dark and light mode easily, safely and quickly
It saves theme to localstorage of the browser
Supports TailwindCSS, but requires some settings
- You can use it like below
const mode = useMode()<button onClick={mode.toggle}>Switch</button>Mode will be changed if the user clicks the button
This library needs TailwindCSS to be configured especially to work
Add this line to tailwind.config.js
module.exports = {
...
darkMode: 'class',
...
}- Import the library
import useMode from "usemode"modeobject has thosetoggle,setSystem,setDark,setLight,name,isDarkproperties
const mode = useMode()npm i usemode
yarn add usemode
mode.toggle(): functionSwitches mode as dark and light
mode.setSystem(): functionApplies system preferred mode
mode.setDark(): functionApplies dark mode if it hasn't been done yet
mode.setLight(): functionApplies light mode if it hasn't been done yet
mode.name: "light" || "dark"Name of the current mode
mode.isDark: true || falseTrue, if dark mode is used currently. Otherwise, false.