Skip to content

Commit d3c4644

Browse files
feat(web): showcase palette controls
1 parent b8f0110 commit d3c4644

5 files changed

Lines changed: 314 additions & 74 deletions

File tree

‎apps/web/index.html‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,19 @@
2222
const isHexColor = (value) =>
2323
typeof value === "string" && /^#[0-9a-f]{6}$/i.test(value.trim());
2424
if (isHexColor(colors?.accentColor)) {
25+
const accentColor = colors.accentColor.trim().toLowerCase();
26+
const channels = [1, 3, 5].map((offset) =>
27+
Number.parseInt(accentColor.slice(offset, offset + 2), 16),
28+
);
29+
const [red, green, blue] = channels.map((channel) => {
30+
const value = channel / 255;
31+
return value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4;
32+
});
33+
const luminance = 0.2126 * red + 0.7152 * green + 0.0722 * blue;
34+
document.documentElement.style.setProperty("--theme-accent-seed", accentColor);
2535
document.documentElement.style.setProperty(
26-
"--theme-accent-seed",
27-
colors.accentColor.trim().toLowerCase(),
36+
"--theme-accent-foreground",
37+
luminance >= 0.179 ? "#000000" : "#ffffff",
2838
);
2939
}
3040
if (isHexColor(colors?.neutralColor)) {
@@ -33,6 +43,19 @@
3343
colors.neutralColor.trim().toLowerCase(),
3444
);
3545
}
46+
if (typeof colors?.contrast === "number" && Number.isFinite(colors.contrast)) {
47+
const contrast = Math.min(100, Math.max(0, colors.contrast));
48+
const properties = {
49+
"--theme-background-strength": `${4 + contrast * 0.12}%`,
50+
"--theme-surface-strength": `${6 + contrast * 0.16}%`,
51+
"--theme-control-strength": `${3 + contrast * 0.08}%`,
52+
"--theme-accent-strength": `${6 + contrast * 0.2}%`,
53+
"--theme-border-strength": `${8 + contrast * 0.16}%`,
54+
};
55+
for (const [property, value] of Object.entries(properties)) {
56+
document.documentElement.style.setProperty(property, value);
57+
}
58+
}
3659
} catch {}
3760
try {
3861
const storedTheme = window.localStorage.getItem("t3code:theme");

‎apps/web/src/components/settings/SettingsPanels.tsx‎

Lines changed: 117 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,78 @@ const THEME_COLOR_SWATCHES = [
168168

169169
const THEME_NEUTRAL_SWATCHES = ["#737373", "#78716c", "#64748b", "#71717a"] as const;
170170

171+
type ThemeOption = (typeof THEME_OPTIONS)[number];
172+
173+
function ThemeModePreview({
174+
option,
175+
selected,
176+
accentColor,
177+
onSelect,
178+
}: {
179+
option: ThemeOption;
180+
selected: boolean;
181+
accentColor: string;
182+
onSelect: () => void;
183+
}) {
184+
const previewBackground =
185+
option.value === "system"
186+
? "bg-[linear-gradient(to_right,#fafafa_0_50%,#18181b_50%)]"
187+
: option.value === "light"
188+
? "bg-zinc-50"
189+
: "bg-zinc-900";
190+
const previewForeground = option.value === "light" ? "bg-zinc-400" : "bg-zinc-500";
191+
const cardBackground =
192+
option.value === "system"
193+
? "bg-[linear-gradient(to_right,rgba(255,255,255,0.9)_0_32%,rgba(39,39,42,0.95)_32%)]"
194+
: option.value === "light"
195+
? "bg-white/90"
196+
: "bg-zinc-800/95";
197+
198+
return (
199+
<button
200+
type="button"
201+
aria-label={`Use ${option.label.toLowerCase()} theme`}
202+
aria-pressed={selected}
203+
className="group min-w-0 cursor-pointer text-left"
204+
onClick={onSelect}
205+
>
206+
<span
207+
className={`relative block h-24 overflow-hidden rounded-xl border transition group-hover:border-primary/60 ${
208+
selected ? "border-primary ring-2 ring-primary/20" : "border-border"
209+
} ${previewBackground}`}
210+
>
211+
<span className="absolute inset-y-0 left-0 w-[31%] border-r border-black/10 bg-black/[0.04] dark:border-white/10" />
212+
<span
213+
className="absolute top-4 left-3 h-1.5 w-8 rounded-full"
214+
style={{ backgroundColor: accentColor }}
215+
/>
216+
<span className={`absolute top-8 left-3 h-1 w-10 rounded-full ${previewForeground}`} />
217+
<span className="absolute top-4 right-4 h-2 w-16 rounded-full bg-zinc-400/60" />
218+
<span
219+
className={`absolute right-3 bottom-3 h-14 w-[60%] rounded-lg border border-black/10 shadow-sm ${cardBackground}`}
220+
>
221+
<span className="absolute top-3 left-3 h-1.5 w-12 rounded-full bg-zinc-400/60" />
222+
<span className="absolute top-7 left-3 h-1 w-[70%] rounded-full bg-zinc-400/30" />
223+
<span
224+
className="absolute bottom-2 left-3 h-1.5 w-8 rounded-full"
225+
style={{ backgroundColor: accentColor }}
226+
/>
227+
</span>
228+
{option.value === "system" ? (
229+
<span className="absolute inset-y-0 left-1/2 border-l border-white/50" />
230+
) : null}
231+
</span>
232+
<span
233+
className={`mt-2 block text-center text-xs font-medium ${
234+
selected ? "text-foreground" : "text-muted-foreground"
235+
}`}
236+
>
237+
{option.label}
238+
</span>
239+
</button>
240+
);
241+
}
242+
171243
function ThemeColorControl({
172244
label,
173245
value,
@@ -1028,43 +1100,33 @@ export function AppearanceSettingsPanel() {
10281100
<SettingsSection id="appearance" title="Appearance">
10291101
<SettingsRow
10301102
{...searchableSetting("theme")}
1031-
description="Choose how T3 Code looks across the app."
1103+
description="Choose a mode with a quick preview of the workspace."
10321104
resetAction={
10331105
theme !== "system" ? (
10341106
<SettingResetButton label="theme" onClick={() => setTheme("system")} />
10351107
) : null
10361108
}
1037-
control={
1038-
<Select
1039-
value={theme}
1040-
onValueChange={(value) => {
1041-
if (value === "system" || value === "light" || value === "dark") {
1042-
setTheme(value);
1043-
}
1044-
}}
1045-
>
1046-
<SelectTrigger className="w-full sm:w-40" aria-label="Theme preference">
1047-
<SelectValue>
1048-
{THEME_OPTIONS.find((option) => option.value === theme)?.label ?? "System"}
1049-
</SelectValue>
1050-
</SelectTrigger>
1051-
<SelectPopup align="end" alignItemWithTrigger={false}>
1052-
{THEME_OPTIONS.map((option) => (
1053-
<SelectItem hideIndicator key={option.value} value={option.value}>
1054-
{option.label}
1055-
</SelectItem>
1056-
))}
1057-
</SelectPopup>
1058-
</Select>
1059-
}
1060-
/>
1109+
>
1110+
<div className="mt-4 grid grid-cols-3 gap-3 pb-3">
1111+
{THEME_OPTIONS.map((option) => (
1112+
<ThemeModePreview
1113+
key={option.value}
1114+
option={option}
1115+
selected={theme === option.value}
1116+
accentColor={colors.accentColor}
1117+
onSelect={() => setTheme(option.value)}
1118+
/>
1119+
))}
1120+
</div>
1121+
</SettingsRow>
10611122

10621123
<SettingsRow
10631124
{...searchableSetting("theme-colors")}
1064-
description="Pick an accent and neutral tint. T3 Code generates the light and dark palettes from these two colors."
1125+
description="Pick two color seeds, then tune how strongly they separate surfaces and controls."
10651126
resetAction={
10661127
colors.accentColor !== DEFAULT_THEME_COLORS.accentColor ||
1067-
colors.neutralColor !== DEFAULT_THEME_COLORS.neutralColor ? (
1128+
colors.neutralColor !== DEFAULT_THEME_COLORS.neutralColor ||
1129+
colors.contrast !== DEFAULT_THEME_COLORS.contrast ? (
10681130
<SettingResetButton
10691131
label="theme colors"
10701132
onClick={() => setThemeColors(DEFAULT_THEME_COLORS)}
@@ -1082,14 +1144,41 @@ export function AppearanceSettingsPanel() {
10821144
}
10831145
/>
10841146
<ThemeColorControl
1085-
label="Neutral color"
1147+
label="Surface tint"
10861148
value={colors.neutralColor}
10871149
swatches={THEME_NEUTRAL_SWATCHES}
10881150
onChange={(neutralColor) =>
10891151
setThemeColors({ ...colors, neutralColor } satisfies ThemeColors)
10901152
}
10911153
/>
10921154
</div>
1155+
<div className="flex items-center gap-4 border-t border-border/70 py-4">
1156+
<label htmlFor="theme-contrast" className="text-xs font-medium text-foreground">
1157+
Interface contrast
1158+
</label>
1159+
<input
1160+
id="theme-contrast"
1161+
type="range"
1162+
min={0}
1163+
max={100}
1164+
step={5}
1165+
value={colors.contrast}
1166+
style={{ accentColor: colors.accentColor }}
1167+
onChange={(event) =>
1168+
setThemeColors({
1169+
...colors,
1170+
contrast: Number(event.currentTarget.value),
1171+
} satisfies ThemeColors)
1172+
}
1173+
className="min-w-0 flex-1 cursor-pointer"
1174+
/>
1175+
<output
1176+
htmlFor="theme-contrast"
1177+
className="w-8 text-right font-mono text-xs tabular-nums text-muted-foreground"
1178+
>
1179+
{colors.contrast}
1180+
</output>
1181+
</div>
10931182
</SettingsRow>
10941183

10951184
<SettingsRow

‎apps/web/src/hooks/useThemeColors.test.ts‎

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,13 @@ describe("theme colors", () => {
2929
vi.stubGlobal("window", createWindow());
3030
const { normalizeThemeColors } = await import("./useThemeColors");
3131

32-
expect(normalizeThemeColors({ accentColor: " #DB2777 ", neutralColor: "not-a-color" })).toEqual(
33-
{ accentColor: "#db2777", neutralColor: "#737373" },
34-
);
32+
expect(
33+
normalizeThemeColors({
34+
accentColor: " #DB2777 ",
35+
neutralColor: "not-a-color",
36+
contrast: 120,
37+
}),
38+
).toEqual({ accentColor: "#db2777", neutralColor: "#737373", contrast: 100 });
3539
});
3640

3741
it("reads a valid palette from storage", async () => {
@@ -41,23 +45,58 @@ describe("theme colors", () => {
4145
"t3code:theme-colors": JSON.stringify({
4246
accentColor: "#0891b2",
4347
neutralColor: "#57534e",
48+
contrast: 30,
4449
}),
4550
}),
4651
);
4752
const { readThemeColors } = await import("./useThemeColors");
4853

49-
expect(readThemeColors()).toEqual({ accentColor: "#0891b2", neutralColor: "#57534e" });
54+
expect(readThemeColors()).toEqual({
55+
accentColor: "#0891b2",
56+
neutralColor: "#57534e",
57+
contrast: 30,
58+
});
5059
});
5160

52-
it("applies both seeds to the document root", async () => {
61+
it("migrates palettes saved before contrast was configurable", async () => {
62+
vi.stubGlobal(
63+
"window",
64+
createWindow({
65+
"t3code:theme-colors": JSON.stringify({
66+
accentColor: "#0891b2",
67+
neutralColor: "#57534e",
68+
}),
69+
}),
70+
);
71+
const { readThemeColors } = await import("./useThemeColors");
72+
73+
expect(readThemeColors()).toEqual({
74+
accentColor: "#0891b2",
75+
neutralColor: "#57534e",
76+
contrast: 50,
77+
});
78+
});
79+
80+
it("applies the palette and contrast properties to the document root", async () => {
5381
const setProperty = vi.fn();
5482
vi.stubGlobal("window", createWindow());
5583
vi.stubGlobal("document", { documentElement: { style: { setProperty } } });
5684
const { applyThemeColors } = await import("./useThemeColors");
5785

58-
applyThemeColors({ accentColor: "#16a34a", neutralColor: "#78716c" });
86+
applyThemeColors({ accentColor: "#16a34a", neutralColor: "#78716c", contrast: 50 });
5987

6088
expect(setProperty).toHaveBeenCalledWith("--theme-accent-seed", "#16a34a");
89+
expect(setProperty).toHaveBeenCalledWith("--theme-accent-foreground", "#000000");
6190
expect(setProperty).toHaveBeenCalledWith("--theme-neutral-seed", "#78716c");
91+
expect(setProperty).toHaveBeenCalledWith("--theme-accent-strength", "16%");
92+
expect(setProperty).toHaveBeenCalledWith("--theme-border-strength", "16%");
93+
});
94+
95+
it("chooses a readable foreground for light and dark accents", async () => {
96+
vi.stubGlobal("window", createWindow());
97+
const { getThemeAccentForeground } = await import("./useThemeColors");
98+
99+
expect(getThemeAccentForeground("#facc15")).toBe("#000000");
100+
expect(getThemeAccentForeground("#1d4ed8")).toBe("#ffffff");
62101
});
63102
});

‎apps/web/src/hooks/useThemeColors.ts‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import { useCallback, useSyncExternalStore } from "react";
33
export type ThemeColors = {
44
accentColor: string;
55
neutralColor: string;
6+
contrast: number;
67
};
78

89
export const DEFAULT_THEME_COLORS: ThemeColors = {
910
accentColor: "#3b5bdb",
1011
neutralColor: "#737373",
12+
contrast: 50,
1113
};
1214

1315
export const THEME_COLORS_STORAGE_KEY = "t3code:theme-colors";
@@ -27,6 +29,32 @@ export function normalizeThemeColors(value: unknown): ThemeColors {
2729
return {
2830
accentColor: normalizeThemeColor(colors.accentColor) ?? DEFAULT_THEME_COLORS.accentColor,
2931
neutralColor: normalizeThemeColor(colors.neutralColor) ?? DEFAULT_THEME_COLORS.neutralColor,
32+
contrast:
33+
typeof colors.contrast === "number" && Number.isFinite(colors.contrast)
34+
? Math.round(Math.min(100, Math.max(0, colors.contrast)))
35+
: DEFAULT_THEME_COLORS.contrast,
36+
};
37+
}
38+
39+
export function getThemeAccentForeground(accentColor: string): "#000000" | "#ffffff" {
40+
const channels = [1, 3, 5].map((offset) =>
41+
Number.parseInt(accentColor.slice(offset, offset + 2), 16),
42+
);
43+
const [red = 0, green = 0, blue = 0] = channels.map((channel) => {
44+
const value = channel / 255;
45+
return value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4;
46+
});
47+
const luminance = 0.2126 * red + 0.7152 * green + 0.0722 * blue;
48+
return luminance >= 0.179 ? "#000000" : "#ffffff";
49+
}
50+
51+
function getThemeContrastProperties(contrast: number): Record<string, string> {
52+
return {
53+
"--theme-background-strength": `${4 + contrast * 0.12}%`,
54+
"--theme-surface-strength": `${6 + contrast * 0.16}%`,
55+
"--theme-control-strength": `${3 + contrast * 0.08}%`,
56+
"--theme-accent-strength": `${6 + contrast * 0.2}%`,
57+
"--theme-border-strength": `${8 + contrast * 0.16}%`,
3058
};
3159
}
3260

@@ -42,8 +70,13 @@ export function readThemeColors(): ThemeColors {
4270

4371
export function applyThemeColors(colors: ThemeColors): void {
4472
if (typeof document === "undefined") return;
45-
document.documentElement.style.setProperty("--theme-accent-seed", colors.accentColor);
46-
document.documentElement.style.setProperty("--theme-neutral-seed", colors.neutralColor);
73+
const rootStyle = document.documentElement.style;
74+
rootStyle.setProperty("--theme-accent-seed", colors.accentColor);
75+
rootStyle.setProperty("--theme-accent-foreground", getThemeAccentForeground(colors.accentColor));
76+
rootStyle.setProperty("--theme-neutral-seed", colors.neutralColor);
77+
for (const [property, value] of Object.entries(getThemeContrastProperties(colors.contrast))) {
78+
rootStyle.setProperty(property, value);
79+
}
4780
}
4881

4982
function emitChange(): void {

0 commit comments

Comments
 (0)