Charter
Problem. A React app's light / dark / automatic preference has to show the right colors on the first frame, on pages that are statically prerendered and served from a CDN, where server-rendered HTML alone flashes the wrong colors for a returning visitor.
Goals.
- Apply the stored preference before first paint, with no flash.
- Switch the UI the moment the user chooses, and keep every open tab in step.
- Work on prerendered, CDN-served pages, with no cookies, loaders or server functions.
- Keep the user's preference and the resolved value apart in the API.
Non-goals.
- Server-side persistence: an app that stores the preference elsewhere passes its own
persistAppearance.
Appearance is the preference, color scheme is what the page shows
Status: Accepted
Context. A preference of automatic and the light or dark value it resolves to are different things, and one word
for both makes every API that touches them ambiguous.
Decision. The vocabulary follows Apple's Human Interface Guidelines: the appearance (light, dark, automatic)
is what the user chose, and the color scheme (light, dark) is what the page applies after automatic resolves.
Neither is called a theme.
Consequences. Every export names which one it carries (Appearance, ColorScheme, resolveColorScheme), and
<html> carries both: the color scheme as a class and the color-scheme property, the appearance as data-appearance.
The preference lives in localStorage
Status: Accepted
Context. Prerendered HTML is cached and shared across visitors, so nothing the server renders can carry one visitor's preference.
Decision. The default persistence is localStorage, read by the provider in its first render and by an inline
<AppearanceScript> in <head> before the browser paints. The server renders the defaults, and the script corrects
<html> before first paint.
Consequences. The package needs no cookies, loaders or server functions. Markup that depends on the preference still
hydrates against the server's default once. A custom persistAppearance replaces the localStorage write, which leaves
the pre-paint script nothing to read on the next visit.
License
Released under the MIT License.