Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
fix(web): theme Clerk surfaces (#6300)
Wout Stiensf131228a5908
diff --git a/apps/web/src/components/clerk/clerkAppearance.test.ts b/apps/web/src/components/clerk/clerkAppearance.test.tsnew file mode 100644index 000000000..2ffbd9d60--- /dev/null+++ b/apps/web/src/components/clerk/clerkAppearance.test.ts@@ -0,0 +1,91 @@+import { describe, expect, it } from "vite-plus/test";++import {+ EMBER_THEME,+ GROVE_THEME,+ IRIS_THEME,+ OCEAN_THEME,+ T3_CHAT_THEME,+ themeColorToHex,+ type ThemeColors,+} from "../../themePalette";+import { clerkAppearance } from "./clerkAppearance";++function contrastRatio(first: string, second: string): number {+ const toRgb = (value: string) => {+ const hex = themeColorToHex(value)?.slice(1, 7);+ if (!hex) throw new Error(`Expected a theme color, received ${value}`);+ return [0, 1, 2].map(+ (channel) => Number.parseInt(hex.slice(channel * 2, channel * 2 + 2), 16) / 255,+ );+ };+ const luminance = (value: string) =>+ toRgb(value)+ .map((channel) => (channel <= 0.03928 ? channel / 12.92 : ((channel + 0.055) / 1.055) ** 2.4))+ .reduce((sum, channel, index) => sum + channel * [0.2126, 0.7152, 0.0722][index]!, 0);+ const lighter = Math.max(luminance(first), luminance(second));+ const darker = Math.min(luminance(first), luminance(second));+ return (lighter + 0.05) / (darker + 0.05);+}++function mixThemeColors(first: string, second: string, firstWeight: number): string {+ const channels = [first, second].map((value) => {+ const hex = themeColorToHex(value)?.slice(1, 7);+ if (!hex) throw new Error(`Expected a theme color, received ${value}`);+ return [0, 1, 2].map((channel) => Number.parseInt(hex.slice(channel * 2, channel * 2 + 2), 16));+ });+ const mixed = channels[0]!.map((channel, index) =>+ Math.round(channel * firstWeight + channels[1]![index]! * (1 - firstWeight)),+ );+ return `#${mixed.map((channel) => channel.toString(16).padStart(2, "0")).join("")}`;+}++const builtInThemeModes = [+ T3_CHAT_THEME,+ GROVE_THEME,+ OCEAN_THEME,+ EMBER_THEME,+ IRIS_THEME,+].flatMap((theme) =>+ [theme.colors, theme.variants?.dark].filter((colors): colors is ThemeColors => !!colors),+);++describe("clerkAppearance", () => {+ it("maps theme colors without overriding Clerk's component structure", () => {+ expect(clerkAppearance).toEqual({+ variables: {+ colorPrimary: "var(--update-foreground)",+ colorPrimaryForeground: "var(--card)",+ colorDanger: "var(--error)",+ colorSuccess: "var(--success)",+ colorWarning: "var(--warning)",+ colorNeutral: "var(--foreground)",+ colorForeground: "var(--foreground)",+ colorMuted: "color-mix(in srgb, var(--card) 98%, var(--foreground))",+ colorMutedForeground: "var(--muted-foreground)",+ colorBackground: "var(--card)",+ colorInputForeground: "var(--foreground)",+ colorInput: "var(--secondary)",+ colorRing: "var(--ring)",+ },+ elements: {+ formFieldErrorText: { color: "var(--error-foreground)" },+ formFieldWarningText: { color: "var(--warning-foreground)" },+ formFieldSuccessText: { color: "var(--success-foreground)" },+ otpCodeFieldErrorText: { color: "var(--error-foreground)" },+ otpCodeFieldSuccessText: { color: "var(--success-foreground)" },+ },+ });+ });++ it.each(builtInThemeModes)("keeps Clerk text readable across a built-in palette", (colors) => {+ const mutedSurface = mixThemeColors(colors.surface, colors.text, 0.98);++ expect(contrastRatio(colors.text, colors.surface)).toBeGreaterThanOrEqual(4.5);+ expect(contrastRatio(colors.mutedForeground, mutedSurface)).toBeGreaterThanOrEqual(4.5);+ expect(contrastRatio(colors.text, colors.secondary)).toBeGreaterThanOrEqual(4.5);+ expect(contrastRatio(colors.updateForeground, colors.surface)).toBeGreaterThanOrEqual(4.5);+ expect(contrastRatio(colors.errorForeground, colors.surface)).toBeGreaterThanOrEqual(4.5);+ expect(contrastRatio(colors.warningForeground, colors.surface)).toBeGreaterThanOrEqual(4.5);+ });+});diff --git a/apps/web/src/components/clerk/clerkAppearance.ts b/apps/web/src/components/clerk/clerkAppearance.tsnew file mode 100644index 000000000..307e3f517--- /dev/null+++ b/apps/web/src/components/clerk/clerkAppearance.ts@@ -0,0 +1,34 @@+import type { ClerkProviderProps } from "@clerk/react";++/** Keeps Clerk's stock component structure while binding its color system to+ * the live T3 Code palette. CSS variables make theme changes propagate to+ * portaled sign-in and profile surfaces without remounting Clerk. */+export const clerkAppearance = {+ variables: {+ // Clerk reuses its primary color for filled buttons and bare links. The+ // app's update foreground is the palette's action hue cast for readable+ // text, while the card surface provides the inverse filled-control pair.+ colorPrimary: "var(--update-foreground)",+ colorPrimaryForeground: "var(--card)",+ colorDanger: "var(--error)",+ colorSuccess: "var(--success)",+ colorWarning: "var(--warning)",+ colorNeutral: "var(--foreground)",+ colorForeground: "var(--foreground)",+ // The stock dark theme's muted token is translucent. Clerk uses this as+ // the footer's background, so derive an opaque muted surface from the card.+ colorMuted: "color-mix(in srgb, var(--card) 98%, var(--foreground))",+ colorMutedForeground: "var(--muted-foreground)",+ colorBackground: "var(--card)",+ colorInputForeground: "var(--foreground)",+ colorInput: "var(--secondary)",+ colorRing: "var(--ring)",+ },+ elements: {+ formFieldErrorText: { color: "var(--error-foreground)" },+ formFieldWarningText: { color: "var(--warning-foreground)" },+ formFieldSuccessText: { color: "var(--success-foreground)" },+ otpCodeFieldErrorText: { color: "var(--error-foreground)" },+ otpCodeFieldSuccessText: { color: "var(--success-foreground)" },+ },+} satisfies NonNullable<ClerkProviderProps["appearance"]>;diff --git a/apps/web/src/main.tsx b/apps/web/src/main.tsxindex c8f6f4de9..3cdc8188b 100644--- a/apps/web/src/main.tsx+++ b/apps/web/src/main.tsx@@ -16,6 +16,7 @@ import {syncDocumentWindowControlsOverlayClass,} from "./lib/windowControlsOverlay";import { AppRoot } from "./AppRoot";+import { clerkAppearance } from "./components/clerk/clerkAppearance";// Electron loads the app from a file-backed shell, so hash history avoids path resolution issues.const history = isElectron ? createHashHistory() : createBrowserHistory();@@ -35,11 +36,15 @@ ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(<React.StrictMode>{clerkPublishableKey && hasCloudPublicConfig() ? (isElectron ? (- <ElectronClerkProvider publishableKey={clerkPublishableKey} passkeys={passkeys}>+ <ElectronClerkProvider+ appearance={clerkAppearance}+ publishableKey={clerkPublishableKey}+ passkeys={passkeys}+ ><ManagedRelayAuthProvider>{app}</ManagedRelayAuthProvider></ElectronClerkProvider>) : (- <ClerkProvider publishableKey={clerkPublishableKey}>+ <ClerkProvider appearance={clerkAppearance} publishableKey={clerkPublishableKey}><ManagedRelayAuthProvider>{app}</ManagedRelayAuthProvider></ClerkProvider>)