TypeScript
fix(html): honor preview light and dark mode (#24521)
Bilal Bakr8176b589310f
diff --git a/apps/server/src/htmlRender/HtmlRender.test.ts b/apps/server/src/htmlRender/HtmlRender.test.tsindex 1bbdf0e483e..80d367b2f8a 100644--- a/apps/server/src/htmlRender/HtmlRender.test.ts+++ b/apps/server/src/htmlRender/HtmlRender.test.ts@@ -316,6 +316,31 @@ describe("HtmlRender", () => {},);+ it.live("matches preview media queries and text colors to the requested appearance", (ctx) =>+ Effect.gen(function* () {+ const executable = (yield* HostProcess.Environment)[TEST_BROWSER_ENV];+ if (!executable) return ctx.skip(`Set ${TEST_BROWSER_ENV} to run this test.`);+ yield* Effect.gen(function* () {+ const htmlRender = yield* HtmlRender.HtmlRender;+ for (const appearance of [undefined, "dark", "light"] as const) {+ const preview = yield* htmlRender.preview({+ appearance,+ html: [+ "<style>h1{color:#18181b}@media(prefers-color-scheme:dark){h1{color:#fafafa}}</style>",+ "<h1>The moon garden opens at dusk</h1>",+ '<script>console.log(matchMedia("(prefers-color-scheme:dark)").matches,',+ 'getComputedStyle(document.querySelector("h1")).color);</script>',+ ].join(""),+ });+ expect(preview.consoleMessages).toContainEqual({+ level: "log",+ text: appearance === "light" ? "false rgb(24, 24, 27)" : "true rgb(250, 250, 250)",+ });+ }+ }).pipe(Effect.provide(layerHtmlRender(executable)));+ }),+ );+it.live("keeps every local file but the page itself out of the browser", (ctx) =>Effect.gen(function* () {const executable = (yield* HostProcess.Environment)[TEST_BROWSER_ENV];diff --git a/apps/server/src/htmlRender/HtmlRender.ts b/apps/server/src/htmlRender/HtmlRender.tsindex 810143c75a7..a6ed087ed20 100644--- a/apps/server/src/htmlRender/HtmlRender.ts+++ b/apps/server/src/htmlRender/HtmlRender.ts@@ -492,6 +492,7 @@ const make = Effect.gen(function* () {html: inlined.html,width,urlFragment: htmlRenderThemeFragment(theme),+ appearance,}).pipe(Effect.provideContext(services)),);return {diff --git a/apps/server/src/htmlRender/headlessChrome.ts b/apps/server/src/htmlRender/headlessChrome.tsindex d9339b9c469..614917a23f8 100644--- a/apps/server/src/htmlRender/headlessChrome.ts+++ b/apps/server/src/htmlRender/headlessChrome.ts@@ -1,3 +1,4 @@+import type { ThemeAppearance } from "@t3tools/shared/themePalettes";import * as Deferred from "effect/Deferred";import * as Effect from "effect/Effect";import * as Fiber from "effect/Fiber";@@ -413,7 +414,7 @@ const launchBrowser = Effect.fnUntraced(function* (input: {);});- const openPage = Effect.fnUntraced(function* (width: number) {+ const openPage = Effect.fnUntraced(function* (width: number, appearance: ThemeAppearance) {const { targetId } = yield* send("Target.createTarget",{ url: "about:blank" },@@ -436,6 +437,12 @@ const launchBrowser = Effect.fnUntraced(function* (input: {yield* send("Page.enable", {}, Ignored, sessionId);yield* send("Runtime.enable", {}, Ignored, sessionId);yield* send("Log.enable", {}, Ignored, sessionId);+ yield* send(+ "Emulation.setEmulatedMedia",+ { features: [{ name: "prefers-color-scheme", value: appearance }] },+ Ignored,+ sessionId,+ );// Previews have no use for WebRTC, whose ICE servers can make the browser// resolve names outside the proxy. Runs before page scripts in every frame.yield* send(@@ -550,6 +557,7 @@ export const captureHtmlScreenshot = Effect.fn("headlessChrome.captureHtmlScreenreadonly html: string;readonly width: number;readonly urlFragment: string;+ readonly appearance: ThemeAppearance;}) {const path = yield* Path.Path;const directory = yield* scratchDirectory;@@ -558,7 +566,7 @@ export const captureHtmlScreenshot = Effect.fn("headlessChrome.captureHtmlScreennoSandbox: input.noSandbox,profileDirectory: path.join(directory, "profile"),});- const page = yield* browser.openPage(input.width);+ const page = yield* browser.openPage(input.width, input.appearance);const contentHeight = yield* page.load(pageBody(input.html), input.urlFragment);const capturedHeight = Math.max(1, Math.min(contentHeight, MAX_CAPTURE_HEIGHT));const png = yield* page.screenshot(capturedHeight);@@ -598,7 +606,7 @@ export const measureHtmlHeights = Effect.fn("headlessChrome.measureHtmlHeights")return yield* Effect.forEach(input.widths,(width) =>- browser.openPage(width).pipe(+ browser.openPage(width, "dark").pipe(Effect.flatMap((page) =>page.load(body, input.urlFragment).pipe(Effect.ensuring(page.close)),),