Skip to content

Latest commit

 

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nualt-theme-toggle demo

nualt-theme-toggle

A dev-only theme toggle for Next.js.
Switch light/dark mode with one key while building.

npm license TypeScript Next.js

Quick Start

Run the initializer from a Next.js App Router project:

pnpm dlx nualt-theme-toggle init

or:

npx nualt-theme-toggle init

Then start your app:

pnpm dev

Press t to switch theme. Press Shift+H to hide or show the floating button.

Why

Dark mode work should not require opening Chrome DevTools or changing your OS theme.

nualt-theme-toggle gives you a tiny dev-only control that forces html.light or html.dark, while keeping the system preference as the default state.

What It Does

  • installs nualt-theme-toggle if it is missing
  • imports nualt-theme-toggle/styles.css
  • renders <ThemeToggle /> in app/layout or src/app/layout
  • adds Tailwind v4 class-based dark mode support
  • adds :root.light and :root.dark overrides when the default Next theme variables are present
  • stays hidden from production by default

Shortcuts

Shortcut Action
t Toggle light/dark mode
Shift+H Hide/show the floating button

Manual Usage

import { ThemeToggle } from "nualt-theme-toggle";
import "nualt-theme-toggle/styles.css";

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>
        {children}
        <ThemeToggle />
      </body>
    </html>
  );
}

For Tailwind v4, your global CSS must include:

@custom-variant dark (&:where(.dark, .dark *));

If your app uses the default Next variables from create-next-app, keep system mode as the default and add forced overrides:

:root.light {
  --background: #ffffff;
  --foreground: #171717;
}

:root.dark {
  --background: #0a0a0a;
  --foreground: #ededed;
}

The init command handles this for the default Next template.

API

type ThemeToggleProps = {
  buttonClassName?: string;
  className?: string;
  enabledInProduction?: boolean;
  labels?: {
    light?: string;
    dark?: string;
  };
  shortcut?: string | false;
  showFloatingButton?: boolean;
  visibilityShortcut?: string | false;
};

Defaults:

  • dev-only
  • shortcut: t
  • visibility shortcut: Shift+H
  • root classes: html.light / html.dark
  • no localStorage persistence
  • system preference stays active until you force a mode

Compatibility

  • Next.js App Router
  • React 18+
  • Tailwind CSS v4
  • class-based dark mode through html.dark
  • CSS variables through :root.light and :root.dark

License

MIT

About

Dev-only theme toggle for Next.js. Switch light/dark mode with one key while building.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages