React Night Toggle is a lightweight and customizable dark mode switch for React apps, built with TypeScript, framer-motion, and lucide-react.
Easily add a night/day theme toggle with smooth animations, customizable icons, and flexible color options.
- π Clean & modern dark mode toggle
- β‘ Built with TypeScript
- π¨ Customizable icons (uses
lucide-reactby default) - π¨ Customizable colors via
sunColor&moonColorprops - β¨ Smooth Framer Motion animations
- π¦ Lightweight and easy to use in any React project
npm install react-night-toggle
# or
yarn add react-night-toggle
# or
pnpm add react-night-toggleimport { useState } from 'react';
import { DarkModeSwitch } from 'react-night-toggle';
export default function App() {
const [dark, setDark] = useState(false);
const toggleDarkMode = (checked: boolean) => setDark(checked);
return (
<div className={dark ? 'dark' : ''}>
<DarkModeSwitch
checked={dark}
onChange={toggleDarkMode}
sunColor="orange" // optional, defaults to currentColor
moonColor="black" // optional, defaults to currentColor
/>
<h1>{dark ? 'π Dark Mode' : 'βοΈ Light Mode'}</h1>
</div>
);
}You can pass your own icons instead of the default Sun/Moon:
import { useState } from 'react';
import { DarkModeSwitch } from 'react-night-toggle';
import { CloudSun, Star } from 'lucide-react';
export default function App() {
const [dark, setDark] = useState(false);
// onChange handler to toggle dark mode
const toggleDarkMode = (checked: boolean) => setDark(checked);
return (
<div>
<DarkModeSwitch
checked={dark}
onChange={toggleDarkMode}
lightIcon={<CloudSun size={24} />}
darkIcon={<Star size={24} />}
/>
<h2>{dark ? 'Dark Mode Enabled π' : 'Light Mode Enabled βοΈ'}</h2>
</div>
);
}You can automatically follow the userβs system color scheme by setting followSystem to true:
import { useState } from 'react';
import { DarkModeSwitch } from 'react-night-toggle';
export default function App() {
const [dark, setDark] = useState(false);
return (
<div>
<DarkModeSwitch followSystem checked={dark} onChange={setDark} size={40} />
<h2>{dark ? 'Dark Mode Enabled π' : 'Light Mode Enabled βοΈ'}</h2>
</div>
);
}You can customize the colors of the Sun and Moon icons:
import { useState } from 'react';
import { DarkModeSwitch } from 'react-night-toggle';
export default function App() {
const [dark, setDark] = useState(false);
return (
<div>
<DarkModeSwitch
checked={dark}
onChange={setDark}
size={56}
sunColor="orange"
moonColor="blueviolet"
/>
<h2>{dark ? 'Dark Mode Enabled π' : 'Light Mode Enabled βοΈ'}</h2>
</div>
);
}You can increase the size of the toggle button by passing a numeric value or a string:
import { useState } from 'react';
import { DarkModeSwitch } from 'react-night-toggle';
export default function App() {
const [dark, setDark] = useState(false);
return (
<div>
<DarkModeSwitch checked={dark} onChange={setDark} size={72} />
<h2>{dark ? 'Dark Mode Enabled π' : 'Light Mode Enabled βοΈ'}</h2>
</div>
);
}β¨ This way you can use any React node (Lucide, Material UI, custom SVGs, etc.) for icons.
| Prop | Type | Default | Description |
|---|---|---|---|
checked |
boolean |
β (required) | Whether dark mode is active (true = dark, false = light). |
onChange |
(checked: boolean) => void |
β (required) | Callback fired when the toggle is switched. |
size |
number | string |
24 |
Size of the toggle button (applied to both icons). |
lightIcon |
React.ReactNode |
<Sun /> |
Custom icon for light mode. |
darkIcon |
React.ReactNode |
<Moon /> |
Custom icon for dark mode. |
sunColor |
string |
currentColor |
Color of the default Sun icon. Ignored if lightIcon is provided. |
moonColor |
string |
currentColor |
Color of the default Moon icon. Ignored if darkIcon is provided. |
followSystem |
boolean |
false |
Automatically follow the userβs system color scheme. Overrides checked when set to true. |
Contributions are welcome!
Feel free to open an issue or submit a pull request.
Give a βοΈ if this project helped you!
