dotted‑world‑map

How do I make an SVG map follow dark mode?

Leave the map's color at its default, currentColor, and put the SVG inline in your page: the dots take the text colour around them, so they turn light when your page turns dark, with no JavaScript. If the page has no dark theme, one CSS rule gives the map one: color-scheme: light dark; color: light-dark(#1b1a17, #e9e6df);.

npm install @setkernel/dotted-map-next

An inline map and one CSS rule

getSVG() draws the dots in currentColor on a transparent background unless you say otherwise. className sets the class of the root <svg>, which the rule styles. Switch your system between light and dark: the map below follows it.

map.mjs
import { DottedMap } from '@setkernel/dotted-map-next';

const map = new DottedMap({ height: 40 });
const svg = map.getSVG({ className: 'world-map', title: 'World map' });
world-map.css
.world-map {
  color-scheme: light dark;
  color: light-dark(#1b1a17, #e9e6df);
}
World map
Drawn by map.mjs, with the CSS above, when this page was built.

light-dark() picks its first colour in a light colour scheme and its second in a dark one, and color-scheme: light dark lets the map take the scheme the reader chose. On a page that already switches its text colour for dark mode, leave the rule out: the dots follow the text.

As a file or an image

In a file or an <img>, the page's CSS cannot reach the SVG, and currentColor is black. Draw the map twice, in a colour for each scheme, and let <picture> choose: still no JavaScript.

two-files.mjs
import { writeFileSync } from 'node:fs';
import { DottedMap } from '@setkernel/dotted-map-next';

const map = new DottedMap({ height: 40 });
writeFileSync('map-light.svg', map.getSVG({ color: '#1b1a17' }));
writeFileSync('map-dark.svg', map.getSVG({ color: '#e9e6df' }));
picture.html
<picture>
  <source srcset="map-dark.svg" media="(prefers-color-scheme: dark)" />
  <img src="map-light.svg" alt="World map" />
</picture>