@dinachi/cli 0.11.0 → 1.0.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 (49) hide show
  1. package/CHANGELOG.md +146 -0
  2. package/dist/index.js +74 -8
  3. package/package.json +2 -2
  4. package/templates/accordion/accordion.tsx +66 -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 +46 -17
  8. package/templates/alert-dialog/index.ts +2 -0
  9. package/templates/animated-tabs/animated-tabs.tsx +65 -5
  10. package/templates/autocomplete/autocomplete.tsx +174 -18
  11. package/templates/avatar/avatar.tsx +5 -2
  12. package/templates/carousel/carousel.tsx +102 -24
  13. package/templates/checkbox/checkbox.tsx +60 -4
  14. package/templates/checkbox-group/checkbox-group.tsx +82 -8
  15. package/templates/collapsible/collapsible.tsx +13 -4
  16. package/templates/combobox/combobox.tsx +299 -23
  17. package/templates/combobox/index.ts +1 -0
  18. package/templates/compare-slider/compare-slider.tsx +103 -28
  19. package/templates/compare-slider/index.ts +1 -1
  20. package/templates/context-menu/context-menu.tsx +37 -15
  21. package/templates/dialog/dialog.tsx +41 -12
  22. package/templates/drawer/drawer.tsx +18 -3
  23. package/templates/expandable-card/expandable-card.tsx +65 -42
  24. package/templates/field/field.tsx +2 -0
  25. package/templates/hold-to-confirm/hold-to-confirm.tsx +99 -16
  26. package/templates/hold-to-confirm/index.ts +1 -1
  27. package/templates/input/input.tsx +59 -1
  28. package/templates/menu/index.ts +5 -0
  29. package/templates/menu/menu.tsx +105 -10
  30. package/templates/menubar/menubar.tsx +26 -8
  31. package/templates/navigation-menu/navigation-menu.tsx +67 -23
  32. package/templates/number-field/index.ts +1 -0
  33. package/templates/number-field/number-field.tsx +71 -2
  34. package/templates/otp-field/otp-field.tsx +57 -11
  35. package/templates/popover/popover.tsx +18 -3
  36. package/templates/preview-card/preview-card.tsx +16 -2
  37. package/templates/radio/radio.tsx +71 -6
  38. package/templates/select/select.tsx +223 -10
  39. package/templates/slider/slider.tsx +77 -8
  40. package/templates/sortable/sortable.tsx +76 -9
  41. package/templates/swipeable-row/index.ts +1 -1
  42. package/templates/swipeable-row/swipeable-row.tsx +125 -41
  43. package/templates/switch/switch.tsx +41 -4
  44. package/templates/tabs/tabs.tsx +74 -10
  45. package/templates/textarea/index.ts +1 -1
  46. package/templates/textarea/textarea.tsx +70 -4
  47. package/templates/toast/toast.tsx +3 -1
  48. package/templates/toggle/toggle.tsx +39 -7
  49. package/templates/toggle-group/toggle-group.tsx +96 -18
@@ -13,6 +13,7 @@ import {
13
13
  type PanInfo,
14
14
  } from "motion/react"
15
15
  import { cn } from "@/lib/utils"
16
+ import { changeAsAgent, useAgentControl, useMergedRef } from "@/components/ui/agent"
16
17
 
17
18
  /** `useReducedMotion` reads the OS setting only, and only at mount. Also honour an
18
19
  * app-level `<MotionConfig reducedMotion>`, so a user preference in the host app
@@ -62,11 +63,33 @@ function SwipeableRowGroup({ children }: { children: React.ReactNode }) {
62
63
  )
63
64
  }
64
65
 
66
+ /** Passed to `onSelect` and `onDismiss`. `cancel()` refuses an agent's press, and keeps a
67
+ * swiped row in place. */
68
+ export interface SwipeableRowChangeDetails {
69
+ reason: "trigger-press" | "swipe" | "imperative-action"
70
+ event: Event | undefined
71
+ isCanceled: boolean
72
+ cancel: () => void
73
+ }
74
+
75
+ function personChange(reason: "trigger-press" | "swipe", event?: Event): SwipeableRowChangeDetails {
76
+ const details: SwipeableRowChangeDetails = {
77
+ reason,
78
+ event,
79
+ isCanceled: false,
80
+ cancel: () => {
81
+ details.isCanceled = true
82
+ },
83
+ }
84
+ return details
85
+ }
86
+
65
87
  export interface SwipeableRowAction {
66
88
  /** Accessible name for the button. It has only an icon, so this is the only name. */
67
89
  label: string
68
90
  icon: React.ReactNode
69
- onSelect: () => void
91
+ onSelect: (details: SwipeableRowChangeDetails) => void
92
+ disabled?: boolean
70
93
  /** Styles the button as destructive, and lends its icon to the full-swipe layer. */
71
94
  destructive?: boolean
72
95
  className?: string
@@ -81,7 +104,7 @@ export interface SwipeableRowProps
81
104
  * Committing a full swipe. Omit it and the row only ever opens — there is no
82
105
  * threshold, and no destructive layer.
83
106
  */
84
- onDismiss?: () => void
107
+ onDismiss?: (details: SwipeableRowChangeDetails) => void
85
108
  /** Fraction of the row's width past which releasing commits `onDismiss`. */
86
109
  dismissAt?: number
87
110
  /** Width of each action button in px. */
@@ -105,6 +128,18 @@ export interface SwipeableRowProps
105
128
  * The projection is measured from where the row *is*, not from how far the finger moved.
106
129
  * Under drag elasticity those two disagree, and the eye is following the row.
107
130
  */
131
+ /** The row's text with its pieces kept apart, so a title, badge and name read as words. */
132
+ function wordsOf(el: Element | null): string | undefined {
133
+ if (!el) return undefined
134
+ const words: string[] = []
135
+ const walker = el.ownerDocument.createTreeWalker(el, NodeFilter.SHOW_TEXT)
136
+ while (walker.nextNode()) {
137
+ const text = walker.currentNode.nodeValue?.trim()
138
+ if (text) words.push(text)
139
+ }
140
+ return words.join(" ") || undefined
141
+ }
142
+
108
143
  const SwipeableRow = React.forwardRef<HTMLDivElement, SwipeableRowProps>(
109
144
  // The gesture is already out in `useSwipeGesture`. What is left is four layers that
110
145
  // each exist only under some condition — actions, the committed wash, the armed icon,
@@ -125,6 +160,7 @@ const SwipeableRow = React.forwardRef<HTMLDivElement, SwipeableRowProps>(
125
160
  const reducedMotion = useReducedMotionConfig()
126
161
  const x = useMotionValue(0)
127
162
  const rowRef = React.useRef<HTMLDivElement>(null)
163
+ const contentRef = React.useRef<HTMLDivElement>(null)
128
164
 
129
165
  /** Where the row rests with its actions showing. Derived, so adding an action moves
130
166
  * the resting point instead of leaving a button off the edge. */
@@ -174,6 +210,25 @@ const SwipeableRow = React.forwardRef<HTMLDivElement, SwipeableRowProps>(
174
210
  onDismiss,
175
211
  })
176
212
 
213
+ // The row is a region its action buttons sit in, named by its content.
214
+ useAgentControl(() => {
215
+ const row = rowRef.current
216
+ return {
217
+ kind: "region",
218
+ element: row,
219
+ label: row?.matches("[aria-label], [aria-labelledby]")
220
+ ? undefined
221
+ : wordsOf(contentRef.current),
222
+ actions: {
223
+ ...(onDismiss && {
224
+ dismiss: () =>
225
+ changeAsAgent((_: undefined, d: SwipeableRowChangeDetails) => onDismiss(d), undefined) ??
226
+ void animate(x, -width, SETTLE),
227
+ }),
228
+ },
229
+ }
230
+ })
231
+
177
232
  const destructive = actions.find((action) => action.destructive)
178
233
  const snap = reducedMotion
179
234
  ? { duration: 0 }
@@ -181,40 +236,20 @@ const SwipeableRow = React.forwardRef<HTMLDivElement, SwipeableRowProps>(
181
236
 
182
237
  return (
183
238
  <div
184
- ref={mergeRefs(ref, rowRef)}
239
+ ref={useMergedRef(ref, rowRef)}
185
240
  className={cn("relative overflow-hidden rounded-xl", className)}
186
241
  {...props}
187
242
  >
188
243
  <div className="absolute inset-y-0 right-0 flex items-stretch">
189
244
  {actions.map((action) => (
190
- // Real buttons, always focusable. A swipe is not an accessible affordance on
191
- // its own, so the keyboard path is the same set of controls — focusing one
192
- // opens the row so the reader can see what they are on.
193
- <motion.button
245
+ <ActionButton
194
246
  key={action.label}
195
- type="button"
196
- aria-label={action.label}
247
+ action={action}
248
+ width={actionWidth}
249
+ motionStyle={reducedMotion ? null : { scale: actionScale, opacity: actionOpacity }}
197
250
  onFocus={() => settle(openAt)}
198
251
  onBlur={() => settle(0)}
199
- onClick={action.onSelect}
200
- // Width inline rather than as a class, so `actionWidth` cannot drift out of
201
- // step with a Tailwind arbitrary value.
202
- style={{
203
- width: actionWidth,
204
- ...(reducedMotion
205
- ? null
206
- : { scale: actionScale, opacity: actionOpacity }),
207
- }}
208
- className={cn(
209
- "flex shrink-0 items-center justify-center focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring",
210
- action.destructive
211
- ? "bg-destructive text-destructive-foreground"
212
- : "bg-muted text-foreground",
213
- action.className
214
- )}
215
- >
216
- {action.icon}
217
- </motion.button>
252
+ />
218
253
  ))}
219
254
  </div>
220
255
 
@@ -269,6 +304,7 @@ const SwipeableRow = React.forwardRef<HTMLDivElement, SwipeableRowProps>(
269
304
  )}
270
305
  >
271
306
  <div
307
+ ref={contentRef}
272
308
  className="min-w-0 flex-1 px-4 py-3.5"
273
309
  // Under reduced motion the row never moves, so the actions have to be given
274
310
  // room rather than revealed.
@@ -283,6 +319,59 @@ const SwipeableRow = React.forwardRef<HTMLDivElement, SwipeableRowProps>(
283
319
  )
284
320
  SwipeableRow.displayName = "SwipeableRow"
285
321
 
322
+ /**
323
+ * One action under the row. Real buttons, always focusable: a swipe is not an accessible
324
+ * affordance on its own, so the keyboard path is the same set of controls, and focusing
325
+ * one opens the row so the reader can see what they are on. An agent presses them too,
326
+ * hidden or not.
327
+ */
328
+ function ActionButton({
329
+ action,
330
+ width,
331
+ motionStyle,
332
+ onFocus,
333
+ onBlur,
334
+ }: {
335
+ action: SwipeableRowAction
336
+ width: number
337
+ motionStyle: { scale: MotionValue<number>; opacity: MotionValue<number> } | null
338
+ onFocus: () => void
339
+ onBlur: () => void
340
+ }) {
341
+ const element = React.useRef<HTMLButtonElement>(null)
342
+ useAgentControl(() => ({
343
+ kind: "button",
344
+ element: element.current,
345
+ label: action.label,
346
+ disabled: action.disabled,
347
+ actions: {
348
+ press: () => changeAsAgent((_: undefined, d: SwipeableRowChangeDetails) => action.onSelect(d), undefined),
349
+ },
350
+ }))
351
+ return (
352
+ <motion.button
353
+ ref={element}
354
+ type="button"
355
+ aria-label={action.label}
356
+ disabled={action.disabled}
357
+ onFocus={onFocus}
358
+ onBlur={onBlur}
359
+ onClick={(event) => action.onSelect(personChange("trigger-press", event.nativeEvent))}
360
+ // Width inline rather than as a class, so `actionWidth` cannot drift out of step
361
+ // with a Tailwind arbitrary value.
362
+ style={{ width, ...motionStyle }}
363
+ className={cn(
364
+ "flex shrink-0 items-center justify-center focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring",
365
+ "disabled:opacity-50",
366
+ action.destructive ? "bg-destructive text-destructive-foreground" : "bg-muted text-foreground",
367
+ action.className
368
+ )}
369
+ >
370
+ {action.icon}
371
+ </motion.button>
372
+ )
373
+ }
374
+
286
375
  /**
287
376
  * Everything the release decides: where the row lands, and whether it lands at all.
288
377
  *
@@ -300,7 +389,7 @@ function useSwipeGesture({
300
389
  openAt: number
301
390
  width: number
302
391
  dismissAt: number
303
- onDismiss?: () => void
392
+ onDismiss?: (details: SwipeableRowChangeDetails) => void
304
393
  }) {
305
394
  const groupRef = React.useContext(SwipeableRowGroupContext)
306
395
 
@@ -330,7 +419,7 @@ function useSwipeGesture({
330
419
  )
331
420
 
332
421
  const onDragEnd = React.useCallback(
333
- (_: unknown, info: PanInfo) => {
422
+ (event: unknown, info: PanInfo) => {
334
423
  const projected = x.get() + info.velocity.x * PROJECTION
335
424
 
336
425
  if (onDismiss && width && projected < -width * dismissAt) {
@@ -342,7 +431,12 @@ function useSwipeGesture({
342
431
  velocity: info.velocity.x,
343
432
  stiffness: 420,
344
433
  damping: 46,
345
- onComplete: onDismiss,
434
+ // Refused, the row comes back rather than leaving a gap where it was.
435
+ onComplete: () => {
436
+ const details = personChange("swipe", event instanceof Event ? event : undefined)
437
+ onDismiss(details)
438
+ if (details.isCanceled) animate(x, 0, SETTLE)
439
+ },
346
440
  })
347
441
  return
348
442
  }
@@ -355,14 +449,4 @@ function useSwipeGesture({
355
449
  return { settle, onDragEnd }
356
450
  }
357
451
 
358
- /** The row needs its own measurement and the caller may want the node too. */
359
- function mergeRefs<T>(...refs: (React.Ref<T> | undefined)[]): React.RefCallback<T> {
360
- return (value) => {
361
- for (const ref of refs) {
362
- if (typeof ref === "function") ref(value)
363
- else if (ref) (ref as React.RefObject<T | null>).current = value
364
- }
365
- }
366
- }
367
-
368
452
  export { SwipeableRow, SwipeableRowGroup }
@@ -3,14 +3,48 @@
3
3
  import * as React from "react"
4
4
  import { Switch as SwitchPrimitive } from "@base-ui/react/switch"
5
5
  import { cn } from "@/lib/utils"
6
+ import { agentChecked, changeAsAgent, useAgentControl, useAgentState, useMergedRef } from "@/components/ui/agent"
6
7
 
8
+ type SwitchProps = React.ComponentPropsWithoutRef<typeof SwitchPrimitive.Root>
9
+
10
+ /**
11
+ * Base UI's switch, holding its own checked state so an agent can flip it the way a parent
12
+ * would. Controlled use is unchanged, and `onCheckedChange` sees the agent's changes first
13
+ * and can cancel them.
14
+ */
7
15
  const Switch = React.forwardRef<
8
16
  React.ComponentRef<typeof SwitchPrimitive.Root>,
9
- React.ComponentPropsWithoutRef<typeof SwitchPrimitive.Root>
10
- >(({ className, ...props }, ref) => (
17
+ SwitchProps
18
+ >(({ className, checked: checkedProp, defaultChecked = false, onCheckedChange, ...props }, ref) => {
19
+ const [checked, change] = useAgentState<boolean, SwitchPrimitive.Root.ChangeEventDetails>(
20
+ checkedProp,
21
+ defaultChecked,
22
+ onCheckedChange
23
+ )
24
+ const element = React.useRef<HTMLElement | null>(null)
25
+ const mergedRef = useMergedRef(ref, element)
26
+
27
+ useAgentControl(() => {
28
+ const set = (next: boolean) => {
29
+ if (props.readOnly) return "The switch is read-only."
30
+ return next === checked ? undefined : changeAsAgent(change, next)
31
+ }
32
+ return {
33
+ kind: "switch",
34
+ element: element.current,
35
+ checked,
36
+ disabled: props.disabled,
37
+ actions: {
38
+ toggle: () => set(!checked),
39
+ setChecked: (value: unknown) => set(agentChecked(value)),
40
+ },
41
+ }
42
+ })
43
+
44
+ return (
11
45
  <span className="relative inline-flex [&>input]:!absolute">
12
46
  <SwitchPrimitive.Root
13
- ref={ref}
47
+ ref={mergedRef}
14
48
  className={cn(
15
49
  "inline-flex h-6 w-11 shrink-0 cursor-pointer items-center rounded-full border-2 border-transparent transition-colors duration-[var(--motion-duration-fast,150ms)]",
16
50
  "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",
@@ -20,9 +54,12 @@ const Switch = React.forwardRef<
20
54
  className
21
55
  )}
22
56
  {...props}
57
+ checked={checked}
58
+ onCheckedChange={change}
23
59
  />
24
60
  </span>
25
- ))
61
+ )
62
+ })
26
63
  Switch.displayName = "Switch"
27
64
 
28
65
  const SwitchThumb = React.forwardRef<
@@ -1,17 +1,75 @@
1
1
  import * as React from "react";
2
2
  import { Tabs as BaseTabs } from "@base-ui/react/tabs";
3
3
  import { cn } from "@/lib/utils";
4
+ import {
5
+ agentChoicesOf,
6
+ changeAsAgent,
7
+ matchAgentChoice,
8
+ useAgentControl,
9
+ useAgentItem,
10
+ useHasAgent,
11
+ useMergedRef,
12
+ type AgentItems,
13
+ } from "@/components/ui/agent";
4
14
 
15
+ type Details = BaseTabs.Root.ChangeEventDetails;
16
+
17
+ const TabsAgentContext = React.createContext<AgentItems | null>(null);
18
+
19
+ /**
20
+ * Base UI keeps the value, so its fallbacks for a missing or disabled tab still apply.
21
+ * The agent selects a tab by clicking it, and the app sees that change with the agent's
22
+ * details, which it can cancel.
23
+ */
5
24
  const Tabs = React.forwardRef<
6
25
  React.ComponentRef<typeof BaseTabs.Root>,
7
26
  React.ComponentProps<typeof BaseTabs.Root>
8
- >(({ className, ...props }, ref) => (
9
- <BaseTabs.Root
10
- ref={ref}
11
- className={cn("w-full", className)}
12
- {...props}
13
- />
14
- ));
27
+ >(({ className, onValueChange, ...props }, ref) => {
28
+ const element = React.useRef<HTMLDivElement | null>(null);
29
+ const [items] = React.useState<AgentItems>(() => new Map());
30
+ const pending = React.useRef<Details | null>(null);
31
+
32
+ const change = (value: unknown, details: Details) => {
33
+ const agent = pending.current;
34
+ pending.current = null;
35
+ onValueChange?.(value, agent ?? details);
36
+ if (agent?.isCanceled) details.cancel();
37
+ };
38
+
39
+ useAgentControl(() => {
40
+ const choices = agentChoicesOf(items);
41
+ const active = [...items].find(([el]) => el.getAttribute("aria-selected") === "true")?.[1];
42
+ const select = (wanted: unknown) => {
43
+ const match = matchAgentChoice(choices, wanted);
44
+ if (typeof match === "string") return match;
45
+ const tab = [...items].find(([, v]) => v === match.value)?.[0];
46
+ return changeAsAgent<unknown, Details>((_, details) => {
47
+ pending.current = details;
48
+ tab?.click();
49
+ pending.current = null;
50
+ }, match.value);
51
+ };
52
+ return {
53
+ kind: "tabs",
54
+ element: choices.length ? element.current : null,
55
+ value: choices.find((c) => c.value === active)?.label ?? "",
56
+ items: choices.map((c) => c.label),
57
+ disabled: choices.length > 0 && choices.every((c) => c.disabled),
58
+ actions: { select },
59
+ };
60
+ });
61
+
62
+ return (
63
+ <TabsAgentContext.Provider value={useHasAgent() ? items : null}>
64
+ <BaseTabs.Root
65
+ ref={useMergedRef(ref, element)}
66
+ className={cn("w-full", className)}
67
+ onValueChange={change}
68
+ {...props}
69
+ />
70
+ </TabsAgentContext.Provider>
71
+ );
72
+ });
15
73
  Tabs.displayName = "Tabs";
16
74
 
17
75
  const TabsList = React.forwardRef<
@@ -32,9 +90,14 @@ TabsList.displayName = "TabsList";
32
90
  const TabsTrigger = React.forwardRef<
33
91
  React.ComponentRef<typeof BaseTabs.Tab>,
34
92
  React.ComponentProps<typeof BaseTabs.Tab>
35
- >(({ className, ...props }, ref) => (
93
+ >(({ className, ...props }, ref) => {
94
+ const items = React.useContext(TabsAgentContext);
95
+ const element = React.useRef<HTMLButtonElement | null>(null);
96
+ useAgentItem(items, element, props.value);
97
+ return (
36
98
  <BaseTabs.Tab
37
- ref={ref}
99
+ ref={useMergedRef(ref, element)}
100
+ data-agent-part={items ? "" : undefined}
38
101
  className={cn(
39
102
  "inline-flex items-center justify-center whitespace-nowrap rounded-sm px-3 py-1.5 text-sm font-medium ring-offset-background",
40
103
  "transition-[color,background-color,box-shadow] duration-[var(--motion-duration-fast,150ms)]",
@@ -46,7 +109,8 @@ const TabsTrigger = React.forwardRef<
46
109
  )}
47
110
  {...props}
48
111
  />
49
- ));
112
+ );
113
+ });
50
114
  TabsTrigger.displayName = "TabsTrigger";
51
115
 
52
116
  const TabsContent = React.forwardRef<
@@ -1,2 +1,2 @@
1
1
  export { Textarea } from './textarea'
2
- export type { TextareaProps } from './textarea'
2
+ export type { TextareaChangeDetails, TextareaProps } from './textarea'
@@ -1,17 +1,83 @@
1
1
  import * as React from "react"
2
+ import { Input as BaseInput } from "@base-ui/react/input"
2
3
  import { cn } from "@/lib/utils"
4
+ import { createAgentChangeDetails, useAgentControl, useMergedRef } from "@/components/ui/agent"
3
5
 
4
- export type TextareaProps = React.TextareaHTMLAttributes<HTMLTextAreaElement>
6
+ export interface TextareaChangeDetails {
7
+ reason: string
8
+ event: Event | undefined
9
+ isCanceled: boolean
10
+ cancel(): void
11
+ }
12
+
13
+ export interface TextareaProps extends React.TextareaHTMLAttributes<HTMLTextAreaElement> {
14
+ /** Called with the new text on each edit, a person's or an agent's. An agent's can be canceled. */
15
+ onValueChange?: (value: string, details: TextareaChangeDetails) => void
16
+ }
17
+
18
+ function writeValue(textarea: HTMLTextAreaElement, value: string) {
19
+ Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, "value")?.set?.call(textarea, value)
20
+ }
21
+
22
+ function personDetails(event: Event): TextareaChangeDetails {
23
+ const details = { reason: "none", event, isCanceled: false, cancel: () => void (details.isCanceled = true) }
24
+ return details
25
+ }
5
26
 
6
27
  const Textarea = React.forwardRef<HTMLTextAreaElement, TextareaProps>(
7
- ({ className, ...props }, ref) => {
28
+ ({ className, onValueChange, onChange, ...props }, ref) => {
29
+ const element = React.useRef<HTMLTextAreaElement>(null)
30
+ const mergedRef = useMergedRef(ref, element)
31
+
32
+ // Set while the agent's input event runs, as onValueChange has already heard of it.
33
+ const agentWrite = React.useRef(false)
34
+
35
+ const setValue = (value: string) => {
36
+ const textarea = element.current
37
+ if (!textarea) return "The textarea is not mounted."
38
+ if (textarea.readOnly) return "The textarea is read-only."
39
+ // The app may refuse first. Then a real input event, so onChange hears of it too.
40
+ if (onValueChange) {
41
+ const details = createAgentChangeDetails<TextareaChangeDetails>("none")
42
+ onValueChange(value, details)
43
+ if (details.isCanceled) return "The app refused the change."
44
+ }
45
+ agentWrite.current = true
46
+ try {
47
+ writeValue(textarea, value)
48
+ textarea.dispatchEvent(new Event("input", { bubbles: true }))
49
+ } finally {
50
+ agentWrite.current = false
51
+ }
52
+ }
53
+
54
+ useAgentControl(() => ({
55
+ kind: "textarea",
56
+ element: element.current,
57
+ value: element.current?.value,
58
+ actions: {
59
+ setValue: (value: string) => setValue(String(value ?? "")),
60
+ clear: () => setValue(""),
61
+ },
62
+ }))
63
+
64
+ const Control = BaseInput as unknown as React.ComponentType<React.ComponentProps<"textarea"> & { render: React.ReactElement }>
8
65
  return (
9
- <textarea
66
+ <Control
67
+ render={<textarea />}
10
68
  className={cn(
11
69
  "flex min-h-[80px] w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50",
12
70
  className
13
71
  )}
14
- ref={ref}
72
+ ref={mergedRef}
73
+ onChange={
74
+ onValueChange
75
+ ? (event) => {
76
+ onChange?.(event)
77
+ if (!agentWrite.current) onValueChange(event.target.value, personDetails(event.nativeEvent))
78
+ }
79
+ : onChange
80
+ }
15
81
  {...props}
16
82
  />
17
83
  )
@@ -180,6 +180,8 @@ const ToastClose = React.forwardRef<
180
180
  <BaseToast.Close
181
181
  ref={ref}
182
182
  aria-label="Close"
183
+ data-agent-dismiss=""
184
+ data-agent-part=""
183
185
  className={cn(
184
186
  "absolute top-2 right-2 flex h-5 w-5 items-center justify-center rounded border-none bg-transparent opacity-60 transition-opacity",
185
187
  "hover:opacity-100 focus:outline-none focus:ring-1 focus:ring-ring",
@@ -279,7 +281,7 @@ function ToastList({ renderToast }: { renderToast?: RenderToastFn }) {
279
281
  <ToastDescription>{toast.description}</ToastDescription>
280
282
  )}
281
283
  </div>
282
- {toast.actionProps && <ToastAction {...toast.actionProps} />}
284
+ {toast.actionProps && <ToastAction />}
283
285
  <ToastClose />
284
286
  </ToastContent>
285
287
  )}
@@ -4,6 +4,7 @@ import * as React from "react"
4
4
  import { Toggle as TogglePrimitive } from "@base-ui/react/toggle"
5
5
  import { type VariantProps, cva } from "class-variance-authority"
6
6
  import { cn } from "@/lib/utils"
7
+ import { agentChecked, changeAsAgent, useAgentControl, useAgentState, useMergedRef } from "@/components/ui/agent"
7
8
 
8
9
  const toggleVariants = cva(
9
10
  [
@@ -49,16 +50,47 @@ export interface ToggleProps
49
50
  extends React.ComponentPropsWithoutRef<typeof TogglePrimitive>,
50
51
  VariantProps<typeof toggleVariants> {}
51
52
 
53
+ /**
54
+ * Base UI's toggle, holding its own pressed state so an agent can press it the way a
55
+ * parent would. Controlled use is unchanged, and `onPressedChange` sees the agent's
56
+ * changes first and can cancel them. The agent reads pressed as `checked`.
57
+ */
52
58
  const Toggle = React.forwardRef<
53
59
  React.ComponentRef<typeof TogglePrimitive>,
54
60
  ToggleProps
55
- >(({ className, variant, size, ...props }, ref) => (
56
- <TogglePrimitive
57
- ref={ref}
58
- className={cn(toggleVariants({ variant, size }), className)}
59
- {...props}
60
- />
61
- ))
61
+ >(({ className, variant, size, pressed: pressedProp, defaultPressed = false, onPressedChange, ...props }, ref) => {
62
+ const [pressed, change] = useAgentState<boolean, TogglePrimitive.ChangeEventDetails>(
63
+ pressedProp,
64
+ defaultPressed,
65
+ onPressedChange
66
+ )
67
+ const element = React.useRef<HTMLButtonElement | null>(null)
68
+ const mergedRef = useMergedRef(ref, element)
69
+
70
+ useAgentControl(() => {
71
+ const set = (next: boolean) => (next === pressed ? undefined : changeAsAgent(change, next))
72
+ return {
73
+ kind: "toggle",
74
+ element: element.current,
75
+ checked: pressed,
76
+ disabled: props.disabled,
77
+ actions: {
78
+ toggle: () => set(!pressed),
79
+ setChecked: (value: unknown) => set(agentChecked(value)),
80
+ },
81
+ }
82
+ })
83
+
84
+ return (
85
+ <TogglePrimitive
86
+ ref={mergedRef}
87
+ className={cn(toggleVariants({ variant, size }), className)}
88
+ {...props}
89
+ pressed={pressed}
90
+ onPressedChange={change}
91
+ />
92
+ )
93
+ })
62
94
 
63
95
  Toggle.displayName = "Toggle"
64
96