@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
@@ -28,20 +28,23 @@ const ScrollArea = React.forwardRef<
28
28
  ref
29
29
  ) => (
30
30
  <ScrollAreaPrimitive.Root
31
+ data-slot="scroll-area"
31
32
  ref={ref}
32
33
  className={cn("relative overflow-hidden", className)}
33
34
  {...props}
34
35
  >
35
36
  <ScrollAreaPrimitive.Viewport
37
+ data-slot="scroll-area-viewport"
36
38
  className={cn("size-full rounded-[inherit]", viewportClassName)}
37
39
  >
38
- <ScrollAreaPrimitive.Content className="min-w-full">
40
+ <ScrollAreaPrimitive.Content data-slot="scroll-area-content" className="min-w-full">
39
41
  {children}
40
42
  </ScrollAreaPrimitive.Content>
41
43
  </ScrollAreaPrimitive.Viewport>
42
44
 
43
45
  {(orientation === "vertical" || orientation === "both") && (
44
46
  <ScrollAreaPrimitive.Scrollbar
47
+ data-slot="scroll-area-scrollbar"
45
48
  orientation="vertical"
46
49
  className={cn(
47
50
  "flex touch-none select-none p-0.5 transition-colors",
@@ -49,12 +52,13 @@ const ScrollArea = React.forwardRef<
49
52
  scrollbarClassName
50
53
  )}
51
54
  >
52
- <ScrollAreaPrimitive.Thumb className="relative flex-1 rounded-full bg-border" />
55
+ <ScrollAreaPrimitive.Thumb data-slot="scroll-area-thumb" className="relative flex-1 rounded-full bg-border" />
53
56
  </ScrollAreaPrimitive.Scrollbar>
54
57
  )}
55
58
 
56
59
  {(orientation === "horizontal" || orientation === "both") && (
57
60
  <ScrollAreaPrimitive.Scrollbar
61
+ data-slot="scroll-area-scrollbar"
58
62
  orientation="horizontal"
59
63
  className={cn(
60
64
  "flex touch-none select-none p-0.5 transition-colors",
@@ -62,11 +66,11 @@ const ScrollArea = React.forwardRef<
62
66
  scrollbarClassName
63
67
  )}
64
68
  >
65
- <ScrollAreaPrimitive.Thumb className="relative flex-1 rounded-full bg-border" />
69
+ <ScrollAreaPrimitive.Thumb data-slot="scroll-area-thumb" className="relative flex-1 rounded-full bg-border" />
66
70
  </ScrollAreaPrimitive.Scrollbar>
67
71
  )}
68
72
 
69
- {orientation === "both" && <ScrollAreaPrimitive.Corner className="bg-background" />}
73
+ {orientation === "both" && <ScrollAreaPrimitive.Corner data-slot="scroll-area-corner" className="bg-background" />}
70
74
  </ScrollAreaPrimitive.Root>
71
75
  )
72
76
  )
@@ -81,6 +85,7 @@ const ScrollAreaRoot = React.forwardRef<
81
85
  React.ComponentPropsWithoutRef<typeof ScrollAreaPrimitive.Root>
82
86
  >(({ className, ...props }, ref) => (
83
87
  <ScrollAreaPrimitive.Root
88
+ data-slot="scroll-area-root"
84
89
  ref={ref}
85
90
  className={cn("relative overflow-hidden", className)}
86
91
  {...props}
@@ -93,6 +98,7 @@ const ScrollAreaViewport = React.forwardRef<
93
98
  React.ComponentPropsWithoutRef<typeof ScrollAreaPrimitive.Viewport>
94
99
  >(({ className, ...props }, ref) => (
95
100
  <ScrollAreaPrimitive.Viewport
101
+ data-slot="scroll-area-viewport"
96
102
  ref={ref}
97
103
  className={cn("size-full rounded-[inherit]", className)}
98
104
  {...props}
@@ -105,6 +111,7 @@ const ScrollAreaContent = React.forwardRef<
105
111
  React.ComponentPropsWithoutRef<typeof ScrollAreaPrimitive.Content>
106
112
  >(({ className, ...props }, ref) => (
107
113
  <ScrollAreaPrimitive.Content
114
+ data-slot="scroll-area-content"
108
115
  ref={ref}
109
116
  className={cn("min-w-full", className)}
110
117
  {...props}
@@ -117,6 +124,7 @@ const ScrollAreaScrollbar = React.forwardRef<
117
124
  React.ComponentPropsWithoutRef<typeof ScrollAreaPrimitive.Scrollbar>
118
125
  >(({ className, orientation = "vertical", ...props }, ref) => (
119
126
  <ScrollAreaPrimitive.Scrollbar
127
+ data-slot="scroll-area-scrollbar"
120
128
  ref={ref}
121
129
  orientation={orientation}
122
130
  className={cn(
@@ -136,6 +144,7 @@ const ScrollAreaThumb = React.forwardRef<
136
144
  React.ComponentPropsWithoutRef<typeof ScrollAreaPrimitive.Thumb>
137
145
  >(({ className, ...props }, ref) => (
138
146
  <ScrollAreaPrimitive.Thumb
147
+ data-slot="scroll-area-thumb"
139
148
  ref={ref}
140
149
  className={cn("relative flex-1 rounded-full bg-border", className)}
141
150
  {...props}
@@ -148,6 +157,7 @@ const ScrollAreaCorner = React.forwardRef<
148
157
  React.ComponentPropsWithoutRef<typeof ScrollAreaPrimitive.Corner>
149
158
  >(({ className, ...props }, ref) => (
150
159
  <ScrollAreaPrimitive.Corner
160
+ data-slot="scroll-area-corner"
151
161
  ref={ref}
152
162
  className={cn("bg-background", className)}
153
163
  {...props}
@@ -127,6 +127,7 @@ const ScrollProgress = React.forwardRef<HTMLDivElement, ScrollProgressProps>(
127
127
  <motion.div
128
128
  ref={ref}
129
129
  aria-hidden
130
+ data-slot="scroll-progress"
130
131
  style={{ scaleX, ...style }}
131
132
  // `will-change-transform` stays on permanently, against the usual advice to hint
132
133
  // only imminent motion: this element is transformed on essentially every scroll
@@ -169,6 +169,7 @@ const ScrollReveal = React.forwardRef<HTMLDivElement, ScrollRevealProps>(
169
169
  <div ref={wrapperRef}>
170
170
  <motion.div
171
171
  ref={ref}
172
+ data-slot="scroll-reveal"
172
173
  className={cn(className)}
173
174
  initial={hidden}
174
175
  animate={inView ? shown : hidden}
@@ -4,20 +4,195 @@ import * as React from "react"
4
4
  import { Select as SelectPrimitive } from "@base-ui/react/select"
5
5
  import { cn } from "@/lib/utils"
6
6
  import { Check, ChevronDown } from "lucide-react"
7
+ import {
8
+ agentNodeText,
9
+ changeAsAgent,
10
+ matchAgentChoice,
11
+ sameAgentValue,
12
+ useAgentControl,
13
+ useAgentState,
14
+ useHasAgent,
15
+ useMergedRef,
16
+ waitForAgent,
17
+ } from "@/components/ui/agent"
7
18
 
8
- // Direct re-exports
9
- const Select = SelectPrimitive.Root
10
- const SelectGroup = SelectPrimitive.Group
11
- const SelectValue = SelectPrimitive.Value
19
+ interface SelectAgent {
20
+ trigger: React.MutableRefObject<HTMLElement | null>
21
+ /** Items in the open popup. */
22
+ items: Map<HTMLElement, unknown>
23
+ /** The same items, rendered hidden while the popup is closed so the agent can see them. */
24
+ catalog: Map<HTMLElement, unknown>
25
+ labels: Map<unknown, string>
26
+ value: unknown
27
+ multiple: boolean
28
+ open: boolean
29
+ disabled: boolean
30
+ setOpen: (open: boolean) => void
31
+ change: (value: unknown) => string | void
32
+ }
33
+
34
+ const SelectAgentContext = React.createContext<SelectAgent | null>(null)
35
+ const SelectCatalogContext = React.createContext(false)
36
+
37
+ function itemLabels(items: SelectPrimitive.Root.Props<unknown>["items"]): Map<unknown, string> {
38
+ const labels = new Map<unknown, string>()
39
+ if (!items) return labels
40
+ const entries = Array.isArray(items)
41
+ ? (items as ReadonlyArray<{ value: unknown; label: React.ReactNode }>).map((i) => [i.value, i.label] as const)
42
+ : Object.entries(items as Record<string, React.ReactNode>)
43
+ for (const [value, label] of entries) labels.set(value, agentNodeText(label))
44
+ return labels
45
+ }
46
+
47
+ /**
48
+ * Base UI's root, holding its own value and open state so an agent can set them the way
49
+ * a parent would. Controlled use is unchanged: the props win and every change, the
50
+ * agent's included, goes through `onValueChange` and `onOpenChange` first.
51
+ */
52
+ function Select<Value, Multiple extends boolean | undefined = false>({
53
+ value: valueProp,
54
+ defaultValue,
55
+ onValueChange,
56
+ open: openProp,
57
+ defaultOpen = false,
58
+ onOpenChange,
59
+ ...props
60
+ }: SelectPrimitive.Root.Props<Value, Multiple>) {
61
+ type Props = SelectPrimitive.Root.Props<Value, Multiple>
62
+ type ValueChange = NonNullable<Props["onValueChange"]>
63
+ type OpenChange = NonNullable<Props["onOpenChange"]>
64
+ type Details = Parameters<ValueChange>[1]
65
+ type OpenDetails = Parameters<OpenChange>[1]
66
+ type Current = Parameters<ValueChange>[0]
67
+
68
+ const [value, changeValue] = useAgentState<Current, Details>(
69
+ valueProp as Current | undefined,
70
+ (defaultValue ?? (props.multiple ? [] : null)) as Current,
71
+ onValueChange
72
+ )
73
+ const [open, changeOpen] = useAgentState<boolean, OpenDetails>(openProp, defaultOpen, onOpenChange)
74
+
75
+ const trigger = React.useRef<HTMLElement | null>(null)
76
+ const [items] = React.useState(() => new Map<HTMLElement, unknown>())
77
+ const [catalog] = React.useState(() => new Map<HTMLElement, unknown>())
78
+
79
+ const agent: SelectAgent = {
80
+ trigger,
81
+ items,
82
+ catalog,
83
+ labels: itemLabels(props.items as Props["items"]),
84
+ value,
85
+ multiple: Boolean(props.multiple),
86
+ open,
87
+ disabled: Boolean(props.disabled),
88
+ setOpen: (next) => changeAsAgent(changeOpen, next),
89
+ // A person cannot choose a different option either, though they can open it to look.
90
+ change: (next) => (props.readOnly ? "The select is read-only." : changeAsAgent(changeValue, next as Current)),
91
+ }
92
+
93
+ return (
94
+ <SelectAgentContext.Provider value={useHasAgent() ? agent : null}>
95
+ <SelectPrimitive.Root<Value, Multiple>
96
+ {...props}
97
+ value={value as Props["value"]}
98
+ onValueChange={changeValue}
99
+ open={open}
100
+ onOpenChange={changeOpen}
101
+ />
102
+ </SelectAgentContext.Provider>
103
+ )
104
+ }
105
+
106
+ function SelectGroup(props: React.ComponentProps<typeof SelectPrimitive.Group>) {
107
+ return <SelectPrimitive.Group data-slot="select-group" {...props} />
108
+ }
109
+
110
+ function SelectValue(props: React.ComponentProps<typeof SelectPrimitive.Value>) {
111
+ return <SelectPrimitive.Value data-slot="select-value" {...props} />
112
+ }
12
113
 
13
114
  const SelectTrigger = React.forwardRef<
14
115
  React.ComponentRef<typeof SelectPrimitive.Trigger>,
15
116
  React.ComponentPropsWithoutRef<typeof SelectPrimitive.Trigger> & {
16
117
  readonly children?: React.ReactNode
17
118
  }
18
- >(({ className, children, ...props }, ref) => (
119
+ >(({ className, children, ...props }, ref) => {
120
+ const select = React.useContext(SelectAgentContext)
121
+ const mergedRef = useMergedRef(ref, select?.trigger)
122
+
123
+ useAgentControl(() => {
124
+ if (!select) return null
125
+ const element = select.trigger.current
126
+ const choices = () => {
127
+ const read = (map: Map<HTMLElement, unknown>) =>
128
+ Array.from(map, ([el, value]) => ({
129
+ value,
130
+ label: agentNodeText(el.textContent),
131
+ disabled: el.hasAttribute("data-disabled"),
132
+ }))
133
+ const live = read(select.items)
134
+ if (live.length) return live
135
+ const hidden = read(select.catalog)
136
+ if (hidden.length) return hidden
137
+ return Array.from(select.labels, ([value, label]) => ({ value, label, disabled: false }))
138
+ }
139
+ const labelOf = (value: unknown) =>
140
+ choices().find((c) => sameAgentValue(c.value, value))?.label ?? (value == null ? "" : String(value))
141
+ const find = (wanted: unknown) => matchAgentChoice(choices(), wanted)
142
+ // Waits for the popup when nothing has listed the choices yet, outside the catalog.
143
+ const ready = async () => {
144
+ if (choices().length) return
145
+ select.setOpen(true)
146
+ await waitForAgent(() => (choices().length ? true : null))
147
+ }
148
+ const current: unknown[] = Array.isArray(select.value) ? select.value : []
149
+ const items = choices().map((c) => c.label)
150
+
151
+ return {
152
+ kind: "select",
153
+ element,
154
+ value: select.multiple ? current.map(labelOf) : labelOf(select.value),
155
+ open: select.open,
156
+ disabled: select.disabled,
157
+ ...(select.multiple ? { multiple: true } : {}),
158
+ ...(items.length ? { items } : {}),
159
+ actions: {
160
+ select: async (wanted: string) => {
161
+ await ready()
162
+ const match = find(wanted)
163
+ if (typeof match === "string") return match
164
+ if (!select.multiple) {
165
+ const refused = select.change(match.value)
166
+ select.setOpen(false)
167
+ return refused
168
+ }
169
+ if (current.some((v) => sameAgentValue(v, match.value))) return
170
+ return select.change([...current, match.value])
171
+ },
172
+ ...(select.multiple
173
+ ? {
174
+ setValue: async (wanted: unknown) => {
175
+ await ready()
176
+ const list = Array.isArray(wanted) ? wanted : [wanted]
177
+ const next: unknown[] = []
178
+ for (const one of list) {
179
+ const match = find(one)
180
+ if (typeof match === "string") return match
181
+ next.push(match.value)
182
+ }
183
+ return select.change(next)
184
+ },
185
+ }
186
+ : {}),
187
+ [select.open ? "close" : "open"]: () => select.setOpen(!select.open),
188
+ },
189
+ }
190
+ })
191
+
192
+ return (
19
193
  <SelectPrimitive.Trigger
20
- ref={ref}
194
+ data-slot="select-trigger"
195
+ ref={mergedRef}
21
196
  className={cn(
22
197
  "flex h-10 w-full items-center justify-between rounded-md border border-input bg-transparent px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 [&>span]:line-clamp-1",
23
198
  className
@@ -29,7 +204,8 @@ const SelectTrigger = React.forwardRef<
29
204
  <ChevronDown className="h-4 w-4 opacity-50" />
30
205
  </SelectPrimitive.Icon>
31
206
  </SelectPrimitive.Trigger>
32
- ))
207
+ )
208
+ })
33
209
  SelectTrigger.displayName = "SelectTrigger"
34
210
 
35
211
  const SelectContent = React.forwardRef<
@@ -59,6 +235,7 @@ const SelectContent = React.forwardRef<
59
235
  className="z-50"
60
236
  >
61
237
  <SelectPrimitive.Popup
238
+ data-slot="select-content"
62
239
  ref={ref}
63
240
  className={cn(
64
241
  "relative max-h-96 min-w-[var(--anchor-width)] overflow-hidden rounded-md border bg-popover text-popover-foreground shadow-md",
@@ -82,9 +259,26 @@ const SelectContent = React.forwardRef<
82
259
  </SelectPrimitive.Positioner>
83
260
  )
84
261
 
85
- if (!portal) return content
262
+ const select = React.useContext(SelectAgentContext)
263
+ // Base UI unmounts a closed popup, which would leave the agent nothing to choose from.
264
+ // Inside AgentProvider the items also render hidden while it is closed.
265
+ const catalog =
266
+ select && !select.open ? (
267
+ <SelectCatalogContext.Provider value>
268
+ <div hidden data-agent-hidden>
269
+ {children}
270
+ </div>
271
+ </SelectCatalogContext.Provider>
272
+ ) : null
86
273
 
87
- return <SelectPrimitive.Portal>{content}</SelectPrimitive.Portal>
274
+ const popup = portal ? <SelectPrimitive.Portal>{content}</SelectPrimitive.Portal> : content
275
+ if (!catalog) return popup
276
+ return (
277
+ <>
278
+ {popup}
279
+ {catalog}
280
+ </>
281
+ )
88
282
  })
89
283
  SelectContent.displayName = "SelectContent"
90
284
 
@@ -93,6 +287,7 @@ const SelectBackdrop = React.forwardRef<
93
287
  React.ComponentPropsWithoutRef<typeof SelectPrimitive.Backdrop>
94
288
  >(({ className, ...props }, ref) => (
95
289
  <SelectPrimitive.Backdrop
290
+ data-slot="select-backdrop"
96
291
  ref={ref}
97
292
  className={cn("fixed inset-0 z-40", className)}
98
293
  {...props}
@@ -105,6 +300,7 @@ const SelectScrollUpArrow = React.forwardRef<
105
300
  React.ComponentPropsWithoutRef<typeof SelectPrimitive.ScrollUpArrow>
106
301
  >(({ className, ...props }, ref) => (
107
302
  <SelectPrimitive.ScrollUpArrow
303
+ data-slot="select-scroll-up-arrow"
108
304
  ref={ref}
109
305
  className={cn(
110
306
  "flex cursor-default items-center justify-center py-1",
@@ -122,6 +318,7 @@ const SelectScrollDownArrow = React.forwardRef<
122
318
  React.ComponentPropsWithoutRef<typeof SelectPrimitive.ScrollDownArrow>
123
319
  >(({ className, ...props }, ref) => (
124
320
  <SelectPrimitive.ScrollDownArrow
321
+ data-slot="select-scroll-down-arrow"
125
322
  ref={ref}
126
323
  className={cn(
127
324
  "flex cursor-default items-center justify-center py-1",
@@ -139,6 +336,7 @@ const SelectFieldLabel = React.forwardRef<
139
336
  React.ComponentPropsWithoutRef<typeof SelectPrimitive.Label>
140
337
  >(({ className, ...props }, ref) => (
141
338
  <SelectPrimitive.Label
339
+ data-slot="select-field-label"
142
340
  ref={ref}
143
341
  className={cn("block text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70", className)}
144
342
  {...props}
@@ -151,6 +349,7 @@ const SelectLabel = React.forwardRef<
151
349
  React.ComponentPropsWithoutRef<typeof SelectPrimitive.GroupLabel>
152
350
  >(({ className, ...props }, ref) => (
153
351
  <SelectPrimitive.GroupLabel
352
+ data-slot="select-label"
154
353
  ref={ref}
155
354
  className={cn("py-1.5 pl-2 pr-2 text-sm font-medium", className)}
156
355
  {...props}
@@ -163,9 +362,35 @@ const SelectItem = React.forwardRef<
163
362
  React.ComponentPropsWithoutRef<typeof SelectPrimitive.Item> & {
164
363
  readonly showIndicator?: boolean
165
364
  }
166
- >(({ className, children, showIndicator = false, ...props }, ref) => (
365
+ >(({ className, children, showIndicator = false, ...props }, ref) => {
366
+ const select = React.useContext(SelectAgentContext)
367
+ const inCatalog = React.useContext(SelectCatalogContext)
368
+ const element = React.useRef<HTMLDivElement | null>(null)
369
+ const mergedRef = useMergedRef(ref, element)
370
+ const { value } = props
371
+ const registry = select ? (inCatalog ? select.catalog : select.items) : null
372
+
373
+ React.useEffect(() => {
374
+ const el = element.current
375
+ if (!registry || !el) return
376
+ registry.set(el, value)
377
+ return () => {
378
+ registry.delete(el)
379
+ }
380
+ }, [registry, value])
381
+
382
+ if (inCatalog) {
383
+ return (
384
+ <div ref={element} data-disabled={props.disabled ? "" : undefined}>
385
+ {children}
386
+ </div>
387
+ )
388
+ }
389
+
390
+ return (
167
391
  <SelectPrimitive.Item
168
- ref={ref}
392
+ data-slot="select-item"
393
+ ref={mergedRef}
169
394
  className={cn(
170
395
  "relative flex w-full cursor-default select-none items-center rounded-sm py-1.5 text-sm outline-none",
171
396
  "data-[highlighted]:bg-accent data-[highlighted]:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50",
@@ -183,7 +408,8 @@ const SelectItem = React.forwardRef<
183
408
  )}
184
409
  <SelectPrimitive.ItemText>{children}</SelectPrimitive.ItemText>
185
410
  </SelectPrimitive.Item>
186
- ))
411
+ )
412
+ })
187
413
  SelectItem.displayName = "SelectItem"
188
414
 
189
415
  const SelectItemIndicator = React.forwardRef<
@@ -191,6 +417,7 @@ const SelectItemIndicator = React.forwardRef<
191
417
  React.ComponentPropsWithoutRef<typeof SelectPrimitive.ItemIndicator>
192
418
  >(({ className, children, ...props }, ref) => (
193
419
  <SelectPrimitive.ItemIndicator
420
+ data-slot="select-item-indicator"
194
421
  ref={ref}
195
422
  className={cn("flex h-3.5 w-3.5 items-center justify-center", className)}
196
423
  {...props}
@@ -205,6 +432,7 @@ const SelectArrow = React.forwardRef<
205
432
  React.ComponentPropsWithoutRef<typeof SelectPrimitive.Arrow>
206
433
  >(({ className, ...props }, ref) => (
207
434
  <SelectPrimitive.Arrow
435
+ data-slot="select-arrow"
208
436
  ref={ref}
209
437
  className={cn("fill-popover stroke-border", className)}
210
438
  {...props}
@@ -217,6 +445,7 @@ const SelectSeparator = React.forwardRef<
217
445
  React.ComponentPropsWithoutRef<typeof SelectPrimitive.Separator>
218
446
  >(({ className, ...props }, ref) => (
219
447
  <SelectPrimitive.Separator
448
+ data-slot="select-separator"
220
449
  ref={ref}
221
450
  className={cn("-mx-1 my-1 h-px bg-muted", className)}
222
451
  {...props}
@@ -9,6 +9,7 @@ const Separator = React.forwardRef<
9
9
  React.ComponentPropsWithoutRef<typeof SeparatorPrimitive>
10
10
  >(({ className, orientation = "horizontal", ...props }, ref) => (
11
11
  <SeparatorPrimitive
12
+ data-slot="separator"
12
13
  ref={ref}
13
14
  orientation={orientation}
14
15
  className={cn(
@@ -3,6 +3,7 @@ import { cn } from "@/lib/utils"
3
3
  function Skeleton({ className, ...props }: React.ComponentProps<"div">) {
4
4
  return (
5
5
  <div
6
+ data-slot="skeleton"
6
7
  className={cn("animate-pulse rounded-md bg-muted", className)}
7
8
  {...props}
8
9
  />
@@ -2,25 +2,95 @@ import * as React from "react";
2
2
  import { Slider as BaseSlider } from "@base-ui/react/slider";
3
3
  import { DirectionProvider } from "@base-ui/react/direction-provider";
4
4
  import { cn } from "@/lib/utils";
5
+ import { agentRound, changeAsAgent, createAgentChangeDetails, useAgentControl, useAgentState, useHasAgent, useMergedRef } from "@/components/ui/agent";
5
6
 
7
+ type SliderNumber = number | readonly number[];
8
+
9
+ /** One number, snapped to the step from min, or what is wrong with it. */
10
+ function snapped(item: unknown, min: number, max: number, step: number): number | string {
11
+ const n = Number(item);
12
+ if (item === "" || item === null || !Number.isFinite(n)) return `"${String(item)}" is not a number.`;
13
+ if (n < min || n > max) return `${n} is out of range: ${min} to ${max}.`;
14
+ return Math.min(max, agentRound(min + Math.round((n - min) / step) * step, min, step));
15
+ }
16
+
17
+ /** Snaps to the step and checks the bounds and shape, or says what is wrong. */
18
+ function sliderValue(wanted: unknown, current: SliderNumber, min: number, max: number, step: number): SliderNumber | string {
19
+ const count = Array.isArray(current) ? current.length : 0;
20
+ const list: unknown[] = Array.isArray(wanted) ? wanted : [wanted];
21
+ if (list.length !== Math.max(count, 1)) return count ? `Send ${count} numbers, low to high.` : "Send one number.";
22
+ const next = list.map((item) => snapped(item, min, max, step));
23
+ const wrong = next.find((n) => typeof n === "string");
24
+ if (wrong !== undefined) return wrong;
25
+ if (next.some((n, i) => i > 0 && n < next[i - 1])) return "Values must go from low to high.";
26
+ return count ? (next as number[]) : (next[0] as number);
27
+ }
28
+
29
+ /**
30
+ * Base UI's root, holding its own value so an agent can set it the way a parent would.
31
+ * Controlled use is unchanged, and `onValueChange` sees the agent's change first and can
32
+ * cancel it.
33
+ */
6
34
  const Slider = React.forwardRef<
7
35
  React.ComponentRef<typeof BaseSlider.Root>,
8
36
  React.ComponentProps<typeof BaseSlider.Root>
9
- >(({ className, ...props }, ref) => (
10
- <BaseSlider.Root
11
- ref={ref}
12
- className={cn("relative flex w-full flex-col gap-2", className)}
13
- {...props}
14
- />
15
- ));
37
+ >(({ className, value: valueProp, defaultValue, onValueChange, ...props }, ref) => {
38
+ type Details = BaseSlider.Root.ChangeEventDetails;
39
+ const { min = 0, max = 100, step = 1 } = props;
40
+ const [value, changeValue] = useAgentState<SliderNumber, Details>(
41
+ valueProp,
42
+ defaultValue ?? min,
43
+ onValueChange as ((value: SliderNumber, details: Details) => void) | undefined
44
+ );
45
+ const element = React.useRef<HTMLDivElement>(null);
46
+
47
+ useAgentControl(() => {
48
+ const root = element.current;
49
+ // Unnamed, the slider takes the name a thumb was given.
50
+ const named = root?.matches("[aria-label], [aria-labelledby], [data-agent-label]");
51
+ return {
52
+ kind: "slider",
53
+ element: root,
54
+ label: named ? undefined : root?.querySelector("input")?.getAttribute("aria-label") ?? undefined,
55
+ value: Array.isArray(value) ? [...value] : (value as number),
56
+ min,
57
+ max,
58
+ step,
59
+ disabled: props.disabled,
60
+ actions: {
61
+ setValue: (wanted: unknown) => {
62
+ const next = sliderValue(wanted, value, min, max, step);
63
+ if (typeof next === "string") return next;
64
+ // An agent's change is whole at once, so it is committed too, as a drag is on release.
65
+ return changeAsAgent(changeValue, next) ?? void props.onValueCommitted?.(next, createAgentChangeDetails("none"));
66
+ },
67
+ },
68
+ };
69
+ });
70
+
71
+ return (
72
+ <BaseSlider.Root
73
+ ref={useMergedRef(ref, element)}
74
+ className={cn("relative flex w-full flex-col gap-2", className)}
75
+ value={value}
76
+ onValueChange={changeValue}
77
+ data-slot="slider"
78
+ {...props}
79
+ />
80
+ );
81
+ });
16
82
  Slider.displayName = "Slider";
17
83
 
84
+ /** The root reports every thumb as one control, so the thumbs' own inputs stay out. */
85
+ const hideFromAgent = (node: HTMLInputElement | null) => node?.setAttribute("data-agent-part", "");
86
+
18
87
  const SliderValue = React.forwardRef<
19
88
  React.ComponentRef<typeof BaseSlider.Value>,
20
89
  React.ComponentProps<typeof BaseSlider.Value>
21
90
  >(({ className, ...props }, ref) => (
22
91
  <BaseSlider.Value
23
92
  ref={ref}
93
+ data-slot="slider-value"
24
94
  className={cn("text-sm font-medium", className)}
25
95
  {...props}
26
96
  />
@@ -33,6 +103,7 @@ const SliderControl = React.forwardRef<
33
103
  >(({ className, ...props }, ref) => (
34
104
  <BaseSlider.Control
35
105
  ref={ref}
106
+ data-slot="slider-control"
36
107
  className={cn("relative flex w-full touch-none select-none items-center", className)}
37
108
  {...props}
38
109
  />
@@ -45,6 +116,7 @@ const SliderTrack = React.forwardRef<
45
116
  >(({ className, ...props }, ref) => (
46
117
  <BaseSlider.Track
47
118
  ref={ref}
119
+ data-slot="slider-track"
48
120
  className={cn(
49
121
  "relative h-2 w-full grow rounded-full bg-secondary shadow-[inset_0_0_0_1px] shadow-secondary-foreground/5",
50
122
  className
@@ -60,6 +132,7 @@ const SliderRange = React.forwardRef<
60
132
  >(({ className, ...props }, ref) => (
61
133
  <BaseSlider.Indicator
62
134
  ref={ref}
135
+ data-slot="slider-range"
63
136
  className={cn("absolute h-full bg-primary rounded-full", className)}
64
137
  {...props}
65
138
  />
@@ -69,9 +142,11 @@ SliderRange.displayName = "SliderRange";
69
142
  const SliderThumb = React.forwardRef<
70
143
  React.ComponentRef<typeof BaseSlider.Thumb>,
71
144
  React.ComponentProps<typeof BaseSlider.Thumb>
72
- >(({ className, ...props }, ref) => (
145
+ >(({ className, inputRef, ...props }, ref) => (
73
146
  <BaseSlider.Thumb
74
147
  ref={ref}
148
+ data-slot="slider-thumb"
149
+ inputRef={useMergedRef(inputRef, useHasAgent() ? hideFromAgent : undefined)}
75
150
  className={cn(
76
151
  "block h-5 w-5 rounded-full border-2 border-primary bg-background ring-offset-background",
77
152
  "transition-[color,background-color,border-color,transform,scale] duration-150",
@@ -91,6 +166,7 @@ const SliderLabel = React.forwardRef<
91
166
  >(({ className, ...props }, ref) => (
92
167
  <BaseSlider.Label
93
168
  ref={ref}
169
+ data-slot="slider-label"
94
170
  className={cn("block text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70", className)}
95
171
  {...props}
96
172
  />