@devries-cameron20 / t3code-1 / commits / 00ec286fef
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(mobile): adaptive SVG project icons follow the app's color scheme (#24462)

Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Tom Moses committed Nov 14, 2026 at 08:10 UTC 00ec286fef2c84d187f6acc8a524a0502c93a549
4 files changed +287 -4
apps/mobile/src/components/ProjectFavicon.test.tsx
+3
@@ -13,6 +13,9 @@ vi.mock("./ui/AppSymbol", () => ({ SymbolView: () => null }));
13 vi.mock("./ui/AppText", () => ({
14 AppText: ({ children }: { children?: ReactNode }) => <span>{children}</span>,
15 }));
16 +vi.mock("../features/settings/appearance/AppearancePreferencesProvider", () => ({
17 + useAppearancePreferences: () => ({ themeAppearance: "light" }),
18 +}));
19 vi.mock("../state/assets", async () => {
20 const { Atom } = await import("effect/reactivity");
21 const url = Atom.make<string | null>("https://example.test/favicon.png");
apps/mobile/src/components/ProjectFavicon.tsx
+16 -4
@@ -1,7 +1,7 @@
1 import { SymbolView } from "./ui/AppSymbol";
2 import { AppText } from "./ui/AppText";
3 import { Image } from "expo-image";
4 -import { memo, useLayoutEffect, useState } from "react";
4 +import { memo, useLayoutEffect, useMemo, useState } from "react";
5 import { View } from "react-native";
6 import type { EnvironmentId, ProjectIconOverride } from "@t3tools/contracts";
7 import {
@@ -11,6 +11,7 @@ import {
11 } from "@t3tools/shared/projectFavicon";
12 import { useAtomValue } from "@effect/atom-react";
13 import { Atom } from "effect/reactivity";
14 +import { useAppearancePreferences } from "../features/settings/appearance/AppearancePreferencesProvider";
15 import { projectFaviconUrlAtom } from "../state/assets";
16 import {
17 countGlyphs,
@@ -20,6 +21,7 @@ import {
21 resolveProjectIconSymbol,
22 type ProjectIconGlyph,
23 } from "../lib/projectIcon";
24 +import { resolveProjectFaviconColorScheme } from "../lib/projectFaviconColorScheme";
25
26 import {
27 beginProjectFaviconRequest,
@@ -160,6 +162,16 @@ function ProjectFaviconImage(props: {
162 : "loading",
163 );
164
165 + const { themeAppearance } = useAppearancePreferences();
166 + const requestFaviconUrl = faviconRequest?.faviconUrl;
167 + const inlineFaviconUrl = useMemo(
168 + () =>
169 + requestFaviconUrl?.startsWith("data:")
170 + ? resolveProjectFaviconColorScheme(requestFaviconUrl, themeAppearance)
171 + : null,
172 + [requestFaviconUrl, themeAppearance],
173 + );
174 +
175 const showImage = faviconRequest !== null && status === "loaded";
176 const fallbackGlyph = showImage ? null : projectAutoMonogram(props.projectTitle);
177
@@ -187,11 +199,11 @@ function ProjectFaviconImage(props: {
199 <Image
200 key={faviconRequest.faviconUrl}
201 source={
190 - faviconRequest.faviconUrl.startsWith("data:")
191 - ? { uri: faviconRequest.faviconUrl }
202 + inlineFaviconUrl !== null
203 + ? { uri: inlineFaviconUrl }
204 : { uri: faviconRequest.faviconUrl, cacheKey: faviconRequest.cacheKey }
205 }
194 - cachePolicy={faviconRequest.faviconUrl.startsWith("data:") ? "memory" : "memory-disk"}
206 + cachePolicy={inlineFaviconUrl !== null ? "memory" : "memory-disk"}
207 recyclingKey={faviconRequest.cacheKey}
208 accessibilityLabel={`${props.projectTitle} favicon`}
209 style={{
apps/mobile/src/lib/projectFaviconColorScheme.test.ts new
+148
@@ -0,0 +1,148 @@
1 +import { describe, expect, it } from "vite-plus/test";
2 +
3 +import { resolveProjectFaviconColorScheme } from "./projectFaviconColorScheme";
4 +
5 +const svgDataUrl = (svg: string) =>
6 + `data:image/svg+xml;base64,${Buffer.from(svg, "utf8").toString("base64")}`;
7 +const decode = (dataUrl: string) =>
8 + Buffer.from(dataUrl.slice(dataUrl.indexOf(",") + 1), "base64").toString("utf8");
9 +
10 +const PATH = `<path d="M 40 220 L 220 40 L 400 220 L 220 400 Z"/>`;
11 +// Strokes with currentColor and switches the root color per scheme with @media.
12 +const ISSUE_ICON = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 440 440" fill="none" stroke="currentColor" stroke-width="9"><style>:root{color:#111827}@media(prefers-color-scheme:dark){:root{color:#F8FAFC}}</style>${PATH}</svg>`;
13 +
14 +describe("resolveProjectFaviconColorScheme", () => {
15 + it("gives the root the dark color the stylesheet picks in dark mode", () => {
16 + const resolved = decode(resolveProjectFaviconColorScheme(svgDataUrl(ISSUE_ICON), "dark"));
17 +
18 + expect(resolved).toBe(
19 + `<svg color="#F8FAFC" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 440 440" fill="none" stroke="currentColor" stroke-width="9"><style>:root{color:#111827}:root{color:#F8FAFC}</style>${PATH}</svg>`,
20 + );
21 + });
22 +
23 + it("gives the root the light color in light mode", () => {
24 + const resolved = decode(resolveProjectFaviconColorScheme(svgDataUrl(ISSUE_ICON), "light"));
25 +
26 + expect(resolved).toBe(
27 + `<svg color="#111827" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 440 440" fill="none" stroke="currentColor" stroke-width="9"><style>:root{color:#111827}</style>${PATH}</svg>`,
28 + );
29 + });
30 +
31 + it("keeps the rules of the matching color-scheme block and drops the other", () => {
32 + const icon = `<svg xmlns="http://www.w3.org/2000/svg"><style>.a{fill:#000} @media (prefers-color-scheme: dark) { .a{fill:#fff} .b{stroke:#eee} } @media (prefers-color-scheme: light) { .b{stroke:#111} }</style></svg>`;
33 +
34 + expect(decode(resolveProjectFaviconColorScheme(svgDataUrl(icon), "dark"))).toBe(
35 + `<svg xmlns="http://www.w3.org/2000/svg"><style>.a{fill:#000} .a{fill:#fff} .b{stroke:#eee} </style></svg>`,
36 + );
37 + expect(decode(resolveProjectFaviconColorScheme(svgDataUrl(icon), "light"))).toBe(
38 + `<svg xmlns="http://www.w3.org/2000/svg"><style>.a{fill:#000} .b{stroke:#111} </style></svg>`,
39 + );
40 + });
41 +
42 + it("replaces an existing root color attribute", () => {
43 + const icon = `<svg xmlns="http://www.w3.org/2000/svg" color='red' stroke="currentColor"><style>svg{color:blue}</style></svg>`;
44 +
45 + expect(decode(resolveProjectFaviconColorScheme(svgDataUrl(icon), "light"))).toBe(
46 + `<svg color="blue" xmlns="http://www.w3.org/2000/svg" stroke="currentColor"><style>svg{color:blue}</style></svg>`,
47 + );
48 + });
49 +
50 + it("takes the last root color declaration outside other at-rules", () => {
51 + const icon = `<svg xmlns="http://www.w3.org/2000/svg"><style>:root{color:red;color:blue}@media print{:root{color:green}}</style></svg>`;
52 +
53 + expect(decode(resolveProjectFaviconColorScheme(svgDataUrl(icon), "light"))).toBe(
54 + `<svg color="blue" xmlns="http://www.w3.org/2000/svg"><style>:root{color:red;color:blue}@media print{:root{color:green}}</style></svg>`,
55 + );
56 + const variable = icon.replace("color:blue", "color:var(--icon)");
57 + expect(resolveProjectFaviconColorScheme(svgDataUrl(variable), "dark")).toBe(
58 + svgDataUrl(variable),
59 + );
60 + const media = `<svg><style>:root{color:blue}</style><style media="(prefers-color-scheme: dark)">:root{color:white}.a{fill:white}</style><style media="print">:root{color:red}</style></svg>`;
61 + expect(decode(resolveProjectFaviconColorScheme(svgDataUrl(media), "light"))).toBe(
62 + `<svg color="blue"><style>:root{color:blue}</style><style media="print">:root{color:red}</style></svg>`,
63 + );
64 + expect(decode(resolveProjectFaviconColorScheme(svgDataUrl(media), "dark"))).toBe(
65 + `<svg color="white"><style>:root{color:blue}</style><style>:root{color:white}.a{fill:white}</style><style media="print">:root{color:red}</style></svg>`,
66 + );
67 + });
68 +
69 + it("lets an important root color beat later normal ones", () => {
70 + const icon = `<svg xmlns="http://www.w3.org/2000/svg"><style>:root{color:#111 !important}@media (prefers-color-scheme: dark){:root{color:#fff!important}}:root{color:red}</style></svg>`;
71 +
72 + expect(decode(resolveProjectFaviconColorScheme(svgDataUrl(icon), "dark"))).toBe(
73 + `<svg color="#fff" xmlns="http://www.w3.org/2000/svg"><style>:root{color:#111 !important}:root{color:#fff!important}:root{color:red}</style></svg>`,
74 + );
75 + expect(decode(resolveProjectFaviconColorScheme(svgDataUrl(icon), "light"))).toBe(
76 + `<svg color="#111" xmlns="http://www.w3.org/2000/svg"><style>:root{color:#111 !important}:root{color:red}</style></svg>`,
77 + );
78 + });
79 +
80 + it("keeps an important root color across style elements", () => {
81 + const icon = `<svg xmlns="http://www.w3.org/2000/svg"><style>:root{color:#fff !important}</style><style>:root{color:#000}</style></svg>`;
82 +
83 + expect(decode(resolveProjectFaviconColorScheme(svgDataUrl(icon), "dark"))).toBe(
84 + `<svg color="#fff" xmlns="http://www.w3.org/2000/svg"><style>:root{color:#fff !important}</style><style>:root{color:#000}</style></svg>`,
85 + );
86 + });
87 +
88 + it("ignores braces and color-scheme blocks inside CSS comments", () => {
89 + const icon = `<svg xmlns="http://www.w3.org/2000/svg"><style>/* @media (prefers-color-scheme: dark) { */.a{fill:#000}/* } */@media (prefers-color-scheme: dark){/* } */.a{fill:#fff}}</style></svg>`;
90 +
91 + expect(decode(resolveProjectFaviconColorScheme(svgDataUrl(icon), "light"))).toBe(
92 + `<svg xmlns="http://www.w3.org/2000/svg"><style>.a{fill:#000}</style></svg>`,
93 + );
94 + expect(decode(resolveProjectFaviconColorScheme(svgDataUrl(icon), "dark"))).toBe(
95 + `<svg xmlns="http://www.w3.org/2000/svg"><style>.a{fill:#000}.a{fill:#fff}</style></svg>`,
96 + );
97 + });
98 +
99 + it("colors the root element, not an svg tag in an earlier comment", () => {
100 + const icon = `<!-- <svg width="1"> --><SVG xmlns="http://www.w3.org/2000/svg"><style>:root{color:#fff}</style></SVG>`;
101 +
102 + expect(decode(resolveProjectFaviconColorScheme(svgDataUrl(icon), "dark"))).toBe(
103 + `<!-- <svg width="1"> --><SVG color="#fff" xmlns="http://www.w3.org/2000/svg"><style>:root{color:#fff}</style></SVG>`,
104 + );
105 + });
106 +
107 + it("leaves other media queries for the renderer", () => {
108 + const icon = `<svg xmlns="http://www.w3.org/2000/svg"><style>@media screen and (prefers-color-scheme: dark){.a{fill:#fff}}@media not (prefers-color-scheme: dark){.a{fill:#000}}</style></svg>`;
109 +
110 + expect(resolveProjectFaviconColorScheme(svgDataUrl(icon), "dark")).toBe(svgDataUrl(icon));
111 + });
112 +
113 + it("ignores a root color that is not a plain color value", () => {
114 + const icon = `<svg xmlns="http://www.w3.org/2000/svg"><style>:root{color:red$'}</style></svg>`;
115 +
116 + expect(resolveProjectFaviconColorScheme(svgDataUrl(icon), "dark")).toBe(svgDataUrl(icon));
117 + const padded = `<svg xmlns="http://www.w3.org/2000/svg"><style>:root{color:x${" \t".repeat(3000)}!bad}</style></svg>`;
118 + expect(resolveProjectFaviconColorScheme(svgDataUrl(padded), "dark")).toBe(svgDataUrl(padded));
119 + });
120 +
121 + it("ignores style elements inside XML comments", () => {
122 + const icon = `<svg xmlns="http://www.w3.org/2000/svg"><style>:root{color:blue}</style><!-- <style>:root{color:red!important}</style> --></svg>`;
123 +
124 + expect(decode(resolveProjectFaviconColorScheme(svgDataUrl(icon), "dark"))).toBe(
125 + icon.replace("<svg ", '<svg color="blue" '),
126 + );
127 + });
128 +
129 + it("keeps root selector specificity across style elements", () => {
130 + const icon = `<svg xmlns="http://www.w3.org/2000/svg"><style>:root{color:blue}</style><style>svg{color:red}</style></svg>`;
131 +
132 + expect(decode(resolveProjectFaviconColorScheme(svgDataUrl(icon), "dark"))).toBe(
133 + icon.replace("<svg ", '<svg color="blue" '),
134 + );
135 + const important = icon.replace("color:red", "color:red!important");
136 + expect(decode(resolveProjectFaviconColorScheme(svgDataUrl(important), "dark"))).toBe(
137 + important.replace("<svg ", '<svg color="red" '),
138 + );
139 + });
140 +
141 + it("returns non-SVG and remote URLs unchanged", () => {
142 + const png = "data:image/png;base64,iVBORw0KGgo=";
143 + const remote = "https://host/api/assets/token/icon.svg";
144 +
145 + expect(resolveProjectFaviconColorScheme(png, "dark")).toBe(png);
146 + expect(resolveProjectFaviconColorScheme(remote, "dark")).toBe(remote);
147 + });
148 +});
apps/mobile/src/lib/projectFaviconColorScheme.ts new
+120
@@ -0,0 +1,120 @@
1 +import * as Base64 from "effect/encoding/Base64";
2 +import * as Result from "effect/Result";
3 +
4 +import type { MobileThemeAppearance } from "./mobileTheme";
5 +
6 +const SVG_DATA_URL_PREFIX = "data:image/svg+xml;base64,";
7 +const STYLE_ELEMENT_RE = /<!--[\s\S]*?-->|(<style\b[^>]*>)([\s\S]*?)(<\/style>)/gi;
8 +const CSS_COMMENT_RE = /\/\*[\s\S]*?\*\//g;
9 +const COLOR_SCHEME_MEDIA_RE = /@media\s*\(\s*prefers-color-scheme\s*:\s*(light|dark)\s*\)\s*\{/gi;
10 +const AT_RULE_BLOCK_RE = /@[^{};]*\{/g;
11 +const RULE_RE = /([^{}]*)\{([^{}]*)\}/g;
12 +const IMPORTANT_RE = /!\s*important$/i;
13 +const PLAIN_COLOR_RE = /^(?:#[\da-f]{3,8}|[a-z]+|(?:rgba?|hsla?)\([\d\s.,%/]+\))$/i;
14 +const COMMENT_OR_SVG_TAG_RE = /<!--[\s\S]*?-->|<svg\b[^>]*>/gi;
15 +const COLOR_ATTRIBUTE_RE = /\s+color\s*=\s*(?:"[^"]*"|'[^']*')/i;
16 +const MEDIA_ATTRIBUTE_RE = /\s+media\s*=\s*(?:"([^"]*)"|'([^']*)')/i;
17 +const COLOR_SCHEME_CONDITION_RE = /^\(\s*prefers-color-scheme\s*:\s*(light|dark)\s*\)$/i;
18 +
19 +// CoreSVG ignores root stylesheet colors and misreads @media; resolve both before decoding.
20 +export function resolveProjectFaviconColorScheme(url: string, scheme: MobileThemeAppearance) {
21 + if (!url.startsWith(SVG_DATA_URL_PREFIX)) return url;
22 + const decoded = Base64.decodeString(url.slice(SVG_DATA_URL_PREFIX.length));
23 + if (Result.isFailure(decoded)) return url;
24 + const svg = decoded.success;
25 +
26 + let stylesheet = "";
27 + const styled = svg.replace(
28 + STYLE_ELEMENT_RE,
29 + (match, open: string, css: string | undefined, close: string) => {
30 + if (css === undefined) return match;
31 + const mediaAttribute = open.match(MEDIA_ATTRIBUTE_RE);
32 + const media = (mediaAttribute?.[1] ?? mediaAttribute?.[2])?.trim();
33 + if (media) {
34 + const condition = media.match(COLOR_SCHEME_CONDITION_RE);
35 + if (!condition) return match;
36 + if (condition[1]?.toLowerCase() !== scheme) return "";
37 + open = open.replace(MEDIA_ATTRIBUTE_RE, "");
38 + }
39 + // Comments can hold braces or a commented-out `@media`, which the block matcher would count.
40 + const resolvedCss = replaceBlocks(
41 + css.replace(CSS_COMMENT_RE, ""),
42 + COLOR_SCHEME_MEDIA_RE,
43 + (match, body) => (match[1]?.toLowerCase() === scheme ? body : ""),
44 + );
45 + stylesheet += `${resolvedCss}\n`;
46 + return `${open}${resolvedCss}${close}`;
47 + },
48 + );
49 + const rootColor = lastRootColor(stylesheet);
50 + const resolved = rootColor === undefined ? styled : withRootColor(styled, rootColor);
51 + return resolved === svg ? url : `${SVG_DATA_URL_PREFIX}${Base64.encode(resolved)}`;
52 +}
53 +
54 +function withRootColor(svg: string, color: string) {
55 + let applied = false;
56 + return svg.replace(COMMENT_OR_SVG_TAG_RE, (match) => {
57 + if (applied || match.startsWith("<!--")) return match;
58 + applied = true;
59 + return `${match.slice(0, 4)} color="${color}"${match.slice(4).replace(COLOR_ATTRIBUTE_RE, "")}`;
60 + });
61 +}
62 +
63 +function replaceBlocks(
64 + css: string,
65 + opening: RegExp,
66 + replace: (match: RegExpExecArray, body: string) => string,
67 +) {
68 + let result = "";
69 + let cursor = 0;
70 + for (const match of css.matchAll(opening)) {
71 + if (match.index < cursor) continue;
72 + const bodyStart = match.index + match[0].length;
73 + const bodyEnd = matchingBrace(css, bodyStart);
74 + if (bodyEnd === -1) break;
75 + result += css.slice(cursor, match.index) + replace(match, css.slice(bodyStart, bodyEnd));
76 + cursor = bodyEnd + 1;
77 + }
78 + return result + css.slice(cursor);
79 +}
80 +
81 +function matchingBrace(css: string, from: number) {
82 + let depth = 1;
83 + for (let index = from; index < css.length; index++) {
84 + if (css[index] === "{") depth++;
85 + else if (css[index] === "}" && --depth === 0) return index;
86 + }
87 + return -1;
88 +}
89 +
90 +function lastRootColor(css: string) {
91 + let color: string | undefined;
92 + let priority = -1;
93 + const topLevel = replaceBlocks(css, AT_RULE_BLOCK_RE, () => "");
94 + for (const [, selectors = "", declarations = ""] of topLevel.matchAll(RULE_RE)) {
95 + const rootSelectors = selectors
96 + .replace(/<!\[CDATA\[|\]\]>/g, "")
97 + .split(",")
98 + .map((selector) => selector.trim().toLowerCase());
99 + const specificity = rootSelectors.includes(":root")
100 + ? 1
101 + : rootSelectors.includes("svg")
102 + ? 0
103 + : -1;
104 + if (specificity === -1) continue;
105 + for (const declaration of declarations.split(";")) {
106 + const separator = declaration.indexOf(":");
107 + if (separator === -1 || declaration.slice(0, separator).trim().toLowerCase() !== "color") {
108 + continue;
109 + }
110 + let value = declaration.slice(separator + 1).trim();
111 + const important = IMPORTANT_RE.test(value);
112 + if (important) value = value.slice(0, value.lastIndexOf("!")).trim();
113 + const nextPriority = specificity + (important ? 2 : 0);
114 + if (nextPriority < priority) continue;
115 + color = PLAIN_COLOR_RE.test(value) ? value : undefined;
116 + priority = nextPriority;
117 + }
118 + }
119 + return color;
120 +}