diff --git a/apps/web/index.html b/apps/web/index.html index 89f4eb77dc..d3a15c2052 100644 --- a/apps/web/index.html +++ b/apps/web/index.html @@ -22,9 +22,19 @@ 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-seed", - colors.accentColor.trim().toLowerCase(), + "--theme-accent-foreground", + luminance >= 0.179 ? "#000000" : "#ffffff", ); } if (isHexColor(colors?.neutralColor)) { @@ -33,6 +43,19 @@ colors.neutralColor.trim().toLowerCase(), ); } + if (typeof colors?.contrast === "number" && Number.isFinite(colors.contrast)) { + const contrast = Math.min(100, Math.max(0, colors.contrast)); + const properties = { + "--theme-background-strength": `${4 + contrast * 0.12}%`, + "--theme-surface-strength": `${6 + contrast * 0.16}%`, + "--theme-control-strength": `${3 + contrast * 0.08}%`, + "--theme-accent-strength": `${6 + contrast * 0.2}%`, + "--theme-border-strength": `${8 + contrast * 0.16}%`, + }; + for (const [property, value] of Object.entries(properties)) { + document.documentElement.style.setProperty(property, value); + } + } } catch {} try { const storedTheme = window.localStorage.getItem("t3code:theme"); diff --git a/apps/web/src/components/settings/SettingsPanels.tsx b/apps/web/src/components/settings/SettingsPanels.tsx index 27fb350b00..6d94c02f1e 100644 --- a/apps/web/src/components/settings/SettingsPanels.tsx +++ b/apps/web/src/components/settings/SettingsPanels.tsx @@ -168,6 +168,78 @@ const THEME_COLOR_SWATCHES = [ const THEME_NEUTRAL_SWATCHES = ["#737373", "#78716c", "#64748b", "#71717a"] as const; +type ThemeOption = (typeof THEME_OPTIONS)[number]; + +function ThemeModePreview({ + option, + selected, + accentColor, + onSelect, +}: { + option: ThemeOption; + selected: boolean; + accentColor: string; + onSelect: () => void; +}) { + const previewBackground = + option.value === "system" + ? "bg-[linear-gradient(to_right,#fafafa_0_50%,#18181b_50%)]" + : option.value === "light" + ? "bg-zinc-50" + : "bg-zinc-900"; + const previewForeground = option.value === "light" ? "bg-zinc-400" : "bg-zinc-500"; + const cardBackground = + option.value === "system" + ? "bg-[linear-gradient(to_right,rgba(255,255,255,0.9)_0_32%,rgba(39,39,42,0.95)_32%)]" + : option.value === "light" + ? "bg-white/90" + : "bg-zinc-800/95"; + + return ( + + ); +} + function ThemeColorControl({ label, value, @@ -1028,43 +1100,33 @@ export function AppearanceSettingsPanel() { setTheme("system")} /> ) : null } - control={ - - } - /> + > +
+ {THEME_OPTIONS.map((option) => ( + setTheme(option.value)} + /> + ))} +
+
setThemeColors(DEFAULT_THEME_COLORS)} @@ -1082,7 +1144,7 @@ export function AppearanceSettingsPanel() { } /> @@ -1090,6 +1152,33 @@ export function AppearanceSettingsPanel() { } /> +
+ + + setThemeColors({ + ...colors, + contrast: Number(event.currentTarget.value), + } satisfies ThemeColors) + } + className="min-w-0 flex-1 cursor-pointer" + /> + + {colors.contrast} + +
{ vi.stubGlobal("window", createWindow()); const { normalizeThemeColors } = await import("./useThemeColors"); - expect(normalizeThemeColors({ accentColor: " #DB2777 ", neutralColor: "not-a-color" })).toEqual( - { accentColor: "#db2777", neutralColor: "#737373" }, - ); + expect( + normalizeThemeColors({ + accentColor: " #DB2777 ", + neutralColor: "not-a-color", + contrast: 120, + }), + ).toEqual({ accentColor: "#db2777", neutralColor: "#737373", contrast: 100 }); }); it("reads a valid palette from storage", async () => { + vi.stubGlobal( + "window", + createWindow({ + "t3code:theme-colors": JSON.stringify({ + accentColor: "#0891b2", + neutralColor: "#57534e", + contrast: 30, + }), + }), + ); + const { readThemeColors } = await import("./useThemeColors"); + + expect(readThemeColors()).toEqual({ + accentColor: "#0891b2", + neutralColor: "#57534e", + contrast: 30, + }); + }); + + it("migrates palettes saved before contrast was configurable", async () => { vi.stubGlobal( "window", createWindow({ @@ -46,18 +70,33 @@ describe("theme colors", () => { ); const { readThemeColors } = await import("./useThemeColors"); - expect(readThemeColors()).toEqual({ accentColor: "#0891b2", neutralColor: "#57534e" }); + expect(readThemeColors()).toEqual({ + accentColor: "#0891b2", + neutralColor: "#57534e", + contrast: 50, + }); }); - it("applies both seeds to the document root", async () => { + it("applies the palette and contrast properties to the document root", async () => { const setProperty = vi.fn(); vi.stubGlobal("window", createWindow()); vi.stubGlobal("document", { documentElement: { style: { setProperty } } }); const { applyThemeColors } = await import("./useThemeColors"); - applyThemeColors({ accentColor: "#16a34a", neutralColor: "#78716c" }); + 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"); }); }); diff --git a/apps/web/src/hooks/useThemeColors.ts b/apps/web/src/hooks/useThemeColors.ts index 25b59351cc..d35a29880d 100644 --- a/apps/web/src/hooks/useThemeColors.ts +++ b/apps/web/src/hooks/useThemeColors.ts @@ -3,11 +3,13 @@ import { useCallback, useSyncExternalStore } from "react"; export type ThemeColors = { accentColor: string; neutralColor: string; + contrast: number; }; export const DEFAULT_THEME_COLORS: ThemeColors = { accentColor: "#3b5bdb", neutralColor: "#737373", + contrast: 50, }; export const THEME_COLORS_STORAGE_KEY = "t3code:theme-colors"; @@ -27,6 +29,32 @@ export function normalizeThemeColors(value: unknown): ThemeColors { return { accentColor: normalizeThemeColor(colors.accentColor) ?? DEFAULT_THEME_COLORS.accentColor, neutralColor: normalizeThemeColor(colors.neutralColor) ?? DEFAULT_THEME_COLORS.neutralColor, + contrast: + typeof colors.contrast === "number" && Number.isFinite(colors.contrast) + ? Math.round(Math.min(100, Math.max(0, colors.contrast))) + : DEFAULT_THEME_COLORS.contrast, + }; +} + +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 { + return { + "--theme-background-strength": `${4 + contrast * 0.12}%`, + "--theme-surface-strength": `${6 + contrast * 0.16}%`, + "--theme-control-strength": `${3 + contrast * 0.08}%`, + "--theme-accent-strength": `${6 + contrast * 0.2}%`, + "--theme-border-strength": `${8 + contrast * 0.16}%`, }; } @@ -42,8 +70,13 @@ export function readThemeColors(): ThemeColors { export function applyThemeColors(colors: ThemeColors): void { if (typeof document === "undefined") return; - document.documentElement.style.setProperty("--theme-accent-seed", colors.accentColor); - document.documentElement.style.setProperty("--theme-neutral-seed", colors.neutralColor); + 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); + } } function emitChange(): void { diff --git a/apps/web/src/index.css b/apps/web/src/index.css index ce2632ce33..be18480315 100644 --- a/apps/web/src/index.css +++ b/apps/web/src/index.css @@ -854,27 +854,40 @@ 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; - --background: color-mix(in srgb, var(--theme-neutral-seed) 3%, var(--color-zinc-25)); + --theme-background-strength: 10%; + --theme-surface-strength: 14%; + --theme-control-strength: 7%; + --theme-accent-strength: 16%; + --theme-border-strength: 16%; + --background: color-mix( + in srgb, + var(--theme-neutral-seed) var(--theme-background-strength), + var(--color-zinc-25) + ); --app-chrome-background: var(--background); --surface-raised: color-mix(in srgb, var(--card) 20%, transparent); --foreground: var(--color-zinc-800); - --card: color-mix(in srgb, var(--theme-neutral-seed) 1.5%, var(--color-white)); + --card: color-mix(in srgb, var(--theme-neutral-seed) 2%, var(--color-white)); --card-foreground: var(--foreground); --popover: var(--card); --popover-foreground: var(--foreground); - /* Keep button text readable even when the seed itself is very light. */ - --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) 7%, transparent); + --primary: var(--theme-accent-seed); + --primary-foreground: var(--theme-accent-foreground); + --secondary: color-mix( + in srgb, + var(--theme-neutral-seed) var(--theme-control-strength), + transparent + ); --secondary-foreground: var(--foreground); - --muted: color-mix(in srgb, var(--theme-neutral-seed) 7%, transparent); + --muted: color-mix(in srgb, var(--theme-neutral-seed) var(--theme-control-strength), transparent); --muted-foreground: var(--color-zinc-500); - --accent: color-mix(in srgb, var(--theme-accent-seed) 8%, transparent); + --accent: color-mix(in srgb, var(--theme-accent-seed) var(--theme-accent-strength), transparent); --accent-foreground: var(--foreground); --destructive: var(--color-red-500); - --border: color-mix(in srgb, var(--theme-neutral-seed) 16%, transparent); - --input: color-mix(in srgb, var(--theme-neutral-seed) 19%, transparent); + --border: color-mix(in srgb, var(--theme-neutral-seed) var(--theme-border-strength), transparent); + --input: color-mix(in srgb, var(--theme-neutral-seed) var(--theme-border-strength), transparent); --ring: var(--primary); --destructive-foreground: var(--color-red-700); --info: var(--primary); @@ -899,26 +912,57 @@ html[data-mobile-composer-route-transition="true"]::view-transition-old(t3-mobil color-scheme: dark; /* Keep the workspace in the same neutral-black family as sidebar v2. Surfaces lift from this base instead of starting from a milky gray. */ - --background: color-mix(in srgb, var(--theme-neutral-seed) 10%, var(--color-neutral-950)); + --background: color-mix( + in srgb, + var(--theme-neutral-seed) var(--theme-background-strength), + var(--color-neutral-950) + ); --app-chrome-background: var(--background); --surface-raised: var(--secondary); --foreground: var(--color-neutral-100); - --card: color-mix(in srgb, var(--theme-neutral-seed) 14%, var(--background)); + --card: color-mix( + in srgb, + var(--theme-neutral-seed) var(--theme-surface-strength), + var(--background) + ); --card-foreground: var(--foreground); - --popover: color-mix(in srgb, var(--theme-neutral-seed) 18%, var(--background)); + --popover: color-mix( + in srgb, + var(--theme-neutral-seed) var(--theme-border-strength), + var(--background) + ); --popover-foreground: var(--foreground); - /* Dark mode lifts the seed instead and uses dark text for the same reason. */ - --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) 13%, transparent); + --primary: var(--theme-accent-seed); + --primary-foreground: var(--theme-accent-foreground); + --secondary: color-mix( + in srgb, + var(--theme-neutral-seed) var(--theme-surface-strength), + transparent + ); --secondary-foreground: var(--foreground); - --muted: color-mix(in srgb, var(--theme-neutral-seed) 13%, transparent); + --muted: color-mix( + in srgb, + var(--theme-neutral-seed) var(--theme-surface-strength), + transparent + ); --muted-foreground: color-mix(in srgb, var(--color-neutral-500) 90%, var(--color-white)); - --accent: color-mix(in srgb, var(--theme-accent-seed) 16%, transparent); + --accent: color-mix( + in srgb, + var(--theme-accent-seed) var(--theme-accent-strength), + transparent + ); --accent-foreground: var(--foreground); --destructive: color-mix(in srgb, var(--color-red-500) 90%, var(--color-white)); - --border: color-mix(in srgb, var(--theme-neutral-seed) 16%, transparent); - --input: color-mix(in srgb, var(--theme-neutral-seed) 20%, transparent); + --border: color-mix( + in srgb, + var(--theme-neutral-seed) var(--theme-border-strength), + transparent + ); + --input: color-mix( + in srgb, + var(--theme-neutral-seed) var(--theme-border-strength), + transparent + ); --ring: var(--primary); --destructive-foreground: var(--color-red-400); --info: var(--primary); @@ -944,16 +988,20 @@ html[data-mobile-composer-route-transition="true"]::view-transition-old(t3-mobil changing the color system when the beta is toggled. */ [data-sidebar-version="v1"], [data-sidebar-version="v2"] { - --background: color-mix(in srgb, var(--theme-neutral-seed) 3%, var(--color-zinc-25)); + --background: color-mix( + in srgb, + var(--theme-neutral-seed) var(--theme-background-strength), + var(--color-zinc-25) + ); --foreground: var(--color-zinc-800); - --card: color-mix(in srgb, var(--theme-neutral-seed) 1.5%, var(--color-white)); + --card: color-mix(in srgb, var(--theme-neutral-seed) 2%, var(--color-white)); --card-foreground: var(--foreground); - --accent: color-mix(in srgb, var(--theme-accent-seed) 8%, transparent); + --accent: color-mix(in srgb, var(--theme-accent-seed) var(--theme-accent-strength), transparent); --accent-foreground: var(--foreground); - --muted: color-mix(in srgb, var(--theme-neutral-seed) 7%, transparent); + --muted: color-mix(in srgb, var(--theme-neutral-seed) var(--theme-control-strength), transparent); --muted-foreground: var(--color-zinc-500); - --border: color-mix(in srgb, var(--theme-neutral-seed) 16%, transparent); - --input: color-mix(in srgb, var(--theme-neutral-seed) 19%, transparent); + --border: color-mix(in srgb, var(--theme-neutral-seed) var(--theme-border-strength), transparent); + --input: var(--border); --sidebar: var(--background); --sidebar-foreground: var(--foreground); --sidebar-muted-foreground: var(--muted-foreground); @@ -968,23 +1016,31 @@ html[data-mobile-composer-route-transition="true"]::view-transition-old(t3-mobil .dark [data-sidebar-version="v1"], .dark [data-sidebar-version="v2"] { - --background: color-mix(in srgb, var(--theme-neutral-seed) 10%, var(--color-neutral-950)); + --background: color-mix( + in srgb, + var(--theme-neutral-seed) var(--theme-background-strength), + var(--color-neutral-950) + ); --foreground: var(--color-neutral-100); - --card: color-mix(in srgb, var(--theme-neutral-seed) 14%, var(--background)); + --card: color-mix( + in srgb, + var(--theme-neutral-seed) var(--theme-surface-strength), + var(--background) + ); --card-foreground: var(--foreground); - --accent: color-mix(in srgb, var(--theme-accent-seed) 16%, transparent); + --accent: color-mix(in srgb, var(--theme-accent-seed) var(--theme-accent-strength), transparent); --accent-foreground: var(--foreground); - --muted: color-mix(in srgb, var(--theme-neutral-seed) 13%, transparent); + --muted: color-mix(in srgb, var(--theme-neutral-seed) var(--theme-surface-strength), transparent); --muted-foreground: color-mix(in srgb, var(--color-neutral-500) 90%, var(--color-white)); - --border: color-mix(in srgb, var(--theme-neutral-seed) 16%, transparent); - --input: color-mix(in srgb, var(--theme-neutral-seed) 20%, transparent); + --border: color-mix(in srgb, var(--theme-neutral-seed) var(--theme-border-strength), transparent); + --input: var(--border); --sidebar: var(--card); --sidebar-foreground: var(--foreground); --sidebar-muted-foreground: var(--muted-foreground); --sidebar-control-surface: var(--muted); - --sidebar-row-hover: color-mix(in srgb, var(--foreground) 8%, transparent); - --sidebar-row-active: color-mix(in srgb, var(--foreground) 11%, transparent); - --sidebar-row-selected: color-mix(in srgb, var(--foreground) 7%, transparent); + --sidebar-row-hover: var(--accent); + --sidebar-row-active: var(--accent); + --sidebar-row-selected: var(--muted); --sidebar-border: var(--border); /* The stage-channel header art must ramp to THIS panel's surface, not the global chrome background, or the fade shows a seam (same rule as the