writing / web

A Dark Mode Button That Remembers

A light and dark toggle that matches your device until you pick one, then remembers your pick. Here's how I worked through the roadblocks to build it.

Ernesto MelchorApplication Systems & AI Engineer

I wanted one small feature on my blog: a light and dark button that matches your phone or computer’s light or dark setting until you pick one yourself, and then remembers your pick.

The code for this is well covered. Josh Comeau’s The Quest for the Perfect Dark Mode is the article to read, and my version adapts his technique. This is about the decisions around that code: what I tested before leaving WordPress.com, the one rule that makes the feature behave, and what I chose not to build.

What I tested on WordPress

In October 2026 my site was on the WordPress.com Personal plan, and it was light by default.

I started with a plugin. I could install plugins on my plan, but the free dark mode plugin I looked at generated its own dark colors. Using my exact colors was part of its paid tier.

Then I tried CSS alone. WordPress stripped out a checkbox when I tried one on a draft page, but a Details block worked, and the background switched colors. Then the choice reset on every page load. CSS can hold a state while the page is open, but it can’t save it for the next page. Saving it takes code, and in the browser that means JavaScript.

WordPress.com’s docs say custom JavaScript needs a plugin-enabled plan and activated hosting features. WordPress.com listed my site as “simple”, which suggested those weren’t active.

There was probably a way through. I could have paid for the plugin, or activated hosting features and written a small plugin of my own. But I kept hitting roadblocks, and I wanted to clear the path, ship the site, and move on to other projects. I also wanted the freedom to build other things on my site. Moving was my choice. The blog is now a static site built with Astro and TypeScript.

I set the requirements and an AI assistant wrote the code. Then I went through it line by line until I could explain how it works. This article comes from that review.

The rule: the site only saves a choice when the reader clicks the button

Two scripts share one saved note in localStorage, called ec-theme. The first runs before the page appears:

(function () {
  var root = document.documentElement, theme;
  try { theme = localStorage.getItem("ec-theme"); } catch (e) {}
  if (theme !== "light" && theme !== "dark") {
    theme = window.matchMedia("(prefers-color-scheme: light)").matches ? "light" : "dark";
  }
  root.dataset.theme = theme;
  root.dataset.js = "1";
})();

The script looks for the note. If one is not found, it asks the device whether it’s set to light. If the answer is yes, it shows the site in light mode. If it’s not, it shows the site in dark mode.

A page opensthe script runs before anything is shown
Is there a saved note?ec-theme is exactly "light" or "dark"
Yes: use it. No: ask the device.prefers-color-scheme: light ? light : dark
Put the label on the page<html data-theme="light"> or "dark"
CSS shows the matching colorsnothing is saved unless the reader clicks
Every page load runs this path. Only a click on the button writes the note.

The script writes the answer as a label on the top element of the page. It doesn’t save it in the note. Only a click on the button writes the note.

That’s the whole feature. Comeau’s code follows the same rule, but his article doesn’t say why it matters. If a reader never clicks, nothing is ever saved, so the script asks the device again on every new page. If their phone switches to dark tomorrow, the next page they open is dark. If the script saved the device’s answer, the site would treat it as the reader’s pick and stop matching their device.

The saved note only records a choice the reader made by clicking.

After a click, the script finds the note, reads “dark”, and stays dark unless the reader clicks again.

Why changing one label switches every color

:root,
html[data-theme="dark"] {
  color-scheme: dark;
  --bg: #08100d;
  --ink: #f2f5f3;
}
html[data-theme="light"] {
  color-scheme: light;
  --bg: #f6f4ee;
  --ink: #1b2420;
}

The rest of my CSS uses the variables, like background: var(--bg), for the theme colors.

The script only changes a label on the top element of the page. Both sets of colors are already loaded in the CSS, so the switch is instant. Those hex values are my exact colors, which is what the free plugin wouldn’t give me.

Five small details that keep it fast, safe, and working

No flash of the wrong colors. The script is inline in <head>, ahead of the stylesheets. An inline script like this blocks the browser, so nothing is painted until it has run, and the first paint already has the right colors. Comeau’s version sits at the start of <body> and sets the CSS variables directly. Mine sets one label and lets the CSS do the rest. The button’s own script can load later, because nobody can click a button they can’t see yet.

A strict security policy. My site gives browsers a guest list for scripts. The build takes a fingerprint (a SHA-256 hash) of my one inline script and puts it on the list. If someone slips another script into a page, or changes mine, it won’t match, so the browser won’t run it. That makes it much harder for anyone to use my blog to track or mislead the people reading it.

If someone edits the saved note. Anyone can change the note in their own browser’s developer tools. If someone sets it to banana, the check for exactly “light” or “dark” fails and the script asks the device instead.

If the browser blocks saving. Some browsers can block access to localStorage. Both scripts wrap it in try/catch, so the button still switches the colors, but the choice only lasts for that page.

No cookies. A server could remember the choice with a cookie, but my site is static files with no server code, and I wanted nothing to leave the browser.

What I left out

After you click the button, the site stops matching your phone’s light or dark mode. From then on, it uses the reader’s pick until they clear their browser data. Going back to matching the phone would need a third setting, which turns the button into a three-way switch. I kept it simple with two.

If your phone switches between light and dark mode while you’re reading. If you haven’t clicked the button, the site checks your device each time a page opens, not while you read. If your phone switches to dark mid-post, the next page you open catches up. For a blog, I prefer that experience.

If a reader has JavaScript turned off, the site is always dark and has no button. The script never runs, so the CSS hides the button and uses dark. A few lines of CSS could match the device’s setting without JavaScript. This one is my preference, not a technical limit. I like dark, so that’s the default when the script can’t run.

Through all of this, the saved note has one job. It only records a choice the reader made by clicking.

Save decisions, not defaults.

Store what the reader chose. Work out everything else fresh on each page, so the site keeps matching their device until they pick.

← All writing