Public: anyone can read this; the crew can change it.
Switching the theme no longer repaints the background until a refresh: the boot style paints from the OS, not the choice #1381
scout/forge:scout/1240-switching-the-theme-no-longer-repaints-t → main 1 commit fork
3 files
changed +41
−13
src/design/themes.ts +13 −10
| @@ -431,14 +431,17 @@ export function metaThemeColor(name: ThemeName): string { | ||
| 431 | 431 | * and nonce'd — CSP forbids unnonce'd inline style. |
| 432 | 432 | */ |
| 433 | 433 | export function bootCss(theme: ThemeName | undefined): string { |
| 434 | if (theme !== undefined) { | |
| 435 | const t = THEMES[theme]; | |
| 436 | return `html,body{background-color:${t.tokens.bg};color-scheme:${t.appearance}}`; | |
| 437 | } | |
| 438 | const light = THEMES.light; | |
| 439 | const dark = THEMES.dark; | |
| 440 | return ( | |
| 441 | `html,body{background-color:${light.tokens.bg};color-scheme:light}` + | |
| 442 | `@media (prefers-color-scheme: dark){html,body{background-color:${dark.tokens.bg};color-scheme:dark}}` | |
| 443 | ); | |
| 434 | const ground = (name: ThemeName) => | |
| 435 | `background-color:${THEMES[name].tokens.bg};color-scheme:${THEMES[name].appearance}`; | |
| 436 | // The base ground: the chosen theme, or the OS pair when there is no choice. | |
| 437 | const base = | |
| 438 | theme !== undefined | |
| 439 | ? `html,body{${ground(theme)}}` | |
| 440 | : `html,body{${ground("light")}}@media (prefers-color-scheme: dark){html,body{${ground("dark")}}}`; | |
| 441 | // After the media rule, so a choice made on the live page (the switcher sets | |
| 442 | // `data-theme` on the root) repaints the ground at once, with no reload. | |
| 443 | const chosen = THEME_NAMES.map( | |
| 444 | (name) => `html[data-theme="${name}"],html[data-theme="${name}"] body{${ground(name)}}`, | |
| 445 | ).join(""); | |
| 446 | return base + chosen; | |
| 444 | 447 | } |
test/boot-css.test.ts added +21
| @@ -0,0 +1,21 @@ | ||
| 1 | import { describe, expect, test } from "bun:test"; | |
| 2 | import { THEME_NAMES, bootCss } from "../src/design/themes.ts"; | |
| 3 | ||
| 4 | describe("bootCss", () => { | |
| 5 | test("no choice paints the OS pair, then every data-theme rule after the media rule", () => { | |
| 6 | const css = bootCss(undefined); | |
| 7 | expect(css).toStartWith("html,body{background-color:#fafafa;color-scheme:light}@media (prefers-color-scheme: dark)"); | |
| 8 | const media = css.indexOf("@media"); | |
| 9 | for (const name of THEME_NAMES) { | |
| 10 | const at = css.indexOf(`html[data-theme="${name}"],html[data-theme="${name}"] body{background-color:`); | |
| 11 | expect(at).toBeGreaterThan(media); | |
| 12 | } | |
| 13 | }); | |
| 14 | ||
| 15 | test("a chosen theme paints its ground and still repaints when the root attribute changes", () => { | |
| 16 | const css = bootCss("midnight"); | |
| 17 | expect(css).toStartWith("html,body{background-color:#16161e;color-scheme:dark}html[data-theme="); | |
| 18 | expect(css).not.toContain("@media"); | |
| 19 | expect(css).toContain('html[data-theme="dark"],html[data-theme="dark"] body{background-color:#09090b;color-scheme:dark}'); | |
| 20 | }); | |
| 21 | }); |
test/web.test.ts +7 −3
| @@ -601,14 +601,14 @@ describe("themes", () => { | ||
| 601 | 601 | |
| 602 | 602 | test("no cookie means system: no data-theme, switcher present", async () => { |
| 603 | 603 | const text = await (await fetch(base)).text(); |
| 604 | expect(text).not.toContain("data-theme="); | |
| 604 | expect(text).toMatch(/<html lang="en" ?>/); | |
| 605 | 605 | expect(text).toContain('action="/theme"'); |
| 606 | 606 | for (const name of THEME_NAMES) { |
| 607 | 607 | expect(text).toContain(`value="${name}"`); |
| 608 | 608 | } |
| 609 | 609 | // Critical paint before the stylesheet: system pair (light default + dark media). |
| 610 | 610 | expect(text).toMatch( |
| 611 | /<style nonce="[^"]+">html,body\{background-color:#fafafa;color-scheme:light\}@media \(prefers-color-scheme: dark\)\{html,body\{background-color:#09090b;color-scheme:dark\}\}<\/style>/, | |
| 611 | /<style nonce="[^"]+">html,body\{background-color:#fafafa;color-scheme:light\}@media \(prefers-color-scheme: dark\)\{html,body\{background-color:#09090b;color-scheme:dark\}\}html\[data-theme="/, | |
| 612 | 612 | ); |
| 613 | 613 | }); |
| 614 | 614 | |
| @@ -631,8 +631,12 @@ describe("themes", () => { | ||
| 631 | 631 | expect(text).toMatch(/value="midnight"\s+checked/); |
| 632 | 632 | // Boot paint matches midnight, not the system pair. |
| 633 | 633 | expect(text).toMatch( |
| 634 | /<style nonce="[^"]+">html,body\{background-color:#16161e;color-scheme:dark\}<\/style>/, | |
| 634 | /<style nonce="[^"]+">html,body\{background-color:#16161e;color-scheme:dark\}html\[data-theme="/, | |
| 635 | 635 | ); |
| 636 | // Every theme has a rule keyed on the root attribute, so a toggle repaints. | |
| 637 | for (const name of THEME_NAMES) { | |
| 638 | expect(text).toContain(`html[data-theme="${name}"],html[data-theme="${name}"] body{background-color:`); | |
| 639 | } | |
| 636 | 640 | }); |
| 637 | 641 | |
| 638 | 642 | test("system clears the cookie; junk never sticks", async () => { |
new above
offline