---
title: "Dark Mode"
description: "How dark mode works in defuss-shadcn: token pairs, system preference detection, persistence, and implementation."
slug: "dark-mode"
---

import { DocPage, PageHeader, SkillPanel, Example, ExampleLabel, ExampleHint, ExampleCode, SourceFiles, SourceNote, StatesSection, CodeBlock, Demo, DemoCode, ChangelogEntries, ArchBody, StatsClaim, StatsCards, PageOverlay, DocText, DocLink, H2, SkillField, SkillText, SkillRow, SkillGrid, Variant, ApiPill, Dim, CodeWindow, CodeCard, TableCard } from "../../lib/components/index";

<DocPage meta={meta}>

<PageHeader slug="dark-mode" title="Dark Mode" introStyle="max-width:38rem;">
  Dark mode is built into the token layer. Add <code>class="dark"</code> to <code>&lt;html&gt;</code> and every component switches automatically - no per-component overrides needed.
</PageHeader>

<H2>How it works</H2>

<p class="text-sm text-muted-foreground mb-5">The token file (<code>default-semantic-tokens.css</code>) defines two complete sets of color values. The <code>:root</code> block provides light mode values. The <code>.dark</code> selector overrides every color token with its dark mode counterpart.</p>

<CodeWindow lang="css" title="default-semantic-tokens.css - simplified" mb="1.5rem" code={"/* Light mode (default) */\n:root {\n  color-scheme: light;\n  --background: oklch(1 0 0);        /* white */\n  --foreground: oklch(0.145 0 0);    /* near-black */\n  --primary: oklch(0.205 0 0);\n  --primary-foreground: oklch(0.985 0 0);\n  /* ... all other tokens ... */\n}\n\n/* Dark mode - every token flips */\n.dark {\n  color-scheme: dark;\n  --background: oklch(0.145 0 0);    /* near-black */\n  --foreground: oklch(0.985 0 0);    /* near-white */\n  --primary: oklch(0.922 0 0);\n  --primary-foreground: oklch(0.205 0 0);\n  /* ... all other tokens ... */\n}"} />

<p class="text-sm text-muted-foreground mb-5">Components reference tokens with <code>var(--background)</code>, <code>var(--foreground)</code>, etc. When the <code>.dark</code> class is present on <code>&lt;html&gt;</code>, the CSS cascade replaces every value. No component CSS needs to change.</p>

<CodeWindow lang="markup" title="toggling dark mode" mb="2.5rem" code={"<!-- Light mode -->\n<html lang=\"en\">\n<!-- Dark mode - just add the class -->\n<html lang=\"en\" class=\"dark\">"} />

<div class="sep" />

<H2>Token pairs</H2>

<p class="text-sm text-muted-foreground mb-5">Every surface token has a matching foreground token. When the surface color shifts for dark mode, the foreground shifts to maintain contrast.</p>

<TableCard title="light vs dark token values (OKLCH)" mb="2.5rem"><table class="table" data-variant="simple">
      <thead>
        <tr>
          <th class="whitespace-nowrap">Token</th>
          <th>Light</th>
          <th>Dark</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>
            <code>--background</code>
          </td>
          <td>
            <code>oklch(1 0 0)</code>
          </td>
          <td>
            <code>oklch(0.145 0 0)</code>
          </td>
        </tr>
        <tr>
          <td>
            <code>--foreground</code>
          </td>
          <td>
            <code>oklch(0.145 0 0)</code>
          </td>
          <td>
            <code>oklch(0.985 0 0)</code>
          </td>
        </tr>
        <tr>
          <td>
            <code>--primary</code>
          </td>
          <td>
            <code>oklch(0.205 0 0)</code>
          </td>
          <td>
            <code>oklch(0.922 0 0)</code>
          </td>
        </tr>
        <tr>
          <td>
            <code>--primary-foreground</code>
          </td>
          <td>
            <code>oklch(0.985 0 0)</code>
          </td>
          <td>
            <code>oklch(0.205 0 0)</code>
          </td>
        </tr>
        <tr>
          <td>
            <code>--card</code>
          </td>
          <td>
            <code>oklch(1 0 0)</code>
          </td>
          <td>
            <code>oklch(0.205 0 0)</code>
          </td>
        </tr>
        <tr>
          <td>
            <code>--muted</code>
          </td>
          <td>
            <code>oklch(0.970 0 0)</code>
          </td>
          <td>
            <code>oklch(0.269 0 0)</code>
          </td>
        </tr>
        <tr>
          <td>
            <code>--muted-foreground</code>
          </td>
          <td>
            <code>oklch(0.556 0 0)</code>
          </td>
          <td>
            <code>oklch(0.708 0 0)</code>
          </td>
        </tr>
        <tr>
          <td>
            <code>--border</code>
          </td>
          <td>
            <code>oklch(0.922 0 0)</code>
          </td>
          <td>
            <code>oklch(0.275 0 0)</code>
          </td>
        </tr>
        <tr>
          <td>
            <code>--destructive</code>
          </td>
          <td>
            <code>oklch(0.577 0.245 27.325)</code>
          </td>
          <td>
            <code>oklch(0.704 0.191 22.216)</code>
          </td>
        </tr>
      </tbody>
    </table></TableCard>

<div class="sep" />

<H2>Color scheme declaration</H2>

<p class="text-sm text-muted-foreground mb-5">The <code>&lt;meta name="color-scheme"&gt;</code> tag and the <code>color-scheme</code> CSS property tell the browser which color schemes your page supports. This affects native UI elements like scrollbars, form controls, and the default background.</p>

<CodeWindow lang="markup" title="HTML meta tag" mb="1.5rem" code={"<!-- In <head> - tells browser both schemes are supported -->\n<meta name=\"color-scheme\" content=\"light dark\">"} />

<CodeWindow lang="css" title="CSS color-scheme property in tokens" mb="2.5rem" code={":root  { color-scheme: light; }\n.dark  { color-scheme: dark; }"} />

<div class="sep" />

<H2>System preference detection</H2>

<p class="text-sm text-muted-foreground mb-5">Use <code>prefers-color-scheme</code> to detect the user's OS-level dark mode preference. The JavaScript <code>matchMedia</code> API provides both initial detection and a live listener for real-time changes.</p>

<CodeWindow lang="javascript" title="detect and react to OS theme" mb="2.5rem" code={"// after load, df$ (installed by core.js / all.js) does the DOM work;\n// the choice is a persisted store: '' = follow the OS\nconst scheme = df$.store.persisted('defuss-shadcn-theme', '');\nconst darkMQ = matchMedia('(prefers-color-scheme: dark)');\n\n// Follow OS changes in real time -\n// unless the user picked a theme by hand\ndarkMQ.addEventListener('change', (e) => {\n  if (scheme.value) return;\n  df$('html')\n    .toggleClass('dark', e.matches)\n    .css('color-scheme', e.matches ? 'dark' : 'light');\n});"} />

<div class="sep" />

<H2>Persistence</H2>

<p class="text-sm text-muted-foreground mb-5">When a user explicitly toggles dark mode, keep their choice in a persisted store - <code>df$.store.persisted(key, initial)</code>, the system's one way to Web Storage (<a class="link" href="https://www.npmjs.com/package/defuss-store">defuss-store</a>: validated, versioned, in memory when the browser blocks storage). On page load, check the saved choice first - if there is none, fall back to the OS setting.</p>

<CodeWindow lang="javascript" title="persistence logic - runs before first paint" mb="1.5rem" code={"// Synchronous in <head> (no defer, no module): it must run before the\n// first paint, so before any library - plain DOM on purpose, not df$.\n// The store's bytes are a small envelope: { format: 'defuss-store', value }\nlet saved = '';\ntry {\n  const raw = JSON.parse(localStorage.getItem('defuss-shadcn-theme'));\n  saved = raw && raw.format === 'defuss-store' ? raw.value : raw;\n} catch {}\nconst prefersDark = matchMedia('(prefers-color-scheme: dark)').matches;\n\nif (saved === 'dark' || (!saved && prefersDark)) {\n  document.documentElement.classList.add('dark');\n  document.documentElement.style.colorScheme = 'dark';\n}"} />

<p class="text-sm text-muted-foreground mb-5"><strong>Key detail:</strong> This script runs synchronously in <code>&lt;head&gt;</code> (no <code>defer</code> or <code>type="module"</code>) so the dark class is applied before the browser paints. This prevents a flash of light theme on page load.</p>

<div class="sep" />

<H2>Adding a toggle button</H2>

<p class="text-sm text-muted-foreground mb-5">A minimal dark mode toggle in three parts: the initialization script in <code>&lt;head&gt;</code>, the toggle button, and the click handler.</p>

<CodeWindow lang="markup" title="step 1 - initialization script (in <head>)" mb="1.5rem" code={"<script>\n  // Before paint - before df$ exists: the persisted choice, else the OS default\n  let saved = '';\n  try {\n    const raw = JSON.parse(localStorage.getItem('defuss-shadcn-theme'));\n    saved = raw && raw.format === 'defuss-store' ? raw.value : raw;\n  } catch {}\n  const prefersDark = matchMedia('(prefers-color-scheme: dark)').matches;\n  if (saved === 'dark' || (!saved && prefersDark)) {\n    document.documentElement.classList.add('dark');\n    document.documentElement.style.colorScheme = 'dark';\n  }\n</script>"} />

<CodeWindow lang="html" title="step 2 - toggle button markup" mb="1.5rem" code={"<button id=\"theme-toggle\" class=\"btn\" data-size=\"icon\"\n        aria-label=\"Toggle dark mode\">\n  <!-- Sun icon (shown in dark mode) -->\n  <i data-lucide=\"sun\" class=\"theme-icon-sun\"></i>\n  <!-- Moon icon (shown in light mode) -->\n  <i data-lucide=\"moon\" class=\"theme-icon-moon\"></i>\n</button>\n\n<style>\n  /* the icons follow the class - no script touches them */\n  .theme-icon-sun { display: none; }\n  .dark .theme-icon-sun { display: inline; }\n  .dark .theme-icon-moon { display: none; }\n</style>"} />

<CodeWindow lang="javascript" title="step 3 - click handler" mb="2.5rem" code={"// df$ - the query runtime core installed - reads and writes the DOM,\n// df$.store keeps the choice (validated, memory if storage is blocked)\nconst scheme = df$.store.persisted('defuss-shadcn-theme', '');\ndf$('#theme-toggle').on('click', () => {\n  const dark = !df$('html').hasClass('dark');\n  df$('html')\n    .toggleClass('dark', dark)\n    .css('color-scheme', dark ? 'dark' : 'light');\n  scheme.set(dark ? 'dark' : 'light');\n});"} />

<div class="sep" />

<H2>Custom themes</H2>

<p class="text-sm text-muted-foreground mb-8">Dark mode is just one dimension of theming. The full token system supports drop-in color themes from <a href="https://tweakcn.com" target="_blank" rel="noopener" class="btn" data-variant="link">tweakcn.com</a> - each theme includes both light and dark values. See the <a href="theming.html" class="btn" data-variant="link">Theming</a> page for details on custom themes, token structure, and the OKLCH color space.</p>

</DocPage>
