Switch Button - React Component | Kokonut UI

Switch Button

Light and dark theme toggle whose sun folds into a moon while the new theme spreads from the button as a circle.

Install with npm:

npx shadcn@latest add https://kokonutui.com/r/switch-button.json

Usage

With no props, the button toggles the dark class on <html>, which is what Tailwind's class-based dark: variant reads. It does not save the choice.

The new theme spreads out from the button as a circle, using the View Transitions API. In browsers without it, or with reduced motion turned on, the theme switches instantly.

If a theme provider already owns the theme, pass the applied theme and a setter instead. With next-themes:

"use client";

import { useTheme } from "next-themes";
import SwitchButton from "@/components/kokonutui/switch-button";

export function ThemeSwitch() {
  const { resolvedTheme, setTheme } = useTheme();

return (
    <SwitchButton
      onThemeChange={setTheme}
      theme={resolvedTheme === "dark" ? "dark" : "light"}
    />
  );
}

Installation

Switch Button is a free, open-source React component in the Kokonut UI Buttons collection. Add it with the shadcn CLI, which copies the source into components/kokonutui/switch-button.tsx, so the code is yours to change.

npx shadcn@latest add @kokonutui/switch-button

Props

Also takes every prop of React.ButtonHTMLAttributes<HTMLButtonElement>.

Theme

type Theme = "light" | "dark";

Dependencies

Needs Version Comes from
cn from lib/utils.ts shadcn init
React 19 Your app
Tailwind CSS 4 Your app

No provider, font or global CSS to set up: the file works as soon as it lands.

Related Buttons