skip to content
mpkcn

~/components/theme-toggle

Theme Toggle

A dark / light switch for next-themes, as an icon or as a terminal flag.

variant="icon"
variant="text"

Installation

terminal
npx mpkcn add theme-toggle
deps:next-themes@0.4.6· tested with react@19.3.0

The CLI also adds the theme provider (theme-provider.tsx, next-themes with the mpkcn defaults), wraps your root layout in it and installs next-themes. Nothing else to set up.

Usage

tsx
import { ThemeToggle } from "@/components/mpkcn/theme-toggle";

export function Header() {
  return (
    <header className="flex items-center justify-between border-b border-mpk-line px-6 py-3">
      <span className="font-mpk-mono">mpkcn</span>
      <ThemeToggle />
    </header>
  );
}

It reads resolvedTheme and switches between dark and light. Until it has mounted it renders an invisible placeholder of the same size, so there is no hydration mismatch and no layout shift.

Examples

Icon

The default. A round button with a moon in dark mode and a sun in light mode; they swap with a small spin.

variant="icon"

Text

A mono flag with a hairline border that shows the current theme: --dark in dark mode, --light in light mode. It turns blue on hover.

variant="text"

Props

PropTypeDefaultDescription
variant"icon" | "text""icon"icon: round button with a moon or sun. text: mono button reading --dark / --light (the current theme).
classNamestring—Extra classes for the button.

Accessibility

  • It is a native button with an aria-label and title that name the action, e.g. "Switch to light theme".
  • The icons are aria-hidden; the label carries the meaning.
  • Focus is visible with a 1px amber outline.
  • The icon spin and hover lift are turned off under prefers-reduced-motion.