Dark Mode
Switch PeakUI to its dark theme with a class or a data attribute on the document root.
PeakUI ships a light and a dark theme. The light theme applies by default. The dark theme applies when the root element has the class default-dark or the attribute data-theme="default-dark":
<html class="default-dark">
<!-- or -->
<html data-theme="default-dark">default-light and data-theme="default-light" do the opposite. Set either on the root: inside a subtree the raw tokens and the utilities follow, but component palettes still resolve from the root, so a light region inside a dark page is not a supported combination.
The selector is default-dark, not dark. PeakUI does not follow prefers-color-scheme on its own; you decide when the dark theme applies.
Next.js with next-themes
next-themes handles the system preference, persistence and the flash on first paint.
Install next-themes
npm install next-themesApp Router
Map the two themes to PeakUI's selectors with the value prop:
"use client";
import type {ReactNode} from "react";
import {ThemeProvider} from "next-themes";
export function Providers({children}: {children: ReactNode}) {
return (
<ThemeProvider
attribute="data-theme"
defaultTheme="system"
value={{dark: "default-dark", light: "default-light"}}
>
{children}
</ThemeProvider>
);
}import type {ReactNode} from "react";
import {Providers} from "./providers";
import "./globals.css";
export default function RootLayout({children}: {children: ReactNode}) {
return (
<html suppressHydrationWarning lang="en">
<body>
<Providers>{children}</Providers>
</body>
</html>
);
}Theme switcher
"use client";
import {Button} from "@peakui/react";
import {useTheme} from "next-themes";
export function ThemeSwitcher() {
const {resolvedTheme, setTheme} = useTheme();
return (
<Button variant="tertiary" onPress={() => setTheme(resolvedTheme === "dark" ? "light" : "dark")}>
Toggle theme
</Button>
);
}Without a library
Set the attribute yourself. This follows the system preference:
"use client";
import {useEffect} from "react";
export function useSystemTheme() {
useEffect(() => {
const media = window.matchMedia("(prefers-color-scheme: dark)");
const apply = () => {
document.documentElement.dataset.theme = media.matches ? "default-dark" : "default-light";
};
apply();
media.addEventListener("change", apply);
return () => media.removeEventListener("change", apply);
}, []);
}The dark: variant
Tailwind's dark: variant follows prefers-color-scheme unless you redefine it. To make it follow the PeakUI theme, declare it once in your CSS:
@import "tailwindcss";
@import "@peakui/styles";
@custom-variant dark (&:where(.default-dark, .default-dark *, [data-theme="default-dark"], [data-theme="default-dark"] *));Styling for both themes
You rarely need dark: at all. The semantic tokens change with the theme, so one class covers both:
<main className="min-h-screen bg-background text-foreground">
<section className="rounded-xl bg-surface p-6 shadow-surface">
<p className="text-muted">This text is readable in both themes.</p>
</section>
</main>