Light and dark

A visitor picks light or dark once, in the user button or in the portal bar, and expects the whole thing to follow: Meerkat's own pages and the application behind them. The pages are ours to paint. The application is yours, and no two of them read a scheme the same way - which is why the mechanism is declared on the route rather than guessed.

This page is written for the person integrating an application. For the pages Meerkat serves itself, see the built-in pages.

Where the choice lives

The visitor's choice is a cookie, MEERKAT_SCHEME, holding light, dark or auto, kept for a year and SameSite=Lax. It is also stored on their account, so it is put back on a browser that has never seen them.

auto means "follow the system", and it is the default: nothing is imposed until somebody chooses.

Where it is set

Two sections of the route editor (Infra, Routes, open a route; both need the route's UI box ticked):

Section What you decide there
Color scheme how the application takes light and dark: the Mechanism, its tag and values, This application has a system mode, and Override localStorage
User button whether the button offers the switch: Offer the color-scheme switch. Without a switch, The button wears forces the button alone light or dark

When the portal is on, the switch is in the portal bar instead of the user button, and the route's Color scheme section still says how its application takes it.

The Color scheme section of a route: the class mechanism on html, a light and a dark value, This application has a system mode, and the localStorage override

What the gateway always does

On every UI route the gateway injects a small agent, /meerkat/page.js. Unless the route is always light or always dark, when a scheme is chosen it sets on <html>:

On auto both are removed, and the browser is back to following the system.

That alone is enough for an application whose CSS is written on prefers-color-scheme or on light-dark(). Everything below is for applications that switch some other way.

Choosing the mechanism

The Mechanism list, as the console shows it, with the value stored on the route:

Mechanism Stored as What the gateway writes Typical markup
always light none, the button wearing light nothing on the page; no switch; the button and the portal bar wear light an application with one light look
always dark none, the button wearing dark the same, in dark an application with one dark look
color-scheme only empty the CSS color-scheme only your CSS reads prefers-color-scheme or light-dark()
set attribute attribute one attribute, named by you, always written, to the light value or the dark one <html data-theme="dark">
add attribute add-attribute the two values are attribute names, added bare and removed like classes <body dark-theme>
class class the two values are class names, the two removed and the current one added <body class="dark">
script script nothing itself: it calls a function you write anything else

The fields that follow the choice:

Which is why an empty value means something in the last two: nothing on the tag in that state. That is the most widespread shape there is - nothing in light, dark in dark:

Mechanism Light value Dark value Result
class (empty) dark <body> in light, <body class="dark"> in dark
add attribute (empty) dark-theme <body> in light, <body dark-theme> in dark
set attribute on data-theme light dark always written, one or the other

When the agent runs. The agent is a deferred script injected at the top of <body>. It runs once the document is parsed, before the application's own deferred and module scripts, but after any blocking script the application has in its <head>. A class is never written on <html> in place of a tag that does not exist: a class left on the wrong element is a theme nothing removes.

This application has a system mode

Ticked by default. Untick it for an application that knows light and dark and nothing else: the visitor's "follow my system" is then turned into light or dark before it reaches the application, and the switch offers two choices instead of three.

The script mechanism

For an application that switches some other way - swapping two stylesheets, dispatching an event, calling a function its bundle exported - choose script, then Add a script. You write the body of a function that receives colorScheme: "light", "dark" or, when the application has a system mode, "auto". It is called on page load and on every change.

// colorScheme is "light", "dark" or "auto"
const dark = colorScheme === 'dark' ||
  (colorScheme === 'auto' && matchMedia('(prefers-color-scheme: dark)').matches);
document.querySelector('#theme-dark').disabled = !dark;
document.querySelector('#theme-light').disabled = dark;

The body is served as its own same-origin file, /meerkat/scheme.js, so an application's Content-Security-Policy (script-src 'self') lets it run. It is limited to 4000 characters: a few lines reaching into the application, not a bundle.

What "always light" and "always dark" are

Both say this UI has no colour scheme of its own: light and dark mean nothing here. It is not color-scheme only, which takes the CSS color-scheme and nothing more.

The switch is then not offered in the user button, and the agent leaves the document alone. The look you chose is what the user button and the portal bar wear, so they match the page instead of floating light on a dark application.

Offering the switch, and dressing the button

Two things that used to travel together and are now separate:

When a route does not offer the switch, The button wears says what the injected button itself wears: what the visitor chose, light or dark. It dresses the button alone; the page is never touched.

Applications that keep their own theme

Here is the case that bites, and the reason this page exists.

An application that remembers its own light or dark in localStorage restores it on load, over whatever the gateway just applied. The visitor's choice holds until the app's own script runs, then snaps back. The same portal then looks right in one browser and wrong in another, and what differs is only what that browser had in store.

Fighting it on the document does not work. The way to work with it is to speak its own storage, under Override localStorage in the Color scheme section:

Field What it is
Override the application's stored theme the switch. Without it the gateway never touches the application's storage
Key the localStorage key the application keeps its choice under: theme, color-mode, vuetify:theme...
System what "follow the system" is called there. Empty removes the entry instead. Hidden when the application has no system mode
Dark what to write for dark. Defaults to dark
Light what to write for light. Defaults to light

The values are the application's own vocabulary - dark, night, 1 - and the gateway does not guess them: guessing would be writing a dialect we do not speak.

An empty System value is a choice of its own: with nothing stored, the application falls back to its own default, which for most of them is the system.

The write is an inline script at the top of <body>, just before the agent, and runs only once the visitor has chosen. It runs before the application's deferred and module boot scripts, so the value is already in place when the app first reads it - and the chrome then simply inherits the document. An application that reads its storage from a blocking script in its <head> has read it before.

ng-m3-theme from the Softwarity ecosystem is the case that taught this: its service keeps system, light or dark under a key, and in system mode it clears the document's color-scheme on every run. Anything the gateway set was wiped a tick later. Writing the choice into that key instead lets the application apply it the way it already knows how, before its first paint - so here the System value is system, not empty.

Two worked examples

An Angular Material application with a class on body

The application's styles switch on <body class="dark-theme">, and nothing in light.

Field Value
Mechanism class
Tag name body
Light value (empty)
Dark value dark-theme

The visitor picks dark, and the page becomes:

<html style="color-scheme: dark" data-meerkat-scheme="dark">
  <body class="mat-typography dark-theme">

A Vuetify application that keeps its theme in localStorage

The application stores light or dark under the key vuetify:theme, and reads it at boot.

Field Value
Mechanism color-scheme only
This application has a system mode unticked
Override the application's stored theme ticked
Key vuetify:theme
Dark dark
Light light

The visitor picks dark: before the application boots, the gateway has written localStorage['vuetify:theme'] = 'dark', and the application dresses itself in dark the way it always does. Check it in the browser's DevTools, Application, Local Storage.

Debugging it

What the route declared travels on the agent's own script tag, so one look at the served HTML says what the gateway thinks it was told:

  1. Open the application through the gateway.
  2. Open the page source (View source), or the browser's DevTools, Elements.
  3. Search for /meerkat/page.js, near the top of <body>.
<script defer src="/meerkat/page.js?v=..."
 data-scheme="select"
 data-scheme-mechanism="attribute"
 data-scheme-tag="html"
 data-scheme-light="light"
 data-scheme-dark="dark"
 data-scheme-attribute="data-theme"
 data-scheme-storage="theme"
 data-scheme-storage-light="light"
 data-scheme-storage-dark="dark"
 data-scheme-storage-auto="system"></script>

data-scheme="select" says the route offers the switch, data-scheme-no-auto="1" that the application has no system mode, and data-scheme-wear which look an always light or always dark route wears. With script, the tag carries data-scheme-mechanism="script" and is preceded by <script defer src="/meerkat/scheme.js?...">.

If a field you filled in is absent there, the route did not save it. If it is present and nothing happens, the tag name is usually the answer.