Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ThemePreviewCircles.tsx

224 lines7.3 KB

1import { MoonIcon, SunIcon } from "lucide-react";
2import type { CSSProperties } from "react";
3import { cn } from "../../lib/utils";
4import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
5import {
6 getThemeColorsForMode,
7 getThemeModes,
8 type ThemeAppearance,
9 type ThemeDefinition,
10} from "../../themePalette";
11​
12const THEME_PREVIEW_ROLES = [
13 "sidebar",
14 "canvas",
15 "surface",
16 "accentSurface",
17 "accent",
18 "messageSurface",
19 "messageAction",
20] as const;
21type ThemePreviewRole = (typeof THEME_PREVIEW_ROLES)[number];
22type ThemeCardPreview = {
23 mode: ThemeAppearance;
24 colors: Readonly<Record<ThemePreviewRole, string>>;
25};
26export type ThemeCardDefinition = {
27 id: string;
28 label: string;
29 previews: ReadonlyArray<ThemeCardPreview>;
30};
31export type ThemeMode = ThemeAppearance | "system";
32export type ThemeCardPreviewColors = ThemeCardPreview["colors"];
33​
34const STANDARD_THEME_PREVIEW_COLORS: Record<
35 ThemeAppearance,
36 Readonly<Record<ThemePreviewRole, string>>
37> = {
38 light: {
39 sidebar: "#fafafa",
40 canvas: "#fcfcfc",
41 surface: "#ffffff",
42 accentSurface: "#f4f4f5",
43 accent: "#f4f4f5",
44 messageSurface: "#e4e4e7",
45 messageAction: "#4f46e5",
46 },
47 dark: {
48 sidebar: "#0f0f10",
49 canvas: "#0a0a0a",
50 surface: "#121212",
51 accentSurface: "#27272a",
52 accent: "#1c1c1f",
53 messageSurface: "#27272a",
54 messageAction: "#8b9cff",
55 },
56};
57​
58export const STANDARD_THEME_CARDS: ReadonlyArray<ThemeCardDefinition> = [
59 {
60 id: "default",
61 label: "T3 Code",
62 previews: (["light", "dark"] as const).map((mode) => ({
63 mode,
64 colors: STANDARD_THEME_PREVIEW_COLORS[mode],
65 })),
66 },
67];
68​
69export function previewColorsOf(
70 card: ThemeCardDefinition,
71 mode: ThemeAppearance,
72): ThemeCardPreviewColors | null {
73 return card.previews.find((preview) => preview.mode === mode)?.colors ?? null;
75​
76export function getThemeCardDefinition(theme: ThemeDefinition): ThemeCardDefinition {
77 return {
78 id: theme.id,
79 label: theme.label,
80 previews: getThemeModes(theme).map((mode) => {
81 const colors = getThemeColorsForMode(theme, mode) ?? theme.colors;
82 return {
83 mode,
84 colors: {
85 sidebar: colors.sidebar,
86 canvas: colors.canvas,
87 surface: colors.surface,
88 accentSurface: colors.accentSurface,
89 accent: colors.accent,
90 messageSurface: colors.messageSurface,
91 messageAction: colors.messageAction,
92 },
93 };
94 }),
95 };
97​
98// Interpolating in oklab keeps the glow falloff perceptually even (no gray
99// mid-tones or banding rings), and premultiplied alpha keeps the fade to
100// transparent clean.
101function getThemePreviewStyle(
102 colors: ThemeCardPreviewColors,
103 mode: ThemeAppearance,
104): CSSProperties {
105 const isDark = mode === "dark";
106 // The canvas carries the ball's light/dark identity, so it stays dominant:
107 // a near-true base with a contained accent glow, instead of an accent wash
108 // that makes both modes read alike.
109 const modeBase = isDark
110 ? `color-mix(in oklab, ${colors.canvas} 80%, #09090b)`
111 : `color-mix(in oklab, ${colors.canvas} 80%, #ffffff)`;
112 const accentPosition = isDark ? "28% 78%" : "72% 22%";
113 const actionPosition = isDark ? "82% 18%" : "18% 82%";
114 const accentFade = isDark ? 62 : 72;
115 return {
116 backgroundColor: modeBase,
117 backgroundImage: [
118 `radial-gradient(circle at ${accentPosition} in oklab, ${colors.accent} 0%, color-mix(in oklab, ${colors.accent} ${accentFade}%, transparent) 28%, transparent 58%)`,
119 // The action color is a soft tint from the opposite corner, not a second
120 // light source — two bright hotspots read as headlights.
121 `radial-gradient(circle at ${actionPosition} in oklab, color-mix(in oklab, ${colors.messageAction} 45%, transparent) 0%, transparent 55%)`,
122 ].join(", "),
123 };
125​
126// The gradient halves of each ball can match the card surface, so every ball
127// carries a faint mode-appropriate inner ring to keep its silhouette legible.
128function themePreviewEdgeShadow(mode: ThemeAppearance): string {
129 return mode === "dark"
130 ? "inset 0 0 0 1px rgb(255 255 255 / 0.14), 0 1px 2px rgb(0 0 0 / 0.18)"
131 : "inset 0 0 0 1px rgb(0 0 0 / 0.10), 0 1px 2px rgb(0 0 0 / 0.08)";
133​
134export function ThemePreviewCircle({
135 colors,
136 mode,
137}: {
138 colors: ThemeCardPreviewColors;
139 mode: ThemeAppearance;
140}) {
141 return (
142 <span
143 aria-hidden
144 className="relative block size-14 shrink-0 overflow-hidden rounded-full border-2 border-background"
145 style={{ boxShadow: themePreviewEdgeShadow(mode) }}
146 >
147 <span
148 className="absolute inset-0 scale-110 rounded-full blur-[3px]"
149 style={getThemePreviewStyle(colors, mode)}
150 />
151 </span>
152 );
154​
155/**
156 * A theme card's light and dark balls. Clicking a ball assigns that theme to
157 * that half of the appearance mix; assigned balls carry a ring and a sun or
158 * moon badge.
159 */
160export function ThemePreviewCircles({
161 label,
162 activeModes,
163 onSelectMode,
164 previews,
165}: {
166 label: string;
167 activeModes: ReadonlyArray<ThemeMode>;
168 onSelectMode: (mode: ThemeMode) => void;
169 previews: ThemeCardDefinition["previews"];
170}) {
171 return (
172 <div className="flex min-h-16 items-center justify-center gap-2.5 px-3 pt-3">
173 {previews.map((preview) => {
174 const mode = preview.mode;
175 const isPicked = activeModes.includes(mode);
176 return (
177 <Tooltip key={mode}>
178 <TooltipTrigger
179 render={
180 <button
181 aria-label={`Use ${label} ${mode} mode`}
182 aria-pressed={isPicked}
183 className={cn(
184 "relative flex size-[68px] shrink-0 transform-gpu cursor-pointer items-center justify-center rounded-full p-1 outline-none transition-transform hover:scale-105 focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-card",
185 isPicked && "hover:scale-100",
186 )}
187 onClick={(event) => {
188 event.stopPropagation();
189 onSelectMode(mode);
190 }}
191 type="button"
192 >
193 <ThemePreviewCircle colors={preview.colors} mode={mode} />
194 {isPicked ? (
195 <>
196 <span
197 aria-hidden
198 className="pointer-events-none absolute inset-0 rounded-full"
199 style={{ boxShadow: "inset 0 0 0 2px var(--ring)" }}
200 />
201 <span
202 aria-hidden
203 className="pointer-events-none absolute bottom-0.5 right-0.5 flex size-5 items-center justify-center rounded-full border border-border/70 bg-background text-foreground shadow-sm"
204 >
205 {mode === "light" ? (
206 <SunIcon className="size-3" />
207 ) : (
208 <MoonIcon className="size-3" />
209 )}
210 </span>
211 </>
212 ) : null}
213 </button>
214 }
215 />
216 <TooltipPopup>
217 {mode === "light" ? "Use for light mode only" : "Use for dark mode only"}
218 </TooltipPopup>
219 </Tooltip>
220 );
221 })}
222 </div>
223 );