Skip to content
satin

Dark mode

Dark with one attribute, a page that follows the system, and no flash on load.

Turning it on

Every Satin colour is a token with a Light and a Dark value. Which one applies depends on an attribute on any parent, usually <html>: the dark class or data-theme="dark" for dark, and data-theme="system" to follow the operating system. system is a prefers-color-scheme media query, so it needs no JavaScript and changes the moment the system does.

index.html
<!-- Dark, always -->
<html class="dark">
<html data-theme="dark">

<!-- Follows the operating system's setting, in CSS alone -->
<html data-theme="system">

<!-- Light: no attribute, or data-theme="light" -->
<html>

A Light / Dark / System toggle

The toggle sets the attribute and remembers the choice. Keep system as an option and as the default: most people never change it, and their page then follows the time of day on its own.

ThemeToggle.tsx
'use client' // for Next.js; harmless elsewhere

import * as React from 'react'
import { SegmentedControl } from '@uisatin/ui/segmented-control'

const themes = [
  { value: 'light', label: 'Light' },
  { value: 'dark', label: 'Dark' },
  { value: 'system', label: 'System' },
]

export function ThemeToggle() {
  // The head script has already set the attribute; read it once mounted, so the server
  // and the first client render agree.
  const [theme, setTheme] = React.useState('system')
  React.useEffect(() => setTheme(document.documentElement.dataset.theme ?? 'system'), [])

  return (
    <SegmentedControl
      aria-label="Theme"
      size="xs"
      value={theme}
      onValueChange={(next) => {
        setTheme(next)
        document.documentElement.dataset.theme = next
        try {
          localStorage.setItem('theme', next)
        } catch {}
      }}
      items={themes}
    />
  )
}

No flash on load

If the attribute is set by your app's JavaScript, the page paints light first and turns dark a moment later. Set it before the first paint instead, with a few lines inline at the top of <head>. They read the saved choice and fall back to system; if storage is blocked, the page still follows the system.

index.html
<html lang="en" data-theme="system">
  <head>
    <script>
      // Inline, before any stylesheet or app script, so it runs before the first paint.
      try {
        document.documentElement.dataset.theme = localStorage.getItem('theme') || 'system'
      } catch {}
    </script>
    …

In React, the attribute on <html> may now differ from what the server rendered; add suppressHydrationWarning to <html> to tell React that's expected.

Rendered on the server

When pages render per request, the server can set the attribute itself: the toggle saves the choice in a cookie as well, and the root layout reads it. No script runs and nothing has to match after hydration. The Next.js and React Router guides do it this way.

Cookie
// In the toggle: save the choice where the server can read it.
document.cookie = `theme=${next}; path=/; max-age=31536000; samesite=lax`

// On the server: render the attribute from it (Next.js app/layout.tsx).
const theme = (await cookies()).get('theme')?.value ?? 'system'
return <html lang="en" data-theme={theme}>…</html>

A dark panel in a light page

The attribute works on any element, not only <html>. Put data-theme="dark" on a panel and everything inside it turns dark, while the rest of the page stays light. The panel paints its own background from the tokens, since Satin doesn't paint the element that carries the attribute. It works one way only: there is no light scope inside a dark page.

NewSatin Pro is coming
Nested
<section data-theme="dark" style={{ background: 'var(--sa-bg-card)', borderRadius: 16, padding: 20 }}>
  <Badge tone="brand">New</Badge>
  <Button>Upgrade</Button>
  <Button variant="secondary">Later</Button>
</section>

The page around Satin

Satin's stylesheet styles Satin's elements and leaves the page to you, so a dark theme doesn't reach your <body> by itself. Paint it from the same tokens, and set color-scheme so scrollbars and native inputs match.

app.css
/* Satin styles its own elements, not the page: paint the page from its tokens. */
body {
  background: var(--sa-bg-canvas);
  color: var(--sa-text-default);
}

/* Native scrollbars and form controls follow the theme too. */
:root { color-scheme: light; }
:root:is(.dark, [data-theme='dark']) { color-scheme: dark; }
@media (prefers-color-scheme: dark) {
  :root[data-theme='system'] { color-scheme: dark; }
}

Your own Tailwind and dark:

Satin's dark styles are its own prefixed classes (sa:dark:…), so they don't depend on your Tailwind and your dark: utilities don't change Satin. Your dark: keeps its own rule: by default that's prefers-color-scheme, which ignores the toggle. To make both switch together, point your variant at the same attribute.

app.css
/* Your Tailwind v4 CSS: make your own dark: follow the same attribute as Satin. */
@custom-variant dark {
  &:where(.dark, .dark *, [data-theme='dark'], [data-theme='dark'] *) {
    @slot;
  }
  @media (prefers-color-scheme: dark) {
    &:where([data-theme='system'], [data-theme='system'] *) {
      @slot;
    }
  }
}

On Tailwind v3.4, darkMode: ['selector', '[data-theme="dark"]'] in tailwind.config.js follows an explicit Dark; system then needs the media query in your own CSS.