@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,225 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useEffect, useState } from "react";
|
|
4
|
+
import { Loader2, FileText, GitCommit, ChevronRight, Check, AlertCircle } from "lucide-react";
|
|
5
|
+
import { Button } from "@behavioral-agentic-design-system/core/components/ui/button";
|
|
6
|
+
import { Input } from "@behavioral-agentic-design-system/core/components/ui/input";
|
|
7
|
+
import { Textarea } from "@behavioral-agentic-design-system/core/components/ui/textarea";
|
|
8
|
+
import { ScrollArea } from "@behavioral-agentic-design-system/core/components/ui/scroll-area";
|
|
9
|
+
|
|
10
|
+
type LayerEntry = { slug: string; label: string; files: string[] };
|
|
11
|
+
type SaveStatus = "idle" | "saving" | "success" | "error";
|
|
12
|
+
|
|
13
|
+
export function LayersTab() {
|
|
14
|
+
const [layers, setLayers] = useState<LayerEntry[]>([]);
|
|
15
|
+
const [openLayers, setOpenLayers] = useState<Set<string>>(new Set());
|
|
16
|
+
const [activePath, setActivePath] = useState<string | null>(null);
|
|
17
|
+
const [content, setContent] = useState<string>("");
|
|
18
|
+
const [originalContent, setOriginalContent] = useState<string>("");
|
|
19
|
+
const [commitMessage, setCommitMessage] = useState<string>("");
|
|
20
|
+
const [status, setStatus] = useState<SaveStatus>("idle");
|
|
21
|
+
const [statusMessage, setStatusMessage] = useState<string>("");
|
|
22
|
+
|
|
23
|
+
// Load tree
|
|
24
|
+
useEffect(() => {
|
|
25
|
+
fetch("/api/layers")
|
|
26
|
+
.then((r) => r.json())
|
|
27
|
+
.then((data) => {
|
|
28
|
+
setLayers(data.layers ?? []);
|
|
29
|
+
if (data.layers?.length) {
|
|
30
|
+
setOpenLayers(new Set([data.layers[0].slug]));
|
|
31
|
+
}
|
|
32
|
+
})
|
|
33
|
+
.catch(() => setLayers([]));
|
|
34
|
+
}, []);
|
|
35
|
+
|
|
36
|
+
// Load file when active changes
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
if (!activePath) return;
|
|
39
|
+
setStatus("idle");
|
|
40
|
+
setStatusMessage("");
|
|
41
|
+
fetch(`/api/layers/file?path=${encodeURIComponent(activePath)}`)
|
|
42
|
+
.then((r) => r.json())
|
|
43
|
+
.then((data) => {
|
|
44
|
+
setContent(data.content ?? "");
|
|
45
|
+
setOriginalContent(data.content ?? "");
|
|
46
|
+
setCommitMessage(`docs: edit ${activePath}`);
|
|
47
|
+
})
|
|
48
|
+
.catch(() => {
|
|
49
|
+
setContent("");
|
|
50
|
+
setOriginalContent("");
|
|
51
|
+
});
|
|
52
|
+
}, [activePath]);
|
|
53
|
+
|
|
54
|
+
const dirty = content !== originalContent;
|
|
55
|
+
|
|
56
|
+
const handleSave = async () => {
|
|
57
|
+
if (!activePath) return;
|
|
58
|
+
setStatus("saving");
|
|
59
|
+
setStatusMessage("");
|
|
60
|
+
try {
|
|
61
|
+
const res = await fetch("/api/layers/file", {
|
|
62
|
+
method: "POST",
|
|
63
|
+
headers: { "Content-Type": "application/json" },
|
|
64
|
+
body: JSON.stringify({ path: activePath, content, message: commitMessage, commit: true }),
|
|
65
|
+
});
|
|
66
|
+
const data = await res.json();
|
|
67
|
+
if (!res.ok) {
|
|
68
|
+
setStatus("error");
|
|
69
|
+
setStatusMessage(data.error ?? "Save failed");
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
setOriginalContent(content);
|
|
73
|
+
setStatus("success");
|
|
74
|
+
setStatusMessage("Saved + committed");
|
|
75
|
+
setTimeout(() => setStatus("idle"), 2500);
|
|
76
|
+
} catch (e) {
|
|
77
|
+
setStatus("error");
|
|
78
|
+
setStatusMessage(e instanceof Error ? e.message : "Save failed");
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const toggleLayer = (slug: string) => {
|
|
83
|
+
setOpenLayers((prev) => {
|
|
84
|
+
const next = new Set(prev);
|
|
85
|
+
if (next.has(slug)) next.delete(slug);
|
|
86
|
+
else next.add(slug);
|
|
87
|
+
return next;
|
|
88
|
+
});
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
return (
|
|
92
|
+
<div className="size-full flex motion-safe:animate-in motion-safe:fade-in motion-safe:duration-500">
|
|
93
|
+
{/* Sidebar */}
|
|
94
|
+
<aside className="w-72 shrink-0 border-r bg-card">
|
|
95
|
+
<ScrollArea className="size-full">
|
|
96
|
+
<div className="p-3 space-y-1">
|
|
97
|
+
<h2 className="text-sm font-semibold px-2 py-1.5 text-muted-foreground">
|
|
98
|
+
Design system layers
|
|
99
|
+
</h2>
|
|
100
|
+
{layers.map((layer) => {
|
|
101
|
+
const open = openLayers.has(layer.slug);
|
|
102
|
+
return (
|
|
103
|
+
<div key={layer.slug}>
|
|
104
|
+
<button
|
|
105
|
+
type="button"
|
|
106
|
+
onClick={() => toggleLayer(layer.slug)}
|
|
107
|
+
className="w-full flex items-center gap-2 px-2 py-1.5 text-sm rounded hover:bg-accent hover:text-accent-foreground"
|
|
108
|
+
>
|
|
109
|
+
<ChevronRight
|
|
110
|
+
className={`h-3.5 w-3.5 transition-transform ${open ? "rotate-90" : ""}`}
|
|
111
|
+
/>
|
|
112
|
+
<span className="font-medium">{layer.label}</span>
|
|
113
|
+
<span className="ml-auto text-xs text-muted-foreground">{layer.files.length}</span>
|
|
114
|
+
</button>
|
|
115
|
+
{open && (
|
|
116
|
+
<div className="ml-5 border-l border-border">
|
|
117
|
+
{layer.files.map((file) => {
|
|
118
|
+
const fileName = file.split("/").pop() ?? file;
|
|
119
|
+
const subPath = file.slice(layer.slug.length + 1);
|
|
120
|
+
const isActive = activePath === file;
|
|
121
|
+
return (
|
|
122
|
+
<button
|
|
123
|
+
key={file}
|
|
124
|
+
type="button"
|
|
125
|
+
onClick={() => setActivePath(file)}
|
|
126
|
+
className={`w-full flex items-center gap-2 pl-3 pr-2 py-1.5 text-xs rounded text-left ${
|
|
127
|
+
isActive
|
|
128
|
+
? "bg-primary/10 text-foreground"
|
|
129
|
+
: "text-muted-foreground hover:text-foreground hover:bg-accent/40"
|
|
130
|
+
}`}
|
|
131
|
+
>
|
|
132
|
+
<FileText className="h-3 w-3 shrink-0" />
|
|
133
|
+
<span className="truncate" title={subPath}>
|
|
134
|
+
{fileName}
|
|
135
|
+
</span>
|
|
136
|
+
</button>
|
|
137
|
+
);
|
|
138
|
+
})}
|
|
139
|
+
</div>
|
|
140
|
+
)}
|
|
141
|
+
</div>
|
|
142
|
+
);
|
|
143
|
+
})}
|
|
144
|
+
</div>
|
|
145
|
+
</ScrollArea>
|
|
146
|
+
</aside>
|
|
147
|
+
|
|
148
|
+
{/* Editor */}
|
|
149
|
+
<section className="flex-1 flex flex-col min-w-0">
|
|
150
|
+
{!activePath ? (
|
|
151
|
+
<div className="flex-1 flex items-center justify-center text-muted-foreground">
|
|
152
|
+
<div className="text-center">
|
|
153
|
+
<FileText className="h-8 w-8 mx-auto mb-3 opacity-50" />
|
|
154
|
+
<p className="text-sm">Pick a file from the sidebar to edit.</p>
|
|
155
|
+
</div>
|
|
156
|
+
</div>
|
|
157
|
+
) : (
|
|
158
|
+
<>
|
|
159
|
+
<header className="border-b px-6 py-3 flex items-center justify-between gap-4 shrink-0">
|
|
160
|
+
<div className="min-w-0">
|
|
161
|
+
<p className="text-xs text-muted-foreground font-mono truncate">{activePath}</p>
|
|
162
|
+
<h2 className="text-base font-semibold truncate">
|
|
163
|
+
{activePath.split("/").pop()}
|
|
164
|
+
</h2>
|
|
165
|
+
</div>
|
|
166
|
+
{dirty && (
|
|
167
|
+
<span className="text-xs text-muted-foreground shrink-0">Unsaved</span>
|
|
168
|
+
)}
|
|
169
|
+
</header>
|
|
170
|
+
|
|
171
|
+
<Textarea
|
|
172
|
+
value={content}
|
|
173
|
+
onChange={(e) => setContent(e.target.value)}
|
|
174
|
+
className={`flex-1 rounded-none border-0 font-mono text-sm leading-relaxed resize-none focus-visible:ring-0 ${
|
|
175
|
+
dirty || status !== "idle" ? "pb-32" : ""
|
|
176
|
+
}`}
|
|
177
|
+
spellCheck={false}
|
|
178
|
+
/>
|
|
179
|
+
|
|
180
|
+
{(dirty || status !== "idle") && (
|
|
181
|
+
<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">
|
|
182
|
+
<div className="max-w-7xl mx-auto px-8 py-8 flex items-center gap-4">
|
|
183
|
+
<GitCommit className="h-4 w-4 text-muted-foreground shrink-0" />
|
|
184
|
+
<Input
|
|
185
|
+
value={commitMessage}
|
|
186
|
+
onChange={(e) => setCommitMessage(e.target.value)}
|
|
187
|
+
placeholder="commit message"
|
|
188
|
+
className="flex-1"
|
|
189
|
+
/>
|
|
190
|
+
{status === "success" && statusMessage && (
|
|
191
|
+
<span className="flex items-center gap-2 text-sm text-foreground shrink-0">
|
|
192
|
+
<Check className="h-4 w-4 text-primary" />
|
|
193
|
+
<span className="truncate">{statusMessage}</span>
|
|
194
|
+
</span>
|
|
195
|
+
)}
|
|
196
|
+
{status === "error" && statusMessage && (
|
|
197
|
+
<span className="flex items-center gap-2 text-sm text-destructive shrink-0">
|
|
198
|
+
<AlertCircle className="h-4 w-4" />
|
|
199
|
+
<span className="truncate">{statusMessage}</span>
|
|
200
|
+
</span>
|
|
201
|
+
)}
|
|
202
|
+
<Button
|
|
203
|
+
onClick={handleSave}
|
|
204
|
+
disabled={!dirty || status === "saving"}
|
|
205
|
+
size="sm"
|
|
206
|
+
className="shrink-0"
|
|
207
|
+
>
|
|
208
|
+
{status === "saving" ? (
|
|
209
|
+
<>
|
|
210
|
+
<Loader2 className="h-3.5 w-3.5 mr-2 animate-spin" />
|
|
211
|
+
Saving
|
|
212
|
+
</>
|
|
213
|
+
) : (
|
|
214
|
+
"Save & commit"
|
|
215
|
+
)}
|
|
216
|
+
</Button>
|
|
217
|
+
</div>
|
|
218
|
+
</div>
|
|
219
|
+
)}
|
|
220
|
+
</>
|
|
221
|
+
)}
|
|
222
|
+
</section>
|
|
223
|
+
</div>
|
|
224
|
+
);
|
|
225
|
+
}
|
|
@@ -4,7 +4,11 @@ import { cva, type VariantProps } from "class-variance-authority"
|
|
|
4
4
|
import { cn } from "../../lib/utils"
|
|
5
5
|
|
|
6
6
|
const alertVariants = cva(
|
|
7
|
-
|
|
7
|
+
// Icon is explicitly pinned to row 1 / col 1 so it sits alongside the title,
|
|
8
|
+
// not auto-placed somewhere ambiguous. mt-1 nudges its top down ~4px so the
|
|
9
|
+
// icon's vertical center aligns with the title's cap height (text-lg
|
|
10
|
+
// leading-tight ≈ 22px line-box; cap height ≈ 14px from top).
|
|
11
|
+
"relative grid w-full grid-cols-[0_1fr] items-start gap-y-1 rounded-lg border px-6 py-5 text-sm has-[>svg]:grid-cols-[calc(var(--spacing)*4)_1fr] has-[>svg]:gap-x-3 [&>svg]:size-4 [&>svg]:row-start-1 [&>svg]:col-start-1 [&>svg]:mt-1 [&>svg]:text-current",
|
|
8
12
|
{
|
|
9
13
|
variants: {
|
|
10
14
|
variant: {
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useCallback, useEffect, useState } from "react";
|
|
4
|
+
|
|
5
|
+
export type DesignControls = {
|
|
6
|
+
/** Border radius in rem. Maps to --radius. */
|
|
7
|
+
radius: number;
|
|
8
|
+
/** Tailwind v4 base spacing unit in rem. Maps to --spacing. */
|
|
9
|
+
spacing: number;
|
|
10
|
+
/** Letter spacing in em. Applied to documentElement. */
|
|
11
|
+
letterSpacing: number;
|
|
12
|
+
/** Shadow opacity 0..1. Maps to --shadow-opacity. */
|
|
13
|
+
shadowOpacity: number;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export const DESIGN_CONTROL_DEFAULTS: DesignControls = {
|
|
17
|
+
radius: 0.625,
|
|
18
|
+
spacing: 0.25,
|
|
19
|
+
letterSpacing: 0,
|
|
20
|
+
shadowOpacity: 0.1,
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export const DESIGN_CONTROL_RANGES = {
|
|
24
|
+
radius: { min: 0, max: 2, step: 0.025, unit: "rem" },
|
|
25
|
+
spacing: { min: 0.15, max: 0.4, step: 0.005, unit: "rem" },
|
|
26
|
+
letterSpacing: { min: -0.05, max: 0.1, step: 0.005, unit: "em" },
|
|
27
|
+
shadowOpacity: { min: 0, max: 0.5, step: 0.01, unit: "" },
|
|
28
|
+
} as const;
|
|
29
|
+
|
|
30
|
+
const STORAGE_KEY = "BADS.designControls";
|
|
31
|
+
|
|
32
|
+
function applyControls(controls: DesignControls) {
|
|
33
|
+
if (typeof document === "undefined") return;
|
|
34
|
+
const root = document.documentElement.style;
|
|
35
|
+
root.setProperty("--radius", `${controls.radius}rem`);
|
|
36
|
+
root.setProperty("--spacing", `${controls.spacing}rem`);
|
|
37
|
+
root.setProperty("--shadow-opacity", String(controls.shadowOpacity));
|
|
38
|
+
// The Tailwind v4 --tracking-* tokens (in @theme inline) and the
|
|
39
|
+
// unlayered `html, body { letter-spacing: var(--tracking-offset, 0em) }`
|
|
40
|
+
// rule both fold this in, so every text element retracks live.
|
|
41
|
+
root.setProperty("--tracking-offset", `${controls.letterSpacing}em`);
|
|
42
|
+
// Clean up an older boot-time inline that wrote --tracking-normal directly
|
|
43
|
+
// (which would shadow our Tailwind @theme token of the same name).
|
|
44
|
+
root.removeProperty("--tracking-normal");
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function readStored(): DesignControls {
|
|
48
|
+
if (typeof window === "undefined") return DESIGN_CONTROL_DEFAULTS;
|
|
49
|
+
const raw = window.localStorage.getItem(STORAGE_KEY);
|
|
50
|
+
if (!raw) return DESIGN_CONTROL_DEFAULTS;
|
|
51
|
+
try {
|
|
52
|
+
const parsed = JSON.parse(raw);
|
|
53
|
+
return {
|
|
54
|
+
radius: typeof parsed.radius === "number" ? parsed.radius : DESIGN_CONTROL_DEFAULTS.radius,
|
|
55
|
+
spacing: typeof parsed.spacing === "number" ? parsed.spacing : DESIGN_CONTROL_DEFAULTS.spacing,
|
|
56
|
+
letterSpacing:
|
|
57
|
+
typeof parsed.letterSpacing === "number"
|
|
58
|
+
? parsed.letterSpacing
|
|
59
|
+
: DESIGN_CONTROL_DEFAULTS.letterSpacing,
|
|
60
|
+
shadowOpacity:
|
|
61
|
+
typeof parsed.shadowOpacity === "number"
|
|
62
|
+
? parsed.shadowOpacity
|
|
63
|
+
: DESIGN_CONTROL_DEFAULTS.shadowOpacity,
|
|
64
|
+
};
|
|
65
|
+
} catch {
|
|
66
|
+
return DESIGN_CONTROL_DEFAULTS;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Cross-iframe design controls (radius / spacing / letter-spacing / shadow
|
|
72
|
+
* opacity). Same localStorage + storage event pattern as the other global
|
|
73
|
+
* theme hooks.
|
|
74
|
+
*/
|
|
75
|
+
export function useGlobalDesignControls() {
|
|
76
|
+
const [controls, setControlsState] = useState<DesignControls>(DESIGN_CONTROL_DEFAULTS);
|
|
77
|
+
|
|
78
|
+
useEffect(() => {
|
|
79
|
+
if (typeof window === "undefined") return;
|
|
80
|
+
const stored = readStored();
|
|
81
|
+
setControlsState(stored);
|
|
82
|
+
applyControls(stored);
|
|
83
|
+
}, []);
|
|
84
|
+
|
|
85
|
+
useEffect(() => {
|
|
86
|
+
if (typeof window === "undefined") return;
|
|
87
|
+
const onStorage = (e: StorageEvent) => {
|
|
88
|
+
if (e.key !== STORAGE_KEY) return;
|
|
89
|
+
const stored = readStored();
|
|
90
|
+
setControlsState(stored);
|
|
91
|
+
applyControls(stored);
|
|
92
|
+
};
|
|
93
|
+
window.addEventListener("storage", onStorage);
|
|
94
|
+
return () => window.removeEventListener("storage", onStorage);
|
|
95
|
+
}, []);
|
|
96
|
+
|
|
97
|
+
const setControl = useCallback(<K extends keyof DesignControls>(key: K, value: DesignControls[K]) => {
|
|
98
|
+
setControlsState((prev) => {
|
|
99
|
+
const next = { ...prev, [key]: value };
|
|
100
|
+
applyControls(next);
|
|
101
|
+
if (typeof window !== "undefined") {
|
|
102
|
+
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(next));
|
|
103
|
+
}
|
|
104
|
+
return next;
|
|
105
|
+
});
|
|
106
|
+
}, []);
|
|
107
|
+
|
|
108
|
+
const reset = useCallback(() => {
|
|
109
|
+
setControlsState(DESIGN_CONTROL_DEFAULTS);
|
|
110
|
+
applyControls(DESIGN_CONTROL_DEFAULTS);
|
|
111
|
+
if (typeof window !== "undefined") {
|
|
112
|
+
window.localStorage.removeItem(STORAGE_KEY);
|
|
113
|
+
}
|
|
114
|
+
}, []);
|
|
115
|
+
|
|
116
|
+
return { controls, setControl, reset };
|
|
117
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useCallback, useEffect, useState } from "react";
|
|
4
|
+
|
|
5
|
+
export const MONO_FONT_OPTIONS = {
|
|
6
|
+
jetbrains: {
|
|
7
|
+
label: "JetBrains Mono",
|
|
8
|
+
stack: "var(--font-jetbrains), 'JetBrains Mono', ui-monospace, monospace",
|
|
9
|
+
},
|
|
10
|
+
"ibm-plex": {
|
|
11
|
+
label: "IBM Plex Mono",
|
|
12
|
+
stack: "'IBM Plex Mono', ui-monospace, monospace",
|
|
13
|
+
},
|
|
14
|
+
"fira-code": {
|
|
15
|
+
label: "Fira Code",
|
|
16
|
+
stack: "'Fira Code', ui-monospace, monospace",
|
|
17
|
+
},
|
|
18
|
+
system: {
|
|
19
|
+
label: "System Mono",
|
|
20
|
+
stack: "ui-monospace, SFMono-Regular, Menlo, Consolas, 'Liberation Mono', monospace",
|
|
21
|
+
},
|
|
22
|
+
} as const;
|
|
23
|
+
|
|
24
|
+
export type MonoFontKey = keyof typeof MONO_FONT_OPTIONS;
|
|
25
|
+
|
|
26
|
+
const STORAGE_KEY = "BADS.monoFont";
|
|
27
|
+
const DEFAULT: MonoFontKey = "jetbrains";
|
|
28
|
+
|
|
29
|
+
function isValidKey(value: string | null): value is MonoFontKey {
|
|
30
|
+
return !!value && value in MONO_FONT_OPTIONS;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function applyFont(key: MonoFontKey) {
|
|
34
|
+
if (typeof document === "undefined") return;
|
|
35
|
+
document.documentElement.style.setProperty(
|
|
36
|
+
"--font-mono",
|
|
37
|
+
MONO_FONT_OPTIONS[key].stack,
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function useGlobalMonoFont() {
|
|
42
|
+
const [font, setFontState] = useState<MonoFontKey>(DEFAULT);
|
|
43
|
+
|
|
44
|
+
useEffect(() => {
|
|
45
|
+
if (typeof window === "undefined") return;
|
|
46
|
+
const stored = window.localStorage.getItem(STORAGE_KEY);
|
|
47
|
+
const next = isValidKey(stored) ? stored : DEFAULT;
|
|
48
|
+
setFontState(next);
|
|
49
|
+
applyFont(next);
|
|
50
|
+
}, []);
|
|
51
|
+
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
if (typeof window === "undefined") return;
|
|
54
|
+
const onStorage = (e: StorageEvent) => {
|
|
55
|
+
if (e.key !== STORAGE_KEY) return;
|
|
56
|
+
const next = isValidKey(e.newValue) ? e.newValue : DEFAULT;
|
|
57
|
+
setFontState(next);
|
|
58
|
+
applyFont(next);
|
|
59
|
+
};
|
|
60
|
+
window.addEventListener("storage", onStorage);
|
|
61
|
+
return () => window.removeEventListener("storage", onStorage);
|
|
62
|
+
}, []);
|
|
63
|
+
|
|
64
|
+
const setFont = useCallback((next: MonoFontKey) => {
|
|
65
|
+
setFontState(next);
|
|
66
|
+
applyFont(next);
|
|
67
|
+
if (typeof window !== "undefined") {
|
|
68
|
+
window.localStorage.setItem(STORAGE_KEY, next);
|
|
69
|
+
}
|
|
70
|
+
}, []);
|
|
71
|
+
|
|
72
|
+
return { font, setFont };
|
|
73
|
+
}
|