Getting started

Actions

Forms

Layout

Navigation

Overlays

Feedback

Data display

Controllers

Helpers

Recipes

Color scheme

The head script that applies a saved color scheme before first paint, and the resolved scheme and color-scheme value for a preference.

Reach for them for a theme switch and the <head> script that goes with it. ColorSchemeController saves and applies the choice in the browser; these do the same decisions without a DOM, so they run on the server and in tests. Each result below is computed when the page is built, by running the call shown. Theming explains why setting color-scheme is all a scheme needs.

Import

import {
	COLOR_SCHEME_PREFERENCES,
	DEFAULT_COLOR_SCHEME_ATTRIBUTE,
	DEFAULT_COLOR_SCHEME_CLASSES,
	DEFAULT_COLOR_SCHEME_STORAGE_KEY,
	colorSchemeBootScript,
	colorSchemeValue,
	resolveColorScheme,
	type ColorSchemeBootOptions,
	type ColorSchemeMarkers,
	type ColorSchemePreference,
	type ResolvedColorScheme,
} from '@ecopages/radiant-ui/helpers';

Preference and resolved scheme

A ColorSchemePreference is what the visitor chose: system, light or dark. COLOR_SCHEME_PREFERENCES lists them in the order a toggle cycles through them. A ResolvedColorScheme is what the page shows, light or dark. resolveColorScheme(preference, systemDark) replaces system with what the system asks for, and colorSchemeValue(preference) is the color-scheme value to write: light dark for system, so the page follows the system with no script, else the preference.

CallResult
COLOR_SCHEME_PREFERENCES["system","light","dark"]
resolveColorScheme('system', true)"dark"
resolveColorScheme('system', false)"light"
resolveColorScheme('light', true)"light"
colorSchemeValue('system')"light dark"
colorSchemeValue('dark')"dark"

Boot script

colorSchemeBootScript(options) returns the inline script that applies the saved scheme before first paint, as a string for a blocking <script> in <head>. It does what ColorSchemeController does on attach(), with no import, so the page shows the right scheme before any module loads: it reads the preference from localStorage (a value outside the three, or blocked storage, is system), resolves system through prefers-color-scheme, then writes the preference attribute, color-scheme and the markers on <html>. The script has no whitespace, since it sits in every page.

OptionDescription
storageKeyThe localStorage key of the saved preference. Default DEFAULT_COLOR_SCHEME_STORAGE_KEY (rui-color-scheme).
preferenceAttributeThe attribute that carries the preference. Default DEFAULT_COLOR_SCHEME_ATTRIBUTE (data-color-scheme).
classesThe class names toggled for the resolved scheme. Default DEFAULT_COLOR_SCHEME_CLASSES (dark and light); false writes none.
attributeAn attribute set to the resolved scheme, such as data-theme. None by default.
queryKeyA query parameter that overrides the saved preference for this page load, such as a shared link's ?mode=dark. The saved value is not changed.

classes and attribute are the ColorSchemeMarkers, which the controller takes too. Pass the controller the same options, so the two agree; the script cannot run the controller's apply function, nor target an element other than <html>.

import { colorSchemeBootScript } from '@ecopages/radiant-ui/helpers';

// the dependencies of an Ecopages html template
dependencies: { scripts: [{ content: colorSchemeBootScript({ storageKey: 'theme', attribute: 'data-theme' }) }] }