React Dark Mode with Tailwind CSS
React dark mode with Tailwind CSS in SheenUI is one data-theme attribute that the tokens, the dark variant and the useTheme hook all read.
Switch the theme
useTheme returns the saved choice and a setter; this page's header toggle uses the same hook.
import { useTheme } from "@/components/sheenui/hooks/use-preferences";
// Package build (after the first npm release, or a packed tarball): import { useTheme } from "@sheenui/theme";
export function ThemeButton() {
const { theme, setTheme } = useTheme();
return <button onClick={() => setTheme(theme === "dark" ? "light" : "dark")}>Theme: {theme}</button>;
}How dark mode works in SheenUI
The theme is the data-theme attribute on the html element: light, dark, or absent for the system setting. The tokens read it through light-dark() and attribute selectors, and Tailwind's dark: variant is declared against the same attribute, so utility classes and tokens always agree. The useTheme hook writes the attribute, saves it and syncs other tabs.
Dark glass is tuned separately: less backdrop saturation, deeper tints and the same contrast targets (WCAG AA). Reduced transparency and the contrast preference override both themes with solid surfaces, so dark mode never weakens accessibility. Read accessible glassmorphism for the checks.
Three things are worth testing in your own app: the first paint (no flash of the wrong theme), the system setting when nothing is saved, and the forced-colors and high-contrast modes, where glass becomes solid and borders become system colors. All three are handled by the theme, but custom components you add must use the tokens instead of fixed colors to inherit that behavior.
For first paint, inline sheenPreferenceScript in the head; the installation page shows where. The Next.js and Vite guide covers the hydration warning.
The attributes on the html element
Every preference is a data attribute on <html>, saved in localStorage under a sheen: key. CSS reacts to the attribute, so a preference needs no JavaScript after the first paint.
data-theme:lightordark; absent follows the operating system.data-motion:reducedturns springs into short fades; absent is full motion.data-transparency:reducedmakes every glass surface solid; absent is glass.data-glass:liteorfull, the cheaper blur for slow devices.data-sheen-style:liquid(the default) oraurora, the two style presets.
The dark variant in Tailwind CSS v4
Tailwind v4 reads dark: from the system setting unless you redefine it. The SheenUI styles already do that against data-theme; in a project that does not import them, declare the same variant yourself:
@import "tailwindcss";
@custom-variant dark (&:where([data-theme="dark"], [data-theme="dark"] *));Then dark:bg-slate-900/50 follows the attribute that useTheme writes, and a plain @media (prefers-color-scheme) no longer decides. See the Tailwind CSS v4 setup for the rest of the CSS.
Colors in your own components
A custom component inherits dark mode when it takes its colors from tokens instead of writing dark: pairs. The semantic tokens (--sheen-bg, --sheen-fg, --sheen-border and the rest) are defined with light-dark(), and color-scheme follows data-theme, so one class resolves to a different value in each theme. Tailwind exposes them as bg-sheen-bg, text-sheen-fg, text-sheen-fg-muted and border-sheen-border; glass-1, glass-2 and glass-3 are the three glass levels, each with its own dark tint.
export function Note({ children }: { children: React.ReactNode }) {
return (
<aside className="glass-2 rounded-sheen-lg border border-sheen-border p-4 text-sheen-fg">
<p className="text-sm text-sheen-fg-muted">{children}</p>
</aside>
);
}That component needs no theme logic: switch data-theme and the tint, the border and both text colors change together.
What the contrast targets are
The text tokens are measured in the browser against the worst case, the brightest backdrop blob peak under plain, glass-1 and glass-2 surfaces, in both themes: body and status text must reach 4.5:1, and focus rings and control edges 3:1. The numbers sit in the comment at the top of the token file. If you add your own accent or status color, check it the same way on a glass surface, not only on the flat page color.
No flash of the wrong theme
A server-rendered page does not know the saved choice, so the first paint can show the wrong theme until React loads. Put sheenPreferenceScript inline in the head: it runs before first paint, copies the saved values onto the html element and falls back to the Liquid Glass preset when storage is unavailable. In a Next.js root layout:
import { sheenPreferenceScript } from "@/components/sheenui/hooks/use-preferences";
// Package build (after the first npm release, or a packed tarball): import { sheenPreferenceScript } from "@sheenui/theme";
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en" data-sheen-style="liquid" suppressHydrationWarning>
<head>
<script dangerouslySetInnerHTML={{ __html: sheenPreferenceScript }} />
</head>
<body>{children}</body>
</html>
);
}suppressHydrationWarning is needed because the script changes the html element before React compares it with the server markup. In a Vite app, paste the script text into index.html.
Frequently asked questions
Related pages
By Lokendra Singh Rathore (GitHub, opens in a new tab). Published , updated .