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
size: "sm" | "default" | "lg" (Default: "default")showLabel: boolean (Default: true) Shows "Light" or "Dark" next to the icon.theme: The applied theme, when a theme provider owns it (for next-themes, itsresolvedTheme). Pair it withonThemeChange. Without it, the button toggles thedarkclass on<html>itself.onThemeChange:(theme: Theme) => voidCalled with the theme to switch to.reveal: boolean (Default: true) Spreads the new theme from the button as a circle.falseswitches instantly.
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
- Command Button: A button with its shortcut drawn as keycaps that sink as you press the real keys.
- Slide Text Button: Animated link button with a vertical text slide on hover and default and ghost variants, built with React, Next.js Link, and Tailwind CSS.