Mode Toggler
A minimal Sun ↔ Moon theme toggle button built on next-themes. Supports an optional click sound, composable className, and dynamic aria-label.
Installation
Mode Toggler is published as a shadcn registry item. The CLI drops the file into your project, installs its dependencies, and adds any primitives it relies on.
$npx shadcn@latest add @akoder/mode-togglerThe @akoder namespace ships with the shadcn CLI, so there is nothing to configure first. Install any other component the same way from the components page.
Usage
Props
| Prop | Type | Default | Notes |
|---|---|---|---|
className | string | — | Extra classes forwarded to the <Button>. Useful for sizing, rounding, or colour overrides when embedding inside a nav strip. |
audioSrc | string | — | URL of an audio clip that plays on each toggle. Lazy-instantiated — no network request until the prop is first provided. |
audioDelay | number | 200 | ms between the audio starting and the theme switching. Only active when audioSrc is set. |
Notes & Features
- next-themes integration. Uses
resolvedThemeso the correct icon renders on first paint — no flash with OS dark mode. - Accessible.
aria-labelupdates dynamically: "switch to light mode" in dark, "switch to dark mode" in light. - Composable. Pass
classNameto strip the border/rounding for embedding in a pill group or navbar strip. - Audio optional. Omit
audioSrcfor a silent toggle; pass a.mp3or.oggURL to match the site's own click sound.
Setup notes
The toggle reads and writes the next-themes context, so your root layout has to wrap children with ThemeProvider:
Manual installation
Rather not use the CLI? Everything Mode Toggler needs is below. Install its dependencies, then copy the file into the matching path in your project.
Dependencies
$npm install lucide-react next-themesshadcn primitives
$npx shadcn@latest add buttonSource