@behavioral-agentic-design-system/core 0.1.5 → 0.2.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 +1 -1
- package/src/components/ui/alert.tsx +2 -2
- package/src/components/ui/button.tsx +3 -0
- package/src/components/ui/checkbox.tsx +2 -2
- package/src/components/ui/dialog.tsx +28 -7
- package/src/components/ui/field.tsx +265 -0
- package/src/components/ui/input.tsx +29 -8
- package/src/components/ui/progress.tsx +6 -1
- package/src/components/ui/radio-group.tsx +2 -2
- package/src/components/ui/resizable.tsx +3 -3
- package/src/components/ui/scroll-area.tsx +4 -4
- package/src/components/ui/sheet.tsx +6 -2
- package/src/components/ui/slider.tsx +7 -2
- package/src/components/ui/switch.tsx +7 -2
- package/src/components/ui/tabs.tsx +69 -2
- package/src/lib/color-name.ts +182 -0
- package/src/lib/theme-editor.ts +213 -0
- package/src/styles.css +222 -77
- package/src/tokens.default.json +984 -0
- package/src/tokens.json +188 -90
|
@@ -64,7 +64,7 @@ function TabsTrigger({
|
|
|
64
64
|
<TabsPrimitive.Trigger
|
|
65
65
|
data-slot="tabs-trigger"
|
|
66
66
|
className={cn(
|
|
67
|
-
"inline-flex items-center justify-center whitespace-nowrap rounded-sm px-3 py-1.5 text-xs font-medium text-muted-foreground hover:text-foreground ring-offset-background transition-all focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 data-[state=active]:bg-
|
|
67
|
+
"inline-flex items-center justify-center whitespace-nowrap rounded-sm px-3 py-1.5 text-xs font-medium text-muted-foreground hover:text-foreground ring-offset-background transition-all focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 data-[state=active]:bg-accent data-[state=active]:text-accent-foreground data-[state=active]:shadow-xs",
|
|
68
68
|
className
|
|
69
69
|
)}
|
|
70
70
|
{...props}
|
|
@@ -85,4 +85,71 @@ function TabsContent({
|
|
|
85
85
|
)
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
-
|
|
88
|
+
/**
|
|
89
|
+
* Cross-fading panel container for app-shell tabs.
|
|
90
|
+
*
|
|
91
|
+
* Wraps a set of `<TabsContent forceMount>` siblings, stacks them inside its
|
|
92
|
+
* positioned ancestor, and animates between them via opacity instead of
|
|
93
|
+
* unmount/remount. Use this whenever switching tabs would otherwise cause a
|
|
94
|
+
* visible flash — most commonly for iframes, charts, or anything with a
|
|
95
|
+
* non-trivial initial render. Pair with `forceMount` on every TabsContent
|
|
96
|
+
* inside (otherwise Radix unmounts inactive ones and the cross-fade has
|
|
97
|
+
* nothing to fade between).
|
|
98
|
+
*
|
|
99
|
+
* Layout requirement:
|
|
100
|
+
* TabsPanels itself is `position: absolute; inset: 0`, so it must live
|
|
101
|
+
* inside a `position: relative` container with explicit dimensions
|
|
102
|
+
* (typically a flex-1 section). Percentage heights on flex children are
|
|
103
|
+
* unreliable across browsers; absolute-fill of an explicit-size relative
|
|
104
|
+
* ancestor is the bulletproof pattern.
|
|
105
|
+
*
|
|
106
|
+
* Behavior:
|
|
107
|
+
* - Inactive panels: opacity 0, pointer-events: none.
|
|
108
|
+
* - Active panel: opacity 1, full pointer events.
|
|
109
|
+
* - All panels fill the same box (absolute inset-0), so no layout shift.
|
|
110
|
+
* - Transition uses --tabs-fade-duration (default 180ms, ease-out) so
|
|
111
|
+
* consumers can override timing per-app via CSS.
|
|
112
|
+
*
|
|
113
|
+
* For static-content tabs that don't have render cost, the plain
|
|
114
|
+
* `TabsContent` (without TabsPanels) is fine.
|
|
115
|
+
*/
|
|
116
|
+
function TabsPanels({
|
|
117
|
+
className,
|
|
118
|
+
children,
|
|
119
|
+
...props
|
|
120
|
+
}: React.ComponentProps<"div">) {
|
|
121
|
+
return (
|
|
122
|
+
<div
|
|
123
|
+
data-slot="tabs-panels"
|
|
124
|
+
className={cn(
|
|
125
|
+
"absolute inset-0",
|
|
126
|
+
// Every TabsContent child becomes an absolute, fading layer that
|
|
127
|
+
// fills the panels container.
|
|
128
|
+
"[&>[data-slot=tabs-content]]:absolute",
|
|
129
|
+
"[&>[data-slot=tabs-content]]:inset-0",
|
|
130
|
+
"[&>[data-slot=tabs-content]]:m-0",
|
|
131
|
+
"[&>[data-slot=tabs-content]]:transition-opacity",
|
|
132
|
+
"[&>[data-slot=tabs-content]]:duration-[var(--tabs-fade-duration,180ms)]",
|
|
133
|
+
"[&>[data-slot=tabs-content]]:ease-out",
|
|
134
|
+
// Inactive: invisible and untouchable, but still in the DOM so
|
|
135
|
+
// iframes/charts/etc don't reload on every tab switch.
|
|
136
|
+
"[&>[data-slot=tabs-content][data-state=inactive]]:opacity-0",
|
|
137
|
+
"[&>[data-slot=tabs-content][data-state=inactive]]:pointer-events-none",
|
|
138
|
+
"[&>[data-slot=tabs-content][data-state=active]]:opacity-100",
|
|
139
|
+
className
|
|
140
|
+
)}
|
|
141
|
+
{...props}
|
|
142
|
+
>
|
|
143
|
+
{children}
|
|
144
|
+
</div>
|
|
145
|
+
)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export {
|
|
149
|
+
Tabs,
|
|
150
|
+
TabsList,
|
|
151
|
+
TabsTrigger,
|
|
152
|
+
TabsContent,
|
|
153
|
+
TabsPanels,
|
|
154
|
+
tabsListVariants,
|
|
155
|
+
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* hex → human-readable name. Looks up against the CSS named color list and
|
|
3
|
+
* returns the perceptually closest match. "#FF0000" → "Red", "#6495ED" →
|
|
4
|
+
* "Cornflower Blue", "#3A1F0B" → "Dark Brown" (closest is "SaddleBrown" → "Saddle Brown").
|
|
5
|
+
*
|
|
6
|
+
* Distance is in linear-RGB. Not as perceptually accurate as Lab, but Lab is
|
|
7
|
+
* overkill for naming — names are categorical, not gradient-precise.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
type NamedColor = { name: string; hex: string };
|
|
11
|
+
|
|
12
|
+
// CSS named colors, title-cased. Sorted roughly by hue family for deterministic
|
|
13
|
+
// nearest-match ordering when distances tie. The picker resolves any 6-digit
|
|
14
|
+
// hex to one of these.
|
|
15
|
+
const NAMED_COLORS: NamedColor[] = [
|
|
16
|
+
// reds / pinks
|
|
17
|
+
{ name: "Maroon", hex: "#800000" },
|
|
18
|
+
{ name: "Dark Red", hex: "#8B0000" },
|
|
19
|
+
{ name: "Brown", hex: "#A52A2A" },
|
|
20
|
+
{ name: "Firebrick", hex: "#B22222" },
|
|
21
|
+
{ name: "Crimson", hex: "#DC143C" },
|
|
22
|
+
{ name: "Red", hex: "#FF0000" },
|
|
23
|
+
{ name: "Tomato", hex: "#FF6347" },
|
|
24
|
+
{ name: "Coral", hex: "#FF7F50" },
|
|
25
|
+
{ name: "Indian Red", hex: "#CD5C5C" },
|
|
26
|
+
{ name: "Light Coral", hex: "#F08080" },
|
|
27
|
+
{ name: "Salmon", hex: "#FA8072" },
|
|
28
|
+
{ name: "Light Salmon", hex: "#FFA07A" },
|
|
29
|
+
{ name: "Dark Salmon", hex: "#E9967A" },
|
|
30
|
+
{ name: "Pink", hex: "#FFC0CB" },
|
|
31
|
+
{ name: "Light Pink", hex: "#FFB6C1" },
|
|
32
|
+
{ name: "Hot Pink", hex: "#FF69B4" },
|
|
33
|
+
{ name: "Deep Pink", hex: "#FF1493" },
|
|
34
|
+
{ name: "Pale Violet Red", hex: "#DB7093" },
|
|
35
|
+
{ name: "Medium Violet Red", hex: "#C71585" },
|
|
36
|
+
// oranges
|
|
37
|
+
{ name: "Dark Orange", hex: "#FF8C00" },
|
|
38
|
+
{ name: "Orange", hex: "#FFA500" },
|
|
39
|
+
{ name: "Orange Red", hex: "#FF4500" },
|
|
40
|
+
{ name: "Peach Puff", hex: "#FFDAB9" },
|
|
41
|
+
{ name: "Bisque", hex: "#FFE4C4" },
|
|
42
|
+
{ name: "Moccasin", hex: "#FFE4B5" },
|
|
43
|
+
{ name: "Navajo White", hex: "#FFDEAD" },
|
|
44
|
+
{ name: "Papaya Whip", hex: "#FFEFD5" },
|
|
45
|
+
{ name: "Antique White", hex: "#FAEBD7" },
|
|
46
|
+
// yellows
|
|
47
|
+
{ name: "Gold", hex: "#FFD700" },
|
|
48
|
+
{ name: "Yellow", hex: "#FFFF00" },
|
|
49
|
+
{ name: "Light Yellow", hex: "#FFFFE0" },
|
|
50
|
+
{ name: "Lemon Chiffon", hex: "#FFFACD" },
|
|
51
|
+
{ name: "Pale Goldenrod", hex: "#EEE8AA" },
|
|
52
|
+
{ name: "Khaki", hex: "#F0E68C" },
|
|
53
|
+
{ name: "Dark Khaki", hex: "#BDB76B" },
|
|
54
|
+
{ name: "Goldenrod", hex: "#DAA520" },
|
|
55
|
+
{ name: "Dark Goldenrod", hex: "#B8860B" },
|
|
56
|
+
{ name: "Olive", hex: "#808000" },
|
|
57
|
+
{ name: "Olive Drab", hex: "#6B8E23" },
|
|
58
|
+
{ name: "Dark Olive Green", hex: "#556B2F" },
|
|
59
|
+
// greens
|
|
60
|
+
{ name: "Yellow Green", hex: "#9ACD32" },
|
|
61
|
+
{ name: "Green Yellow", hex: "#ADFF2F" },
|
|
62
|
+
{ name: "Chartreuse", hex: "#7FFF00" },
|
|
63
|
+
{ name: "Lawn Green", hex: "#7CFC00" },
|
|
64
|
+
{ name: "Lime", hex: "#00FF00" },
|
|
65
|
+
{ name: "Lime Green", hex: "#32CD32" },
|
|
66
|
+
{ name: "Light Green", hex: "#90EE90" },
|
|
67
|
+
{ name: "Pale Green", hex: "#98FB98" },
|
|
68
|
+
{ name: "Dark Sea Green", hex: "#8FBC8F" },
|
|
69
|
+
{ name: "Medium Spring Green", hex: "#00FA9A" },
|
|
70
|
+
{ name: "Spring Green", hex: "#00FF7F" },
|
|
71
|
+
{ name: "Sea Green", hex: "#2E8B57" },
|
|
72
|
+
{ name: "Medium Sea Green", hex: "#3CB371" },
|
|
73
|
+
{ name: "Forest Green", hex: "#228B22" },
|
|
74
|
+
{ name: "Green", hex: "#008000" },
|
|
75
|
+
{ name: "Dark Green", hex: "#006400" },
|
|
76
|
+
// teals / cyans
|
|
77
|
+
{ name: "Teal", hex: "#008080" },
|
|
78
|
+
{ name: "Dark Cyan", hex: "#008B8B" },
|
|
79
|
+
{ name: "Light Sea Green", hex: "#20B2AA" },
|
|
80
|
+
{ name: "Cadet Blue", hex: "#5F9EA0" },
|
|
81
|
+
{ name: "Dark Turquoise", hex: "#00CED1" },
|
|
82
|
+
{ name: "Medium Turquoise", hex: "#48D1CC" },
|
|
83
|
+
{ name: "Turquoise", hex: "#40E0D0" },
|
|
84
|
+
{ name: "Aqua", hex: "#00FFFF" },
|
|
85
|
+
{ name: "Aquamarine", hex: "#7FFFD4" },
|
|
86
|
+
{ name: "Pale Turquoise", hex: "#AFEEEE" },
|
|
87
|
+
{ name: "Light Cyan", hex: "#E0FFFF" },
|
|
88
|
+
// blues
|
|
89
|
+
{ name: "Powder Blue", hex: "#B0E0E6" },
|
|
90
|
+
{ name: "Light Blue", hex: "#ADD8E6" },
|
|
91
|
+
{ name: "Sky Blue", hex: "#87CEEB" },
|
|
92
|
+
{ name: "Light Sky Blue", hex: "#87CEFA" },
|
|
93
|
+
{ name: "Deep Sky Blue", hex: "#00BFFF" },
|
|
94
|
+
{ name: "Dodger Blue", hex: "#1E90FF" },
|
|
95
|
+
{ name: "Cornflower Blue", hex: "#6495ED" },
|
|
96
|
+
{ name: "Steel Blue", hex: "#4682B4" },
|
|
97
|
+
{ name: "Royal Blue", hex: "#4169E1" },
|
|
98
|
+
{ name: "Blue", hex: "#0000FF" },
|
|
99
|
+
{ name: "Medium Blue", hex: "#0000CD" },
|
|
100
|
+
{ name: "Dark Blue", hex: "#00008B" },
|
|
101
|
+
{ name: "Navy", hex: "#000080" },
|
|
102
|
+
{ name: "Midnight Blue", hex: "#191970" },
|
|
103
|
+
{ name: "Slate Blue", hex: "#6A5ACD" },
|
|
104
|
+
{ name: "Dark Slate Blue", hex: "#483D8B" },
|
|
105
|
+
{ name: "Medium Slate Blue", hex: "#7B68EE" },
|
|
106
|
+
// purples / magentas
|
|
107
|
+
{ name: "Indigo", hex: "#4B0082" },
|
|
108
|
+
{ name: "Purple", hex: "#800080" },
|
|
109
|
+
{ name: "Dark Magenta", hex: "#8B008B" },
|
|
110
|
+
{ name: "Dark Violet", hex: "#9400D3" },
|
|
111
|
+
{ name: "Dark Orchid", hex: "#9932CC" },
|
|
112
|
+
{ name: "Blue Violet", hex: "#8A2BE2" },
|
|
113
|
+
{ name: "Medium Purple", hex: "#9370DB" },
|
|
114
|
+
{ name: "Medium Orchid", hex: "#BA55D3" },
|
|
115
|
+
{ name: "Magenta", hex: "#FF00FF" },
|
|
116
|
+
{ name: "Violet", hex: "#EE82EE" },
|
|
117
|
+
{ name: "Plum", hex: "#DDA0DD" },
|
|
118
|
+
{ name: "Orchid", hex: "#DA70D6" },
|
|
119
|
+
{ name: "Thistle", hex: "#D8BFD8" },
|
|
120
|
+
{ name: "Lavender", hex: "#E6E6FA" },
|
|
121
|
+
// browns
|
|
122
|
+
{ name: "Saddle Brown", hex: "#8B4513" },
|
|
123
|
+
{ name: "Sienna", hex: "#A0522D" },
|
|
124
|
+
{ name: "Chocolate", hex: "#D2691E" },
|
|
125
|
+
{ name: "Peru", hex: "#CD853F" },
|
|
126
|
+
{ name: "Sandy Brown", hex: "#F4A460" },
|
|
127
|
+
{ name: "Burly Wood", hex: "#DEB887" },
|
|
128
|
+
{ name: "Tan", hex: "#D2B48C" },
|
|
129
|
+
{ name: "Rosy Brown", hex: "#BC8F8F" },
|
|
130
|
+
{ name: "Wheat", hex: "#F5DEB3" },
|
|
131
|
+
// grays / neutrals
|
|
132
|
+
{ name: "Black", hex: "#000000" },
|
|
133
|
+
{ name: "Dim Gray", hex: "#696969" },
|
|
134
|
+
{ name: "Gray", hex: "#808080" },
|
|
135
|
+
{ name: "Dark Gray", hex: "#A9A9A9" },
|
|
136
|
+
{ name: "Silver", hex: "#C0C0C0" },
|
|
137
|
+
{ name: "Light Gray", hex: "#D3D3D3" },
|
|
138
|
+
{ name: "Gainsboro", hex: "#DCDCDC" },
|
|
139
|
+
{ name: "White Smoke", hex: "#F5F5F5" },
|
|
140
|
+
{ name: "White", hex: "#FFFFFF" },
|
|
141
|
+
{ name: "Slate Gray", hex: "#708090" },
|
|
142
|
+
{ name: "Light Slate Gray", hex: "#778899" },
|
|
143
|
+
{ name: "Dark Slate Gray", hex: "#2F4F4F" },
|
|
144
|
+
];
|
|
145
|
+
|
|
146
|
+
function rgbFromHex(hex: string): [number, number, number] {
|
|
147
|
+
const h = hex.replace("#", "");
|
|
148
|
+
const expanded =
|
|
149
|
+
h.length === 3
|
|
150
|
+
? h.split("").map((c) => c + c).join("")
|
|
151
|
+
: h;
|
|
152
|
+
return [
|
|
153
|
+
parseInt(expanded.slice(0, 2), 16),
|
|
154
|
+
parseInt(expanded.slice(2, 4), 16),
|
|
155
|
+
parseInt(expanded.slice(4, 6), 16),
|
|
156
|
+
];
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function distSquared(a: [number, number, number], b: [number, number, number]): number {
|
|
160
|
+
return (a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2 + (a[2] - b[2]) ** 2;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Returns the closest CSS named color for the given hex. Title-cased.
|
|
165
|
+
* Examples:
|
|
166
|
+
* "#FF0000" → "Red"
|
|
167
|
+
* "#6495ED" → "Cornflower Blue"
|
|
168
|
+
* "#3A1F0B" → "Saddle Brown"
|
|
169
|
+
*/
|
|
170
|
+
export function nameFromHex(hex: string): string {
|
|
171
|
+
const target = rgbFromHex(hex);
|
|
172
|
+
let best: NamedColor = NAMED_COLORS[0];
|
|
173
|
+
let bestDist = Infinity;
|
|
174
|
+
for (const candidate of NAMED_COLORS) {
|
|
175
|
+
const d = distSquared(target, rgbFromHex(candidate.hex));
|
|
176
|
+
if (d < bestDist) {
|
|
177
|
+
bestDist = d;
|
|
178
|
+
best = candidate;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return best.name;
|
|
182
|
+
}
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import tokens from "../tokens.json";
|
|
2
|
+
|
|
3
|
+
export const THEME_EDITOR_STORAGE_KEY = "BADS.themeEditorState";
|
|
4
|
+
export const THEME_EDITOR_STYLE_ID = "bads-live-theme-editor";
|
|
5
|
+
|
|
6
|
+
export type ThemeEditorMode = "light" | "dark";
|
|
7
|
+
|
|
8
|
+
export const THEME_COLOR_KEYS = [
|
|
9
|
+
"background",
|
|
10
|
+
"foreground",
|
|
11
|
+
"card",
|
|
12
|
+
"card-foreground",
|
|
13
|
+
"popover",
|
|
14
|
+
"popover-foreground",
|
|
15
|
+
"primary",
|
|
16
|
+
"primary-foreground",
|
|
17
|
+
"secondary",
|
|
18
|
+
"secondary-foreground",
|
|
19
|
+
"muted",
|
|
20
|
+
"muted-foreground",
|
|
21
|
+
"accent",
|
|
22
|
+
"accent-foreground",
|
|
23
|
+
"destructive",
|
|
24
|
+
"destructive-foreground",
|
|
25
|
+
"border",
|
|
26
|
+
"input",
|
|
27
|
+
"ring",
|
|
28
|
+
"chart-1",
|
|
29
|
+
"chart-2",
|
|
30
|
+
"chart-3",
|
|
31
|
+
"chart-4",
|
|
32
|
+
"chart-5",
|
|
33
|
+
"sidebar",
|
|
34
|
+
"sidebar-foreground",
|
|
35
|
+
"sidebar-primary",
|
|
36
|
+
"sidebar-primary-foreground",
|
|
37
|
+
"sidebar-accent",
|
|
38
|
+
"sidebar-accent-foreground",
|
|
39
|
+
"sidebar-border",
|
|
40
|
+
"sidebar-ring",
|
|
41
|
+
] as const;
|
|
42
|
+
|
|
43
|
+
export type ThemeColorKey = (typeof THEME_COLOR_KEYS)[number];
|
|
44
|
+
|
|
45
|
+
export type ThemeEditorCommon = {
|
|
46
|
+
fontSans: string;
|
|
47
|
+
fontSerif: string;
|
|
48
|
+
fontMono: string;
|
|
49
|
+
radius: string;
|
|
50
|
+
spacing: string;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export type ThemeEditorState = {
|
|
54
|
+
light: Record<ThemeColorKey, string>;
|
|
55
|
+
dark: Record<ThemeColorKey, string>;
|
|
56
|
+
common: ThemeEditorCommon;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
type TokenRecord = Record<string, { $value?: string }>;
|
|
60
|
+
|
|
61
|
+
const tokenData = tokens as {
|
|
62
|
+
theme: { light: TokenRecord; dark: TokenRecord };
|
|
63
|
+
fontFamily: { sans: { $value: string }; serif: { $value: string }; mono: { $value: string } };
|
|
64
|
+
borderRadius: { lg: { $value: string } };
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
function readThemeMode(mode: ThemeEditorMode): Record<ThemeColorKey, string> {
|
|
68
|
+
return Object.fromEntries(
|
|
69
|
+
THEME_COLOR_KEYS.map((key) => [key, tokenData.theme[mode][key]?.$value ?? ""])
|
|
70
|
+
) as Record<ThemeColorKey, string>;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export const DEFAULT_THEME_EDITOR_STATE: ThemeEditorState = {
|
|
74
|
+
light: readThemeMode("light"),
|
|
75
|
+
dark: readThemeMode("dark"),
|
|
76
|
+
common: {
|
|
77
|
+
fontSans: tokenData.fontFamily.sans.$value,
|
|
78
|
+
fontSerif: tokenData.fontFamily.serif.$value,
|
|
79
|
+
fontMono: tokenData.fontFamily.mono.$value,
|
|
80
|
+
radius: tokenData.borderRadius.lg.$value,
|
|
81
|
+
spacing: tokenData.theme.light.spacing?.$value ?? "0.25rem",
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
export function isThemeEditorState(value: unknown): value is ThemeEditorState {
|
|
86
|
+
if (!value || typeof value !== "object") return false;
|
|
87
|
+
const candidate = value as Partial<ThemeEditorState>;
|
|
88
|
+
if (!candidate.light || !candidate.dark || !candidate.common) return false;
|
|
89
|
+
|
|
90
|
+
const hasColors = (mode: Partial<Record<ThemeColorKey, unknown>>) =>
|
|
91
|
+
THEME_COLOR_KEYS.every((key) => typeof mode[key] === "string");
|
|
92
|
+
|
|
93
|
+
return (
|
|
94
|
+
hasColors(candidate.light) &&
|
|
95
|
+
hasColors(candidate.dark) &&
|
|
96
|
+
typeof candidate.common.fontSans === "string" &&
|
|
97
|
+
typeof candidate.common.fontSerif === "string" &&
|
|
98
|
+
typeof candidate.common.fontMono === "string" &&
|
|
99
|
+
typeof candidate.common.radius === "string" &&
|
|
100
|
+
typeof candidate.common.spacing === "string"
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function readStoredThemeEditorState(): ThemeEditorState {
|
|
105
|
+
if (typeof window === "undefined") return DEFAULT_THEME_EDITOR_STATE;
|
|
106
|
+
|
|
107
|
+
try {
|
|
108
|
+
const raw = window.localStorage.getItem(THEME_EDITOR_STORAGE_KEY);
|
|
109
|
+
if (!raw) return DEFAULT_THEME_EDITOR_STATE;
|
|
110
|
+
|
|
111
|
+
const parsed = JSON.parse(raw);
|
|
112
|
+
if (isThemeEditorState(parsed)) {
|
|
113
|
+
return parsed;
|
|
114
|
+
}
|
|
115
|
+
} catch {
|
|
116
|
+
return DEFAULT_THEME_EDITOR_STATE;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return DEFAULT_THEME_EDITOR_STATE;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function emitVariables(values: Record<string, string>) {
|
|
123
|
+
return Object.entries(values)
|
|
124
|
+
.filter(([, value]) => value.trim().length > 0)
|
|
125
|
+
.map(([key, value]) => ` --${key}: ${value};`)
|
|
126
|
+
.join("\n");
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function themeEditorStateToCss(state: ThemeEditorState) {
|
|
130
|
+
const common = {
|
|
131
|
+
"font-sans": state.common.fontSans,
|
|
132
|
+
"font-serif": state.common.fontSerif,
|
|
133
|
+
"font-mono": state.common.fontMono,
|
|
134
|
+
radius: state.common.radius,
|
|
135
|
+
spacing: state.common.spacing,
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
return `:root {
|
|
139
|
+
${emitVariables({ ...common, ...state.light })}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
.dark {
|
|
143
|
+
${emitVariables(state.dark)}
|
|
144
|
+
}`;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function clearManagedInlineThemeProperties(root: HTMLElement) {
|
|
148
|
+
for (const key of THEME_COLOR_KEYS) {
|
|
149
|
+
root.style.removeProperty(`--${key}`);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
root.style.removeProperty("--font-sans");
|
|
153
|
+
root.style.removeProperty("--font-serif");
|
|
154
|
+
root.style.removeProperty("--font-mono");
|
|
155
|
+
root.style.removeProperty("--radius");
|
|
156
|
+
root.style.removeProperty("--spacing");
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function writeStoredThemeEditorState(state: ThemeEditorState) {
|
|
160
|
+
if (typeof window === "undefined") return;
|
|
161
|
+
window.localStorage.setItem(THEME_EDITOR_STORAGE_KEY, JSON.stringify(state));
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const COLOR_KEY_SET = new Set<string>(THEME_COLOR_KEYS);
|
|
165
|
+
|
|
166
|
+
export function parseCssToThemeEditorState(css: string): ThemeEditorState {
|
|
167
|
+
const next: ThemeEditorState = {
|
|
168
|
+
light: { ...DEFAULT_THEME_EDITOR_STATE.light },
|
|
169
|
+
dark: { ...DEFAULT_THEME_EDITOR_STATE.dark },
|
|
170
|
+
common: { ...DEFAULT_THEME_EDITOR_STATE.common },
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
const blockRe = /(:root|\.dark)\s*\{([^}]*)\}/g;
|
|
174
|
+
let match;
|
|
175
|
+
while ((match = blockRe.exec(css)) !== null) {
|
|
176
|
+
const selector = match[1];
|
|
177
|
+
const body = match[2];
|
|
178
|
+
const declRe = /--([\w-]+)\s*:\s*([^;]+);/g;
|
|
179
|
+
let decl;
|
|
180
|
+
while ((decl = declRe.exec(body)) !== null) {
|
|
181
|
+
const key = decl[1];
|
|
182
|
+
const value = decl[2].trim();
|
|
183
|
+
if (selector === ":root") {
|
|
184
|
+
if (COLOR_KEY_SET.has(key)) {
|
|
185
|
+
next.light[key as ThemeColorKey] = value;
|
|
186
|
+
} else if (key === "font-sans") next.common.fontSans = value;
|
|
187
|
+
else if (key === "font-serif") next.common.fontSerif = value;
|
|
188
|
+
else if (key === "font-mono") next.common.fontMono = value;
|
|
189
|
+
else if (key === "radius") next.common.radius = value;
|
|
190
|
+
else if (key === "spacing") next.common.spacing = value;
|
|
191
|
+
} else if (selector === ".dark") {
|
|
192
|
+
if (COLOR_KEY_SET.has(key)) {
|
|
193
|
+
next.dark[key as ThemeColorKey] = value;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
return next;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export function applyThemeEditorState(state: ThemeEditorState, doc: Document = document) {
|
|
203
|
+
const style =
|
|
204
|
+
doc.getElementById(THEME_EDITOR_STYLE_ID) ??
|
|
205
|
+
Object.assign(doc.createElement("style"), { id: THEME_EDITOR_STYLE_ID });
|
|
206
|
+
|
|
207
|
+
style.textContent = themeEditorStateToCss(state);
|
|
208
|
+
if (!style.parentNode) {
|
|
209
|
+
doc.head.appendChild(style);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
clearManagedInlineThemeProperties(doc.documentElement);
|
|
213
|
+
}
|