@dinachi/cli 0.12.0 → 1.1.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 (74) hide show
  1. package/CHANGELOG.md +131 -0
  2. package/dist/index.js +73 -7
  3. package/package.json +1 -1
  4. package/templates/accordion/accordion.tsx +70 -12
  5. package/templates/agent/agent.tsx +1865 -0
  6. package/templates/agent/index.ts +52 -0
  7. package/templates/alert-dialog/alert-dialog.tsx +59 -19
  8. package/templates/alert-dialog/index.ts +2 -0
  9. package/templates/animated-icon/animated-icon.tsx +1 -0
  10. package/templates/animated-list/animated-list.tsx +2 -1
  11. package/templates/animated-tabs/animated-tabs.tsx +69 -5
  12. package/templates/autocomplete/autocomplete.tsx +188 -21
  13. package/templates/avatar/avatar.tsx +3 -0
  14. package/templates/badge/badge.tsx +1 -0
  15. package/templates/button/button.tsx +1 -0
  16. package/templates/card/card.tsx +6 -1
  17. package/templates/carousel/carousel.tsx +109 -24
  18. package/templates/checkbox/checkbox.tsx +61 -4
  19. package/templates/checkbox-group/checkbox-group.tsx +83 -8
  20. package/templates/collapsible/collapsible.tsx +15 -4
  21. package/templates/combobox/combobox.tsx +312 -26
  22. package/templates/compare-slider/compare-slider.tsx +104 -28
  23. package/templates/compare-slider/index.ts +1 -1
  24. package/templates/context-menu/context-menu.tsx +93 -24
  25. package/templates/dialog/dialog.tsx +53 -14
  26. package/templates/drawer/drawer.tsx +36 -8
  27. package/templates/expandable-card/expandable-card.tsx +70 -43
  28. package/templates/field/field.tsx +7 -1
  29. package/templates/fieldset/fieldset.tsx +2 -0
  30. package/templates/form/form.tsx +1 -0
  31. package/templates/hold-to-confirm/hold-to-confirm.tsx +100 -16
  32. package/templates/hold-to-confirm/index.ts +1 -1
  33. package/templates/input/input.tsx +60 -1
  34. package/templates/label/label.tsx +1 -0
  35. package/templates/link/link.tsx +1 -0
  36. package/templates/load-transition/load-transition.tsx +1 -0
  37. package/templates/marquee/marquee.tsx +1 -0
  38. package/templates/menu/menu.tsx +45 -12
  39. package/templates/menubar/menubar.tsx +41 -11
  40. package/templates/meter/meter.tsx +5 -1
  41. package/templates/navigation-menu/navigation-menu.tsx +79 -24
  42. package/templates/number-field/index.ts +1 -0
  43. package/templates/number-field/number-field.tsx +77 -2
  44. package/templates/number-ticker/number-ticker.tsx +3 -0
  45. package/templates/otp-field/otp-field.tsx +61 -11
  46. package/templates/popover/popover.tsx +36 -7
  47. package/templates/preview-card/preview-card.tsx +27 -5
  48. package/templates/progress/progress.tsx +5 -1
  49. package/templates/progress-ring/progress-ring.tsx +1 -0
  50. package/templates/radio/radio.tsx +74 -6
  51. package/templates/scroll-area/scroll-area.tsx +14 -4
  52. package/templates/scroll-progress/scroll-progress.tsx +1 -0
  53. package/templates/scroll-reveal/scroll-reveal.tsx +1 -0
  54. package/templates/select/select.tsx +241 -12
  55. package/templates/separator/separator.tsx +1 -0
  56. package/templates/skeleton/skeleton.tsx +1 -0
  57. package/templates/slider/slider.tsx +84 -8
  58. package/templates/sortable/sortable.tsx +79 -9
  59. package/templates/stagger-list/stagger-list.tsx +2 -0
  60. package/templates/streaming-text/streaming-text.tsx +1 -0
  61. package/templates/swipeable-row/index.ts +1 -1
  62. package/templates/swipeable-row/swipeable-row.tsx +126 -41
  63. package/templates/switch/switch.tsx +43 -4
  64. package/templates/tabs/tabs.tsx +79 -10
  65. package/templates/text/text.tsx +1 -0
  66. package/templates/text-morph/text-morph.tsx +2 -0
  67. package/templates/text-shimmer/text-shimmer.tsx +1 -0
  68. package/templates/textarea/index.ts +1 -1
  69. package/templates/textarea/textarea.tsx +71 -4
  70. package/templates/toast/toast.tsx +13 -2
  71. package/templates/toggle/toggle.tsx +40 -7
  72. package/templates/toggle-group/toggle-group.tsx +98 -18
  73. package/templates/toolbar/toolbar.tsx +6 -0
  74. package/templates/tooltip/tooltip.tsx +9 -3
@@ -10,6 +10,13 @@ import {
10
10
  type PanInfo,
11
11
  } from "motion/react"
12
12
  import { cn } from "@/lib/utils"
13
+ import {
14
+ changeAsAgent,
15
+ matchAgentChoice,
16
+ useAgentControl,
17
+ useHasAgent,
18
+ useMergedRef,
19
+ } from "@/components/ui/agent"
13
20
 
14
21
  /**
15
22
  * A carousel that lands where the flick was aimed.
@@ -42,10 +49,34 @@ const PROJECTION = 0.2
42
49
 
43
50
  const SNAP = { type: "spring", stiffness: 380, damping: 40, mass: 0.9 } as const
44
51
 
52
+ /** What moved the carousel. Cancel one from `onIndexChange` and it stays where it was. */
53
+ export interface CarouselChangeDetails {
54
+ reason: "arrow-press" | "dot-press" | "drag" | "scroll" | "imperative-action"
55
+ event: Event | undefined
56
+ isCanceled: boolean
57
+ cancel: () => void
58
+ }
59
+
60
+ function personChange(reason: Exclude<CarouselChangeDetails["reason"], "imperative-action">, event?: Event) {
61
+ const details: CarouselChangeDetails = {
62
+ reason,
63
+ event,
64
+ isCanceled: false,
65
+ cancel: () => {
66
+ details.isCanceled = true
67
+ },
68
+ }
69
+ return details
70
+ }
71
+
72
+ /** Inside `AgentProvider` the carousel is one control, so its arrows and dots stay out. */
73
+ const usePart = () => (useHasAgent() ? "" : undefined)
74
+
45
75
  interface CarouselContextValue {
46
76
  index: number
47
77
  count: number
48
- goTo: (next: number, velocity?: number) => void
78
+ goTo: (next: number, details: CarouselChangeDetails, velocity?: number) => void
79
+ disabled: boolean
49
80
  reducedMotion: boolean
50
81
  x: ReturnType<typeof useMotionValue<number>>
51
82
  offsets: number[]
@@ -56,7 +87,7 @@ interface CarouselContextValue {
56
87
  * shortened list leaves stale nodes behind and a stale node still reports an offset. */
57
88
  measureSlides: (count: number) => number[]
58
89
  currentIndex: () => number
59
- setIndex: (next: number) => void
90
+ setIndex: (next: number, details: CarouselChangeDetails) => number
60
91
  }
61
92
 
62
93
  const CarouselContext = React.createContext<CarouselContextValue | null>(null)
@@ -77,7 +108,10 @@ export interface CarouselProps extends React.ComponentProps<"div"> {
77
108
  label?: string
78
109
  index?: number
79
110
  defaultIndex?: number
80
- onIndexChange?: (index: number) => void
111
+ /** Fired when the slide changes. Cancel `details` to stay on the current slide. */
112
+ onIndexChange?: (index: number, details: CarouselChangeDetails) => void
113
+ /** Turns off the drag, the arrows and the dots. */
114
+ disabled?: boolean
81
115
  }
82
116
 
83
117
  const Carousel = React.forwardRef<HTMLDivElement, CarouselProps>(
@@ -87,6 +121,7 @@ const Carousel = React.forwardRef<HTMLDivElement, CarouselProps>(
87
121
  index: indexProp,
88
122
  defaultIndex = 0,
89
123
  onIndexChange,
124
+ disabled = false,
90
125
  className,
91
126
  children,
92
127
  ...props
@@ -112,13 +147,16 @@ const Carousel = React.forwardRef<HTMLDivElement, CarouselProps>(
112
147
  indexRef.current = index
113
148
  }, [index])
114
149
 
150
+ /** Moves to `next` unless the app cancels, and returns the slide it is on after. */
115
151
  const setIndex = React.useCallback(
116
- (next: number) => {
152
+ (next: number, details: CarouselChangeDetails) => {
117
153
  // Scrolling reports the same slide many times over. Only a change is news.
118
- if (next === indexRef.current) return
154
+ if (next === indexRef.current) return next
155
+ onIndexChange?.(next, details)
156
+ if (details.isCanceled) return indexRef.current
119
157
  indexRef.current = next
120
158
  if (indexProp === undefined) setUncontrolled(next)
121
- onIndexChange?.(next)
159
+ return next
122
160
  },
123
161
  [indexProp, onIndexChange]
124
162
  )
@@ -138,13 +176,13 @@ const Carousel = React.forwardRef<HTMLDivElement, CarouselProps>(
138
176
  }, [])
139
177
 
140
178
  const goTo = React.useCallback(
141
- (next: number, velocity = 0) => {
179
+ (next: number, details: CarouselChangeDetails, velocity = 0) => {
142
180
  const last = offsets.length - 1
143
181
  if (last < 0) return
144
- const clamped = Math.min(Math.max(next, 0), last)
145
- setIndex(clamped)
182
+ // A cancelled change still settles the strip, back onto the slide it stayed on.
183
+ const landed = setIndex(Math.min(Math.max(next, 0), last), details)
146
184
 
147
- const to = offsets[clamped]
185
+ const to = offsets[landed]
148
186
  if (to === undefined) return
149
187
 
150
188
  if (reducedMotion) {
@@ -156,11 +194,33 @@ const Carousel = React.forwardRef<HTMLDivElement, CarouselProps>(
156
194
  [offsets, setIndex, reducedMotion, x]
157
195
  )
158
196
 
197
+ const root = React.useRef<HTMLDivElement>(null)
198
+ useAgentControl(() => {
199
+ const nodes = slides.current.slice(0, count)
200
+ const choices = nodes.map((node, i) => ({ value: i + 1, label: slideName(node, i) }))
201
+ return {
202
+ kind: "carousel",
203
+ element: root.current,
204
+ value: choices[index]?.label,
205
+ items: choices.map((c) => c.label),
206
+ disabled,
207
+ actions: {
208
+ goTo: (wanted: unknown) => {
209
+ const match = matchAgentChoice(choices, wanted)
210
+ if (typeof match === "string") return match
211
+ if (offsets.length === 0) return "The carousel has not measured its slides yet"
212
+ return changeAsAgent<number, CarouselChangeDetails>(goTo, match.value - 1)
213
+ },
214
+ },
215
+ }
216
+ })
217
+
159
218
  const context = React.useMemo(
160
219
  () => ({
161
220
  index,
162
221
  count,
163
222
  goTo,
223
+ disabled,
164
224
  reducedMotion,
165
225
  x,
166
226
  offsets,
@@ -175,6 +235,7 @@ const Carousel = React.forwardRef<HTMLDivElement, CarouselProps>(
175
235
  index,
176
236
  count,
177
237
  goTo,
238
+ disabled,
178
239
  reducedMotion,
179
240
  x,
180
241
  offsets,
@@ -188,11 +249,13 @@ const Carousel = React.forwardRef<HTMLDivElement, CarouselProps>(
188
249
  return (
189
250
  <CarouselContext.Provider value={context}>
190
251
  <div
191
- ref={ref}
252
+ ref={useMergedRef(ref, root)}
192
253
  role="group"
193
254
  aria-roledescription="carousel"
194
255
  aria-label={label}
256
+ data-disabled={disabled ? "" : undefined}
195
257
  className={cn("space-y-4", className)}
258
+ data-slot="carousel"
196
259
  {...props}
197
260
  >
198
261
  {children}
@@ -213,6 +276,7 @@ const CarouselViewport = React.forwardRef<HTMLDivElement, CarouselViewportProps>
213
276
  offsets,
214
277
  setOffsets,
215
278
  goTo,
279
+ disabled,
216
280
  reducedMotion,
217
281
  scroller,
218
282
  measureSlides,
@@ -271,12 +335,16 @@ const CarouselViewport = React.forwardRef<HTMLDivElement, CarouselViewportProps>
271
335
  // thing left to do is keep the dots honest about where it stopped.
272
336
  onScroll={(event) => {
273
337
  if (offsets.length === 0) return
274
- setIndex(nearest(offsets, event.currentTarget.scrollLeft))
338
+ const target = nearest(offsets, event.currentTarget.scrollLeft)
339
+ const landed = setIndex(target, personChange("scroll", event.nativeEvent))
340
+ if (landed !== target) event.currentTarget.scrollTo({ left: offsets[landed], behavior: "auto" })
275
341
  }}
276
342
  className={cn(
277
- "relative flex snap-x snap-mandatory gap-3 overflow-x-auto pb-2",
343
+ "relative flex snap-x snap-mandatory gap-3 pb-2",
344
+ disabled ? "overflow-x-hidden" : "overflow-x-auto",
278
345
  className
279
346
  )}
347
+ data-slot="carousel-viewport"
280
348
  {...props}
281
349
  >
282
350
  {positioned}
@@ -284,23 +352,24 @@ const CarouselViewport = React.forwardRef<HTMLDivElement, CarouselViewportProps>
284
352
  )
285
353
  }
286
354
 
287
- const onDragEnd = (_: unknown, info: PanInfo) => {
355
+ const onDragEnd = (event: MouseEvent | TouchEvent | PointerEvent, info: PanInfo) => {
288
356
  if (offsets.length === 0) return
289
357
 
290
358
  const projected = -(x.get() + info.velocity.x * PROJECTION)
291
359
  // Nearest by measured position rather than by dividing through a nominal slide width,
292
360
  // so uneven slides land as accurately as even ones.
293
- goTo(nearest(offsets, projected), info.velocity.x)
361
+ goTo(nearest(offsets, projected), personChange("drag", event), info.velocity.x)
294
362
  }
295
363
 
296
364
  return (
297
365
  <div
298
366
  ref={mergeRefs(ref, scroller)}
299
367
  className={cn("overflow-hidden", className)}
368
+ data-slot="carousel-viewport"
300
369
  {...props}
301
370
  >
302
371
  <motion.div
303
- drag="x"
372
+ drag={disabled ? false : "x"}
304
373
  style={{ x }}
305
374
  dragConstraints={{ left: -(offsets[offsets.length - 1] ?? 0), right: 0 }}
306
375
  // Give at both ends rather than a wall, so the edge of the set is something you
@@ -344,6 +413,7 @@ const CarouselSlide = React.forwardRef<HTMLDivElement, CarouselSlideProps>(
344
413
  aria-roledescription="slide"
345
414
  aria-label={label ? `${counted}: ${label}` : counted}
346
415
  className={cn("w-[72%] shrink-0 snap-start", className)}
416
+ data-slot="carousel-slide"
347
417
  {...props}
348
418
  />
349
419
  )
@@ -366,7 +436,8 @@ export interface CarouselDotsProps extends React.ComponentProps<"div"> {
366
436
  */
367
437
  const CarouselDots = React.forwardRef<HTMLDivElement, CarouselDotsProps>(
368
438
  ({ labels, dotClassName, className, ...props }, ref) => {
369
- const { index, count, goTo } = useCarousel("CarouselDots")
439
+ const { index, count, goTo, disabled } = useCarousel("CarouselDots")
440
+ const part = usePart()
370
441
 
371
442
  return (
372
443
  <div
@@ -374,6 +445,7 @@ const CarouselDots = React.forwardRef<HTMLDivElement, CarouselDotsProps>(
374
445
  role="group"
375
446
  aria-label="Choose a slide"
376
447
  className={cn("flex gap-1.5", className)}
448
+ data-slot="carousel-dots"
377
449
  {...props}
378
450
  >
379
451
  {Array.from({ length: count }, (_, i) => (
@@ -384,7 +456,9 @@ const CarouselDots = React.forwardRef<HTMLDivElement, CarouselDotsProps>(
384
456
  labels?.[i] ? `Go to slide ${i + 1}: ${labels[i]}` : `Go to slide ${i + 1}`
385
457
  }
386
458
  aria-current={i === index ? "true" : undefined}
387
- onClick={() => goTo(i)}
459
+ disabled={disabled}
460
+ data-agent-part={part}
461
+ onClick={(event) => goTo(i, personChange("dot-press", event.nativeEvent))}
388
462
  className={cn(
389
463
  "h-1.5 rounded-full transition-all duration-200 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background",
390
464
  i === index ? "w-6 bg-primary" : "w-1.5 bg-border hover:bg-muted-foreground",
@@ -402,16 +476,18 @@ export type CarouselArrowProps = React.ComponentProps<"button">
402
476
 
403
477
  const CarouselPrevious = React.forwardRef<HTMLButtonElement, CarouselArrowProps>(
404
478
  ({ className, children, ...props }, ref) => {
405
- const { index, goTo } = useCarousel("CarouselPrevious")
479
+ const { index, goTo, disabled } = useCarousel("CarouselPrevious")
406
480
 
407
481
  return (
408
482
  <button
409
483
  ref={ref}
410
484
  type="button"
411
485
  aria-label="Previous slide"
412
- disabled={index === 0}
413
- onClick={() => goTo(index - 1)}
486
+ disabled={disabled || index === 0}
487
+ data-agent-part={usePart()}
488
+ onClick={(event) => goTo(index - 1, personChange("arrow-press", event.nativeEvent))}
414
489
  className={cn(arrowClass, className)}
490
+ data-slot="carousel-previous"
415
491
  {...props}
416
492
  >
417
493
  {children ?? <ChevronIcon direction="left" />}
@@ -423,16 +499,18 @@ CarouselPrevious.displayName = "CarouselPrevious"
423
499
 
424
500
  const CarouselNext = React.forwardRef<HTMLButtonElement, CarouselArrowProps>(
425
501
  ({ className, children, ...props }, ref) => {
426
- const { index, count, goTo } = useCarousel("CarouselNext")
502
+ const { index, count, goTo, disabled } = useCarousel("CarouselNext")
427
503
 
428
504
  return (
429
505
  <button
430
506
  ref={ref}
431
507
  type="button"
432
508
  aria-label="Next slide"
433
- disabled={count === 0 || index === count - 1}
434
- onClick={() => goTo(index + 1)}
509
+ disabled={disabled || count === 0 || index === count - 1}
510
+ data-agent-part={usePart()}
511
+ onClick={(event) => goTo(index + 1, personChange("arrow-press", event.nativeEvent))}
435
512
  className={cn(arrowClass, className)}
513
+ data-slot="carousel-next"
436
514
  {...props}
437
515
  >
438
516
  {children ?? <ChevronIcon direction="right" />}
@@ -463,6 +541,13 @@ function ChevronIcon({ direction }: { direction: "left" | "right" }) {
463
541
  )
464
542
  }
465
543
 
544
+ /** A slide's own name, without the "2 of 5: " its accessible name starts with. */
545
+ function slideName(node: HTMLElement | null | undefined, i: number) {
546
+ const name = node?.getAttribute("aria-label") ?? ""
547
+ const at = name.indexOf(": ")
548
+ return at < 0 ? String(i + 1) : name.slice(at + 2)
549
+ }
550
+
466
551
  function nearest(offsets: number[], target: number) {
467
552
  let closest = 0
468
553
  for (let i = 1; i < offsets.length; i += 1) {
@@ -2,20 +2,76 @@ import * as React from "react";
2
2
  import { Checkbox as BaseCheckbox } from '@base-ui/react/checkbox';
3
3
  import { cn } from "@/lib/utils";
4
4
  import { Check } from "lucide-react";
5
+ import {
6
+ AgentGroupContext,
7
+ agentChecked,
8
+ changeAsAgent,
9
+ useAgentControl,
10
+ useAgentItem,
11
+ useAgentState,
12
+ useMergedRef,
13
+ } from "@/components/ui/agent";
5
14
 
15
+ type CheckboxProps = React.ComponentPropsWithoutRef<typeof BaseCheckbox.Root>;
16
+
17
+ function ariaChecked(el: Element | null): boolean | "mixed" {
18
+ const state = el?.getAttribute("aria-checked");
19
+ return state === "mixed" ? "mixed" : state === "true";
20
+ }
21
+
22
+ /**
23
+ * Base UI's checkbox, holding its own checked state so an agent can check it the way a
24
+ * parent would. Controlled use is unchanged, and `onCheckedChange` sees the agent's
25
+ * changes first and can cancel them. In a group the group's value decides, so the agent
26
+ * goes through the group's `onValueChange` instead.
27
+ */
6
28
  const Checkbox = React.forwardRef<
7
29
  React.ComponentRef<typeof BaseCheckbox.Root>,
8
- React.ComponentPropsWithoutRef<typeof BaseCheckbox.Root>
9
- >(({ className, ...props }, ref) => (
30
+ CheckboxProps
31
+ >(({ className, checked: checkedProp, defaultChecked = false, onCheckedChange, ...props }, ref) => {
32
+ const [checked, change] = useAgentState<boolean, BaseCheckbox.Root.ChangeEventDetails>(
33
+ checkedProp,
34
+ defaultChecked,
35
+ onCheckedChange
36
+ );
37
+ const group = React.useContext(AgentGroupContext);
38
+ const element = React.useRef<HTMLElement | null>(null);
39
+ const mergedRef = useMergedRef(ref, element);
40
+ const grouped = group && (props.parent || props.value !== undefined) ? group : null;
41
+ useAgentItem(grouped?.items ?? null, element, props.parent ? undefined : props.value);
42
+
43
+ useAgentControl(() => {
44
+ const now = ariaChecked(element.current);
45
+ const set = (next: boolean) => {
46
+ if (props.readOnly) return "The checkbox is read-only.";
47
+ if (next === now) return undefined;
48
+ return grouped ? grouped.set(props.parent ? undefined : props.value, next) : changeAsAgent(change, next);
49
+ };
50
+ return {
51
+ kind: "checkbox",
52
+ element: element.current,
53
+ checked: now,
54
+ disabled: props.disabled,
55
+ actions: {
56
+ toggle: () => set(now !== true),
57
+ setChecked: (value: unknown) => set(agentChecked(value)),
58
+ },
59
+ };
60
+ });
61
+
62
+ return (
10
63
  <span className="relative inline-flex [&>input]:!absolute">
11
64
  <BaseCheckbox.Root
12
- ref={ref}
65
+ ref={mergedRef}
66
+ data-slot="checkbox"
13
67
  className={cn(
14
68
  "peer h-4 w-4 shrink-0 rounded-sm border border-primary ring-offset-background focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50",
15
69
  "data-[checked]:bg-primary data-[checked]:text-primary-foreground",
16
70
  className
17
71
  )}
18
72
  {...props}
73
+ checked={checked}
74
+ onCheckedChange={change}
19
75
  >
20
76
  <BaseCheckbox.Indicator
21
77
  className={cn("flex items-center justify-center text-current")}
@@ -24,7 +80,8 @@ const Checkbox = React.forwardRef<
24
80
  </BaseCheckbox.Indicator>
25
81
  </BaseCheckbox.Root>
26
82
  </span>
27
- ));
83
+ );
84
+ });
28
85
  Checkbox.displayName = "Checkbox";
29
86
 
30
87
  export { Checkbox };
@@ -1,17 +1,92 @@
1
1
  import * as React from "react";
2
2
  import { CheckboxGroup as BaseCheckboxGroup } from "@base-ui/react/checkbox-group";
3
3
  import { cn } from "@/lib/utils";
4
+ import {
5
+ AgentGroupContext,
6
+ agentChoicesOf,
7
+ changeAsAgent,
8
+ matchAgentChoice,
9
+ useAgentControl,
10
+ useAgentLatest,
11
+ useAgentState,
12
+ useHasAgent,
13
+ useMergedRef,
14
+ type AgentChoice,
15
+ type AgentGroup,
16
+ } from "@/components/ui/agent";
4
17
 
18
+ type Details = BaseCheckboxGroup.ChangeEventDetails;
19
+
20
+ function valuesFor(choices: AgentChoice<string>[], value: string[], wanted: unknown): string[] | string {
21
+ const next = value.filter((v) => !choices.some((c) => c.value === v && !c.disabled));
22
+ for (const one of Array.isArray(wanted) ? wanted : [wanted]) {
23
+ const match = matchAgentChoice(choices, one);
24
+ if (typeof match === "string") return match;
25
+ if (!next.includes(match.value)) next.push(match.value);
26
+ }
27
+ return next;
28
+ }
29
+
30
+ /**
31
+ * Base UI's group, holding its own value so an agent can check boxes the way a parent
32
+ * would. Controlled use is unchanged, and `onValueChange` sees the agent's changes first
33
+ * and can cancel them.
34
+ */
5
35
  const CheckboxGroup = React.forwardRef<
6
36
  React.ComponentRef<typeof BaseCheckboxGroup>,
7
37
  React.ComponentPropsWithoutRef<typeof BaseCheckboxGroup>
8
- >(({ className, ...props }, ref) => (
9
- <BaseCheckboxGroup
10
- ref={ref}
11
- className={cn("flex flex-col items-start gap-1", className)}
12
- {...props}
13
- />
14
- ));
38
+ >(({ className, value: valueProp, defaultValue, onValueChange, ...props }, ref) => {
39
+ const [value, change] = useAgentState<string[], Details>(valueProp, defaultValue ?? [], onValueChange);
40
+ const element = React.useRef<HTMLDivElement | null>(null);
41
+ const mergedRef = useMergedRef(ref, element);
42
+ const latest = useAgentLatest({ value, change, allValues: props.allValues });
43
+
44
+ const [agent] = React.useState<AgentGroup>(() => {
45
+ const items: AgentGroup["items"] = new Map();
46
+ return {
47
+ items,
48
+ set(item, checked) {
49
+ const { value: now, change: commit, allValues = [] } = latest.current;
50
+ // The parent checkbox leaves disabled children as they are, as Base UI's does.
51
+ const disabled = [...items].filter(([el]) => el.hasAttribute("data-disabled")).map(([, v]) => v);
52
+ const changed = item === undefined ? allValues.filter((v) => !disabled.includes(v)) : [item];
53
+ if (!changed.length) return undefined;
54
+ const next = checked ? [...new Set([...now, ...changed])] : now.filter((v) => !changed.includes(v));
55
+ return changeAsAgent(commit, next);
56
+ },
57
+ };
58
+ });
59
+
60
+ useAgentControl(() => {
61
+ const choices = agentChoicesOf(agent.items);
62
+ const setValue = (wanted: unknown) => {
63
+ const next = valuesFor(choices, value, wanted);
64
+ return typeof next === "string" ? next : changeAsAgent(change, next);
65
+ };
66
+ return {
67
+ kind: "checkbox-group",
68
+ element: choices.length ? element.current : null,
69
+ value: choices.filter((c) => value.includes(c.value)).map((c) => c.label),
70
+ items: choices.map((c) => c.label),
71
+ multiple: true,
72
+ disabled: props.disabled,
73
+ actions: { setValue },
74
+ };
75
+ });
76
+
77
+ return (
78
+ <AgentGroupContext.Provider value={useHasAgent() ? agent : null}>
79
+ <BaseCheckboxGroup
80
+ ref={mergedRef}
81
+ data-slot="checkbox-group"
82
+ className={cn("flex flex-col items-start gap-1", className)}
83
+ {...props}
84
+ value={value}
85
+ onValueChange={change}
86
+ />
87
+ </AgentGroupContext.Provider>
88
+ );
89
+ });
15
90
  CheckboxGroup.displayName = "CheckboxGroup";
16
91
 
17
- export { CheckboxGroup };
92
+ export { CheckboxGroup };
@@ -3,13 +3,22 @@
3
3
  import * as React from "react"
4
4
  import { Collapsible as BaseCollapsible } from "@base-ui/react/collapsible"
5
5
  import { cn } from "@/lib/utils"
6
+ import { type AgentOpenParts, useAgentOpen, useHasAgent, useMergedRef } from "@/components/ui/agent"
6
7
 
8
+ const CollapsibleAgentContext = React.createContext<AgentOpenParts | null>(null)
9
+
10
+ /** Base UI's root, holding its own open state so an agent can open and close it. */
7
11
  const Collapsible = React.forwardRef<
8
12
  React.ComponentRef<typeof BaseCollapsible.Root>,
9
13
  React.ComponentPropsWithoutRef<typeof BaseCollapsible.Root>
10
- >(({ className, ...props }, ref) => (
11
- <BaseCollapsible.Root ref={ref} className={cn("w-full", className)} {...props} />
12
- ))
14
+ >(({ className, open, defaultOpen, onOpenChange, ...props }, ref) => {
15
+ const [state, change, parts] = useAgentOpen("disclosure", open, defaultOpen, onOpenChange)
16
+ return (
17
+ <CollapsibleAgentContext.Provider value={useHasAgent() ? parts : null}>
18
+ <BaseCollapsible.Root data-slot="collapsible" ref={ref} className={cn("w-full", className)} {...props} open={state} onOpenChange={change} />
19
+ </CollapsibleAgentContext.Provider>
20
+ )
21
+ })
13
22
  Collapsible.displayName = "Collapsible"
14
23
 
15
24
  const CollapsibleTrigger = React.forwardRef<
@@ -17,7 +26,8 @@ const CollapsibleTrigger = React.forwardRef<
17
26
  React.ComponentPropsWithoutRef<typeof BaseCollapsible.Trigger>
18
27
  >(({ className, ...props }, ref) => (
19
28
  <BaseCollapsible.Trigger
20
- ref={ref}
29
+ data-slot="collapsible-trigger"
30
+ ref={useMergedRef(ref, React.useContext(CollapsibleAgentContext)?.trigger)}
21
31
  className={cn(
22
32
  "flex w-full items-center justify-between rounded-md p-4 font-medium transition-colors",
23
33
  "hover:bg-accent hover:text-accent-foreground",
@@ -36,6 +46,7 @@ const CollapsiblePanel = React.forwardRef<
36
46
  React.ComponentPropsWithoutRef<typeof BaseCollapsible.Panel>
37
47
  >(({ className, ...props }, ref) => (
38
48
  <BaseCollapsible.Panel
49
+ data-slot="collapsible-panel"
39
50
  ref={ref}
40
51
  className={cn(
41
52
  "overflow-hidden text-sm h-[var(--collapsible-panel-height)]",