demarkus-library journal: 2026-12-02
Reader light/dark switch on branch feat/reader-scheme, uncommitted. Nav has a 3-way form (auto · light · dark) that POSTs /scheme. That sets cookie demarkus_library_scheme (auto clears it) and redirects 303 to the Referer path. The server renders <html data-scheme> from the cookie, so the first paint is right. library.css has color-scheme: var(--reader-scheme, light dark), with [data-scheme] setting the var.
The user chose "theme wins". A theme that sets color-scheme itself (e.g. the #126 hub sheet, only dark with fixed paper/ink) replaces the declaration, and islands.js syncSchemeSwitch hides the switch. It does this by flipping data-scheme in place and probing with isDarkAt, on load and on each body swap. On that deployment the switch stays hidden until the sheet uses light-dark() tokens.
JS posts the form in place (fetch, redirect: manual), sets the attribute, and re-themes mermaid. rethemeDiagrams also runs on a matchMedia change, which pays the debt entry. SchemeRoutes is registered in main, outside the turnstile; CSRF still applies.
Tests: TestSchemeSwitchStoresChoice, TestPagesRenderReaderScheme, and TestBrowserSchemeSwitch (OS change, click, persistence across a full load, hidden under a forced world). Each piece was verified by mutation. Gates green.
Seen: the nav overflows at 600px. This is pre-existing and now recorded in debt.md.
Nav overflow on narrow screens fixed on feat/reader-scheme (same branch as the scheme switch, still uncommitted). nav now has flex-wrap: wrap; justify-content: flex-end. Doors that do not fit wrap to a second row, flush right; .world still grows on row one, so the first row looks unchanged. The pane-scroll room is fine with it, since the nav is flex: 0 0 auto in a column and the canvas takes the rest. TestBrowserNavFitsNarrowScreen fails before the fix at 480px (librarian door, switch).
Gotcha: a headless Chrome --window-size=380,… screenshot is not a 380px layout. Chrome enforces a wider minimum window, so the page lays out wider and the PNG is clipped. Items look missing or overflowing when they are not. For narrow widths use CDP Emulation.setDeviceMetricsOverride, as the browser test does, and measure rects instead of trusting the screenshot.
Applied the code-review fixes on feat/reader-scheme (simplify pass done first: the switch now posts through htmx, hx-post + hx-swap="none", and the server answers 204 to htmx and 303 otherwise). Fixes:
- Mermaid renders queue one at a time, because the theme is global in
mermaid.initialize. A run whose scheme turned mid-draw callsrethemeDiagramswhen it finishes. form.schemehashx-sync="this:replace". htmx 4 defaults toqueue first, which drops clicks after the queued one.savedSchemeandpendingSchemein islands.js: a failed save (finally:request with status ≥400 or no response) reverts the page. On a body swap the page adopts the fresh nav's pressed choice, which covers another tab changing the cookie, unless a post is in flight.- The retheme skip-check matches
:scope > svg.diagram; the popout button's icon is also an<svg>. A re-theme that removes the focused popout button refocuses the new one. - The forced-scheme cache is skipped when
stylesSettledtimed out. It now resolves true or false. - The buttons have aria-labels. README and values.yaml say
DEMARKUS_COOKIE_SECUREcovers the session, CSRF and scheme cookies.
Skipped: the switch flashing before it hides in a forced world (it would need the server to detect forcing in theme CSS); the 2048-character return cap for no-JS long trails; content jumping during a redraw; the example logo's prefers-color-scheme.
Test gotcha: CDP Emulation.setEmulatedMedia returns before the matchMedia change event fires. The event lands at the next rendering step, so wait two requestAnimationFrames before acting, or the test races past the window it means to cover. Mutation of the popout fix only failed after adding that wait.