Toggle
Theme Toggle
NewA light and dark toggle whose sun turns into a moon, as an icon button or a small switch.
npx motionsnack add theme-toggleCustomize
<ThemeToggle />Props
10 properties
| Property | Type | Default | Description |
|---|---|---|---|
| value | 'light' | 'dark' | - | Controlled theme. Leave undefined to let the component manage it. |
| defaultValue | 'light' | 'dark' | 'dark' | Theme on first render when uncontrolled. |
| onChange | (value: 'light' | 'dark') => void | - | Called with the new theme on every change. |
| applyToDocument | boolean | false | Switch the page itself: toggles the dark class on <html> and remembers the choice in localStorage. |
| storageKey | string | 'theme' | localStorage key used with applyToDocument. |
| className | string | '' | Extra classes on the button. |
| variant | 'icon' | 'switch' | 'icon' | Round icon button, or a small track with a sun and a moon at the ends. |
| size | 'sm' | 'md' | 'lg' | 'md' | Icon button 28 / 36 / 44 px (44 on touch), switch track 46 / 56 / 66 px wide. |
| label | string | 'Dark mode' | Accessible name of the toggle. |
| disabled | boolean | false | Disables the toggle. |
About Theme Toggle
Theme Toggle is the light and dark switch for a site header or a settings page. On a click the sun's rays pull in and spin away while the disc grows and a shadow slides across it to carve a crescent, and the same in reverse; it is all SVG and CSS, so it stays sharp at any size and costs nothing to load. It comes as a round icon button or as a small track with a faint sun and moon at its ends and a thumb that carries the living icon across. Left alone it simply reports the chosen theme; with one prop it switches the page itself, toggling the dark class on the document and remembering the choice in local storage without a flash on the next visit. It has three sizes, the library's soft focus outline and a switch role for screen readers.
What's included
- Sun that morphs into a moon, in SVG and CSS
- Icon button or switch with a sliding thumb
- Optional page switching with a remembered choice
- No flash on load when it drives the page
- Three sizes and an adjustable morph speed
- Switch role, keyboard and focus outline
- Light and dark look
- Type
- Animated React toggle component
- Library
- Components · Toggle
- Stack
- Next.js, React, Tailwind CSS
- Added
- 2026-11-23
- Install