cuckcoding.pro
Menu

Build a keyboard-friendly, persistent dark mode

Your new dark mode works beautifully until somebody wants light mode on a dark laptop. A usable preference needs three choices. Build a tiny page that can follow the system, override it and give control back without reloading.

Before you start
A browser, a text editor, Python 3 and a directory for three files. Use one local HTTP address for every check.
What to verify
A native selector that works from the keyboard, remembers an explicit choice and returns to live system changes when reset.

Give all three choices names

Create index.html. A labelled native select provides a visible selection and keyboard behaviour before you design a custom moon icon. The control starts disabled until its change handler can work. With JavaScript unavailable, the page can still follow the system preference through CSS.

index.html
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Chair preferences</title>
<link rel="stylesheet" href="theme.css">
<script src="theme.js" defer></script>
<main>
  <h1>Chair preferences</h1>
  <label for="theme">Theme</label>
  <select id="theme" disabled>
    <option value="system">System</option>
    <option value="light">Light</option>
    <option value="dark">Dark</option>
  </select>
</main>
</html>

The option values are stable identifiers for storage. Translate the words people see without translating the values.

Let CSS handle the system

Put the complementary text and background colours in theme.css. Without a manual light choice, the media query can activate the dark pair. A manual dark choice also works on a light system. The color-scheme property lets built-in controls use the matching colour scheme.

theme.css
:root {
  color-scheme: light;
  --paper: #fffaf0;
  --ink: #231d18;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --paper: #201d1a;
    --ink: #fff5e3;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --paper: #201d1a;
  --ink: #fff5e3;
}
body { background: var(--paper); color: var(--ink); }
:focus-visible { outline: 3px solid currentColor; outline-offset: 3px; }

Keep a visible focus indicator. Tab to the selector in both palettes. It should remain obvious which control will receive the next keystroke.

Persist only a manual override

Save the script in theme.js. Loading accepts only the two supported overrides. System mode removes the attribute and the stored key. The CSS query can then react directly to system changes while the page stays open. If storage is unavailable, the current-page change still works.

theme.js
const picker = document.querySelector('#theme');
const root = document.documentElement;
const key = 'chair-theme';

function apply(value) {
  const manual = value === 'dark' || value === 'light';
  if (manual) root.dataset.theme = value;
  else root.removeAttribute('data-theme');
  picker.value = manual ? value : 'system';
}

let saved;
try { saved = localStorage.getItem(key); } catch {}
apply(saved);
picker.disabled = false;
picker.addEventListener('change', () => {
  const value = picker.value;
  apply(value);
  try {
    if (value === 'system') localStorage.removeItem(key);
    else localStorage.setItem(key, value);
  } catch {}
});

Keep the storage key specific to this feature. Clearing every localStorage key to reset the colour scheme is an impressively expensive way to change a background.

Exercise all the transitions

Start the local server below and open http://127.0.0.1:8000/. With System selected, switch the system or browser emulation to dark and watch the page change. Force Light, reload, and confirm the page stays light. Force Dark with a light system. Finally choose System and toggle the system again without reloading.

Terminal
python3 -m http.server 8000 --bind 127.0.0.1

Ask the agent to list which layer owns each result. If it proposes a timer that polls the system theme, ask which observed transition the existing CSS failed.

Try this prompt with your agent

Prompt
Implement System, Light and Dark using one native selector. Save only manual overrides. Tell me how to check system switching, a manual override surviving reload and reset from manual to system, all from the keyboard.

A trap worth catching

The storage belongs to the origin. Changing from localhost to 127.0.0.1 or changing the port creates a different test environment.

Official documentation