DocumentationDark mode

Open in Create
Search
astronomic.dev

Dark mode

How light and dark work in Astronomic UI, the ModeToggle switch, and loading a page with no flash.

Every part in Astronomic UI works in light and dark with no extra code. Dark mode is a second set of token values. When the page turns dark, the tokens swap and every part follows.

Parts never use Tailwind's dark: variant, so a page has only one place to change: the tokens.

The three modes

A page is in one of three modes. The data-theme attribute on <html> decides which.

Mode<html>What the reader sees
Systemno data-themeLight or dark, from the reader's system setting
Lightdata-theme="light"Light, whatever the system says
Darkdata-theme="dark"Dark, whatever the system says

System is the default. With no switch and no attribute, the page follows the reader's system.

The token pairs

Most color tokens have a light value and a dark value. tokens.css sets the dark values twice, and a preset does the same for the colors it changes. A color a preset leaves alone keeps the value from tokens.css:

css
/* Dark for a reader whose system is dark, unless the page is pinned to light */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --ui-bg: #0a0a0a;
    --ui-fg: #ededed;
    color-scheme: dark;
  }
}

/* Dark for a page pinned to dark */
:root[data-theme="dark"] {
  --ui-bg: #0a0a0a;
  --ui-fg: #ededed;
  color-scheme: dark;
}

Keep the two blocks the same. When you change a dark value, change it in both.

The dark blocks also set color-scheme: dark, so the browser draws its own controls and scrollbars dark. In dark mode --ui-media-dim dims images in a media frame a little, so a bright photo does not glare on a dark page. Soft and Ink turn it off (--ui-media-dim: none).

The token tables on Tokens show each color's light and dark value.

Add the switch

ModeToggle is a button with a moon icon. A press shows the other look and saves the choice. A press back to the system's own look clears the choice, so the page follows the system again.

Put the switch where readers will find it, such as the navbar's utilities slot:

astro
---
import ModeToggle from "@astronomic-dev/ui/components/ModeToggle.astro";
---
<ModeToggle />

Its pressed state tells screen readers whether dark mode is on. Its accessible name is "Dark mode". Pass label to set it in your site's language. Without JavaScript the switch is hidden, and the page follows the system.

The saved choice

The switch saves light or dark in the browser's localStorage, under the key aui-theme. When the reader goes back to the system's look, the key is removed.

If the browser blocks storage, as some private windows do, the switch still works for the page. The choice is not kept for the next visit.

No flash on load

The saved choice must apply before the page paints. If it ran later, a reader who picked dark would see a flash of light first.

Add the kit's small inline script to the <head> of every page:

astro
---
import { modeScript } from "@astronomic-dev/ui/lib/mode.ts";
---
<head>
  <script is:inline set:html={modeScript} />
</head>

It reads aui-theme and sets data-theme on <html> before the first paint. With nothing saved, it does nothing, and the page follows the system.

Tip

For a site that always opens in light (or dark), whatever the system says, use modeScriptFor("light") from the same file in the <head>, and <ModeToggle base="light" />. A press then shows dark and saves it, and a press back to light clears the choice.

Waitlist

Be first in line

Join the waitlist and we'll email you the moment it's out.

You'll also get our newsletter: new themes, plugins and guides. Unsubscribe any time.