@behavioral-agentic-design-system/core 0.42.0 → 0.45.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/LICENSE +21 -0
- package/package.json +25 -9
- package/scripts/a11y-lint.cjs +25 -1
- package/scripts/check-spec.mjs +73 -0
- package/scripts/deal-directions.mjs +186 -0
- package/scripts/health-report.mjs +20 -14
- package/scripts/lib/counted-lint.mjs +100 -0
- package/scripts/lib/marketing-library.mjs +129 -0
- package/scripts/lib/on-system.mjs +249 -0
- package/scripts/lib/random.mjs +22 -0
- package/scripts/lib/spec-parse.d.mts +25 -0
- package/scripts/lib/spec-parse.mjs +188 -0
- package/scripts/lib/tells.mjs +128 -0
- package/scripts/lint-css-vars.mjs +175 -0
- package/scripts/lint-html-nesting.mjs +116 -0
- package/scripts/lint-live-ctas.mjs +138 -0
- package/scripts/on-system-score.mjs +95 -0
- package/scripts/slop-rules.mjs +1 -1
- package/scripts/source-images.mjs +180 -0
- package/scripts/ux-tells.mjs +78 -0
- package/src/api/brand-colors.ts +3 -0
- package/src/api/brand-fonts.ts +3 -0
- package/src/api/brand-mark.ts +6 -1
- package/src/api/dev-write-guard.ts +53 -0
- package/src/api/layers/file.ts +7 -4
- package/src/api/onboarding-extract.ts +85 -30
- package/src/api/onboarding.ts +3 -0
- package/src/api/save-tokens.ts +3 -0
- package/src/app/layout.tsx +6 -0
- package/src/app/preview/canvas/page.tsx +12 -0
- package/src/app/preview/components/page.tsx +734 -0
- package/src/app/preview/studio/blank-screen.tsx +143 -0
- package/src/app/preview/studio/page.tsx +7 -0
- package/src/app/shell.tsx +160 -44
- package/src/components/canvas/canvas.tsx +458 -0
- package/src/components/canvas/layout.ts +72 -0
- package/src/components/canvas/parse-canvas.ts +32 -0
- package/src/components/canvas/sample.ts +90 -0
- package/src/components/canvas/spec-frame.tsx +43 -0
- package/src/components/canvas/types.ts +33 -0
- package/src/components/docs-tab.tsx +7 -7
- package/src/components/home-recent.tsx +73 -0
- package/src/components/home-tab.tsx +13 -13
- package/src/components/layers-tab.tsx +2 -2
- package/src/components/markdown-preview.tsx +60 -12
- package/src/components/studio/control-panel.tsx +299 -0
- package/src/components/studio/control-row.tsx +77 -0
- package/src/components/studio/preview-frame.tsx +165 -0
- package/src/components/studio/preview-pane.tsx +247 -0
- package/src/components/studio/studio.tsx +133 -128
- package/src/components/studio/types.ts +218 -0
- package/src/components/studio/use-studio-tweaks.ts +128 -0
- package/src/components/studio/use-studio-view-state.ts +72 -0
- package/src/components/ui/autocomplete.tsx +1 -1
- package/src/components/ui/color-picker.tsx +20 -4
- package/src/components/ui/data-table.tsx +64 -3
- package/src/components/ui/date-field.tsx +1 -1
- package/src/components/ui/editable.tsx +1 -0
- package/src/components/ui/faceted-filter.tsx +187 -0
- package/src/components/ui/gauge-chart.tsx +18 -4
- package/src/components/ui/inline-edit.tsx +175 -0
- package/src/components/ui/month-picker.tsx +1 -1
- package/src/components/ui/number-input.tsx +28 -5
- package/src/components/ui/otp-input.tsx +208 -0
- package/src/components/ui/phone-input.tsx +274 -0
- package/src/components/ui/search-input.tsx +1 -1
- package/src/components/ui/tags-input.tsx +1 -1
- package/src/components/ui/timer.tsx +1 -0
- package/src/components/ui/transfer-list.tsx +13 -26
- package/src/components/ui/week-picker.tsx +1 -1
- package/src/components/ui/year-picker.tsx +1 -1
- package/src/data/component-added.json +958 -0
- package/src/data/docs-index.ts +13 -1
- package/src/hooks/use-global-mono-font.ts +1 -0
- package/src/hooks/use-global-primary-color.ts +1 -0
- package/src/hooks/use-global-sans-font.ts +1 -0
- package/src/hooks/use-global-serif-font.ts +1 -0
- package/src/hooks/use-global-type-scale.ts +17 -2
- package/src/hooks/use-mobile.ts +2 -0
- package/src/hooks/use-sync-type-scale.ts +7 -11
- package/src/lib/spec-parse.ts +4 -0
- package/src/lib/svg-sanitize.ts +19 -9
- package/src/stories/Autocomplete.stories.tsx +2 -0
- package/src/stories/Canvas.stories.tsx +45 -0
- package/src/stories/ColorPicker.stories.tsx +40 -0
- package/src/stories/DataTable.stories.tsx +30 -0
- package/src/stories/DateField.stories.tsx +21 -0
- package/src/stories/Editable.stories.tsx +2 -0
- package/src/stories/FacetedFilter.stories.tsx +197 -0
- package/src/stories/GaugeChart.stories.tsx +9 -3
- package/src/stories/InlineEdit.stories.tsx +106 -0
- package/src/stories/MarkdownPreview.stories.tsx +53 -0
- package/src/stories/MonthPicker.stories.tsx +21 -0
- package/src/stories/NumberInput.stories.tsx +25 -0
- package/src/stories/OtpInput.stories.tsx +110 -0
- package/src/stories/PhoneInput.stories.tsx +127 -0
- package/src/stories/SearchInput.stories.tsx +2 -0
- package/src/stories/TagsInput.stories.tsx +2 -0
- package/src/stories/Timer.stories.tsx +26 -0
- package/src/stories/TransferList.stories.tsx +4 -0
- package/src/stories/WeekPicker.stories.tsx +21 -0
- package/src/stories/YearPicker.stories.tsx +21 -0
- package/src/stories/story-groups.json +22 -16
- package/src/tokens.json +82 -54
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared contract for the Studio screen. Pure TypeScript: no React, no DOM.
|
|
3
|
+
* The view-state, hook, control-panel and preview packages all import from here
|
|
4
|
+
* so they agree on names, URL keys, ranges, and the 14px type floor.
|
|
5
|
+
*/
|
|
6
|
+
import {
|
|
7
|
+
DESIGN_CONTROL_DEFAULTS,
|
|
8
|
+
DESIGN_CONTROL_RANGES,
|
|
9
|
+
} from "@behavioral-agentic-design-system/core/hooks/use-global-design-controls";
|
|
10
|
+
import {
|
|
11
|
+
TYPE_SCALE_DEFAULTS,
|
|
12
|
+
TYPE_SCALE_RANGES,
|
|
13
|
+
type TypeScale,
|
|
14
|
+
} from "@behavioral-agentic-design-system/core/hooks/use-global-type-scale";
|
|
15
|
+
|
|
16
|
+
/* ── View state: lives in the URL ─────────────────────────────── */
|
|
17
|
+
|
|
18
|
+
export type StudioSubject = "blank" | "cards" | "dashboard";
|
|
19
|
+
export type StudioViewport = "fill" | "390" | "768" | "1280";
|
|
20
|
+
export type StudioPreviewTheme = "match" | "light" | "dark" | "both";
|
|
21
|
+
export type StudioPanelMode = "tweak" | "tokens";
|
|
22
|
+
|
|
23
|
+
/** The single source of truth for everything Studio shows that is NOT a token. */
|
|
24
|
+
export interface StudioViewState {
|
|
25
|
+
subject: StudioSubject;
|
|
26
|
+
viewport: StudioViewport;
|
|
27
|
+
theme: StudioPreviewTheme;
|
|
28
|
+
panel: StudioPanelMode;
|
|
29
|
+
controlsCollapsed: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export const STUDIO_VIEW_DEFAULTS: StudioViewState = {
|
|
33
|
+
subject: "blank",
|
|
34
|
+
viewport: "fill",
|
|
35
|
+
theme: "match",
|
|
36
|
+
panel: "tweak",
|
|
37
|
+
controlsCollapsed: false,
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/** URL param name for each field. */
|
|
41
|
+
export const STUDIO_URL_KEYS = {
|
|
42
|
+
subject: "subject",
|
|
43
|
+
viewport: "vp",
|
|
44
|
+
theme: "theme",
|
|
45
|
+
panel: "panel",
|
|
46
|
+
controlsCollapsed: "controls",
|
|
47
|
+
} as const satisfies Record<keyof StudioViewState, string>;
|
|
48
|
+
|
|
49
|
+
export const STUDIO_SUBJECTS: readonly { value: StudioSubject; label: string; src: string }[] = [
|
|
50
|
+
{ value: "blank", label: "Blank screen", src: "/preview/studio" },
|
|
51
|
+
{ value: "cards", label: "Cards", src: "/preview/cards" },
|
|
52
|
+
{ value: "dashboard", label: "Dashboard", src: "/preview/dashboard" },
|
|
53
|
+
];
|
|
54
|
+
|
|
55
|
+
export const STUDIO_VIEWPORTS: readonly { value: StudioViewport; label: string; width: number | null }[] = [
|
|
56
|
+
{ value: "fill", label: "Fill", width: null },
|
|
57
|
+
{ value: "390", label: "390", width: 390 },
|
|
58
|
+
{ value: "768", label: "768", width: 768 },
|
|
59
|
+
{ value: "1280", label: "1280", width: 1280 },
|
|
60
|
+
];
|
|
61
|
+
|
|
62
|
+
export const STUDIO_THEMES: readonly { value: StudioPreviewTheme; label: string }[] = [
|
|
63
|
+
{ value: "match", label: "Match app" },
|
|
64
|
+
{ value: "light", label: "Light" },
|
|
65
|
+
{ value: "dark", label: "Dark" },
|
|
66
|
+
{ value: "both", label: "Both" },
|
|
67
|
+
];
|
|
68
|
+
|
|
69
|
+
function pick<T extends string>(
|
|
70
|
+
raw: string | null,
|
|
71
|
+
options: readonly { value: T }[],
|
|
72
|
+
fallback: T,
|
|
73
|
+
): T {
|
|
74
|
+
const hit = options.find((o) => o.value === raw);
|
|
75
|
+
return hit ? hit.value : fallback;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const PANEL_MODES: readonly { value: StudioPanelMode }[] = [
|
|
79
|
+
{ value: "tweak" },
|
|
80
|
+
{ value: "tokens" },
|
|
81
|
+
];
|
|
82
|
+
|
|
83
|
+
/** Parse URLSearchParams into a complete state. Unknown or missing values fall back to defaults. */
|
|
84
|
+
export function parseStudioViewState(params: URLSearchParams): StudioViewState {
|
|
85
|
+
const d = STUDIO_VIEW_DEFAULTS;
|
|
86
|
+
const collapsed = params.get(STUDIO_URL_KEYS.controlsCollapsed);
|
|
87
|
+
return {
|
|
88
|
+
subject: pick(params.get(STUDIO_URL_KEYS.subject), STUDIO_SUBJECTS, d.subject),
|
|
89
|
+
viewport: pick(params.get(STUDIO_URL_KEYS.viewport), STUDIO_VIEWPORTS, d.viewport),
|
|
90
|
+
theme: pick(params.get(STUDIO_URL_KEYS.theme), STUDIO_THEMES, d.theme),
|
|
91
|
+
panel: pick(params.get(STUDIO_URL_KEYS.panel), PANEL_MODES, d.panel),
|
|
92
|
+
controlsCollapsed: collapsed === "collapsed" ? true : d.controlsCollapsed,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Write only non-default fields into `params` (mutates and returns it). Removes keys that are back at default. */
|
|
97
|
+
export function writeStudioViewState(
|
|
98
|
+
params: URLSearchParams,
|
|
99
|
+
state: StudioViewState,
|
|
100
|
+
): URLSearchParams {
|
|
101
|
+
const d = STUDIO_VIEW_DEFAULTS;
|
|
102
|
+
const set = (key: string, value: string, isDefault: boolean) => {
|
|
103
|
+
if (isDefault) params.delete(key);
|
|
104
|
+
else params.set(key, value);
|
|
105
|
+
};
|
|
106
|
+
set(STUDIO_URL_KEYS.subject, state.subject, state.subject === d.subject);
|
|
107
|
+
set(STUDIO_URL_KEYS.viewport, state.viewport, state.viewport === d.viewport);
|
|
108
|
+
set(STUDIO_URL_KEYS.theme, state.theme, state.theme === d.theme);
|
|
109
|
+
set(STUDIO_URL_KEYS.panel, state.panel, state.panel === d.panel);
|
|
110
|
+
set(
|
|
111
|
+
STUDIO_URL_KEYS.controlsCollapsed,
|
|
112
|
+
"collapsed",
|
|
113
|
+
state.controlsCollapsed === d.controlsCollapsed,
|
|
114
|
+
);
|
|
115
|
+
return params;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/* ── Tweaks: live in the existing global hooks ────────────────── */
|
|
119
|
+
|
|
120
|
+
export const TEXT_FLOOR_PX = 14;
|
|
121
|
+
|
|
122
|
+
/** text-xs is two steps below base. */
|
|
123
|
+
export function smallestTextPx(base: number, ratio: number): number {
|
|
124
|
+
return base / ratio ** 2;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Largest ratio that keeps text-xs at or above 14px for this base, capped at TYPE_SCALE_RANGES.ratio.max. */
|
|
128
|
+
export function maxRatioForBase(base: number): number {
|
|
129
|
+
return Math.min(TYPE_SCALE_RANGES.ratio.max, Math.sqrt(base / TEXT_FLOOR_PX));
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Smallest base that keeps text-xs at or above 14px for this ratio, floored at TYPE_SCALE_RANGES.base.min. */
|
|
133
|
+
export function minBaseForRatio(ratio: number): number {
|
|
134
|
+
return Math.max(TYPE_SCALE_RANGES.base.min, TEXT_FLOOR_PX * ratio ** 2);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Clamp a scale so it satisfies the floor. Adjusts ratio first, then base. */
|
|
138
|
+
export function clampTypeScale(scale: TypeScale): TypeScale {
|
|
139
|
+
const ratio = Math.max(
|
|
140
|
+
TYPE_SCALE_RANGES.ratio.min,
|
|
141
|
+
Math.min(scale.ratio, maxRatioForBase(scale.base)),
|
|
142
|
+
);
|
|
143
|
+
// If the range minimum ratio still breaches the floor at this base, raise base.
|
|
144
|
+
const base = Math.min(
|
|
145
|
+
TYPE_SCALE_RANGES.base.max,
|
|
146
|
+
Math.max(scale.base, minBaseForRatio(ratio)),
|
|
147
|
+
);
|
|
148
|
+
return { base, ratio };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** Every tweakable value Studio exposes, flattened into one typed object. */
|
|
152
|
+
export interface StudioTweaks {
|
|
153
|
+
primaryColor: string; // option key from useGlobalPrimaryColor
|
|
154
|
+
sansFont: string; // key of SANS_FONT_OPTIONS
|
|
155
|
+
typeBase: number; // px
|
|
156
|
+
typeRatio: number;
|
|
157
|
+
letterSpacing: number; // em
|
|
158
|
+
radius: number; // rem
|
|
159
|
+
shadowOpacity: number; // 0..1
|
|
160
|
+
spacing: number; // rem
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export type StudioTweakKey = keyof StudioTweaks;
|
|
164
|
+
|
|
165
|
+
/** Numeric tweak metadata. Ranges and defaults are imported, never retyped. */
|
|
166
|
+
export const NUMERIC_TWEAKS: Record<
|
|
167
|
+
Exclude<StudioTweakKey, "primaryColor" | "sansFont">,
|
|
168
|
+
{
|
|
169
|
+
label: string;
|
|
170
|
+
group: "type" | "shape" | "space";
|
|
171
|
+
min: number;
|
|
172
|
+
max: number;
|
|
173
|
+
step: number;
|
|
174
|
+
unit: string;
|
|
175
|
+
default: number;
|
|
176
|
+
}
|
|
177
|
+
> = {
|
|
178
|
+
typeBase: {
|
|
179
|
+
label: "Base size",
|
|
180
|
+
group: "type",
|
|
181
|
+
...TYPE_SCALE_RANGES.base,
|
|
182
|
+
default: TYPE_SCALE_DEFAULTS.base,
|
|
183
|
+
},
|
|
184
|
+
typeRatio: {
|
|
185
|
+
label: "Scale ratio",
|
|
186
|
+
group: "type",
|
|
187
|
+
...TYPE_SCALE_RANGES.ratio,
|
|
188
|
+
default: TYPE_SCALE_DEFAULTS.ratio,
|
|
189
|
+
},
|
|
190
|
+
letterSpacing: {
|
|
191
|
+
label: "Letter spacing",
|
|
192
|
+
group: "type",
|
|
193
|
+
...DESIGN_CONTROL_RANGES.letterSpacing,
|
|
194
|
+
default: DESIGN_CONTROL_DEFAULTS.letterSpacing,
|
|
195
|
+
},
|
|
196
|
+
radius: {
|
|
197
|
+
label: "Radius",
|
|
198
|
+
group: "shape",
|
|
199
|
+
...DESIGN_CONTROL_RANGES.radius,
|
|
200
|
+
default: DESIGN_CONTROL_DEFAULTS.radius,
|
|
201
|
+
},
|
|
202
|
+
shadowOpacity: {
|
|
203
|
+
label: "Shadow",
|
|
204
|
+
group: "shape",
|
|
205
|
+
...DESIGN_CONTROL_RANGES.shadowOpacity,
|
|
206
|
+
default: DESIGN_CONTROL_DEFAULTS.shadowOpacity,
|
|
207
|
+
},
|
|
208
|
+
spacing: {
|
|
209
|
+
label: "Spacing unit",
|
|
210
|
+
group: "space",
|
|
211
|
+
...DESIGN_CONTROL_RANGES.spacing,
|
|
212
|
+
default: DESIGN_CONTROL_DEFAULTS.spacing,
|
|
213
|
+
},
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
/** postMessage-free contract: the preview needs no messages. Documented here so nobody adds one. */
|
|
217
|
+
export const STUDIO_SYNC_NOTE =
|
|
218
|
+
"Preview iframes sync through the storage event via GlobalFontClient. Do not add a postMessage channel.";
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useCallback, useMemo } from "react";
|
|
4
|
+
import { useTokenOverrides } from "@behavioral-agentic-design-system/core/hooks/use-token-overrides";
|
|
5
|
+
import {
|
|
6
|
+
DEFAULT_PRIMARY_COLOR,
|
|
7
|
+
useGlobalPrimaryColor,
|
|
8
|
+
} from "@behavioral-agentic-design-system/core/hooks/use-global-primary-color";
|
|
9
|
+
import {
|
|
10
|
+
DEFAULT_SANS_FONT,
|
|
11
|
+
SANS_FONT_OPTIONS,
|
|
12
|
+
useGlobalSansFont,
|
|
13
|
+
} from "@behavioral-agentic-design-system/core/hooks/use-global-sans-font";
|
|
14
|
+
import { useGlobalTypeScale } from "@behavioral-agentic-design-system/core/hooks/use-global-type-scale";
|
|
15
|
+
import { useGlobalDesignControls } from "@behavioral-agentic-design-system/core/hooks/use-global-design-controls";
|
|
16
|
+
import {
|
|
17
|
+
NUMERIC_TWEAKS,
|
|
18
|
+
clampTypeScale,
|
|
19
|
+
type StudioTweakKey,
|
|
20
|
+
type StudioTweaks,
|
|
21
|
+
} from "./types";
|
|
22
|
+
|
|
23
|
+
const PRIMARY_DEFAULT = DEFAULT_PRIMARY_COLOR;
|
|
24
|
+
const SANS_DEFAULT = DEFAULT_SANS_FONT;
|
|
25
|
+
|
|
26
|
+
const DEFAULTS: StudioTweaks = {
|
|
27
|
+
primaryColor: PRIMARY_DEFAULT,
|
|
28
|
+
sansFont: SANS_DEFAULT,
|
|
29
|
+
typeBase: NUMERIC_TWEAKS.typeBase.default,
|
|
30
|
+
typeRatio: NUMERIC_TWEAKS.typeRatio.default,
|
|
31
|
+
letterSpacing: NUMERIC_TWEAKS.letterSpacing.default,
|
|
32
|
+
radius: NUMERIC_TWEAKS.radius.default,
|
|
33
|
+
shadowOpacity: NUMERIC_TWEAKS.shadowOpacity.default,
|
|
34
|
+
spacing: NUMERIC_TWEAKS.spacing.default,
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Studio tweaks: a typed facade over the four existing global hooks. Adds no
|
|
39
|
+
* state of its own, so values stay in the shipped localStorage + storage-event
|
|
40
|
+
* contract that every preview iframe already listens to.
|
|
41
|
+
*/
|
|
42
|
+
export function useStudioTweaks() {
|
|
43
|
+
// Order matters: useTokenOverrides' mount effect runs applyThemeEditorState,
|
|
44
|
+
// which clears inline radius/spacing/fonts/colors. It must mount first so
|
|
45
|
+
// the other hooks' mount effects re-apply their values afterwards.
|
|
46
|
+
useTokenOverrides();
|
|
47
|
+
const { color, setColor, options, addCustomSilent } = useGlobalPrimaryColor();
|
|
48
|
+
const { font, setFont } = useGlobalSansFont();
|
|
49
|
+
const { scale, setScale, reset: resetScale } = useGlobalTypeScale();
|
|
50
|
+
const { controls, setControl, reset: resetControls } = useGlobalDesignControls();
|
|
51
|
+
|
|
52
|
+
const tweaks: StudioTweaks = useMemo(
|
|
53
|
+
() => ({
|
|
54
|
+
primaryColor: color,
|
|
55
|
+
sansFont: font,
|
|
56
|
+
typeBase: scale.base,
|
|
57
|
+
typeRatio: scale.ratio,
|
|
58
|
+
letterSpacing: controls.letterSpacing,
|
|
59
|
+
radius: controls.radius,
|
|
60
|
+
shadowOpacity: controls.shadowOpacity,
|
|
61
|
+
spacing: controls.spacing,
|
|
62
|
+
}),
|
|
63
|
+
[color, font, scale.base, scale.ratio, controls],
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
const set = useCallback(
|
|
67
|
+
<K extends StudioTweakKey>(key: K, value: StudioTweaks[K]) => {
|
|
68
|
+
switch (key) {
|
|
69
|
+
case "primaryColor":
|
|
70
|
+
setColor(value as string);
|
|
71
|
+
break;
|
|
72
|
+
case "sansFont":
|
|
73
|
+
if ((value as string) in SANS_FONT_OPTIONS) setFont(value as string);
|
|
74
|
+
break;
|
|
75
|
+
case "typeBase":
|
|
76
|
+
case "typeRatio": {
|
|
77
|
+
const next = clampTypeScale({
|
|
78
|
+
base: key === "typeBase" ? (value as number) : scale.base,
|
|
79
|
+
ratio: key === "typeRatio" ? (value as number) : scale.ratio,
|
|
80
|
+
});
|
|
81
|
+
if (next.base !== scale.base) setScale("base", next.base);
|
|
82
|
+
if (next.ratio !== scale.ratio) setScale("ratio", next.ratio);
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
default:
|
|
86
|
+
setControl(key, value as number);
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
[setColor, setFont, setScale, setControl, scale.base, scale.ratio],
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
const reset = useCallback(
|
|
93
|
+
(key: StudioTweakKey) => set(key, DEFAULTS[key]),
|
|
94
|
+
[set],
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
const resetAll = useCallback(() => {
|
|
98
|
+
resetControls();
|
|
99
|
+
resetScale();
|
|
100
|
+
setFont(SANS_DEFAULT);
|
|
101
|
+
setColor(PRIMARY_DEFAULT);
|
|
102
|
+
}, [resetControls, resetScale, setFont, setColor]);
|
|
103
|
+
|
|
104
|
+
const isDefault = useCallback(
|
|
105
|
+
(key: StudioTweakKey) => tweaks[key] === DEFAULTS[key],
|
|
106
|
+
[tweaks],
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
const addCustomColor = useCallback(
|
|
110
|
+
(hex: string): string | null => {
|
|
111
|
+
const id = addCustomSilent(hex);
|
|
112
|
+
if (id) setColor(id);
|
|
113
|
+
return id;
|
|
114
|
+
},
|
|
115
|
+
[addCustomSilent, setColor],
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
return {
|
|
119
|
+
tweaks,
|
|
120
|
+
defaults: DEFAULTS,
|
|
121
|
+
set,
|
|
122
|
+
reset,
|
|
123
|
+
resetAll,
|
|
124
|
+
isDefault,
|
|
125
|
+
primaryOptions: options,
|
|
126
|
+
addCustomColor,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
4
|
+
import {
|
|
5
|
+
STUDIO_SUBJECTS,
|
|
6
|
+
STUDIO_VIEW_DEFAULTS,
|
|
7
|
+
parseStudioViewState,
|
|
8
|
+
writeStudioViewState,
|
|
9
|
+
type StudioViewState,
|
|
10
|
+
} from "./types";
|
|
11
|
+
|
|
12
|
+
const URL_DEBOUNCE_MS = 150;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Studio view state (subject, viewport, theme, panel mode, collapsed) mirrored
|
|
16
|
+
* into the URL. Initial render returns the defaults so server and client agree;
|
|
17
|
+
* a mount effect reads window.location.search once. Writes use replaceState,
|
|
18
|
+
* never pushState, and spread the existing history.state so the shell's
|
|
19
|
+
* `shellTab` entry survives.
|
|
20
|
+
*/
|
|
21
|
+
export function useStudioViewState() {
|
|
22
|
+
const [state, setState] = useState<StudioViewState>(STUDIO_VIEW_DEFAULTS);
|
|
23
|
+
const latest = useRef<StudioViewState>(STUDIO_VIEW_DEFAULTS);
|
|
24
|
+
const timer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
25
|
+
const dirty = useRef(false);
|
|
26
|
+
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
const parsed = parseStudioViewState(new URLSearchParams(window.location.search));
|
|
29
|
+
latest.current = parsed;
|
|
30
|
+
setState(parsed);
|
|
31
|
+
}, []);
|
|
32
|
+
|
|
33
|
+
useEffect(
|
|
34
|
+
() => () => {
|
|
35
|
+
if (timer.current) clearTimeout(timer.current);
|
|
36
|
+
},
|
|
37
|
+
[],
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
const set = useCallback(
|
|
41
|
+
<K extends keyof StudioViewState>(key: K, value: StudioViewState[K]) => {
|
|
42
|
+
const next = { ...latest.current, [key]: value };
|
|
43
|
+
latest.current = next;
|
|
44
|
+
dirty.current = true;
|
|
45
|
+
setState(next);
|
|
46
|
+
if (timer.current) clearTimeout(timer.current);
|
|
47
|
+
timer.current = setTimeout(() => {
|
|
48
|
+
timer.current = null;
|
|
49
|
+
if (typeof window === "undefined") return;
|
|
50
|
+
const url = new URL(window.location.href);
|
|
51
|
+
writeStudioViewState(url.searchParams, latest.current);
|
|
52
|
+
window.history.replaceState({ ...window.history.state }, "", url);
|
|
53
|
+
}, URL_DEBOUNCE_MS);
|
|
54
|
+
},
|
|
55
|
+
[],
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
// The mount effect above reads the URL once, so a Studio that is already mounted (the shell keeps visited tabs
|
|
59
|
+
// alive) never sees a link that points it at another subject. Home's "Recently added" sends this event, the
|
|
60
|
+
// same pairing the Docs tab uses with "bads:docs:navigate".
|
|
61
|
+
useEffect(() => {
|
|
62
|
+
const onSubject = (event: Event) => {
|
|
63
|
+
const subject = (event as CustomEvent<unknown>).detail;
|
|
64
|
+
const match = STUDIO_SUBJECTS.find((s) => s.value === subject);
|
|
65
|
+
if (match) set("subject", match.value);
|
|
66
|
+
};
|
|
67
|
+
window.addEventListener("bads:studio:subject", onSubject);
|
|
68
|
+
return () => window.removeEventListener("bads:studio:subject", onSubject);
|
|
69
|
+
}, [set]);
|
|
70
|
+
|
|
71
|
+
return { state, set };
|
|
72
|
+
}
|
|
@@ -108,7 +108,7 @@ export function Autocomplete({
|
|
|
108
108
|
}
|
|
109
109
|
return;
|
|
110
110
|
}
|
|
111
|
-
if (e.key === "Enter") {
|
|
111
|
+
if (e.key === "Enter" && !e.nativeEvent.isComposing) {
|
|
112
112
|
// Free-text contract: only intercept Enter when an option is
|
|
113
113
|
// highlighted. Otherwise the typed text stands and the event falls
|
|
114
114
|
// through (form submit keeps working).
|
|
@@ -350,7 +350,20 @@ export function ColorPicker({
|
|
|
350
350
|
// Colors the user has added through the wheel, appended after the presets.
|
|
351
351
|
const [addedSwatches, setAddedSwatches] = React.useState<string[]>([]);
|
|
352
352
|
const allSwatches = React.useMemo(
|
|
353
|
-
() =>
|
|
353
|
+
() => {
|
|
354
|
+
// Case-insensitive: a controlled parent may hand the same color back
|
|
355
|
+
// as "#3B82F6" while the wheel drafted "#3b82f6", which listed it twice.
|
|
356
|
+
const seen = new Set(swatches.map((c) => c.toLowerCase()));
|
|
357
|
+
return [
|
|
358
|
+
...swatches,
|
|
359
|
+
...addedSwatches.filter((c) => {
|
|
360
|
+
const key = c.toLowerCase();
|
|
361
|
+
if (seen.has(key)) return false;
|
|
362
|
+
seen.add(key);
|
|
363
|
+
return true;
|
|
364
|
+
}),
|
|
365
|
+
];
|
|
366
|
+
},
|
|
354
367
|
[swatches, addedSwatches]
|
|
355
368
|
);
|
|
356
369
|
|
|
@@ -429,14 +442,17 @@ export function ColorPicker({
|
|
|
429
442
|
|
|
430
443
|
const addColor = () => {
|
|
431
444
|
if (disabled || !isValidHex(draft)) return;
|
|
432
|
-
if (!allSwatches.
|
|
445
|
+
if (!allSwatches.some((c) => c.toLowerCase() === draft.toLowerCase()))
|
|
446
|
+
setAddedSwatches((prev) => [...prev, draft]);
|
|
433
447
|
commit(draft);
|
|
434
448
|
if (!swatches.includes(draft)) setCustomInput("");
|
|
435
449
|
onAddSwatch?.(draft);
|
|
436
450
|
setWheelOpen(false);
|
|
437
451
|
};
|
|
438
452
|
|
|
439
|
-
const selectedSwatchIdx = allSwatches.
|
|
453
|
+
const selectedSwatchIdx = allSwatches.findIndex(
|
|
454
|
+
(c) => c.toLowerCase() === (value ?? "").toLowerCase()
|
|
455
|
+
);
|
|
440
456
|
|
|
441
457
|
return (
|
|
442
458
|
<div className={cn("space-y-3", className)}>
|
|
@@ -450,7 +466,7 @@ export function ColorPicker({
|
|
|
450
466
|
className="flex flex-wrap gap-1.5"
|
|
451
467
|
>
|
|
452
468
|
{allSwatches.map((swatch, i) => {
|
|
453
|
-
const isSelected = value === swatch;
|
|
469
|
+
const isSelected = (value ?? "").toLowerCase() === swatch.toLowerCase();
|
|
454
470
|
const isRovingFocus = selectedSwatchIdx >= 0 ? isSelected : i === 0;
|
|
455
471
|
return (
|
|
456
472
|
<button
|
|
@@ -19,6 +19,7 @@ import { Button } from "./button"
|
|
|
19
19
|
import { Checkbox } from "./checkbox"
|
|
20
20
|
import {
|
|
21
21
|
Table,
|
|
22
|
+
TableCaption,
|
|
22
23
|
TableHeader,
|
|
23
24
|
TableBody,
|
|
24
25
|
TableRow,
|
|
@@ -72,6 +73,17 @@ export interface DataTableProps<TData> extends VariantProps<typeof cellVariants>
|
|
|
72
73
|
* definition if needed.
|
|
73
74
|
*/
|
|
74
75
|
rowSelection?: boolean
|
|
76
|
+
/**
|
|
77
|
+
* Fires with the selected rows' data whenever the selection changes (not on
|
|
78
|
+
* mount), so a "Delete selected" bar can act on them. Needs `rowSelection`.
|
|
79
|
+
*/
|
|
80
|
+
onRowSelectionChange?: (rows: TData[]) => void
|
|
81
|
+
/**
|
|
82
|
+
* Stable id for a row. Without it, rows are identified by array index, so
|
|
83
|
+
* after a refetch or re-sort of `data` the selection can land on different
|
|
84
|
+
* records. Pass a primary key when rows have one.
|
|
85
|
+
*/
|
|
86
|
+
getRowId?: (row: TData, index: number) => string
|
|
75
87
|
/**
|
|
76
88
|
* Callback fired when a data row is clicked. Does NOT constitute a
|
|
77
89
|
* selection — pair with rowSelection for accessible multi-select. When
|
|
@@ -95,6 +107,13 @@ export interface DataTableProps<TData> extends VariantProps<typeof cellVariants>
|
|
|
95
107
|
* height when `stickyHeader` is true. Defaults to `"400px"`.
|
|
96
108
|
*/
|
|
97
109
|
scrollHeight?: string
|
|
110
|
+
/**
|
|
111
|
+
* Accessible label announced by screen readers as the table's region name.
|
|
112
|
+
* Also rendered as a visually hidden `<caption>` so assistive tech can
|
|
113
|
+
* identify the table's purpose (WCAG 1.3.1). Required when more than one
|
|
114
|
+
* table appears on a page; strongly recommended otherwise.
|
|
115
|
+
*/
|
|
116
|
+
caption?: string
|
|
98
117
|
className?: string
|
|
99
118
|
}
|
|
100
119
|
|
|
@@ -201,11 +220,14 @@ function DataTable<TData>({
|
|
|
201
220
|
sorting: sortingEnabled = true,
|
|
202
221
|
pagination: paginationOptions,
|
|
203
222
|
rowSelection: rowSelectionEnabled = false,
|
|
223
|
+
onRowSelectionChange,
|
|
224
|
+
getRowId,
|
|
204
225
|
onRowClick,
|
|
205
226
|
emptyMessage = "No results.",
|
|
206
227
|
stickyHeader = false,
|
|
207
228
|
scrollHeight = "400px",
|
|
208
229
|
density = "comfortable",
|
|
230
|
+
caption,
|
|
209
231
|
className,
|
|
210
232
|
}: DataTableProps<TData>) {
|
|
211
233
|
const [sortingState, setSortingState] = React.useState<SortingState>([])
|
|
@@ -231,6 +253,7 @@ function DataTable<TData>({
|
|
|
231
253
|
},
|
|
232
254
|
enableSorting: sortingEnabled,
|
|
233
255
|
enableRowSelection: rowSelectionEnabled,
|
|
256
|
+
getRowId,
|
|
234
257
|
onSortingChange: setSortingState,
|
|
235
258
|
onRowSelectionChange: setRowSelectionState,
|
|
236
259
|
getCoreRowModel: getCoreRowModel(),
|
|
@@ -245,6 +268,22 @@ function DataTable<TData>({
|
|
|
245
268
|
},
|
|
246
269
|
})
|
|
247
270
|
|
|
271
|
+
// Report the selected rows' data. A ref keeps an inline callback from re-running the effect, and the
|
|
272
|
+
// first run is skipped so mounting does not announce an empty selection.
|
|
273
|
+
const onRowSelectionChangeRef = React.useRef(onRowSelectionChange)
|
|
274
|
+
onRowSelectionChangeRef.current = onRowSelectionChange
|
|
275
|
+
const selectionMounted = React.useRef(false)
|
|
276
|
+
React.useEffect(() => {
|
|
277
|
+
if (!selectionMounted.current) {
|
|
278
|
+
selectionMounted.current = true
|
|
279
|
+
return
|
|
280
|
+
}
|
|
281
|
+
onRowSelectionChangeRef.current?.(
|
|
282
|
+
table.getSelectedRowModel().rows.map((r) => r.original)
|
|
283
|
+
)
|
|
284
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps -- table is stable; the selection state is the trigger
|
|
285
|
+
}, [rowSelectionState])
|
|
286
|
+
|
|
248
287
|
const cellClass = cellVariants({ density })
|
|
249
288
|
|
|
250
289
|
return (
|
|
@@ -252,13 +291,23 @@ function DataTable<TData>({
|
|
|
252
291
|
data-slot="data-table"
|
|
253
292
|
className={cn("rounded-md border bg-card overflow-hidden", className)}
|
|
254
293
|
>
|
|
255
|
-
{/* Scrollable table wrapper
|
|
256
|
-
|
|
294
|
+
{/* Scrollable table wrapper — tabIndex makes the region keyboard-reachable
|
|
295
|
+
(WCAG 2.1.1 + axe scrollable-region-focusable). aria-label surfaces the
|
|
296
|
+
caption text as the region name for screen readers. */}
|
|
297
|
+
<div
|
|
298
|
+
className="overflow-x-auto"
|
|
299
|
+
tabIndex={0}
|
|
300
|
+
role="region"
|
|
301
|
+
aria-label={caption ?? "Data table"}
|
|
302
|
+
>
|
|
257
303
|
<Table
|
|
258
304
|
role="grid"
|
|
259
305
|
wrapperClassName={stickyHeader ? "overflow-y-auto" : undefined}
|
|
260
306
|
wrapperStyle={stickyHeader ? { maxHeight: scrollHeight } : undefined}
|
|
261
307
|
>
|
|
308
|
+
{caption ? (
|
|
309
|
+
<TableCaption className="sr-only">{caption}</TableCaption>
|
|
310
|
+
) : null}
|
|
262
311
|
<TableHeader className={stickyHeader ? "sticky top-0 z-10 bg-card" : undefined}>
|
|
263
312
|
{table.getHeaderGroups().map((headerGroup) => (
|
|
264
313
|
<TableRow key={headerGroup.id}>
|
|
@@ -336,7 +385,19 @@ function DataTable<TData>({
|
|
|
336
385
|
onRowClick && "cursor-pointer",
|
|
337
386
|
row.getIsSelected() && "bg-muted"
|
|
338
387
|
)}
|
|
339
|
-
onClick={
|
|
388
|
+
onClick={
|
|
389
|
+
onRowClick
|
|
390
|
+
? (event) => {
|
|
391
|
+
// A click on a control inside the row (the select checkbox, a link, an action
|
|
392
|
+
// button) belongs to that control, not to the row.
|
|
393
|
+
const hit = (event.target as HTMLElement).closest(
|
|
394
|
+
"a, button, input, label, select, textarea, [role='checkbox'], [role='button']"
|
|
395
|
+
)
|
|
396
|
+
if (hit && event.currentTarget.contains(hit)) return
|
|
397
|
+
onRowClick(row.original)
|
|
398
|
+
}
|
|
399
|
+
: undefined
|
|
400
|
+
}
|
|
340
401
|
>
|
|
341
402
|
{row.getVisibleCells().map((cell) => (
|
|
342
403
|
<TableCell
|
|
@@ -65,7 +65,7 @@ function DateField({
|
|
|
65
65
|
className,
|
|
66
66
|
id,
|
|
67
67
|
}: DateFieldProps) {
|
|
68
|
-
const isControlled = value !== undefined
|
|
68
|
+
const isControlled = value !== undefined || onValueChange !== undefined
|
|
69
69
|
const [internalValue, setInternalValue] = React.useState<Date | undefined>(
|
|
70
70
|
isValidDate(defaultValue) ? atMidnight(defaultValue) : undefined
|
|
71
71
|
)
|