fix(web): keep custom palettes readable

This commit is contained in:
t3-code[bot] committed 2026-08-03 06:53:48 +00:00
1 parent d3c4644759
commit f0e4ec40ed
4 files changed
+10 -39

No files matched your search

+2 -12
View File
@@ -22,19 +22,9 @@
const isHexColor = (value) =>
typeof value === "string" && /^#[0-9a-f]{6}$/i.test(value.trim());
if (isHexColor(colors?.accentColor)) {
const accentColor = colors.accentColor.trim().toLowerCase();
const channels = [1, 3, 5].map((offset) =>
Number.parseInt(accentColor.slice(offset, offset + 2), 16),
);
const [red, green, blue] = channels.map((channel) => {
const value = channel / 255;
return value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4;
});
const luminance = 0.2126 * red + 0.7152 * green + 0.0722 * blue;
document.documentElement.style.setProperty("--theme-accent-seed", accentColor);
document.documentElement.style.setProperty(
"--theme-accent-foreground",
luminance >= 0.179 ? "#000000" : "#ffffff",
"--theme-accent-seed",
colors.accentColor.trim().toLowerCase(),
);
}
if (isHexColor(colors?.neutralColor)) {
@@ -86,17 +86,8 @@ describe("theme colors", () => {
applyThemeColors({ accentColor: "#16a34a", neutralColor: "#78716c", contrast: 50 });
expect(setProperty).toHaveBeenCalledWith("--theme-accent-seed", "#16a34a");
expect(setProperty).toHaveBeenCalledWith("--theme-accent-foreground", "#000000");
expect(setProperty).toHaveBeenCalledWith("--theme-neutral-seed", "#78716c");
expect(setProperty).toHaveBeenCalledWith("--theme-accent-strength", "16%");
expect(setProperty).toHaveBeenCalledWith("--theme-border-strength", "16%");
});
it("chooses a readable foreground for light and dark accents", async () => {
vi.stubGlobal("window", createWindow());
const { getThemeAccentForeground } = await import("./useThemeColors");
expect(getThemeAccentForeground("#facc15")).toBe("#000000");
expect(getThemeAccentForeground("#1d4ed8")).toBe("#ffffff");
});
});
+2 -13
View File
@@ -1,4 +1,5 @@
import { useCallback, useSyncExternalStore } from "react";
import { syncBrowserChromeTheme } from "./useTheme";
export type ThemeColors = {
accentColor: string;
@@ -36,18 +37,6 @@ export function normalizeThemeColors(value: unknown): ThemeColors {
};
}
export function getThemeAccentForeground(accentColor: string): "#000000" | "#ffffff" {
const channels = [1, 3, 5].map((offset) =>
Number.parseInt(accentColor.slice(offset, offset + 2), 16),
);
const [red = 0, green = 0, blue = 0] = channels.map((channel) => {
const value = channel / 255;
return value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4;
});
const luminance = 0.2126 * red + 0.7152 * green + 0.0722 * blue;
return luminance >= 0.179 ? "#000000" : "#ffffff";
}
function getThemeContrastProperties(contrast: number): Record<string, string> {
return {
"--theme-background-strength": `${4 + contrast * 0.12}%`,
@@ -72,11 +61,11 @@ export function applyThemeColors(colors: ThemeColors): void {
if (typeof document === "undefined") return;
const rootStyle = document.documentElement.style;
rootStyle.setProperty("--theme-accent-seed", colors.accentColor);
rootStyle.setProperty("--theme-accent-foreground", getThemeAccentForeground(colors.accentColor));
rootStyle.setProperty("--theme-neutral-seed", colors.neutralColor);
for (const [property, value] of Object.entries(getThemeContrastProperties(colors.contrast))) {
rootStyle.setProperty(property, value);
}
syncBrowserChromeTheme();
}
function emitChange(): void {
+6 -5
View File
@@ -854,7 +854,6 @@ html[data-mobile-composer-route-transition="true"]::view-transition-old(t3-mobil
color-scheme: light;
--radius: 0.625rem;
--theme-accent-seed: #3b5bdb;
--theme-accent-foreground: #ffffff;
--theme-neutral-seed: #737373;
--theme-background-strength: 10%;
--theme-surface-strength: 14%;
@@ -873,8 +872,10 @@ html[data-mobile-composer-route-transition="true"]::view-transition-old(t3-mobil
--card-foreground: var(--foreground);
--popover: var(--card);
--popover-foreground: var(--foreground);
--primary: var(--theme-accent-seed);
--primary-foreground: var(--theme-accent-foreground);
/* Keep text, rings, and translucent primary controls readable for any seed.
Raw seed color still drives the more expressive accent surfaces below. */
--primary: color-mix(in oklch, var(--theme-accent-seed) 46%, var(--color-black));
--primary-foreground: var(--color-white);
--secondary: color-mix(
in srgb,
var(--theme-neutral-seed) var(--theme-control-strength),
@@ -932,8 +933,8 @@ html[data-mobile-composer-route-transition="true"]::view-transition-old(t3-mobil
var(--background)
);
--popover-foreground: var(--foreground);
--primary: var(--theme-accent-seed);
--primary-foreground: var(--theme-accent-foreground);
--primary: color-mix(in oklch, var(--theme-accent-seed) 40%, var(--color-white));
--primary-foreground: var(--color-black);
--secondary: color-mix(
in srgb,
var(--theme-neutral-seed) var(--theme-surface-strength),