Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix(web): sync Safari chrome with app background

Gus3e2fcade353d
diff --git a/src/web/src/app/layout.tsx b/src/web/src/app/layout.tsx
index 3fb09f5a3..b94c828ba 100644
--- a/src/web/src/app/layout.tsx
+++ b/src/web/src/app/layout.tsx
@@ -5,6 +5,7 @@ import { ToasterProvider } from "@/components/toaster-provider";
import { TooltipProvider } from "@/components/ui/tooltip";
import { MockNetworkBanner } from "@/components/mock-network-banner";
import { TauriThemeSync } from "@/components/tauri-theme-sync";
+import { ThemeColorSync } from "@/components/theme-color-sync";
import { BRAND_DESCRIPTION, BRAND_SLOGAN, BRAND_TITLE } from "@/lib/brand-copy";
import { caveat, dmMono, dmSans, instrumentSerif, literata, vt323 } from "./fonts";
import "./globals.css";
@@ -125,6 +126,7 @@ export default function RootLayout({
}}
/>
<ThemeProvider attribute="class" defaultTheme="system" enableSystem>
+ <ThemeColorSync />
<TauriThemeSync />
<TooltipProvider>
{children}
diff --git a/src/web/src/components/theme-color-sync.test.ts b/src/web/src/components/theme-color-sync.test.ts
new file mode 100644
index 000000000..6b4ce2671
--- /dev/null
+++ b/src/web/src/components/theme-color-sync.test.ts
@@ -0,0 +1,86 @@
+import { describe, expect, it, vi } from "vitest";
+import { syncThemeColorToRailBackground } from "./theme-color-sync";
+
+function fakeStyle(backgroundColor: string) {
+ return { backgroundColor } as CSSStyleDeclaration;
+}
+
+describe("syncThemeColorToRailBackground", () => {
+ it("updates every theme-color meta to the computed rail backdrop", () => {
+ const metas = [{ content: "light" }, { content: "dark" }];
+ const doc = {
+ body: {},
+ querySelectorAll: vi.fn(() => metas),
+ } as unknown as Document;
+
+ syncThemeColorToRailBackground(
+ doc,
+ () => fakeStyle("rgb(245, 242, 236)"),
+ );
+
+ expect(metas.map((meta) => meta.content)).toEqual([
+ "rgb(245, 242, 236)",
+ "rgb(245, 242, 236)",
+ ]);
+ });
+
+ it("does not rewrite metadata that is already synchronized", () => {
+ const setContent = vi.fn();
+ const meta = {
+ get content() {
+ return "rgb(245, 242, 236)";
+ },
+ set content(value: string) {
+ setContent(value);
+ },
+ };
+ const doc = {
+ body: {},
+ querySelectorAll: vi.fn(() => [meta]),
+ } as unknown as Document;
+
+ syncThemeColorToRailBackground(
+ doc,
+ () => fakeStyle("rgb(245, 242, 236)"),
+ );
+
+ expect(setContent).not.toHaveBeenCalled();
+ });
+
+ it("creates a theme-color meta when the document has none", () => {
+ const createdMeta = { name: "", content: "" };
+ const appendChild = vi.fn();
+ const doc = {
+ body: {},
+ head: { appendChild },
+ querySelectorAll: vi.fn(() => []),
+ createElement: vi.fn(() => createdMeta),
+ } as unknown as Document;
+
+ syncThemeColorToRailBackground(
+ doc,
+ () => fakeStyle("rgb(40, 39, 37)"),
+ );
+
+ expect(createdMeta).toEqual({
+ name: "theme-color",
+ content: "rgb(40, 39, 37)",
+ });
+ expect(appendChild).toHaveBeenCalledWith(createdMeta);
+ });
+
+ it.each(["", "transparent", "rgba(0, 0, 0, 0)"])(
+ "keeps the fallback when the computed background is %j",
+ (backgroundColor) => {
+ const meta = { content: "fallback" };
+ const doc = {
+ body: {},
+ querySelectorAll: vi.fn(() => [meta]),
+ } as unknown as Document;
+
+ syncThemeColorToRailBackground(doc, () => fakeStyle(backgroundColor));
+
+ expect(meta.content).toBe("fallback");
+ },
+ );
+});
diff --git a/src/web/src/components/theme-color-sync.tsx b/src/web/src/components/theme-color-sync.tsx
new file mode 100644
index 000000000..3654f3baf
--- /dev/null
+++ b/src/web/src/components/theme-color-sync.tsx
@@ -0,0 +1,74 @@
+"use client";
+
+import { useEffect } from "react";
+import { useTheme } from "next-themes";
+import { usePathname } from "next/navigation";
+
+const TRANSPARENT_COLORS = new Set(["transparent", "rgba(0, 0, 0, 0)"]);
+
+export function syncThemeColorToRailBackground(
+ doc: Document = document,
+ readStyle: typeof getComputedStyle = getComputedStyle,
+) {
+ // The server rail is transparent, so the body's painted background is its
+ // visible background. Reading the computed value keeps this in sync with CSS.
+ const backgroundColor = readStyle(doc.body).backgroundColor.trim();
+ if (!backgroundColor || TRANSPARENT_COLORS.has(backgroundColor)) return;
+
+ let themeColorMetas = Array.from(
+ doc.querySelectorAll<HTMLMetaElement>('meta[name="theme-color"]'),
+ );
+
+ if (themeColorMetas.length === 0) {
+ const themeColorMeta = doc.createElement("meta");
+ themeColorMeta.name = "theme-color";
+ doc.head.appendChild(themeColorMeta);
+ themeColorMetas = [themeColorMeta];
+ }
+
+ for (const themeColorMeta of themeColorMetas) {
+ if (themeColorMeta.content !== backgroundColor) {
+ themeColorMeta.content = backgroundColor;
+ }
+ }
+}
+
+export function ThemeColorSync() {
+ const { resolvedTheme } = useTheme();
+ const pathname = usePathname();
+
+ useEffect(() => {
+ if (!resolvedTheme) return;
+
+ let frame: number | null = null;
+ const scheduleSync = () => {
+ if (frame !== null) window.cancelAnimationFrame(frame);
+ frame = window.requestAnimationFrame(() => {
+ frame = null;
+ syncThemeColorToRailBackground();
+ });
+ };
+
+ syncThemeColorToRailBackground();
+ scheduleSync();
+
+ // Next can replace viewport metadata during client navigation, including a
+ // query-only mobile pane change where usePathname() does not change.
+ const observer = new MutationObserver(() => {
+ scheduleSync();
+ });
+ observer.observe(document.head, {
+ attributes: true,
+ attributeFilter: ["content", "media"],
+ childList: true,
+ subtree: true,
+ });
+
+ return () => {
+ observer.disconnect();
+ if (frame !== null) window.cancelAnimationFrame(frame);
+ };
+ }, [pathname, resolvedTheme]);
+
+ return null;
+}