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 #1311

Closed opened this in answer to
Closed without merging

mason/forge:mason/1240-switching-the-theme-no-longer-repaints-t → main 2 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
4 files changed +67 −19
src/design/themes.ts +14 −12
@@ -429,16 +429,18 @@ export function metaThemeColor(name: ThemeName): string {
429429 * while Safari often keeps the prior paint. Matching `html`/`body` to the
430430 * theme before the external stylesheet arrives kills that flash. Kept tiny
431431 * and nonce'd — CSP forbids unnonce'd inline style.
432 *
433 * The same for every page, whatever theme it was rendered with: the theme
434 * menu flips `data-theme` on the root without a reload, so the ground must
435 * follow the attribute, not the theme the server saw. With no attribute the
436 * system pair follows the OS; each `html[data-theme]` rule outranks it.
432437 */
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 );
444 }
438 export const bootCss = [
439 `html,body{background-color:${THEMES.light.tokens.bg};color-scheme:light}`,
440 `@media (prefers-color-scheme: dark){html,body{background-color:${THEMES.dark.tokens.bg};color-scheme:dark}}`,
441 ...THEME_NAMES.map((name) => {
442 const t = THEMES[name];
443 const root = `html[data-theme="${name}"]`;
444 return `${root},${root} body{background-color:${t.tokens.bg};color-scheme:${t.appearance}}`;
445 }),
446 ].join("");
src/web/layout.ts +1 −1
@@ -620,7 +620,7 @@ export function page(options: PageOptions, status = 200): Response {
620620 rel="icon"
621621 href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Cdefs%3E%3ClinearGradient id='g' x1='0' y1='0' x2='1' y2='1'%3E%3Cstop offset='0' stop-color='%232563eb'/%3E%3Cstop offset='1' stop-color='%237c3aed'/%3E%3C/linearGradient%3E%3C/defs%3E%3Crect x='3' y='3' width='26' height='26' rx='8' fill='url(%23g)'/%3E%3C/svg%3E"
622622 />
623 ${raw(`<style nonce="${nonce}">${bootCss(theme)}</style>`)}
623 ${raw(`<style nonce="${nonce}">${bootCss}</style>`)}
624624 <link rel="stylesheet" href="${stylesheetAsset().path}" />
625625 ${when(
626626 options.scriptAsset,
test/themes.test.ts added +29
@@ -0,0 +1,29 @@
1 import { describe, expect, test } from "bun:test";
2 import { bootCss, THEME_NAMES, THEMES } from "../src/design/themes.ts";
3
4 describe("bootCss: the ground before the stylesheet arrives (#1240)", () => {
5 test("with no data-theme, the system pair follows the OS", () => {
6 expect(bootCss).toStartWith(
7 "html,body{background-color:#fafafa;color-scheme:light}" +
8 "@media (prefers-color-scheme: dark){html,body{background-color:#09090b;color-scheme:dark}}",
9 );
10 });
11
12 test("every theme paints its own ground from the attribute, after the media rule", () => {
13 const media = bootCss.indexOf("@media");
14 for (const name of THEME_NAMES) {
15 const t = THEMES[name];
16 const rule = `html[data-theme="${name}"],html[data-theme="${name}"] body{background-color:${t.tokens.bg};color-scheme:${t.appearance}}`;
17 expect(bootCss).toContain(rule);
18 expect(bootCss.indexOf(rule)).toBeGreaterThan(media);
19 }
20 });
21
22 test("names no theme on its own, so a toggle never leaves a stale ground", () => {
23 // Every rule outside the system pair is keyed on data-theme.
24 const rest = bootCss.slice(bootCss.indexOf("}}") + 2);
25 for (const rule of rest.split("}").filter(Boolean)) {
26 expect(rule).toStartWith("html[data-theme=");
27 }
28 });
29 });
test/web.test.ts +23 −6
@@ -607,10 +607,21 @@ describe("themes", () => {
607607 for (const name of THEME_NAMES) {
608608 expect(text).toContain(`value="${name}"`);
609609 }
610 // Critical paint before the stylesheet: system pair (light default + dark media).
611 expect(text).toMatch(
612 /<style nonce="[^"]+">html,body\{background-color:#fafafa;color-scheme:light\}@media \(prefers-color-scheme: dark\)\{html,body\{background-color:#09090b;color-scheme:dark\}\}<\/style>/,
610 // Critical paint before the stylesheet: system pair (light default + dark
611 // media), then a rule per theme so a toggle repaints without a reload.
612 const boot = text.match(/<style nonce="[^"]+">([^<]*)<\/style>/)?.[1];
613 expect(boot).toStartWith(
614 "html,body{background-color:#fafafa;color-scheme:light}@media (prefers-color-scheme: dark){html,body{background-color:#09090b;color-scheme:dark}}",
613615 );
616 expect(boot).toContain(
617 'html[data-theme="dark"],html[data-theme="dark"] body{background-color:#09090b;color-scheme:dark}',
618 );
619 expect(boot).toContain(
620 'html[data-theme="light"],html[data-theme="light"] body{background-color:#fafafa;color-scheme:light}',
621 );
622 for (const name of THEME_NAMES) {
623 expect(boot).toContain(`html[data-theme="${name}"] body{`);
624 }
614625 });
615626
616627 test("choosing a theme sets the cookie and stamps the page", async () => {
@@ -630,10 +641,16 @@ describe("themes", () => {
630641 expect(text).toContain('data-theme="midnight"');
631642 // The switcher shows the active choice.
632643 expect(text).toMatch(/value="midnight"\s+checked/);
633 // Boot paint matches midnight, not the system pair.
634 expect(text).toMatch(
635 /<style nonce="[^"]+">html,body\{background-color:#16161e;color-scheme:dark\}<\/style>/,
644 // Boot paint follows the attribute: midnight's ground for this page, and
645 // the same rules as a system page, so switching away repaints at once.
646 const boot = text.match(/<style nonce="[^"]+">([^<]*)<\/style>/)?.[1];
647 expect(boot).toContain(
648 'html[data-theme="midnight"],html[data-theme="midnight"] body{background-color:#16161e;color-scheme:dark}',
636649 );
650 const system = (await (await fetch(base)).text()).match(
651 /<style nonce="[^"]+">([^<]*)<\/style>/,
652 )?.[1];
653 expect(boot).toBe(system!);
637654 });
638655
639656 test("system clears the cookie; junk never sticks", async () => {

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs