Skip to main content

2026-11-30 · 5 min read

Moving a standalone static map into the site as React components: what I measured and what broke

Next.jsData

Translated from the Chinese original: read it in Chinese

The first version of the election map (a tool page in Chinese) was a standalone static front-end app: an index.html plus 6 data files (3.3 MB in all), placed under public/apps/, with an entry page that sent people there through a link. The reason was practical: the whole site sets X-Frame-Options: DENY, which blocks even same-origin iframes, so at the time the only option was "open it on a separate page".

It worked, but it had some drawbacks: it did not follow the site's dark mode, had no site navigation bar, and its styles and code were a separate set, so changing one color meant changing it in two places. This post records rewriting it as in-site React components.

Structure: separate computing from drawing

The original app was one HTML file of 400-odd lines, with state, data processing and the screen all mixed together. In the rewrite I split along "pure functions in lib/, the screen in components/":

  • lib/election/model.ts: dataset definitions, winners, seats, and stats on consecutive terms and party changes
  • lib/election/view.ts: turns "what is selected now" into each unit's color, tooltip and legend on the map
  • lib/election/palette.ts: party colors, gradients, light and dark
  • components/election/: the control bar, the SVG map, the detail panel and the list, one file each

The benefit is that the first three files are pure functions (data in, result out, no React and no DOM), so I can write unit tests for them directly: there are currently 27, including logic like "how many consecutive terms the latest cycle's winner has held" and "how to label two voting days in the same year", which could previously only be verified by clicking through the screen.

Data: load only when switched to

The data went from 6 .js files to 10 plain JSON files (about 2.7 MB uncompressed in total) in public/election/. The first screen needs only the county boundaries (42 KB) and the mayors (about 280 KB); president, legislature, councilors, village chiefs and referendums are each downloaded when the user switches to them, and the township boundaries only when "view townships" is pressed. One E2E test specifically checks that the default view does not fetch the referendum and village chief files.

The numbers I measured (production build, no local throttling, 3 runs):

ItemMeasured
JS (compressed)200–213 KB
CSS31 KB
HTML82 KB
Requests39
LCP380–812 ms

I also wrote these into the performance budget (e2e/perf-budget.spec.ts), so a regression gets blocked. The data files are loaded with fetch, so they are not in the JS column, only in the request count.

State: derive it, don't patch it in an effect

Switching datasets has an old problem: if the current selection is "2026" and you switch to president (which has no 2026), it should fall back to the latest cycle; switching to referendums also means reselecting the question and the metric.

The most intuitive way is to detect an invalid state in useEffect and fix it with setState, but that costs an extra render and flashes a wrong screen. Instead, derive it on every render:

const cyc = cycleKeys.includes(raw.cyc) ? raw.cyc : (cycleKeys.at(-1) ?? raw.cyc);
const metric = def.metrics.some((m) => m.id === raw.metric) ? raw.metric : def.metrics[0].id;
const s = { ...raw, cyc, metric };

The raw selection (raw) is left untouched, and the screen uses the derived s. When the user switches back, the year they originally chose is still there.

Loading follows the same idea: the result of useResource(key, load) carries the key it was loaded for, and when the key changes the old result counts as "loading", so the previous dataset's data is never drawn into the new screen.

The map itself: dark mode and accessibility

Each county is a <path>. The fill is not a hard-coded color but color-mix(in srgb, party color N%, var(--surface)): the bigger the lead, the stronger the color, and the base it mixes with is the site's --surface, so dark mode needs no separate set of colors, and a pale color on a light base naturally becomes a dark one on a dark base.

For operability, each <path> has role="button", tabIndex, aria-pressed and aria-label (the tooltip text), and Enter and Space select it. The selected one is drawn on the top layer (SVG has no z-index; later elements are drawn on top). Hovering with a mouse selects, but only for a mouse; touch and keyboard do not trigger it just by passing over.

Two pitfalls

The gold text lacked contrast. The "different party from the previous cycle" marker uses gold #d4a017, which is fine for a stroke. But the words for "party changed" in the list used the same gold, and the a11y scan (axe) found 10 color-contrast violations in light mode. The fix was to split "gold for strokes" and "gold for text" into two variables: the text one became the darker #8a6500. A color is not one thing; different uses need different contrast.

getByRole("alert") always finds one. I wrote a test: after switching datasets, no error message (role="alert") should appear on the page. It kept failing, because Next.js puts a route announcer on the page (to notify screen readers of navigation), and that is itself role="alert". The right thing was to look for my own .elx-error. A test should look for "my error message", not "any alert".

Small things handled along the way

  • Tailwind scans the whole repo, and a 3 MB JSON file should not be a source of class names, so I added @source not "../../public/election/**" to globals.css.
  • Styles load only on this page: election.css is imported by the component itself and not put in the site-wide globals.css, so other pages do not load these styles.
  • The old static app was removed entirely, and an E2E test confirms the old URL returns 404.

Summary

Before the rewrite I thought moving the screen would take the most time, but what actually took the most was "pulling the testable parts out of the screen". Once pulled apart, what the next post is about became possible: adding an election cycle without changing a single line of site code.

Type to search projects and posts too