Dark mode
How dark mode is wired with next-themes, how to add a toggle, how to give one section its own mode and how to treat images in dark.
Dark mode is a set of token values, not a second stylesheet. The theme provider puts a dark class on the html element, the tokens in app/globals.css change under that class, and every component follows because it reads tokens. This page covers the wiring. Modes explains the selectors in depth.
Reading your preference.
How it is wired
Three pieces, all in this repository:
| Piece | File | What it does |
|---|---|---|
| The tokens | app/globals.css | .dark and [data-theme="dark"] hold the dark values. :root holds the light values. |
| The provider | components/theme-provider.tsx | Wraps next-themes. Sets the class attribute on html, follows the system preference by default, and stores the choice. |
| The root layout | app/layout.tsx | Wraps the app in the provider and sets suppressHydrationWarning on html. |
"use client"
import * as React from "react"import { ThemeProvider as NextThemesProvider } from "next-themes"
function ThemeProvider({ children, ...props}: React.ComponentProps<typeof NextThemesProvider>) { return ( <NextThemesProvider attribute="class" defaultTheme="system" enableSystem disableTransitionOnChange {...props} > {children} </NextThemesProvider> )}
export { ThemeProvider }attribute="class"means the provider writesclass="dark"or no class onhtml. Thedark:variant in Tailwind matches that class, so upstream classes such asdark:bg-input/30apply.defaultTheme="system"andenableSystemstart from the visitor's operating system setting, andlight,darkandsystemare the three values a person can choose.disableTransitionOnChangeremoves transitions for one frame while the theme switches, so components do not fade through a half-changed state.- The provider is a client component and the layout is a server component.
suppressHydrationWarningonhtmlis needed, because next-themes sets the class before React hydrates.
The provider writes the class only. The data-theme attribute is not set on html by the provider; it is for regions inside the page (see below), and it works on the root too if you set it yourself.
Keyboard shortcut
The provider registers no keyboard shortcut. The Next.js template binds the single key D to switch themes; that binding is removed here, because WCAG 2.1.4 does not allow a single-character shortcut that a person cannot turn off or remap. Offer a theme button instead, or a shortcut with a modifier.
Add a toggle
Read the theme with useTheme from next-themes, and write it with setTheme. The example at the top of this page is the whole component: a toggle group with the three choices. Two details matter:
- Use
resolvedThemeto know the mode that is showing (lightordark) andthemeto know the choice (light,darkorsystem). - Render a neutral state until the component has mounted. On the server there is no stored choice, so reading it during the first render would mismatch the HTML.
The documentation header uses a single icon button instead, which switches between the two resolved modes: a sun that hides in dark, and a moon that shows in dark, with the label "Switch between light and dark theme".
Per-section modes
A region can take its own mode. Put data-theme="dark" or className="dark" on the element, and data-theme="light" or className="light" to force light inside a dark region. The brand's dark field is the usual case: a band that is dark in both modes. See Modes.
Follows the page
This section is always dark, in a light page and a dark one.
A single logo component takes its colour from the text colour of its section, so it needs no second file. See Logo.
Images in dark
Light and dark use the same photographs. In dark they drop to 90% brightness so they do not glare off the black ground. Add dark:brightness-90 to the image. Pictures that carry a baked-in background, such as a screenshot of the interface, are the exception: use the component itself, which follows the mode, and never a screenshot of one mode.


Static single-ink files cannot follow the mode: logo.svg is a fixed dark ink and logo-white.svg is white. Use Logo in application UI, and pick the file by the ground it lands on only where a file is required.
Testing both modes
- Look at every screen in both modes before you ship it, and at 375px as well.
pnpm check:contrastchecks every text pair in light and dark.- For a screenshot in dark,
node scripts/screenshot.mjs /docs/theming shot.png --darksets the stored theme before the page loads.