Mode Toggler

A minimal Sun ↔ Moon theme toggle button built on next-themes. Supports an optional click sound, composable className, and dynamic aria-label.

click to toggle

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 https://akoder.xyz/r/mode-toggler.json

Install often? Register the namespace once in components.json and use the shorter form npx shadcn@latest add @akoder/mode-toggler.

Usage

import { ModeToggler } from "@/components/ui/mode-toggler";
 
export function Header() {
  return (
    <header className="flex items-center justify-between px-4 h-12">
      <span>my app</span>
      <ModeToggler />
    </header>
  );
}

Props

PropTypeDefaultNotes
classNamestringExtra classes forwarded to the <Button>. Useful for sizing, rounding, or colour overrides when embedding inside a nav strip.
audioSrcstringURL of an audio clip that plays on each toggle. Lazy-instantiated — no network request until the prop is first provided.
audioDelaynumber200ms between the audio starting and the theme switching. Only active when audioSrc is set.

Notes & Features

Manual installation

Copy components/ui/mode-toggler.tsx into your project. Ensure next-themes and lucide-react are installed, and that your root layout wraps children with ThemeProvider:

// app/layout.tsx
import { ThemeProvider } from "next-themes";
 
export default function RootLayout({ children }) {
  return (
    <html suppressHydrationWarning>
      <body>
        <ThemeProvider attribute="class" defaultTheme="system" enableSystem>
          {children}
        </ThemeProvider>
      </body>
    </html>
  );
}