@behavioral-agentic-design-system/core 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +36 -5
- package/scripts/bads-sync.mjs +81 -0
- package/scripts/build-tokens.cjs +430 -0
- package/src/app/font-ready.tsx +51 -0
- package/src/app/layout.tsx +152 -0
- package/src/app/page.tsx +267 -0
- package/src/app/preview/cards/page.tsx +8 -0
- package/src/app/preview/components/page.tsx +232 -0
- package/src/app/preview/dark-mode-client.tsx +24 -0
- package/src/app/preview/dashboard/page.tsx +4 -0
- package/src/app/preview/global-font-client.tsx +36 -0
- package/src/app/preview/layout.tsx +13 -0
- package/src/app/preview/mail/page.tsx +4 -0
- package/src/app/preview/pricing/page.tsx +4 -0
- package/src/app/preview/typography/page.tsx +11 -0
- package/src/app/providers.tsx +49 -0
- package/src/components/Foundations/Colors.stories.tsx +85 -0
- package/src/components/Foundations/Motion.stories.tsx +169 -0
- package/src/components/Foundations/Radius.stories.tsx +36 -0
- package/src/components/Foundations/Shadows.stories.tsx +30 -0
- package/src/components/Foundations/Spacing.stories.tsx +35 -0
- package/src/components/Foundations/Typography.stories.tsx +68 -0
- package/src/components/bads-update-banner.tsx +120 -0
- package/src/components/customize/design-controls.tsx +165 -0
- package/src/components/customize/font-pickers.tsx +118 -0
- package/src/components/customize/letter-spacing.tsx +105 -0
- package/src/components/customize/save-bar.tsx +379 -0
- package/src/components/customize/theme-io.tsx +245 -0
- package/src/components/customize/type-scale.tsx +134 -0
- package/src/components/customize-tab.tsx +502 -0
- package/src/components/home-tab.tsx +1227 -0
- package/src/components/layers-tab.tsx +225 -0
- package/src/components/ui/alert.tsx +5 -1
- package/src/hooks/use-global-design-controls.ts +117 -0
- package/src/hooks/use-global-mono-font.ts +73 -0
- package/src/hooks/use-global-primary-color.ts +544 -0
- package/src/hooks/use-global-sans-font.ts +98 -0
- package/src/hooks/use-global-serif-font.ts +77 -0
- package/src/hooks/use-global-theme.ts +259 -0
- package/src/hooks/use-global-type-scale.ts +141 -0
- package/src/hooks/use-sync-type-scale.ts +51 -0
- package/src/hooks/use-token-overrides.ts +98 -0
- package/src/styles.css +20 -5
- package/src/tokens.json +5 -5
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useCallback, useEffect, useState } from "react";
|
|
4
|
+
import { Button } from "@behavioral-agentic-design-system/core/components/ui/button";
|
|
5
|
+
import {
|
|
6
|
+
PRIMARY_COLOR_OPTIONS,
|
|
7
|
+
type PrimaryColorKey,
|
|
8
|
+
type PrimaryColorOption,
|
|
9
|
+
} from "@behavioral-agentic-design-system/core/hooks/use-global-primary-color";
|
|
10
|
+
import {
|
|
11
|
+
SANS_FONT_OPTIONS,
|
|
12
|
+
type SansFontKey,
|
|
13
|
+
} from "@behavioral-agentic-design-system/core/hooks/use-global-sans-font";
|
|
14
|
+
import {
|
|
15
|
+
SERIF_FONT_OPTIONS,
|
|
16
|
+
type SerifFontKey,
|
|
17
|
+
} from "@behavioral-agentic-design-system/core/hooks/use-global-serif-font";
|
|
18
|
+
import {
|
|
19
|
+
MONO_FONT_OPTIONS,
|
|
20
|
+
type MonoFontKey,
|
|
21
|
+
} from "@behavioral-agentic-design-system/core/hooks/use-global-mono-font";
|
|
22
|
+
import {
|
|
23
|
+
computeSizes,
|
|
24
|
+
TYPE_SCALE_DEFAULTS,
|
|
25
|
+
type TypeScale,
|
|
26
|
+
} from "@behavioral-agentic-design-system/core/hooks/use-global-type-scale";
|
|
27
|
+
import {
|
|
28
|
+
DESIGN_CONTROL_DEFAULTS,
|
|
29
|
+
type DesignControls,
|
|
30
|
+
} from "@behavioral-agentic-design-system/core/hooks/use-global-design-controls";
|
|
31
|
+
import { Save, Check, AlertCircle, RotateCcw } from "lucide-react";
|
|
32
|
+
|
|
33
|
+
const STORAGE_KEYS = {
|
|
34
|
+
primaryColor: "BADS.primaryColor",
|
|
35
|
+
sansFont: "BADS.sansFont",
|
|
36
|
+
serifFont: "BADS.serifFont",
|
|
37
|
+
monoFont: "BADS.monoFont",
|
|
38
|
+
designControls: "BADS.designControls",
|
|
39
|
+
typeScale: "BADS.typeScale",
|
|
40
|
+
} as const;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* After a successful save, we snapshot the current customization state into
|
|
44
|
+
* this key. diffCount() then compares the live state against this snapshot
|
|
45
|
+
* (rather than the hardcoded defaults) so the "X customizations" counter
|
|
46
|
+
* reflects changes *since the last save*, not changes *since first load*.
|
|
47
|
+
*/
|
|
48
|
+
const SAVED_SNAPSHOT_KEY = "BADS.savedSnapshot";
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Custom brand colors persist in localStorage under this key (managed by
|
|
52
|
+
* useGlobalPrimaryColor's addCustom/renameCustom/removeCustom). buildPayload
|
|
53
|
+
* merges them with PRIMARY_COLOR_OPTIONS at save time so picking a custom
|
|
54
|
+
* color and hitting Save actually writes that color to tokens.json — without
|
|
55
|
+
* it the active-color lookup would miss for any non-built-in id.
|
|
56
|
+
*/
|
|
57
|
+
const CUSTOMS_KEY = "BADS.customPrimaryColors";
|
|
58
|
+
|
|
59
|
+
function readCustomPrimaryColors(): Record<string, PrimaryColorOption> {
|
|
60
|
+
if (typeof window === "undefined") return {};
|
|
61
|
+
try {
|
|
62
|
+
const raw = window.localStorage.getItem(CUSTOMS_KEY);
|
|
63
|
+
if (!raw) return {};
|
|
64
|
+
const parsed = JSON.parse(raw) as Record<string, PrimaryColorOption>;
|
|
65
|
+
return parsed && typeof parsed === "object" ? parsed : {};
|
|
66
|
+
} catch {
|
|
67
|
+
return {};
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
type Snapshot = {
|
|
72
|
+
primaryColor: PrimaryColorKey | null;
|
|
73
|
+
sansFont: SansFontKey | null;
|
|
74
|
+
serifFont: SerifFontKey | null;
|
|
75
|
+
monoFont: MonoFontKey | null;
|
|
76
|
+
designControls: DesignControls | null;
|
|
77
|
+
typeScale: TypeScale | null;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const SANS_DEFAULT: SansFontKey = "hyperlegible";
|
|
81
|
+
const SERIF_DEFAULT: SerifFontKey = "georgia";
|
|
82
|
+
const MONO_DEFAULT: MonoFontKey = "jetbrains";
|
|
83
|
+
const PRIMARY_DEFAULT: PrimaryColorKey = "blue";
|
|
84
|
+
|
|
85
|
+
function readSnapshot(): Snapshot {
|
|
86
|
+
if (typeof window === "undefined") {
|
|
87
|
+
return {
|
|
88
|
+
primaryColor: null,
|
|
89
|
+
sansFont: null,
|
|
90
|
+
serifFont: null,
|
|
91
|
+
monoFont: null,
|
|
92
|
+
designControls: null,
|
|
93
|
+
typeScale: null,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
const get = (key: string) => window.localStorage.getItem(key);
|
|
97
|
+
const parse = <T,>(key: string): T | null => {
|
|
98
|
+
const raw = get(key);
|
|
99
|
+
if (!raw) return null;
|
|
100
|
+
try {
|
|
101
|
+
return JSON.parse(raw) as T;
|
|
102
|
+
} catch {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
return {
|
|
107
|
+
primaryColor: get(STORAGE_KEYS.primaryColor) as PrimaryColorKey | null,
|
|
108
|
+
sansFont: get(STORAGE_KEYS.sansFont) as SansFontKey | null,
|
|
109
|
+
serifFont: get(STORAGE_KEYS.serifFont) as SerifFontKey | null,
|
|
110
|
+
monoFont: get(STORAGE_KEYS.monoFont) as MonoFontKey | null,
|
|
111
|
+
designControls: parse<DesignControls>(STORAGE_KEYS.designControls),
|
|
112
|
+
typeScale: parse<TypeScale>(STORAGE_KEYS.typeScale),
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function readSavedSnapshot(): Snapshot | null {
|
|
117
|
+
if (typeof window === "undefined") return null;
|
|
118
|
+
const raw = window.localStorage.getItem(SAVED_SNAPSHOT_KEY);
|
|
119
|
+
if (!raw) return null;
|
|
120
|
+
try {
|
|
121
|
+
return JSON.parse(raw) as Snapshot;
|
|
122
|
+
} catch {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Compare two values, using the saved snapshot if available and otherwise
|
|
129
|
+
* falling back to a hardcoded default. Returns true when the live value
|
|
130
|
+
* differs from the baseline.
|
|
131
|
+
*/
|
|
132
|
+
function differs<T>(live: T, savedFromSnapshot: T | null | undefined, hardcodedDefault: T): boolean {
|
|
133
|
+
const baseline = savedFromSnapshot ?? hardcodedDefault;
|
|
134
|
+
return live !== baseline;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function diffCount(s: Snapshot, saved: Snapshot | null): number {
|
|
138
|
+
let count = 0;
|
|
139
|
+
if (s.primaryColor && differs(s.primaryColor, saved?.primaryColor, PRIMARY_DEFAULT)) count++;
|
|
140
|
+
if (s.sansFont && differs(s.sansFont, saved?.sansFont, SANS_DEFAULT)) count++;
|
|
141
|
+
if (s.serifFont && differs(s.serifFont, saved?.serifFont, SERIF_DEFAULT)) count++;
|
|
142
|
+
if (s.monoFont && differs(s.monoFont, saved?.monoFont, MONO_DEFAULT)) count++;
|
|
143
|
+
if (s.typeScale) {
|
|
144
|
+
const baseTarget = saved?.typeScale?.base ?? TYPE_SCALE_DEFAULTS.base;
|
|
145
|
+
const ratioTarget = saved?.typeScale?.ratio ?? TYPE_SCALE_DEFAULTS.ratio;
|
|
146
|
+
if (s.typeScale.base !== baseTarget) count++;
|
|
147
|
+
if (s.typeScale.ratio !== ratioTarget) count++;
|
|
148
|
+
}
|
|
149
|
+
if (s.designControls) {
|
|
150
|
+
const radTarget = saved?.designControls?.radius ?? DESIGN_CONTROL_DEFAULTS.radius;
|
|
151
|
+
const spcTarget = saved?.designControls?.spacing ?? DESIGN_CONTROL_DEFAULTS.spacing;
|
|
152
|
+
const lsTarget = saved?.designControls?.letterSpacing ?? DESIGN_CONTROL_DEFAULTS.letterSpacing;
|
|
153
|
+
const shTarget = saved?.designControls?.shadowOpacity ?? DESIGN_CONTROL_DEFAULTS.shadowOpacity;
|
|
154
|
+
if (s.designControls.radius !== radTarget) count++;
|
|
155
|
+
if (s.designControls.spacing !== spcTarget) count++;
|
|
156
|
+
if (s.designControls.letterSpacing !== lsTarget) count++;
|
|
157
|
+
if (s.designControls.shadowOpacity !== shTarget) count++;
|
|
158
|
+
}
|
|
159
|
+
return count;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function slugify(label: string, fallback: string): string {
|
|
163
|
+
const slug = label
|
|
164
|
+
.trim()
|
|
165
|
+
.toLowerCase()
|
|
166
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
167
|
+
.replace(/^-+|-+$/g, "")
|
|
168
|
+
.slice(0, 48);
|
|
169
|
+
return slug || fallback;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function buildPayload(s: Snapshot) {
|
|
173
|
+
const payload: Record<string, unknown> = {};
|
|
174
|
+
|
|
175
|
+
// Merge built-ins with customs from localStorage so adding a brand color in
|
|
176
|
+
// the picker and hitting Save actually writes it. Customs win on key collision
|
|
177
|
+
// (rename should reflect, not be ignored).
|
|
178
|
+
const customs = readCustomPrimaryColors();
|
|
179
|
+
const allColors: Record<string, PrimaryColorOption> = {
|
|
180
|
+
...PRIMARY_COLOR_OPTIONS,
|
|
181
|
+
...customs,
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
if (s.primaryColor && allColors[s.primaryColor]) {
|
|
185
|
+
const opt = allColors[s.primaryColor];
|
|
186
|
+
payload.brandColor = {
|
|
187
|
+
primary: opt.primary,
|
|
188
|
+
primaryHover: opt.primaryHover,
|
|
189
|
+
primaryForeground: opt.primaryForeground,
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
// If the active color is a custom (lives only in this browser), promote
|
|
193
|
+
// it to a built-in entry in brand-colors.json on save. The server picks
|
|
194
|
+
// it up via `pinAsBuiltin` and writes alongside tokens.json — so a Save
|
|
195
|
+
// both bakes the active primary AND makes the swatch available to every
|
|
196
|
+
// future clone.
|
|
197
|
+
if (s.primaryColor in customs) {
|
|
198
|
+
const slug = slugify(opt.label, s.primaryColor.replace(/^custom-/, "c-"));
|
|
199
|
+
payload.pinAsBuiltin = {
|
|
200
|
+
id: slug,
|
|
201
|
+
label: opt.label,
|
|
202
|
+
primary: opt.primary,
|
|
203
|
+
primaryForeground: opt.primaryForeground,
|
|
204
|
+
...(opt.note ? { note: opt.note } : {}),
|
|
205
|
+
// Tell the client which localStorage custom id to drop after the
|
|
206
|
+
// server confirms — kept here so the round-trip is self-describing.
|
|
207
|
+
replaceCustomId: s.primaryColor,
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const fonts: Record<string, string> = {};
|
|
213
|
+
if (s.sansFont && SANS_FONT_OPTIONS[s.sansFont]) {
|
|
214
|
+
fonts.sans = SANS_FONT_OPTIONS[s.sansFont].stack;
|
|
215
|
+
}
|
|
216
|
+
if (s.serifFont && SERIF_FONT_OPTIONS[s.serifFont]) {
|
|
217
|
+
fonts.serif = SERIF_FONT_OPTIONS[s.serifFont].stack;
|
|
218
|
+
}
|
|
219
|
+
if (s.monoFont && MONO_FONT_OPTIONS[s.monoFont]) {
|
|
220
|
+
fonts.mono = MONO_FONT_OPTIONS[s.monoFont].stack;
|
|
221
|
+
}
|
|
222
|
+
if (Object.keys(fonts).length > 0) payload.fonts = fonts;
|
|
223
|
+
|
|
224
|
+
if (s.typeScale) {
|
|
225
|
+
payload.fontSizes = computeSizes(s.typeScale);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
if (s.designControls) {
|
|
229
|
+
payload.radius = s.designControls.radius;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
return payload;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export function SaveBar() {
|
|
236
|
+
const [snapshot, setSnapshot] = useState<Snapshot | null>(null);
|
|
237
|
+
const [savedSnapshot, setSavedSnapshot] = useState<Snapshot | null>(null);
|
|
238
|
+
const [status, setStatus] = useState<"idle" | "saving" | "saved" | "error">("idle");
|
|
239
|
+
const [message, setMessage] = useState<string | null>(null);
|
|
240
|
+
|
|
241
|
+
// Refresh both the live snapshot and the last-saved snapshot on mount and
|
|
242
|
+
// whenever any sibling localStorage key changes. Same-tab updates from the
|
|
243
|
+
// controls don't fire `storage` events, so we also poll on a short
|
|
244
|
+
// interval — cheap and keeps the bar honest.
|
|
245
|
+
useEffect(() => {
|
|
246
|
+
setSnapshot(readSnapshot());
|
|
247
|
+
setSavedSnapshot(readSavedSnapshot());
|
|
248
|
+
const refresh = () => {
|
|
249
|
+
setSnapshot(readSnapshot());
|
|
250
|
+
setSavedSnapshot(readSavedSnapshot());
|
|
251
|
+
};
|
|
252
|
+
window.addEventListener("storage", refresh);
|
|
253
|
+
const interval = window.setInterval(refresh, 500);
|
|
254
|
+
return () => {
|
|
255
|
+
window.removeEventListener("storage", refresh);
|
|
256
|
+
window.clearInterval(interval);
|
|
257
|
+
};
|
|
258
|
+
}, []);
|
|
259
|
+
|
|
260
|
+
const handleSave = useCallback(async () => {
|
|
261
|
+
if (!snapshot) return;
|
|
262
|
+
setStatus("saving");
|
|
263
|
+
setMessage(null);
|
|
264
|
+
try {
|
|
265
|
+
const res = await fetch("/api/save-tokens", {
|
|
266
|
+
method: "POST",
|
|
267
|
+
headers: { "Content-Type": "application/json" },
|
|
268
|
+
body: JSON.stringify(buildPayload(snapshot)),
|
|
269
|
+
});
|
|
270
|
+
const data = await res.json();
|
|
271
|
+
if (!res.ok || !data.ok) {
|
|
272
|
+
setStatus("error");
|
|
273
|
+
setMessage(data.error ?? "Save failed");
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
setStatus("saved");
|
|
277
|
+
const count = Array.isArray(data.updates) ? data.updates.length : 0;
|
|
278
|
+
setMessage(count > 0 ? `Wrote ${count} token${count === 1 ? "" : "s"} to tokens.json` : "Nothing to save");
|
|
279
|
+
|
|
280
|
+
// If the save promoted a custom color to a built-in, drop the
|
|
281
|
+
// localStorage custom (now redundant), point the active color at the
|
|
282
|
+
// new built-in id so the picker's "Active" ring stays on the same
|
|
283
|
+
// swatch, and reload — Fast Refresh on JSON imports is unreliable, so
|
|
284
|
+
// we force a re-mount to pick up the new brand.config.colors entry.
|
|
285
|
+
if (data.pinnedBuiltinId && data.replaceCustomId) {
|
|
286
|
+
try {
|
|
287
|
+
const raw = window.localStorage.getItem(CUSTOMS_KEY);
|
|
288
|
+
const parsed = raw ? (JSON.parse(raw) as Record<string, unknown>) : {};
|
|
289
|
+
delete parsed[data.replaceCustomId];
|
|
290
|
+
window.localStorage.setItem(CUSTOMS_KEY, JSON.stringify(parsed));
|
|
291
|
+
window.localStorage.setItem(STORAGE_KEYS.primaryColor, data.pinnedBuiltinId);
|
|
292
|
+
} catch {}
|
|
293
|
+
window.setTimeout(() => window.location.reload(), 600);
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// Snapshot the live state as the new baseline so the diff counter reads
|
|
298
|
+
// 0 until the user changes something else. The customizations stay in
|
|
299
|
+
// their own localStorage keys (and stay applied visually) — only the
|
|
300
|
+
// counter's reference point moves.
|
|
301
|
+
window.localStorage.setItem(SAVED_SNAPSHOT_KEY, JSON.stringify(snapshot));
|
|
302
|
+
setSavedSnapshot(snapshot);
|
|
303
|
+
window.setTimeout(() => {
|
|
304
|
+
setStatus("idle");
|
|
305
|
+
setMessage(null);
|
|
306
|
+
}, 3500);
|
|
307
|
+
} catch (err) {
|
|
308
|
+
setStatus("error");
|
|
309
|
+
setMessage((err as Error).message);
|
|
310
|
+
}
|
|
311
|
+
}, [snapshot]);
|
|
312
|
+
|
|
313
|
+
const handleResetAll = useCallback(() => {
|
|
314
|
+
if (typeof window === "undefined") return;
|
|
315
|
+
Object.values(STORAGE_KEYS).forEach((key) => window.localStorage.removeItem(key));
|
|
316
|
+
// Reset also clears the saved-snapshot baseline so the bar fully matches
|
|
317
|
+
// the hardcoded defaults again.
|
|
318
|
+
window.localStorage.removeItem(SAVED_SNAPSHOT_KEY);
|
|
319
|
+
// Trigger a reload so all hooks pick up defaults cleanly.
|
|
320
|
+
window.location.reload();
|
|
321
|
+
}, []);
|
|
322
|
+
|
|
323
|
+
if (!snapshot) return null;
|
|
324
|
+
const count = diffCount(snapshot, savedSnapshot);
|
|
325
|
+
if (count === 0 && status === "idle") return null;
|
|
326
|
+
|
|
327
|
+
return (
|
|
328
|
+
<div className="fixed bottom-0 left-0 right-0 z-50 border-t bg-card/95 backdrop-blur supports-[backdrop-filter]:bg-card/80 shadow-lg">
|
|
329
|
+
<div className="max-w-7xl mx-auto px-8 py-8 flex items-center justify-between gap-4">
|
|
330
|
+
<div className="flex items-center gap-3 min-w-0">
|
|
331
|
+
{status === "saved" && (
|
|
332
|
+
<span className="flex items-center gap-2 text-sm text-foreground">
|
|
333
|
+
<Check className="h-4 w-4 text-primary" />
|
|
334
|
+
<span className="truncate">{message}</span>
|
|
335
|
+
</span>
|
|
336
|
+
)}
|
|
337
|
+
{status === "error" && (
|
|
338
|
+
<span className="flex items-center gap-2 text-sm text-destructive">
|
|
339
|
+
<AlertCircle className="h-4 w-4" />
|
|
340
|
+
<span className="truncate">{message}</span>
|
|
341
|
+
</span>
|
|
342
|
+
)}
|
|
343
|
+
{(status === "idle" || status === "saving") && (
|
|
344
|
+
<span className="text-sm">
|
|
345
|
+
<span className="font-semibold">
|
|
346
|
+
{count} customization{count === 1 ? "" : "s"}
|
|
347
|
+
</span>{" "}
|
|
348
|
+
<span className="text-muted-foreground">
|
|
349
|
+
— push them into tokens.json to make them part of the design system
|
|
350
|
+
</span>
|
|
351
|
+
</span>
|
|
352
|
+
)}
|
|
353
|
+
</div>
|
|
354
|
+
|
|
355
|
+
<div className="flex items-center gap-2 shrink-0">
|
|
356
|
+
<Button
|
|
357
|
+
variant="ghost"
|
|
358
|
+
size="sm"
|
|
359
|
+
onClick={handleResetAll}
|
|
360
|
+
disabled={status === "saving"}
|
|
361
|
+
className="gap-2"
|
|
362
|
+
>
|
|
363
|
+
<RotateCcw className="h-4 w-4" />
|
|
364
|
+
Reset all
|
|
365
|
+
</Button>
|
|
366
|
+
<Button
|
|
367
|
+
size="sm"
|
|
368
|
+
onClick={handleSave}
|
|
369
|
+
disabled={status === "saving" || count === 0}
|
|
370
|
+
className="gap-2"
|
|
371
|
+
>
|
|
372
|
+
<Save className="h-4 w-4" />
|
|
373
|
+
{status === "saving" ? "Saving…" : "Save to tokens.json"}
|
|
374
|
+
</Button>
|
|
375
|
+
</div>
|
|
376
|
+
</div>
|
|
377
|
+
</div>
|
|
378
|
+
);
|
|
379
|
+
}
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useState } from "react";
|
|
4
|
+
import { Button } from "@behavioral-agentic-design-system/core/components/ui/button";
|
|
5
|
+
import {
|
|
6
|
+
Dialog,
|
|
7
|
+
DialogContent,
|
|
8
|
+
DialogDescription,
|
|
9
|
+
DialogFooter,
|
|
10
|
+
DialogHeader,
|
|
11
|
+
DialogTitle,
|
|
12
|
+
DialogTrigger,
|
|
13
|
+
} from "@behavioral-agentic-design-system/core/components/ui/dialog";
|
|
14
|
+
import { Textarea } from "@behavioral-agentic-design-system/core/components/ui/textarea";
|
|
15
|
+
import {
|
|
16
|
+
exportThemeCss,
|
|
17
|
+
parseThemeCss,
|
|
18
|
+
useGlobalTheme,
|
|
19
|
+
} from "@behavioral-agentic-design-system/core/hooks/use-global-theme";
|
|
20
|
+
import { useGlobalPrimaryColor } from "@behavioral-agentic-design-system/core/hooks/use-global-primary-color";
|
|
21
|
+
import { Copy, Download, Upload, Check, RotateCcw, ExternalLink } from "lucide-react";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Convert any CSS color string the browser can parse — oklch, hsl, rgb, hex,
|
|
25
|
+
* named — into a normalized #RRGGBB hex. Uses a temporary DOM probe so the
|
|
26
|
+
* browser does the actual color-space math; we just read getComputedStyle.
|
|
27
|
+
* Returns null if the string isn't a valid CSS color.
|
|
28
|
+
*/
|
|
29
|
+
function cssColorToHex(value: string): string | null {
|
|
30
|
+
if (typeof document === "undefined") return null;
|
|
31
|
+
const probe = document.createElement("div");
|
|
32
|
+
probe.style.color = "";
|
|
33
|
+
probe.style.color = value;
|
|
34
|
+
if (!probe.style.color) return null;
|
|
35
|
+
document.body.appendChild(probe);
|
|
36
|
+
const computed = getComputedStyle(probe).color;
|
|
37
|
+
document.body.removeChild(probe);
|
|
38
|
+
const m = computed.match(/^rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/);
|
|
39
|
+
if (!m) return null;
|
|
40
|
+
const toHex = (n: string) =>
|
|
41
|
+
parseInt(n, 10).toString(16).padStart(2, "0").toUpperCase();
|
|
42
|
+
return `#${toHex(m[1])}${toHex(m[2])}${toHex(m[3])}`;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function ThemeIO() {
|
|
46
|
+
const { hasTheme, importTheme, clearTheme } = useGlobalTheme();
|
|
47
|
+
const { addCustom } = useGlobalPrimaryColor();
|
|
48
|
+
|
|
49
|
+
const [importOpen, setImportOpen] = useState(false);
|
|
50
|
+
const [importValue, setImportValue] = useState("");
|
|
51
|
+
const [importError, setImportError] = useState<string | null>(null);
|
|
52
|
+
|
|
53
|
+
const [exportOpen, setExportOpen] = useState(false);
|
|
54
|
+
const [exportValue, setExportValue] = useState("");
|
|
55
|
+
const [copied, setCopied] = useState(false);
|
|
56
|
+
|
|
57
|
+
const handleImport = () => {
|
|
58
|
+
setImportError(null);
|
|
59
|
+
try {
|
|
60
|
+
importTheme(importValue);
|
|
61
|
+
// Tweakcn themes carry a --primary hex/oklch/hsl. Add it to the picker
|
|
62
|
+
// as a custom brand color so it's selectable later — and so a Save to
|
|
63
|
+
// tokens.json automatically promotes it to brand-colors.json (the same
|
|
64
|
+
// chain custom-added colors flow through). Best-effort: silently skip
|
|
65
|
+
// if the color can't be parsed.
|
|
66
|
+
try {
|
|
67
|
+
const parsed = parseThemeCss(importValue);
|
|
68
|
+
const primaryRaw = parsed.light.primary ?? parsed.dark.primary;
|
|
69
|
+
if (primaryRaw) {
|
|
70
|
+
const hex = cssColorToHex(primaryRaw);
|
|
71
|
+
if (hex) {
|
|
72
|
+
addCustom(hex, "Imported (tweakcn)");
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
} catch {}
|
|
76
|
+
setImportOpen(false);
|
|
77
|
+
setImportValue("");
|
|
78
|
+
} catch (err) {
|
|
79
|
+
setImportError(
|
|
80
|
+
err instanceof Error
|
|
81
|
+
? err.message
|
|
82
|
+
: "Couldn't parse that. Make sure it's tweakcn-style CSS.",
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const handleOpenExport = () => {
|
|
88
|
+
setExportValue(exportThemeCss());
|
|
89
|
+
setCopied(false);
|
|
90
|
+
setExportOpen(true);
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const handleCopy = async () => {
|
|
94
|
+
try {
|
|
95
|
+
await navigator.clipboard.writeText(exportValue);
|
|
96
|
+
setCopied(true);
|
|
97
|
+
setTimeout(() => setCopied(false), 2000);
|
|
98
|
+
} catch {
|
|
99
|
+
// clipboard unavailable — leave the textarea selectable
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
return (
|
|
104
|
+
<div>
|
|
105
|
+
<h2 className="text-2xl font-semibold tracking-tight mb-2">
|
|
106
|
+
Theme
|
|
107
|
+
</h2>
|
|
108
|
+
<p className="text-sm text-muted-foreground mb-6 max-w-prose">
|
|
109
|
+
Paste a tweakcn theme to apply it across every preview screen, or
|
|
110
|
+
export the current theme as CSS to share or save.{" "}
|
|
111
|
+
<a
|
|
112
|
+
href="https://tweakcn.com/editor/theme"
|
|
113
|
+
target="_blank"
|
|
114
|
+
rel="noopener noreferrer"
|
|
115
|
+
className="font-medium text-primary hover:underline inline-flex items-center gap-0.5"
|
|
116
|
+
>
|
|
117
|
+
Edit on tweakcn
|
|
118
|
+
<ExternalLink className="h-3 w-3" />
|
|
119
|
+
</a>
|
|
120
|
+
</p>
|
|
121
|
+
|
|
122
|
+
<div className="flex flex-wrap gap-2">
|
|
123
|
+
<Dialog open={importOpen} onOpenChange={setImportOpen}>
|
|
124
|
+
<DialogTrigger asChild>
|
|
125
|
+
<Button variant="outline" size="sm" className="gap-2">
|
|
126
|
+
<Upload className="h-4 w-4" />
|
|
127
|
+
Import theme
|
|
128
|
+
</Button>
|
|
129
|
+
</DialogTrigger>
|
|
130
|
+
<DialogContent className="sm:max-w-2xl max-h-[85vh] flex flex-col">
|
|
131
|
+
<DialogHeader>
|
|
132
|
+
<DialogTitle>Import theme</DialogTitle>
|
|
133
|
+
<DialogDescription>
|
|
134
|
+
Paste a tweakcn-style CSS theme. Both <code>:root</code> and{" "}
|
|
135
|
+
<code>.dark</code> blocks are supported.{" "}
|
|
136
|
+
<a
|
|
137
|
+
href="https://tweakcn.com/editor/theme"
|
|
138
|
+
target="_blank"
|
|
139
|
+
rel="noopener noreferrer"
|
|
140
|
+
className="font-medium text-primary hover:underline inline-flex items-center gap-0.5"
|
|
141
|
+
>
|
|
142
|
+
Build one on tweakcn
|
|
143
|
+
<ExternalLink className="h-3 w-3" />
|
|
144
|
+
</a>
|
|
145
|
+
</DialogDescription>
|
|
146
|
+
</DialogHeader>
|
|
147
|
+
<Textarea
|
|
148
|
+
value={importValue}
|
|
149
|
+
onChange={(e) => setImportValue(e.target.value)}
|
|
150
|
+
placeholder={`:root {\n --background: oklch(1 0 0);\n --primary: oklch(0.55 0.25 262);\n ...\n}\n\n.dark {\n --background: oklch(0.145 0 0);\n ...\n}`}
|
|
151
|
+
className="text-xs h-[50vh] max-h-[50vh] resize-none overflow-auto"
|
|
152
|
+
style={{
|
|
153
|
+
fontFamily:
|
|
154
|
+
"'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, Consolas, monospace",
|
|
155
|
+
}}
|
|
156
|
+
spellCheck={false}
|
|
157
|
+
/>
|
|
158
|
+
{importError && (
|
|
159
|
+
<p className="text-xs text-destructive">{importError}</p>
|
|
160
|
+
)}
|
|
161
|
+
<DialogFooter>
|
|
162
|
+
<Button
|
|
163
|
+
variant="outline"
|
|
164
|
+
size="sm"
|
|
165
|
+
onClick={() => {
|
|
166
|
+
setImportOpen(false);
|
|
167
|
+
setImportValue("");
|
|
168
|
+
setImportError(null);
|
|
169
|
+
}}
|
|
170
|
+
>
|
|
171
|
+
Cancel
|
|
172
|
+
</Button>
|
|
173
|
+
<Button size="sm" onClick={handleImport} disabled={!importValue.trim()}>
|
|
174
|
+
Apply theme
|
|
175
|
+
</Button>
|
|
176
|
+
</DialogFooter>
|
|
177
|
+
</DialogContent>
|
|
178
|
+
</Dialog>
|
|
179
|
+
|
|
180
|
+
<Dialog open={exportOpen} onOpenChange={setExportOpen}>
|
|
181
|
+
<DialogTrigger asChild>
|
|
182
|
+
<Button variant="outline" size="sm" className="gap-2" onClick={handleOpenExport}>
|
|
183
|
+
<Download className="h-4 w-4" />
|
|
184
|
+
Export theme
|
|
185
|
+
</Button>
|
|
186
|
+
</DialogTrigger>
|
|
187
|
+
<DialogContent className="sm:max-w-2xl max-h-[85vh] flex flex-col">
|
|
188
|
+
<DialogHeader>
|
|
189
|
+
<DialogTitle>Export theme</DialogTitle>
|
|
190
|
+
<DialogDescription>
|
|
191
|
+
A snapshot of the current theme variables for both light and
|
|
192
|
+
dark modes. Paste into tweakcn or save for later.{" "}
|
|
193
|
+
<a
|
|
194
|
+
href="https://tweakcn.com/editor/theme"
|
|
195
|
+
target="_blank"
|
|
196
|
+
rel="noopener noreferrer"
|
|
197
|
+
className="font-medium text-primary hover:underline inline-flex items-center gap-0.5"
|
|
198
|
+
>
|
|
199
|
+
Open tweakcn
|
|
200
|
+
<ExternalLink className="h-3 w-3" />
|
|
201
|
+
</a>
|
|
202
|
+
</DialogDescription>
|
|
203
|
+
</DialogHeader>
|
|
204
|
+
<Textarea
|
|
205
|
+
value={exportValue}
|
|
206
|
+
readOnly
|
|
207
|
+
className="text-xs h-[50vh] max-h-[50vh] resize-none overflow-auto"
|
|
208
|
+
style={{
|
|
209
|
+
fontFamily:
|
|
210
|
+
"'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, Consolas, monospace",
|
|
211
|
+
}}
|
|
212
|
+
spellCheck={false}
|
|
213
|
+
onFocus={(e) => e.currentTarget.select()}
|
|
214
|
+
/>
|
|
215
|
+
<DialogFooter>
|
|
216
|
+
<Button variant="outline" size="sm" onClick={() => setExportOpen(false)}>
|
|
217
|
+
Close
|
|
218
|
+
</Button>
|
|
219
|
+
<Button size="sm" onClick={handleCopy} className="gap-2">
|
|
220
|
+
{copied ? (
|
|
221
|
+
<>
|
|
222
|
+
<Check className="h-4 w-4" />
|
|
223
|
+
Copied
|
|
224
|
+
</>
|
|
225
|
+
) : (
|
|
226
|
+
<>
|
|
227
|
+
<Copy className="h-4 w-4" />
|
|
228
|
+
Copy CSS
|
|
229
|
+
</>
|
|
230
|
+
)}
|
|
231
|
+
</Button>
|
|
232
|
+
</DialogFooter>
|
|
233
|
+
</DialogContent>
|
|
234
|
+
</Dialog>
|
|
235
|
+
|
|
236
|
+
{hasTheme && (
|
|
237
|
+
<Button variant="ghost" size="sm" className="gap-2" onClick={clearTheme}>
|
|
238
|
+
<RotateCcw className="h-4 w-4" />
|
|
239
|
+
Reset to default
|
|
240
|
+
</Button>
|
|
241
|
+
)}
|
|
242
|
+
</div>
|
|
243
|
+
</div>
|
|
244
|
+
);
|
|
245
|
+
}
|