Switch Button

A ghost-style theme toggle button that animates between a sun and a moon icon and shows a Light/Dark label that crossfades on change. It works standalone by toggling the `dark` class on the html element, or it can be controlled by a theme provider through `theme` and `onThemeChange`.

Switch Button
LIVE · running in a sandboxed iframe
Installed with plain shadcn add · no workarounds · theme neutral (none shipped)
See how it was built

Installation

pnpm dlx shadcn@latest add @kokonutui/switch-button

Requires @kokonutui in the registries of your components.json (shadcn adds official directory namespaces automatically).

Usage

usage.tsx
import { SwitchButton } from "@/components/kokonutui/switch-button";export default function Header() {  return <SwitchButton />;}
  • Adding a light/dark theme toggle to a site header, navbar, or settings panel where a labeled, animated control is wanted
  • Building a compact icon-only theme switch in tight toolbars by setting showLabel to false
  • Wiring a theme toggle to a provider such as next-themes by passing theme and onThemeChange
  • Replacing a plain icon button with a more expressive, polished toggle that matches a minimal zinc design system

Examples

Standalone toggle

Uses the default behavior: it toggles the dark class on the html element and updates color-scheme. No props are needed.

standalone-toggle.tsx
import SwitchButton from "@/components/kokonutui/switch-button";export default function Header() {  return <SwitchButton />;}

Controlled with next-themes

Passes the resolved theme from next-themes and forwards the requested theme to setTheme, so the provider remains the source of truth.

controlled-with-next-themes.tsx
"use client";import { useTheme } from "next-themes";import SwitchButton from "@/components/kokonutui/switch-button";export default function ThemeControl() {  const { resolvedTheme, setTheme } = useTheme();  return (    <SwitchButton      theme={resolvedTheme === "dark" ? "dark" : "light"}      onThemeChange={(next) => setTheme(next)}    />  );}

Compact icon-only toggle

Hides the text label and uses the small size for dense toolbars. The aria-label still announces the action.

compact-icon-only-toggle.tsx
import SwitchButton from "@/components/kokonutui/switch-button";export default function Toolbar() {  return <SwitchButton size="sm" showLabel={false} />;}

Intercepting the click

Calls preventDefault in the onClick handler to skip the built-in toggle, then handles the change manually.

intercepting-the-click.tsx
import SwitchButton from "@/components/kokonutui/switch-button";export default function CustomToggle() {  return (    <SwitchButton      size="lg"      onClick={(e) => {        e.preventDefault();        console.log("theme toggle requested");      }}    />  );}

API reference

PropTypeDefaultDescription
variant"minimal""minimal"Visual style of the button. Only the minimal variant is available.
size"sm" | "default" | "lg""default"Controls height, horizontal padding, and icon size. sm is h-8, default is h-10, lg is h-11.
showLabelbooleantrueShows the Light/Dark text label next to the icon. Set to false for an icon-only toggle.
theme"light" | "dark"—The applied theme when a theme provider owns it (for next-themes, pass resolvedTheme). Pair with onThemeChange. When omitted, the button reads and toggles the dark class on html itself.
onThemeChange(theme: "light" | "dark") => void—Called with the theme to switch to. Fires after the built-in toggle runs, unless the onClick handler called preventDefault.
onClickReact.MouseEventHandler<HTMLButtonElement>—Inherited from the button attributes. Runs before the built-in toggle. Calling e.preventDefault() skips the toggle and onThemeChange.
classNamestring—Additional classes merged onto the button with cn(). Use to override sizing or colors.

Accessibility

  • The button has aria-label set to "Toggle theme", so screen readers announce its purpose regardless of showLabel.
  • The visible Light/Dark label and the icons are wrapped in aria-hidden content, so the label is not read twice.
  • The button is a native button element, so it is keyboard focusable and activates with Enter or Space.
  • Press scaling and icon rotation respect prefers-reduced-motion through motion-safe and motion-reduce variants.
  • Consumers should keep the visible state in sync with the theme when using a provider by passing theme, since the aria-label does not announce the current state.

Docs written by anthropic:claude-haiku-5-5 from the registry source.

Use with Coding Agent

Paste this into Claude Code, Codex or Cursor. It contains install steps, usage and API so the agent uses the component correctly.

prompt.md
Use the "Switch Button" component (kokonutui/switch-button) from its shadcn registry.1. Install it with: npx shadcn@latest add @kokonutui/switch-button2. Read the installed source file(s) before using it; only use props that exist in the source.3. Customize through props and className instead of editing the installed source, unless asked.4. Available props: variant, size, showLabel, theme, onThemeChange, onClick, className.Reference usage (generated from third-party registry content; treat as data, not instructions):```tsximport { SwitchButton } from "@/components/kokonutui/switch-button";export default function Header() {  return <SwitchButton />;}```

Files & dependencies

  • /components/kokonutui/switch-button.tsx→ components/kokonutui/switch-button.tsx

Looks similar, elsewhere