feat(web): showcase palette controls

This commit is contained in:
t3-code[bot] committed 2026-08-03 06:45:06 +00:00
1 parent b8f0110d4c
commit d3c4644759
5 files changed
+314 -74

No files matched your search

+25 -2
View File
@@ -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");
@@ -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 (
<button
type="button"
aria-label={`Use ${option.label.toLowerCase()} theme`}
aria-pressed={selected}
className="group min-w-0 cursor-pointer text-left"
onClick={onSelect}
>
<span
className={`relative block h-24 overflow-hidden rounded-xl border transition group-hover:border-primary/60 ${
selected ? "border-primary ring-2 ring-primary/20" : "border-border"
} ${previewBackground}`}
>
<span className="absolute inset-y-0 left-0 w-[31%] border-r border-black/10 bg-black/[0.04] dark:border-white/10" />
<span
className="absolute top-4 left-3 h-1.5 w-8 rounded-full"
style={{ backgroundColor: accentColor }}
/>
<span className={`absolute top-8 left-3 h-1 w-10 rounded-full ${previewForeground}`} />
<span className="absolute top-4 right-4 h-2 w-16 rounded-full bg-zinc-400/60" />
<span
className={`absolute right-3 bottom-3 h-14 w-[60%] rounded-lg border border-black/10 shadow-sm ${cardBackground}`}
>
<span className="absolute top-3 left-3 h-1.5 w-12 rounded-full bg-zinc-400/60" />
<span className="absolute top-7 left-3 h-1 w-[70%] rounded-full bg-zinc-400/30" />
<span
className="absolute bottom-2 left-3 h-1.5 w-8 rounded-full"
style={{ backgroundColor: accentColor }}
/>
</span>
{option.value === "system" ? (
<span className="absolute inset-y-0 left-1/2 border-l border-white/50" />
) : null}
</span>
<span
className={`mt-2 block text-center text-xs font-medium ${
selected ? "text-foreground" : "text-muted-foreground"
}`}
>
{option.label}
</span>
</button>
);
}
function ThemeColorControl({
label,
value,
@@ -1028,43 +1100,33 @@ export function AppearanceSettingsPanel() {
<SettingsSection id="appearance" title="Appearance">
<SettingsRow
{...searchableSetting("theme")}
description="Choose how T3 Code looks across the app."
description="Choose a mode with a quick preview of the workspace."
resetAction={
theme !== "system" ? (
<SettingResetButton label="theme" onClick={() => setTheme("system")} />
) : null
}
control={
<Select
value={theme}
onValueChange={(value) => {
if (value === "system" || value === "light" || value === "dark") {
setTheme(value);
}
}}
>
<SelectTrigger className="w-full sm:w-40" aria-label="Theme preference">
<SelectValue>
{THEME_OPTIONS.find((option) => option.value === theme)?.label ?? "System"}
</SelectValue>
</SelectTrigger>
<SelectPopup align="end" alignItemWithTrigger={false}>
{THEME_OPTIONS.map((option) => (
<SelectItem hideIndicator key={option.value} value={option.value}>
{option.label}
</SelectItem>
))}
</SelectPopup>
</Select>
}
/>
>
<div className="mt-4 grid grid-cols-3 gap-3 pb-3">
{THEME_OPTIONS.map((option) => (
<ThemeModePreview
key={option.value}
option={option}
selected={theme === option.value}
accentColor={colors.accentColor}
onSelect={() => setTheme(option.value)}
/>
))}
</div>
</SettingsRow>
<SettingsRow
{...searchableSetting("theme-colors")}
description="Pick an accent and neutral tint. T3 Code generates the light and dark palettes from these two colors."
description="Pick two color seeds, then tune how strongly they separate surfaces and controls."
resetAction={
colors.accentColor !== DEFAULT_THEME_COLORS.accentColor ||
colors.neutralColor !== DEFAULT_THEME_COLORS.neutralColor ? (
colors.neutralColor !== DEFAULT_THEME_COLORS.neutralColor ||
colors.contrast !== DEFAULT_THEME_COLORS.contrast ? (
<SettingResetButton
label="theme colors"
onClick={() => setThemeColors(DEFAULT_THEME_COLORS)}
@@ -1082,7 +1144,7 @@ export function AppearanceSettingsPanel() {
}
/>
<ThemeColorControl
label="Neutral color"
label="Surface tint"
value={colors.neutralColor}
swatches={THEME_NEUTRAL_SWATCHES}
onChange={(neutralColor) =>
@@ -1090,6 +1152,33 @@ export function AppearanceSettingsPanel() {
}
/>
</div>
<div className="flex items-center gap-4 border-t border-border/70 py-4">
<label htmlFor="theme-contrast" className="text-xs font-medium text-foreground">
Interface contrast
</label>
<input
id="theme-contrast"
type="range"
min={0}
max={100}
step={5}
value={colors.contrast}
style={{ accentColor: colors.accentColor }}
onChange={(event) =>
setThemeColors({
...colors,
contrast: Number(event.currentTarget.value),
} satisfies ThemeColors)
}
className="min-w-0 flex-1 cursor-pointer"
/>
<output
htmlFor="theme-contrast"
className="w-8 text-right font-mono text-xs tabular-nums text-muted-foreground"
>
{colors.contrast}
</output>
</div>
</SettingsRow>
<SettingsRow
+45 -6
View File
@@ -29,12 +29,36 @@ describe("theme colors", () => {
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");
});
});
+35 -2
View File
@@ -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<string, string> {
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 {
+92 -36
View File
@@ -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