@behavioral-agentic-design-system/core 0.47.0 → 0.49.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.
Files changed (214) hide show
  1. package/package.json +1 -1
  2. package/scripts/build-tokens.cjs +24 -3
  3. package/scripts/ux-smoke.mjs +5 -1
  4. package/src/api/save-tokens.ts +170 -24
  5. package/src/app/layout.tsx +12 -1
  6. package/src/app/preview/components/page.tsx +114 -18
  7. package/src/app/preview/global-font-client.tsx +2 -0
  8. package/src/app/providers.tsx +2 -0
  9. package/src/app/shell.tsx +50 -156
  10. package/src/components/customize/brand-mark-section.tsx +65 -95
  11. package/src/components/customize/font-pickers.tsx +25 -2
  12. package/src/components/customize/save-bar.tsx +120 -7
  13. package/src/components/customize/theme-io.tsx +62 -21
  14. package/src/components/customize-tab.tsx +3 -1
  15. package/src/components/home-recent.tsx +58 -25
  16. package/src/components/motion/reveal.tsx +168 -12
  17. package/src/components/studio/control-panel.tsx +98 -19
  18. package/src/components/studio/import-theme-dialog.tsx +167 -0
  19. package/src/components/studio/tokens-panel.tsx +215 -0
  20. package/src/components/studio/types.ts +5 -2
  21. package/src/components/studio/use-studio-tweaks.ts +36 -4
  22. package/src/components/studio/use-token-edits.ts +160 -0
  23. package/src/components/ui/number-field.tsx +390 -0
  24. package/src/components/ui/toggle-tip.tsx +38 -29
  25. package/src/data/component-added.json +4 -0
  26. package/src/data/component-stories.json +1214 -0
  27. package/src/data/docs-index.ts +7 -2
  28. package/src/data/hidden-views.ts +22 -0
  29. package/src/data/history.json +51 -51
  30. package/src/data/sticker-sheet.json +1918 -1808
  31. package/src/fonts/Figtree-OFL.txt +93 -0
  32. package/src/fonts/figtree-latin-ext-wght-normal.woff2 +0 -0
  33. package/src/fonts/figtree-latin-wght-italic.woff2 +0 -0
  34. package/src/fonts/figtree-latin-wght-normal.woff2 +0 -0
  35. package/src/fonts-cdn.css +3 -1
  36. package/src/fonts.css +44 -0
  37. package/src/hooks/use-global-heading-font.ts +104 -0
  38. package/src/hooks/use-global-sans-font.ts +8 -3
  39. package/src/hooks/use-global-theme.ts +2 -2
  40. package/src/hooks/use-token-overrides.ts +62 -14
  41. package/src/lib/css-color.ts +42 -0
  42. package/src/lib/storybook-url.ts +28 -0
  43. package/src/lib/theme-editor.ts +7 -1
  44. package/src/lib/theme-import.ts +99 -0
  45. package/src/lib/theme-transition.ts +93 -0
  46. package/src/lib/token-list.ts +133 -0
  47. package/src/lib/token-value.ts +31 -0
  48. package/src/stories/Accordion.stories.tsx +1 -1
  49. package/src/stories/Affix.stories.tsx +1 -1
  50. package/src/stories/AiBadge.stories.tsx +1 -1
  51. package/src/stories/Alert.stories.tsx +1 -1
  52. package/src/stories/AlertDialog.stories.tsx +1 -1
  53. package/src/stories/AngleSlider.stories.tsx +1 -1
  54. package/src/stories/AnnouncementBar.stories.tsx +2 -2
  55. package/src/stories/AspectRatio.stories.tsx +1 -1
  56. package/src/stories/Attachment.stories.tsx +1 -1
  57. package/src/stories/Autocomplete.stories.tsx +1 -1
  58. package/src/stories/Avatar.stories.tsx +1 -1
  59. package/src/stories/Badge.stories.tsx +1 -1
  60. package/src/stories/Blockquote.stories.tsx +1 -1
  61. package/src/stories/Burger.stories.tsx +1 -1
  62. package/src/stories/Button.stories.tsx +1 -1
  63. package/src/stories/ButtonGroup.stories.tsx +1 -1
  64. package/src/stories/Calendar.stories.tsx +1 -1
  65. package/src/stories/Callout.stories.tsx +1 -1
  66. package/src/stories/Canvas.stories.tsx +1 -1
  67. package/src/stories/Card.stories.tsx +1 -1
  68. package/src/stories/Carousel.stories.tsx +1 -1
  69. package/src/stories/Cascader.stories.tsx +1 -1
  70. package/src/stories/Chart.stories.tsx +1 -1
  71. package/src/stories/ChatBubble.stories.tsx +1 -1
  72. package/src/stories/ChatMarker.stories.tsx +1 -1
  73. package/src/stories/Checkbox.stories.tsx +1 -1
  74. package/src/stories/CheckboxCard.stories.tsx +1 -1
  75. package/src/stories/CheckboxGroup.stories.tsx +2 -1
  76. package/src/stories/Chip.stories.tsx +1 -1
  77. package/src/stories/CircularProgress.stories.tsx +1 -1
  78. package/src/stories/Clipboard.stories.tsx +1 -1
  79. package/src/stories/CloseButton.stories.tsx +1 -1
  80. package/src/stories/Code.stories.tsx +1 -1
  81. package/src/stories/Collapsible.stories.tsx +1 -1
  82. package/src/stories/ColorPicker.stories.tsx +1 -1
  83. package/src/stories/ColorSwatch.stories.tsx +1 -1
  84. package/src/stories/Combobox.stories.tsx +1 -1
  85. package/src/stories/Container.stories.tsx +1 -1
  86. package/src/stories/ContextMenu.stories.tsx +1 -1
  87. package/src/stories/CopyButton.stories.tsx +1 -1
  88. package/src/stories/DataList.stories.tsx +1 -1
  89. package/src/stories/DataTable.stories.tsx +1 -1
  90. package/src/stories/DateField.stories.tsx +1 -1
  91. package/src/stories/DatePicker.stories.tsx +1 -1
  92. package/src/stories/DateRangePicker.stories.tsx +1 -1
  93. package/src/stories/Dialog.stories.tsx +1 -1
  94. package/src/stories/Drawer.stories.tsx +1 -1
  95. package/src/stories/DropdownMenu.stories.tsx +1 -1
  96. package/src/stories/Editable.stories.tsx +1 -1
  97. package/src/stories/EmptyState.stories.tsx +1 -1
  98. package/src/stories/FacetedFilter.stories.tsx +1 -1
  99. package/src/stories/Field.stories.tsx +1 -1
  100. package/src/stories/Fieldset.stories.tsx +1 -1
  101. package/src/stories/FileButton.stories.tsx +1 -1
  102. package/src/stories/FileUpload.stories.tsx +1 -1
  103. package/src/stories/FloatLabel.stories.tsx +1 -1
  104. package/src/stories/FloatingActionButton.stories.tsx +1 -1
  105. package/src/stories/FloatingIndicator.stories.tsx +1 -1
  106. package/src/stories/FloatingPanel.stories.tsx +1 -1
  107. package/src/stories/FocusTrap.stories.tsx +1 -1
  108. package/src/stories/FormatByte.stories.tsx +1 -1
  109. package/src/stories/FormatDate.stories.tsx +1 -1
  110. package/src/stories/FormatNumber.stories.tsx +1 -1
  111. package/src/stories/FormatRelativeTime.stories.tsx +1 -1
  112. package/src/stories/GaugeChart.stories.tsx +1 -1
  113. package/src/stories/Grid.stories.tsx +1 -1
  114. package/src/stories/Heatmap.stories.tsx +1 -1
  115. package/src/stories/Highlight.stories.tsx +1 -1
  116. package/src/stories/HoverCard.stories.tsx +1 -1
  117. package/src/stories/Icon.stories.tsx +1 -1
  118. package/src/stories/Image.stories.tsx +1 -1
  119. package/src/stories/Indicator.stories.tsx +1 -1
  120. package/src/stories/InfiniteScroll.stories.tsx +1 -1
  121. package/src/stories/InlineEdit.stories.tsx +1 -1
  122. package/src/stories/Input.stories.tsx +1 -1
  123. package/src/stories/InputClearButton.stories.tsx +1 -1
  124. package/src/stories/InputGroup.stories.tsx +1 -1
  125. package/src/stories/JsonInput.stories.tsx +1 -1
  126. package/src/stories/Kbd.stories.tsx +1 -1
  127. package/src/stories/KeyboardShortcut.stories.tsx +1 -1
  128. package/src/stories/Label.stories.tsx +1 -1
  129. package/src/stories/List.stories.tsx +1 -1
  130. package/src/stories/Listbox.stories.tsx +1 -1
  131. package/src/stories/LoadingDots.stories.tsx +1 -1
  132. package/src/stories/LoadingOverlay.stories.tsx +1 -1
  133. package/src/stories/Mark.stories.tsx +1 -1
  134. package/src/stories/MarkdownPreview.stories.tsx +1 -1
  135. package/src/stories/Marquee.stories.tsx +1 -1
  136. package/src/stories/Masonry.stories.tsx +1 -1
  137. package/src/stories/Meter.stories.tsx +1 -1
  138. package/src/stories/MonthPicker.stories.tsx +1 -1
  139. package/src/stories/MotionToggle.stories.tsx +1 -1
  140. package/src/stories/MultiSelect.stories.tsx +1 -1
  141. package/src/stories/NativeSelect.stories.tsx +1 -1
  142. package/src/stories/NavigationProgress.stories.tsx +1 -1
  143. package/src/stories/Notification.stories.tsx +1 -1
  144. package/src/stories/NumberField.stories.tsx +214 -0
  145. package/src/stories/NumberInput.stories.tsx +1 -1
  146. package/src/stories/OtpInput.stories.tsx +1 -1
  147. package/src/stories/Overlay.stories.tsx +1 -1
  148. package/src/stories/PageHeader.stories.tsx +1 -1
  149. package/src/stories/PasswordInput.stories.tsx +1 -1
  150. package/src/stories/PasswordStrengthInput.stories.tsx +1 -1
  151. package/src/stories/PhoneInput.stories.tsx +1 -1
  152. package/src/stories/Pill.stories.tsx +1 -1
  153. package/src/stories/PinInput.stories.tsx +1 -1
  154. package/src/stories/Popover.stories.tsx +1 -1
  155. package/src/stories/Portal.stories.tsx +1 -1
  156. package/src/stories/Presence.stories.tsx +1 -1
  157. package/src/stories/Progress.stories.tsx +1 -1
  158. package/src/stories/ProgressStack.stories.tsx +1 -1
  159. package/src/stories/Prose.stories.tsx +1 -1
  160. package/src/stories/QrCode.stories.tsx +1 -1
  161. package/src/stories/Questionnaire.stories.tsx +1 -1
  162. package/src/stories/RadioGroup.stories.tsx +1 -1
  163. package/src/stories/RatingGroup.stories.tsx +1 -1
  164. package/src/stories/Resizable.stories.tsx +1 -1
  165. package/src/stories/Result.stories.tsx +1 -1
  166. package/src/stories/Reveal.stories.tsx +1 -1
  167. package/src/stories/RollingNumber.stories.tsx +1 -1
  168. package/src/stories/ScrollArea.stories.tsx +1 -1
  169. package/src/stories/ScrollProgress.stories.tsx +1 -1
  170. package/src/stories/SearchInput.stories.tsx +1 -1
  171. package/src/stories/SegmentedControl.stories.tsx +1 -1
  172. package/src/stories/Select.stories.tsx +1 -1
  173. package/src/stories/Separator.stories.tsx +1 -1
  174. package/src/stories/Sheet.stories.tsx +1 -1
  175. package/src/stories/SignaturePad.stories.tsx +1 -1
  176. package/src/stories/SimpleGrid.stories.tsx +1 -1
  177. package/src/stories/Skeleton.stories.tsx +1 -1
  178. package/src/stories/SkeletonTable.stories.tsx +1 -1
  179. package/src/stories/Slider.stories.tsx +1 -1
  180. package/src/stories/Snackbar.stories.tsx +1 -1
  181. package/src/stories/Sonner.stories.tsx +1 -1
  182. package/src/stories/SortableList.stories.tsx +1 -1
  183. package/src/stories/Sparkline.stories.tsx +1 -1
  184. package/src/stories/Spinner.stories.tsx +1 -1
  185. package/src/stories/Spoiler.stories.tsx +1 -1
  186. package/src/stories/Squircle.stories.tsx +1 -1
  187. package/src/stories/Stack.stories.tsx +1 -1
  188. package/src/stories/StatusBadge.stories.tsx +1 -1
  189. package/src/stories/Switch.stories.tsx +1 -1
  190. package/src/stories/Table.stories.tsx +1 -1
  191. package/src/stories/Tag.stories.tsx +1 -1
  192. package/src/stories/TagsInput.stories.tsx +1 -1
  193. package/src/stories/Text.stories.tsx +1 -1
  194. package/src/stories/Textarea.stories.tsx +1 -1
  195. package/src/stories/ThemeIcon.stories.tsx +1 -1
  196. package/src/stories/TimePicker.stories.tsx +1 -1
  197. package/src/stories/Timeline.stories.tsx +1 -1
  198. package/src/stories/Timer.stories.tsx +1 -1
  199. package/src/stories/Toggle.stories.tsx +1 -1
  200. package/src/stories/ToggleGroup.stories.tsx +1 -1
  201. package/src/stories/ToggleTip.stories.tsx +106 -1
  202. package/src/stories/Tooltip.stories.tsx +56 -1
  203. package/src/stories/TransferList.stories.tsx +1 -1
  204. package/src/stories/TreeSelect.stories.tsx +1 -1
  205. package/src/stories/TypingIndicator.stories.tsx +1 -1
  206. package/src/stories/VisuallyHidden.stories.tsx +1 -1
  207. package/src/stories/Watermark.stories.tsx +1 -1
  208. package/src/stories/WeekPicker.stories.tsx +1 -1
  209. package/src/stories/WordRotate.stories.tsx +1 -1
  210. package/src/stories/YearPicker.stories.tsx +1 -1
  211. package/src/stories/story-groups.json +216 -223
  212. package/src/styles.css +20 -3
  213. package/src/tokens.default.json +6 -1
  214. package/src/tokens.json +7 -2
@@ -21,6 +21,12 @@
21
21
  * Accessibility: under `prefers-reduced-motion: reduce` cards render visible
22
22
  * immediately and never hide/observe (the global base layer also collapses
23
23
  * the transition to ~0ms, so this is belt-and-suspenders).
24
+ *
25
+ * Never strands content: server markup is always visible; without
26
+ * IntersectionObserver nothing is hidden; printing reveals everything first;
27
+ * and once a card has settled its inline motion styles are removed, so it
28
+ * leaves no transform or will-change behind (a lingering transform would turn
29
+ * the wrapper into the containing block for fixed-position descendants).
24
30
  */
25
31
 
26
32
  import * as React from "react";
@@ -42,9 +48,39 @@ const FADE =
42
48
 
43
49
  type RevealItem = {
44
50
  el: HTMLElement;
45
- reveal: (delayMs: number) => void;
51
+ reveal: (delayMs: number, instant?: boolean) => void;
46
52
  };
47
53
 
54
+ /**
55
+ * Whether motion is off, by the same three-state model as styles.css: the app's
56
+ * `data-motion` attribute on <html> wins ("reduce" forces off, "full" forces on),
57
+ * otherwise the OS preference decides.
58
+ */
59
+ function motionReduced(): boolean {
60
+ if (typeof document !== "undefined") {
61
+ const m = document.documentElement.dataset.motion;
62
+ if (m === "reduce") return true;
63
+ if (m === "full") return false;
64
+ }
65
+ return prefersReducedMotion();
66
+ }
67
+
68
+ /** Longest transition-duration on the element, in ms, or the fallback when it can't be read. */
69
+ function transitionMs(el: HTMLElement, fallback: number): number {
70
+ try {
71
+ const parts = getComputedStyle(el).transitionDuration.split(",");
72
+ const ms = Math.max(
73
+ ...parts.map((p) => {
74
+ const v = parseFloat(p);
75
+ return Number.isNaN(v) ? 0 : /ms\s*$/.test(p) ? v : v * 1000;
76
+ }),
77
+ );
78
+ return ms > 0 ? ms : fallback;
79
+ } catch {
80
+ return fallback;
81
+ }
82
+ }
83
+
48
84
  const useIsoLayoutEffect =
49
85
  typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;
50
86
 
@@ -55,19 +91,95 @@ const useIsoLayoutEffect =
55
91
  class CascadeController {
56
92
  private observer: IntersectionObserver | null = null;
57
93
  private items = new Map<Element, RevealItem>();
94
+ /** Cards still hidden and waiting for the observer. */
95
+ private pending = new Set<Element>();
96
+ private globalsBound = false;
58
97
  private batchIndex = 0;
59
98
  private lastEntryAt = 0;
60
99
 
61
100
  private ensureObserver() {
62
101
  if (this.observer || typeof IntersectionObserver === "undefined") return;
102
+ // threshold 0 plus a bottom inset, not a visible-ratio threshold: a ratio
103
+ // can never be met by an element taller than the viewport allows, which
104
+ // would strand a tall card hidden forever. The inset makes the reveal fire
105
+ // once the card's top edge is 12% of the viewport above the bottom, for a
106
+ // card of any height.
63
107
  this.observer = new IntersectionObserver((entries) => this.onIntersect(entries), {
64
- threshold: 0.12,
65
- // Fire slightly before a card is fully in view so the reveal lands as it
66
- // arrives rather than after it's already sitting there.
67
- rootMargin: "0px 0px -8% 0px",
108
+ threshold: 0,
109
+ rootMargin: "0px 0px -12% 0px",
110
+ });
111
+ if (typeof window !== "undefined" && !this.globalsBound) {
112
+ this.globalsBound = true;
113
+ // Printing never scrolls, so show everything before the page is laid out for print.
114
+ window.addEventListener("beforeprint", () => this.revealAllNow());
115
+ // The bottom inset means a card shorter than 12% of the viewport, pinned at the very
116
+ // end of the page, can never cross it. At max scroll, reveal what is still hidden
117
+ // (with the normal fade) so nothing is stranded. The observer stays the main mechanism.
118
+ window.addEventListener("scroll", () => this.checkBottom(), { passive: true });
119
+ // Switching motion off while cards are still hidden shows them at once.
120
+ const off = () => {
121
+ if (motionReduced()) this.revealAllNow();
122
+ };
123
+ new MutationObserver(off).observe(document.documentElement, {
124
+ attributes: true,
125
+ attributeFilter: ["data-motion"],
126
+ });
127
+ if (typeof window.matchMedia === "function") {
128
+ window.matchMedia("(prefers-reduced-motion: reduce)").addEventListener("change", off);
129
+ }
130
+ }
131
+ }
132
+
133
+ private checkQueued = false;
134
+
135
+ /** Schedule one bottom-of-page check for the next frame, however many cards register. */
136
+ private queueBottomCheck() {
137
+ if (this.checkQueued || typeof requestAnimationFrame === "undefined") return;
138
+ this.checkQueued = true;
139
+ requestAnimationFrame(() => {
140
+ this.checkQueued = false;
141
+ this.checkBottom();
68
142
  });
69
143
  }
70
144
 
145
+ private checkBottom() {
146
+ if (this.pending.size === 0) return;
147
+ const root = document.documentElement;
148
+ // 2px of slack covers fractional device pixel ratios.
149
+ if (Math.ceil(window.scrollY + window.innerHeight) < root.scrollHeight - 2) return;
150
+ this.revealPending();
151
+ }
152
+
153
+ /**
154
+ * Fade in the still-hidden cards that are on screen at the bottom of the page, in page
155
+ * order. Cards above the viewport are left to the observer: they animate when scrolled
156
+ * back to, so a jump to the end does not promote the whole page at once.
157
+ */
158
+ private revealPending() {
159
+ const vh = window.innerHeight || document.documentElement.clientHeight || 0;
160
+ const waiting = [...this.pending]
161
+ .map((el) => ({ el, rect: el.getBoundingClientRect() }))
162
+ .filter(({ rect }) => rect.bottom > 0 && rect.top < vh)
163
+ .sort((a, b) => a.rect.top - b.rect.top);
164
+ let step = 0;
165
+ for (const { el } of waiting) {
166
+ this.items.get(el)?.reveal(Math.min(step++, MAX_STAGGER_STEPS) * STAGGER_STEP_MS);
167
+ this.observer?.unobserve(el);
168
+ this.pending.delete(el);
169
+ }
170
+ }
171
+
172
+ /**
173
+ * Show every card at once, without motion. Also settles cards that are mid-animation,
174
+ * so their transitionend listeners and settle timers are cleared now, not at unmount.
175
+ * The observer stays alive so cards mounted later still work.
176
+ */
177
+ private revealAllNow() {
178
+ for (const item of this.items.values()) item.reveal(0, true);
179
+ for (const el of this.pending) this.observer?.unobserve(el);
180
+ this.pending.clear();
181
+ }
182
+
71
183
  private onIntersect(entries: IntersectionObserverEntry[]) {
72
184
  const now = typeof performance !== "undefined" ? performance.now() : 0;
73
185
 
@@ -75,7 +187,7 @@ class CascadeController {
75
187
  // so the wave reads in order regardless of callback ordering. We do NOT hide
76
188
  // on exit — once a card has animated in it stays put, so scrolling back up
77
189
  // never replays it. Each revealed card is unobserved so it can't fire again;
78
- // a full replay only comes from retrigger() (tab re-activation).
190
+ // the card stays revealed for the life of the page.
79
191
  const entering = entries
80
192
  .filter((e) => e.isIntersecting)
81
193
  .sort((a, b) => a.boundingClientRect.top - b.boundingClientRect.top);
@@ -84,11 +196,12 @@ class CascadeController {
84
196
  if (now - this.lastEntryAt > BATCH_QUIET_MS) this.batchIndex = 0;
85
197
  for (const entry of entering) {
86
198
  const item = this.items.get(entry.target);
87
- if (!item) continue;
199
+ if (!item || !this.pending.has(entry.target)) continue;
88
200
  const delay = Math.min(this.batchIndex, MAX_STAGGER_STEPS) * STAGGER_STEP_MS;
89
201
  this.batchIndex += 1;
90
202
  item.reveal(delay);
91
203
  this.observer?.unobserve(entry.target);
204
+ this.pending.delete(entry.target);
92
205
  }
93
206
  this.lastEntryAt = now;
94
207
  }
@@ -96,12 +209,15 @@ class CascadeController {
96
209
  register(item: RevealItem) {
97
210
  this.ensureObserver();
98
211
  this.items.set(item.el, item);
212
+ this.pending.add(item.el);
99
213
  this.observer?.observe(item.el);
214
+ this.queueBottomCheck();
100
215
  }
101
216
 
102
217
  unregister(el: Element) {
103
218
  this.observer?.unobserve(el);
104
219
  this.items.delete(el);
220
+ this.pending.delete(el);
105
221
  }
106
222
  }
107
223
 
@@ -122,34 +238,74 @@ export function Reveal({
122
238
  useIsoLayoutEffect(() => {
123
239
  const el = ref.current;
124
240
  if (!el) return;
125
- if (prefersReducedMotion()) return;
241
+ if (motionReduced()) return;
242
+ // No observer means nothing could ever reveal the card, so leave it visible.
243
+ if (typeof IntersectionObserver === "undefined") return;
126
244
 
127
245
  const vh = window.innerHeight || document.documentElement.clientHeight || 0;
128
246
  const rect = el.getBoundingClientRect();
129
247
  const inViewNow = rect.top < vh && rect.bottom > 0;
130
248
  if (inViewNow) return; // above the fold → just appear
131
249
 
250
+ const clearMotionStyles = () => {
251
+ el.style.transition = "";
252
+ el.style.transitionDelay = "";
253
+ el.style.opacity = "";
254
+ el.style.transform = "";
255
+ el.style.willChange = "";
256
+ };
257
+
132
258
  // Hide without a transition so the hide itself doesn't animate, then arm the
133
- // fade for the scroll-in reveal.
259
+ // fade for the scroll-in reveal. will-change is NOT set here: it is applied
260
+ // only when a card starts animating, so 100+ hidden cards are not all
261
+ // promoted to compositor layers on first load.
134
262
  el.style.transition = "none";
135
263
  el.style.opacity = "0";
136
264
  el.style.transform = HIDDEN_TRANSFORM;
137
265
  void el.offsetHeight; // commit the hidden state before re-enabling transition
138
266
 
267
+ let settleTimer: ReturnType<typeof setTimeout> | undefined;
268
+ let revealing = false;
269
+ const onEnd = (e: TransitionEvent) => {
270
+ if (e.target !== el || e.propertyName !== "transform") return;
271
+ el.removeEventListener("transitionend", onEnd);
272
+ clearTimeout(settleTimer);
273
+ clearMotionStyles();
274
+ };
275
+
139
276
  controller.register({
140
277
  el,
141
- reveal: (delayMs) => {
278
+ reveal: (delayMs, instant) => {
279
+ if (instant) {
280
+ el.removeEventListener("transitionend", onEnd);
281
+ clearTimeout(settleTimer);
282
+ clearMotionStyles();
283
+ return;
284
+ }
285
+ if (revealing) return; // idempotent: a second call must not stack timers
286
+ revealing = true;
287
+ el.addEventListener("transitionend", onEnd);
288
+ el.style.willChange = "opacity, transform";
142
289
  el.style.transition = FADE;
143
290
  el.style.transitionDelay = `${delayMs}ms`;
291
+ // transitionend does not fire if the transition is cancelled, so back it
292
+ // with a timer: the delay plus the duration the token actually resolves to
293
+ // (a fork can lengthen --duration-slow), plus a margin.
294
+ settleTimer = setTimeout(clearMotionStyles, delayMs + transitionMs(el, 1000) + 400);
144
295
  el.style.opacity = "1";
145
296
  el.style.transform = "translateY(0)";
146
297
  },
147
298
  });
148
- return () => controller.unregister(el);
299
+ return () => {
300
+ controller.unregister(el);
301
+ el.removeEventListener("transitionend", onEnd);
302
+ clearTimeout(settleTimer);
303
+ clearMotionStyles();
304
+ };
149
305
  }, []);
150
306
 
151
307
  return (
152
- <div ref={ref} className={className} style={{ willChange: "opacity, transform" }}>
308
+ <div ref={ref} className={className}>
153
309
  {children}
154
310
  </div>
155
311
  );
@@ -19,8 +19,13 @@ import {
19
19
  TooltipTrigger,
20
20
  } from "@behavioral-agentic-design-system/core/components/ui/tooltip";
21
21
  import { Kbd } from "@behavioral-agentic-design-system/core/components/ui/kbd";
22
- import { ColorPalette } from "@behavioral-agentic-design-system/core/components/customize-tab";
22
+ import { BrandMarkSection } from "@behavioral-agentic-design-system/core/components/customize/brand-mark-section";
23
+ import { SaveBar } from "@behavioral-agentic-design-system/core/components/customize/save-bar";
24
+ import { ExportThemeButton } from "@behavioral-agentic-design-system/core/components/customize/theme-io";
25
+ import { SERIF_FONT_OPTIONS } from "@behavioral-agentic-design-system/core/hooks/use-global-serif-font";
26
+ import { MONO_FONT_OPTIONS } from "@behavioral-agentic-design-system/core/hooks/use-global-mono-font";
23
27
  import { SANS_FONT_OPTIONS } from "@behavioral-agentic-design-system/core/hooks/use-global-sans-font";
28
+ import { HEADING_FONT_OPTIONS } from "@behavioral-agentic-design-system/core/hooks/use-global-heading-font";
24
29
  import { RATIO_PRESETS } from "@behavioral-agentic-design-system/core/hooks/use-global-type-scale";
25
30
  import {
26
31
  NUMERIC_TWEAKS,
@@ -31,8 +36,10 @@ import {
31
36
  } from "./types";
32
37
  import { useStudioTweaks } from "./use-studio-tweaks";
33
38
  import { ControlRow } from "./control-row";
39
+ import { TokensPanel } from "./tokens-panel";
40
+ import { useTokenEdits } from "./use-token-edits";
34
41
 
35
- type NumericKey = Exclude<StudioTweakKey, "primaryColor" | "sansFont">;
42
+ type NumericKey = Exclude<StudioTweakKey, "primaryColor" | "sansFont" | "headingFont" | "serifFont" | "monoFont">;
36
43
 
37
44
  const RADIUS_MARKS = [0, 0.25, 0.625, 1, 2];
38
45
 
@@ -77,11 +84,15 @@ export function ControlPanel({
77
84
  /** Omit on mobile, where the outer Controls/Preview switch already does this. */
78
85
  onCollapse?: () => void;
79
86
  }) {
80
- const { tweaks, set, reset, resetAll, isDefault, primaryOptions, addCustomColor } =
81
- useStudioTweaks();
87
+ const studio = useStudioTweaks();
88
+ const { tweaks, set, reset, resetAll, isDefault, primaryOptions, addCustomColor } = studio;
89
+ const tokenEdits = useTokenEdits(studio);
82
90
 
83
91
  const ids = React.useId();
84
92
  const fontId = `${ids}-font`;
93
+ const headingFontId = `${ids}-heading-font`;
94
+ const serifFontId = `${ids}-serif-font`;
95
+ const monoFontId = `${ids}-mono-font`;
85
96
 
86
97
  /* Brand color: the picker speaks hex, the hook speaks option keys. */
87
98
  const optionEntries = Object.entries(primaryOptions);
@@ -191,10 +202,11 @@ export function ControlPanel({
191
202
  <ScrollArea role="region" aria-label="Studio controls" className="min-h-0 flex-1">
192
203
  <div hidden={mode !== "tweak"} className="flex min-w-0 flex-col gap-6 p-4">
193
204
  <p className="text-xs text-muted-foreground">
194
- Changes apply to every preview tab and stay in this browser. Save them from All
195
- tokens.
205
+ Changes apply to every preview and stay in this browser. Save them at the bottom.
196
206
  </p>
197
207
 
208
+ <BrandMarkSection />
209
+
198
210
  <section aria-labelledby={`${ids}-color`} className="flex min-w-0 flex-col gap-5">
199
211
  <GroupHeading>
200
212
  <span id={`${ids}-color`}>Color</span>
@@ -235,7 +247,26 @@ export function ControlPanel({
235
247
  <span id={`${ids}-type`}>Type</span>
236
248
  </GroupHeading>
237
249
  <ControlRow
238
- label="Sans font"
250
+ label="Heading font"
251
+ htmlFor={headingFontId}
252
+ isDefault={isDefault("headingFont")}
253
+ onReset={() => reset("headingFont")}
254
+ >
255
+ <NativeSelect
256
+ id={headingFontId}
257
+ size="xl"
258
+ value={tweaks.headingFont}
259
+ onChange={(e) => set("headingFont", e.target.value)}
260
+ >
261
+ {Object.entries(HEADING_FONT_OPTIONS).map(([key, opt]) => (
262
+ <option key={key} value={key}>
263
+ {opt.label}
264
+ </option>
265
+ ))}
266
+ </NativeSelect>
267
+ </ControlRow>
268
+ <ControlRow
269
+ label="Body font"
239
270
  htmlFor={fontId}
240
271
  isDefault={isDefault("sansFont")}
241
272
  onReset={() => reset("sansFont")}
@@ -253,6 +284,44 @@ export function ControlPanel({
253
284
  ))}
254
285
  </NativeSelect>
255
286
  </ControlRow>
287
+ <ControlRow
288
+ label="Serif font"
289
+ htmlFor={serifFontId}
290
+ isDefault={isDefault("serifFont")}
291
+ onReset={() => reset("serifFont")}
292
+ >
293
+ <NativeSelect
294
+ id={serifFontId}
295
+ size="xl"
296
+ value={tweaks.serifFont}
297
+ onChange={(e) => set("serifFont", e.target.value)}
298
+ >
299
+ {Object.entries(SERIF_FONT_OPTIONS).map(([key, opt]) => (
300
+ <option key={key} value={key}>
301
+ {opt.label}
302
+ </option>
303
+ ))}
304
+ </NativeSelect>
305
+ </ControlRow>
306
+ <ControlRow
307
+ label="Mono font"
308
+ htmlFor={monoFontId}
309
+ isDefault={isDefault("monoFont")}
310
+ onReset={() => reset("monoFont")}
311
+ >
312
+ <NativeSelect
313
+ id={monoFontId}
314
+ size="xl"
315
+ value={tweaks.monoFont}
316
+ onChange={(e) => set("monoFont", e.target.value)}
317
+ >
318
+ {Object.entries(MONO_FONT_OPTIONS).map(([key, opt]) => (
319
+ <option key={key} value={key}>
320
+ {opt.label}
321
+ </option>
322
+ ))}
323
+ </NativeSelect>
324
+ </ControlRow>
256
325
  {numeric("typeBase", { min: baseMin })}
257
326
  {numeric("typeRatio", { max: ratioMax, marks: ratioMarks })}
258
327
  {numeric("letterSpacing")}
@@ -278,20 +347,30 @@ export function ControlPanel({
278
347
  {numeric("spacing")}
279
348
  </section>
280
349
 
281
- <Button
282
- type="button"
283
- variant="outline"
284
- size="xl"
285
- className="w-full"
286
- onClick={resetAll}
287
- >
288
- Reset all tweaks
289
- </Button>
350
+ <section aria-labelledby={`${ids}-save`} className="flex min-w-0 flex-col gap-4">
351
+ <GroupHeading>
352
+ <span id={`${ids}-save`}>Save and export</span>
353
+ </GroupHeading>
354
+ <SaveBar inline />
355
+ <ExportThemeButton />
356
+ <Button
357
+ type="button"
358
+ variant="outline"
359
+ size="xl"
360
+ className="w-full"
361
+ onClick={resetAll}
362
+ >
363
+ Reset all
364
+ </Button>
365
+ </section>
290
366
  </div>
291
367
 
292
- <div hidden={mode !== "tokens"} className="min-w-0 p-4">
293
- <ColorPalette />
294
- </div>
368
+ {/* Mounted only while shown: it is ~280 rows. State lives in the hooks, so nothing is lost on switch. */}
369
+ {mode === "tokens" && (
370
+ <div className="min-w-0 p-4">
371
+ <TokensPanel edits={tokenEdits} />
372
+ </div>
373
+ )}
295
374
  </ScrollArea>
296
375
  </div>
297
376
  </TooltipProvider>
@@ -0,0 +1,167 @@
1
+ "use client";
2
+
3
+ import * as React from "react";
4
+ import { Button } from "@behavioral-agentic-design-system/core/components/ui/button";
5
+ import {
6
+ Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger,
7
+ } from "@behavioral-agentic-design-system/core/components/ui/dialog";
8
+ import { Textarea } from "@behavioral-agentic-design-system/core/components/ui/textarea";
9
+ import { parseThemeInput, type ThemeEntry } from "@behavioral-agentic-design-system/core/lib/theme-import";
10
+ import { TOKEN_ROWS, type TokenRow } from "@behavioral-agentic-design-system/core/lib/token-list";
11
+ import { checkTokenValue } from "@behavioral-agentic-design-system/core/lib/token-value";
12
+ import type { Mode, TokenEdits } from "./use-token-edits";
13
+
14
+ const norm = (v: string) => v.trim().toLowerCase().replace(/\s+/g, " ");
15
+
16
+ /** Index the rows an entry can land on: theme roles and CSS-backed primitives by variable name, everything by path. */
17
+ const BY_VAR = new Map<string, TokenRow>();
18
+ const BY_PATH = new Map<string, TokenRow>();
19
+ for (const r of TOKEN_ROWS) {
20
+ BY_PATH.set(r.path, r);
21
+ if (r.tier === "semantic" && r.path.startsWith("theme.")) BY_VAR.set(r.name, r);
22
+ else if (r.cssVar && r.kind !== "font") BY_VAR.set(r.cssVar.replace(/^--/, "").replace(/^primitive-/, "primitive-"), r);
23
+ }
24
+ // tweakcn and shadcn name the base radius `radius`; the repo's row is borderRadius.lg.
25
+ const RADIUS = BY_PATH.get("borderRadius.lg");
26
+ if (RADIUS) BY_VAR.set("radius", RADIUS);
27
+
28
+ type Resolved = { entry: ThemeEntry; row?: TokenRow; mode: Mode; status: "change" | "same" | "skip"; reason?: string };
29
+
30
+ function resolve(entries: ThemeEntry[], edits: TokenEdits): Resolved[] {
31
+ return entries.map((entry) => {
32
+ const mode = entry.mode;
33
+ const row = entry.from === "json" && BY_PATH.has(entry.name) ? BY_PATH.get(entry.name) : BY_VAR.get(entry.name) ?? BY_PATH.get(entry.name);
34
+ if (!row) {
35
+ const reason = /^font-/.test(entry.name) ? "Fonts are chosen in Tweak" : "No matching token";
36
+ return { entry, mode, status: "skip", reason };
37
+ }
38
+ if (mode === "dark" && row.dark === undefined) return { entry, row, mode, status: "skip", reason: "Only theme colors have a dark value" };
39
+ const { editable, note } = edits.editability(row, mode);
40
+ if (!editable) return { entry, row, mode, status: "skip", reason: note ?? "Read only" };
41
+ // A value that could break out of a declaration is refused here, before Apply, and listed under Not mapped.
42
+ if (row.kind === "font") return { entry, row, mode, status: "skip", reason: "Fonts are chosen in Tweak" };
43
+ const checked = checkTokenValue(entry.value, row.kind);
44
+ if (!checked.ok) return { entry, row, mode, status: "skip", reason: checked.reason };
45
+ return { entry, row, mode, status: norm(edits.valueOf(row, mode)) === norm(entry.value) ? "same" : "change" };
46
+ });
47
+ }
48
+
49
+ const Step = ({ n, children }: { n: number; children: React.ReactNode }) => (
50
+ <li className="flex gap-3">
51
+ <span aria-hidden="true" className="flex size-6 shrink-0 items-center justify-center rounded-full bg-muted text-xs font-medium text-foreground">{n}</span>
52
+ <span className="min-w-0 text-sm text-foreground">{children}</span>
53
+ </li>
54
+ );
55
+
56
+ /**
57
+ * Import a theme into the Studio. Parses pasted or dropped CSS variables or JSON, previews what would change, and
58
+ * Apply writes through the same edit path as the list rows (useTokenEdits), so the result shows in Tweak and the
59
+ * list and is undone by a row's reset or Reset all.
60
+ */
61
+ export function ImportThemeDialog({ edits, children }: { edits: TokenEdits; children: React.ReactNode }) {
62
+ const [open, setOpen] = React.useState(false);
63
+ const [text, setText] = React.useState("");
64
+ const [dragging, setDragging] = React.useState(false);
65
+ const fileRef = React.useRef<HTMLInputElement>(null);
66
+
67
+ const parsed = React.useMemo(() => parseThemeInput(text), [text]);
68
+ const resolved = React.useMemo(() => resolve(parsed.entries, edits), [parsed, edits]);
69
+ const changes = resolved.filter((r) => r.status === "change");
70
+ const same = resolved.filter((r) => r.status === "same").length;
71
+ const skipped = resolved.filter((r) => r.status === "skip");
72
+
73
+ const readFile = async (file: File | null | undefined) => {
74
+ if (!file) return;
75
+ if (file.size > 500_000) return setText("");
76
+ setText(await file.text());
77
+ };
78
+
79
+ const close = (next: boolean) => {
80
+ setOpen(next);
81
+ if (!next) setText("");
82
+ };
83
+
84
+ const apply = () => {
85
+ for (const r of changes) edits.setValue(r.row!, r.mode, r.entry.value);
86
+ close(false);
87
+ };
88
+
89
+ return (
90
+ <Dialog open={open} onOpenChange={close}>
91
+ <DialogTrigger asChild>{children}</DialogTrigger>
92
+ <DialogContent className="flex max-h-[90vh] flex-col gap-4 sm:max-w-2xl">
93
+ <DialogHeader>
94
+ <DialogTitle>Import a theme</DialogTitle>
95
+ <DialogDescription>Bring in colors and scale values from a CSS or JSON theme. You see what changes before anything is applied.</DialogDescription>
96
+ </DialogHeader>
97
+
98
+ <div className="flex min-h-0 flex-1 flex-col gap-4 overflow-y-auto pr-1">
99
+ <ol className="flex flex-col gap-3">
100
+ <Step n={1}>
101
+ Get a theme. Copy the CSS from a theme builder such as <a className="underline underline-offset-4" href="https://tweakcn.com/editor/theme" target="_blank" rel="noopener noreferrer">tweakcn (opens in a new tab)</a>, or use a shadcn theme. To start from your own, choose Export theme in the Tweak tab.
102
+ </Step>
103
+ <Step n={2}>Paste it below, or drop a file on the box, or choose a file.</Step>
104
+ <Step n={3}>Check the summary. It counts what changes, what already matches and what cannot be mapped.</Step>
105
+ <Step n={4}>Choose Apply theme. It uses the same state as manual edits, so it shows in Tweak and the token list. Reset all in Tweak, or a row's reset, undoes it.</Step>
106
+ </ol>
107
+
108
+ <div className="rounded-md border border-border p-3 text-xs text-muted-foreground">
109
+ <p className="font-medium text-foreground">What it reads</p>
110
+ <ul className="mt-1 list-disc pl-4">
111
+ <li>CSS variables in <code>:root</code> and <code>.dark</code> blocks. Colors, radius, shadows and motion map to tokens.</li>
112
+ <li>tokens.json, or part of it, such as a <code>theme</code> block with light and dark values.</li>
113
+ <li>JSON shaped <code>{`{ "light": { ... }, "dark": { ... } }`}</code>.</li>
114
+ <li>Not read: Tokens Studio and Figma exports, SCSS and Tailwind config. Fonts are chosen in Tweak.</li>
115
+ </ul>
116
+ <p className="mt-2">To keep a theme, use Save to tokens.json in Tweak. To share one, use Export theme and paste the CSS back here.</p>
117
+ </div>
118
+
119
+ <div
120
+ onDragOver={(e) => { e.preventDefault(); setDragging(true); }}
121
+ onDragLeave={() => setDragging(false)}
122
+ onDrop={(e) => { e.preventDefault(); setDragging(false); void readFile(e.dataTransfer.files?.[0]); }}
123
+ className={`flex flex-col gap-2 rounded-md ${dragging ? "ring-2 ring-ring" : ""}`}
124
+ >
125
+ <Textarea
126
+ aria-label="Theme CSS or JSON"
127
+ value={text}
128
+ onChange={(e) => setText(e.target.value)}
129
+ spellCheck={false}
130
+ placeholder={`:root {\n --background: oklch(1 0 0);\n --primary: oklch(0.55 0.2 262);\n}\n\n.dark {\n --background: oklch(0.15 0 0);\n}`}
131
+ className="h-40 resize-none overflow-auto font-mono text-xs"
132
+ />
133
+ <div className="flex items-center gap-2">
134
+ <input ref={fileRef} type="file" accept=".css,.json,.txt,text/css,application/json" className="sr-only" tabIndex={-1} aria-hidden="true" onChange={(e) => void readFile(e.target.files?.[0])} />
135
+ <Button type="button" variant="outline" size="xl" onClick={() => fileRef.current?.click()}>Choose file</Button>
136
+ <span className="text-xs text-muted-foreground">.css, .json or .txt, up to 500 KB</span>
137
+ </div>
138
+ </div>
139
+
140
+ <div role="status" aria-live="polite" className="min-h-10 text-sm">
141
+ {parsed.error && <p className="text-destructive">{parsed.error}</p>}
142
+ {!parsed.error && parsed.entries.length > 0 && (
143
+ <div className="flex flex-col gap-2">
144
+ <p data-slot="import-summary" className="text-foreground">
145
+ {`${changes.length} will change, ${same} already match, ${skipped.length} cannot be mapped.`}
146
+ </p>
147
+ {skipped.length > 0 && (
148
+ <details className="text-xs text-muted-foreground">
149
+ <summary className="cursor-pointer text-foreground">Not mapped</summary>
150
+ <ul className="mt-1 max-h-32 overflow-auto">
151
+ {skipped.map((r, i) => <li key={i}><code>{r.entry.name}</code>{`: ${r.reason}`}</li>)}
152
+ </ul>
153
+ </details>
154
+ )}
155
+ </div>
156
+ )}
157
+ </div>
158
+ </div>
159
+
160
+ <DialogFooter>
161
+ <Button type="button" variant="outline" size="xl" onClick={() => close(false)}>Cancel</Button>
162
+ <Button type="button" size="xl" disabled={changes.length === 0} onClick={apply}>Apply theme</Button>
163
+ </DialogFooter>
164
+ </DialogContent>
165
+ </Dialog>
166
+ );
167
+ }