@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
@@ -4,39 +4,98 @@ import * as React from "react"
4
4
  import { ContextMenu as BaseContextMenu } from "@base-ui/react/context-menu"
5
5
  import { cn } from "@/lib/utils"
6
6
  import { Check, ChevronRight, Circle } from "lucide-react"
7
+ import { type AgentOpenParts, useAgentOpen, useHasAgent, useMergedRef } from "@/components/ui/agent"
7
8
 
8
- const ContextMenu: React.FC<React.ComponentProps<typeof BaseContextMenu.Root>> = (props) => (
9
- <BaseContextMenu.Root {...props} />
10
- )
9
+ const ContextMenuAgentContext = React.createContext<(AgentOpenParts & { disabled?: boolean }) | null>(null)
10
+
11
+ /**
12
+ * Base UI places the popup at the point a right-click or long press set. Opened any other
13
+ * way, by an agent or a controlled `open`, there is no such point and it lands in the top
14
+ * left corner, so the positioner anchors on the middle of the trigger area instead.
15
+ */
16
+ const ContextMenuAnchorContext = React.createContext<{
17
+ pointed: React.RefObject<boolean>
18
+ trigger: React.RefObject<HTMLElement | null>
19
+ } | null>(null)
20
+
21
+ /**
22
+ * Base UI's root, holding its own open state so an agent can open and close it. Closed,
23
+ * the agent sees the trigger area, named by its `aria-label` or text; open, the popup.
24
+ * The trigger area carries the root's `disabled`, which Base UI does not mark on it.
25
+ */
26
+ const ContextMenu: React.FC<React.ComponentProps<typeof BaseContextMenu.Root>> = ({
27
+ open,
28
+ defaultOpen,
29
+ onOpenChange,
30
+ ...props
31
+ }) => {
32
+ const [state, change, parts] = useAgentOpen("menu", open, defaultOpen, onOpenChange)
33
+ const pointed = React.useRef(false)
34
+ const trigger = React.useRef<HTMLElement | null>(null)
35
+ const anchor = React.useMemo(() => ({ pointed, trigger }), [])
36
+ if (!state) pointed.current = false
37
+ const onChange: typeof change = (next, details) => {
38
+ if (next) pointed.current = details.reason === "trigger-press"
39
+ change(next, details)
40
+ }
41
+ return (
42
+ <ContextMenuAnchorContext.Provider value={anchor}>
43
+ <ContextMenuAgentContext.Provider value={useHasAgent() ? { ...parts, disabled: props.disabled } : null}>
44
+ <BaseContextMenu.Root {...props} open={state} onOpenChange={onChange} />
45
+ </ContextMenuAgentContext.Provider>
46
+ </ContextMenuAnchorContext.Provider>
47
+ )
48
+ }
11
49
  ContextMenu.displayName = "ContextMenu"
12
50
 
13
51
  const ContextMenuTrigger = React.forwardRef<
14
52
  React.ComponentRef<typeof BaseContextMenu.Trigger>,
15
53
  React.ComponentProps<typeof BaseContextMenu.Trigger>
16
- >(({ className, ...props }, ref) => (
17
- <BaseContextMenu.Trigger
18
- ref={ref}
19
- className={cn("select-none", className)}
20
- {...props}
21
- />
22
- ))
54
+ >(({ className, ...props }, ref) => {
55
+ const agent = React.useContext(ContextMenuAgentContext)
56
+ const anchor = React.useContext(ContextMenuAnchorContext)
57
+ return (
58
+ <BaseContextMenu.Trigger
59
+ ref={useMergedRef(ref, agent?.trigger, anchor?.trigger)}
60
+ data-disabled={agent?.disabled ? "" : undefined}
61
+ className={cn("select-none", className)}
62
+ data-slot="context-menu-trigger"
63
+ {...props}
64
+ />
65
+ )
66
+ })
23
67
  ContextMenuTrigger.displayName = "ContextMenuTrigger"
24
68
 
25
69
  const ContextMenuPortal: React.FC<
26
70
  React.ComponentProps<typeof BaseContextMenu.Portal>
27
- > = (props) => <BaseContextMenu.Portal {...props} />
71
+ > = (props) => <BaseContextMenu.Portal data-slot="context-menu-portal" {...props} />
28
72
  ContextMenuPortal.displayName = "ContextMenuPortal"
29
73
 
30
74
  const ContextMenuPositioner = React.forwardRef<
31
75
  React.ComponentRef<typeof BaseContextMenu.Positioner>,
32
76
  React.ComponentProps<typeof BaseContextMenu.Positioner>
33
- >(({ className, ...props }, ref) => (
34
- <BaseContextMenu.Positioner
35
- ref={ref}
36
- className={cn("outline-none", className)}
37
- {...props}
38
- />
39
- ))
77
+ >(({ className, anchor, ...props }, ref) => {
78
+ const ctx = React.useContext(ContextMenuAnchorContext)
79
+ const middle = React.useMemo(
80
+ () =>
81
+ ctx && {
82
+ getBoundingClientRect() {
83
+ const r = ctx.trigger.current?.getBoundingClientRect()
84
+ return r ? DOMRect.fromRect({ x: r.x + r.width / 2, y: r.y + r.height / 2, width: 0, height: 0 }) : new DOMRect()
85
+ },
86
+ },
87
+ [ctx]
88
+ )
89
+ return (
90
+ <BaseContextMenu.Positioner
91
+ ref={ref}
92
+ anchor={anchor ?? (ctx && !ctx.pointed.current ? middle : undefined)}
93
+ className={cn("z-50 outline-none", className)}
94
+ data-slot="context-menu-positioner"
95
+ {...props}
96
+ />
97
+ )
98
+ })
40
99
  ContextMenuPositioner.displayName = "ContextMenuPositioner"
41
100
 
42
101
  const ContextMenuContent = React.forwardRef<
@@ -46,9 +105,9 @@ const ContextMenuContent = React.forwardRef<
46
105
  <ContextMenuPortal>
47
106
  <ContextMenuPositioner>
48
107
  <BaseContextMenu.Popup
49
- ref={ref}
108
+ ref={useMergedRef(ref, React.useContext(ContextMenuAgentContext)?.popup)}
50
109
  className={cn(
51
- "z-50 min-w-48 overflow-hidden rounded-md border bg-popover p-1 text-popover-foreground shadow-lg",
110
+ "min-w-48 overflow-hidden rounded-md border bg-popover p-1 text-popover-foreground shadow-lg",
52
111
  "origin-[var(--transform-origin)]",
53
112
  "outline-none focus:outline-none focus-visible:outline-none",
54
113
  "transition-[transform,scale,opacity] duration-[var(--motion-duration-fast,150ms)] ease-[var(--motion-ease-out,cubic-bezier(0.23,1,0.32,1))]",
@@ -57,6 +116,7 @@ const ContextMenuContent = React.forwardRef<
57
116
  "motion-reduce:data-[starting-style]:scale-100 motion-reduce:data-[ending-style]:scale-100",
58
117
  className
59
118
  )}
119
+ data-slot="context-menu-content"
60
120
  {...props}
61
121
  />
62
122
  </ContextMenuPositioner>
@@ -80,6 +140,7 @@ const ContextMenuItem = React.forwardRef<
80
140
  inset && "pl-8",
81
141
  className
82
142
  )}
143
+ data-slot="context-menu-item"
83
144
  {...props}
84
145
  />
85
146
  ))
@@ -97,6 +158,7 @@ const ContextMenuLinkItem = React.forwardRef<
97
158
  "data-[highlighted]:bg-accent data-[highlighted]:text-accent-foreground",
98
159
  className
99
160
  )}
161
+ data-slot="context-menu-link-item"
100
162
  {...props}
101
163
  />
102
164
  ))
@@ -115,6 +177,7 @@ const ContextMenuCheckboxItem = React.forwardRef<
115
177
  "data-[highlighted]:bg-accent data-[highlighted]:text-accent-foreground",
116
178
  className
117
179
  )}
180
+ data-slot="context-menu-checkbox-item"
118
181
  {...props}
119
182
  >
120
183
  <BaseContextMenu.CheckboxItemIndicator className="absolute left-2 flex h-3.5 w-3.5 items-center justify-center">
@@ -129,7 +192,7 @@ const ContextMenuRadioGroup = React.forwardRef<
129
192
  React.ComponentRef<typeof BaseContextMenu.RadioGroup>,
130
193
  React.ComponentProps<typeof BaseContextMenu.RadioGroup>
131
194
  >(({ className, ...props }, ref) => (
132
- <BaseContextMenu.RadioGroup ref={ref} className={className} {...props} />
195
+ <BaseContextMenu.RadioGroup ref={ref} className={className} data-slot="context-menu-radio-group" {...props} />
133
196
  ))
134
197
  ContextMenuRadioGroup.displayName = "ContextMenuRadioGroup"
135
198
 
@@ -146,6 +209,7 @@ const ContextMenuRadioItem = React.forwardRef<
146
209
  "data-[highlighted]:bg-accent data-[highlighted]:text-accent-foreground",
147
210
  className
148
211
  )}
212
+ data-slot="context-menu-radio-item"
149
213
  {...props}
150
214
  >
151
215
  <BaseContextMenu.RadioItemIndicator className="absolute left-2 flex h-3.5 w-3.5 items-center justify-center">
@@ -160,7 +224,7 @@ const ContextMenuGroup = React.forwardRef<
160
224
  React.ComponentRef<typeof BaseContextMenu.Group>,
161
225
  React.ComponentProps<typeof BaseContextMenu.Group>
162
226
  >(({ className, ...props }, ref) => (
163
- <BaseContextMenu.Group ref={ref} className={className} {...props} />
227
+ <BaseContextMenu.Group ref={ref} className={className} data-slot="context-menu-group" {...props} />
164
228
  ))
165
229
  ContextMenuGroup.displayName = "ContextMenuGroup"
166
230
 
@@ -177,6 +241,7 @@ const ContextMenuLabel = React.forwardRef<
177
241
  inset && "pl-8",
178
242
  className
179
243
  )}
244
+ data-slot="context-menu-label"
180
245
  {...props}
181
246
  />
182
247
  ))
@@ -189,6 +254,7 @@ const ContextMenuSeparator = React.forwardRef<
189
254
  <BaseContextMenu.Separator
190
255
  ref={ref}
191
256
  className={cn("-mx-1 my-1 h-px bg-border", className)}
257
+ data-slot="context-menu-separator"
192
258
  {...props}
193
259
  />
194
260
  ))
@@ -203,6 +269,7 @@ const ContextMenuShortcut = ({
203
269
  "ml-auto text-xs tracking-widest text-muted-foreground",
204
270
  className
205
271
  )}
272
+ data-slot="context-menu-shortcut"
206
273
  {...props}
207
274
  />
208
275
  )
@@ -230,6 +297,7 @@ const ContextMenuSubTrigger = React.forwardRef<
230
297
  inset && "pl-8",
231
298
  className
232
299
  )}
300
+ data-slot="context-menu-sub-trigger"
233
301
  {...props}
234
302
  >
235
303
  {children}
@@ -243,11 +311,11 @@ const ContextMenuSubContent = React.forwardRef<
243
311
  React.ComponentProps<typeof BaseContextMenu.Popup>
244
312
  >(({ className, ...props }, ref) => (
245
313
  <BaseContextMenu.Portal>
246
- <BaseContextMenu.Positioner className="outline-none" alignOffset={-4} sideOffset={8}>
314
+ <BaseContextMenu.Positioner className="z-50 outline-none" alignOffset={-4} sideOffset={8}>
247
315
  <BaseContextMenu.Popup
248
316
  ref={ref}
249
317
  className={cn(
250
- "z-50 min-w-32 overflow-hidden rounded-md border bg-popover p-1 text-popover-foreground shadow-lg",
318
+ "min-w-32 overflow-hidden rounded-md border bg-popover p-1 text-popover-foreground shadow-lg",
251
319
  "origin-[var(--transform-origin)]",
252
320
  "outline-none focus:outline-none focus-visible:outline-none",
253
321
  "transition-[transform,scale,opacity] duration-[var(--motion-duration-fast,150ms)] ease-[var(--motion-ease-out,cubic-bezier(0.23,1,0.32,1))]",
@@ -256,6 +324,7 @@ const ContextMenuSubContent = React.forwardRef<
256
324
  "motion-reduce:data-[starting-style]:scale-100 motion-reduce:data-[ending-style]:scale-100",
257
325
  className
258
326
  )}
327
+ data-slot="context-menu-sub-content"
259
328
  {...props}
260
329
  />
261
330
  </BaseContextMenu.Positioner>
@@ -3,10 +3,32 @@
3
3
  import * as React from "react"
4
4
  import { Dialog as BaseDialog } from "@base-ui/react/dialog"
5
5
  import { cn } from "@/lib/utils"
6
+ import { type AgentOpenParts, useAgentOpen, useHasAgent, useMergedRef } from "@/components/ui/agent"
6
7
 
7
- const Dialog = BaseDialog.Root
8
- const DialogTrigger = BaseDialog.Trigger
9
- const DialogPortal = BaseDialog.Portal
8
+ const DialogAgentContext = React.createContext<AgentOpenParts | null>(null)
9
+
10
+ /**
11
+ * Base UI's root, holding its own open state so an agent can open and close it the way a
12
+ * parent would. Controlled use is unchanged, and `onOpenChange` sees the agent's changes
13
+ * first and can cancel them. Closed, the agent sees the trigger; open, the popup.
14
+ */
15
+ function Dialog<Payload>({ open, defaultOpen, onOpenChange, ...props }: BaseDialog.Root.Props<Payload>) {
16
+ const [state, change, parts] = useAgentOpen("dialog", open, defaultOpen, onOpenChange)
17
+ return (
18
+ <DialogAgentContext.Provider value={useHasAgent() ? parts : null}>
19
+ <BaseDialog.Root<Payload> {...props} open={state} onOpenChange={change} />
20
+ </DialogAgentContext.Provider>
21
+ )
22
+ }
23
+
24
+ function DialogTrigger<Payload>({ ref, ...props }: BaseDialog.Trigger.Props<Payload> & React.RefAttributes<HTMLElement>) {
25
+ const dialog = React.useContext(DialogAgentContext)
26
+ return <BaseDialog.Trigger<Payload> ref={useMergedRef(ref, dialog?.trigger)} data-slot="dialog-trigger" {...props} />
27
+ }
28
+
29
+ function DialogPortal(props: React.ComponentProps<typeof BaseDialog.Portal>) {
30
+ return <BaseDialog.Portal data-slot="dialog-portal" {...props} />
31
+ }
10
32
 
11
33
  const DialogBackdrop = React.forwardRef<
12
34
  HTMLDivElement,
@@ -21,6 +43,7 @@ const DialogBackdrop = React.forwardRef<
21
43
  "data-[ending-style]:duration-[var(--motion-duration-fast,150ms)]",
22
44
  className
23
45
  )}
46
+ data-slot="dialog-backdrop"
24
47
  {...props}
25
48
  />
26
49
  ))
@@ -36,6 +59,7 @@ const DialogViewport = React.forwardRef<
36
59
  "fixed inset-0 z-50 flex items-center justify-center overflow-auto",
37
60
  className
38
61
  )}
62
+ data-slot="dialog-viewport"
39
63
  {...props}
40
64
  />
41
65
  ))
@@ -44,9 +68,12 @@ DialogViewport.displayName = "DialogViewport"
44
68
  const DialogPopup = React.forwardRef<
45
69
  HTMLDivElement,
46
70
  React.ComponentProps<typeof BaseDialog.Popup>
47
- >(({ className, ...props }, ref) => (
71
+ >(({ className, ...props }, ref) => {
72
+ const dialog = React.useContext(DialogAgentContext)
73
+ const mergedRef = useMergedRef(ref, dialog?.popup)
74
+ return (
48
75
  <BaseDialog.Popup
49
- ref={ref}
76
+ ref={mergedRef}
50
77
  className={cn(
51
78
  "w-[95%] max-w-lg gap-4 rounded-lg border bg-background p-6 shadow-lg",
52
79
  // A dialog is not anchored to its trigger, so scaling from the centre is correct
@@ -60,9 +87,11 @@ const DialogPopup = React.forwardRef<
60
87
  "data-[ending-style]:duration-[var(--motion-duration-fast,150ms)]",
61
88
  className
62
89
  )}
90
+ data-slot="dialog-popup"
63
91
  {...props}
64
92
  />
65
- ))
93
+ )
94
+ })
66
95
  DialogPopup.displayName = "DialogPopup"
67
96
 
68
97
  const DialogTitle = React.forwardRef<
@@ -72,6 +101,7 @@ const DialogTitle = React.forwardRef<
72
101
  <BaseDialog.Title
73
102
  ref={ref}
74
103
  className={cn("text-lg font-medium leading-none tracking-tight", className)}
104
+ data-slot="dialog-title"
75
105
  {...props}
76
106
  />
77
107
  ))
@@ -84,6 +114,7 @@ const DialogDescription = React.forwardRef<
84
114
  <BaseDialog.Description
85
115
  ref={ref}
86
116
  className={cn("text-sm text-muted-foreground", className)}
117
+ data-slot="dialog-description"
87
118
  {...props}
88
119
  />
89
120
  ))
@@ -92,15 +123,21 @@ DialogDescription.displayName = "DialogDescription"
92
123
  const DialogClose = React.forwardRef<
93
124
  HTMLButtonElement,
94
125
  React.ComponentProps<typeof BaseDialog.Close>
95
- >(({ className, ...props }, ref) => (
126
+ >(({ className, render, ...props }, ref) => (
96
127
  <BaseDialog.Close
97
128
  ref={ref}
98
- className={cn(
99
- "inline-flex h-10 items-center justify-center rounded-md border border-input bg-background px-4 py-2 text-sm font-medium shadow-sm transition-colors",
100
- "hover:bg-accent hover:text-accent-foreground focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2",
101
- "disabled:cursor-not-allowed disabled:opacity-50",
102
- className
103
- )}
129
+ render={render}
130
+ className={
131
+ render
132
+ ? className
133
+ : cn(
134
+ "inline-flex h-10 items-center justify-center rounded-md border border-input bg-background px-4 py-2 text-sm font-medium shadow-sm transition-colors",
135
+ "hover:bg-accent hover:text-accent-foreground focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2",
136
+ "disabled:cursor-not-allowed disabled:opacity-50",
137
+ className,
138
+ )
139
+ }
140
+ data-slot="dialog-close"
104
141
  {...props}
105
142
  />
106
143
  ))
@@ -115,6 +152,7 @@ const DialogHeader = ({
115
152
  "flex flex-col space-y-1.5 text-center sm:text-left",
116
153
  className
117
154
  )}
155
+ data-slot="dialog-header"
118
156
  {...props}
119
157
  />
120
158
  )
@@ -129,6 +167,7 @@ const DialogFooter = ({
129
167
  "flex flex-col-reverse gap-2 sm:flex-row sm:justify-end mt-1.5",
130
168
  className
131
169
  )}
170
+ data-slot="dialog-footer"
132
171
  {...props}
133
172
  />
134
173
  )
@@ -141,7 +180,7 @@ const DialogContent = React.forwardRef<
141
180
  <DialogPortal>
142
181
  <DialogBackdrop />
143
182
  <DialogViewport>
144
- <DialogPopup ref={ref} className={className} {...props}>
183
+ <DialogPopup ref={ref} className={className} data-slot="dialog-content" {...props}>
145
184
  {children}
146
185
  </DialogPopup>
147
186
  </DialogViewport>
@@ -3,12 +3,36 @@
3
3
  import * as React from "react"
4
4
  import { Drawer as DrawerPrimitive } from "@base-ui/react/drawer"
5
5
  import { cn } from "@/lib/utils"
6
+ import { type AgentOpenParts, useAgentOpen, useHasAgent, useMergedRef } from "@/components/ui/agent"
6
7
 
7
- const Drawer = DrawerPrimitive.Root
8
- const DrawerTrigger = DrawerPrimitive.Trigger
9
- const DrawerPortal = DrawerPrimitive.Portal
10
- const DrawerClose = DrawerPrimitive.Close
11
- const DrawerSwipeArea = DrawerPrimitive.SwipeArea
8
+ const DrawerAgentContext = React.createContext<AgentOpenParts | null>(null)
9
+
10
+ /** Base UI's root, holding its own open state so an agent can open and close it. */
11
+ function Drawer<Payload>({ open, defaultOpen, onOpenChange, ...props }: DrawerPrimitive.Root.Props<Payload>) {
12
+ const [state, change, parts] = useAgentOpen("dialog", open, defaultOpen, onOpenChange)
13
+ return (
14
+ <DrawerAgentContext.Provider value={useHasAgent() ? parts : null}>
15
+ <DrawerPrimitive.Root<Payload> {...props} open={state} onOpenChange={change} />
16
+ </DrawerAgentContext.Provider>
17
+ )
18
+ }
19
+
20
+ function DrawerTrigger<Payload>({ ref, ...props }: DrawerPrimitive.Trigger.Props<Payload> & React.RefAttributes<HTMLElement>) {
21
+ const parts = React.useContext(DrawerAgentContext)
22
+ return <DrawerPrimitive.Trigger<Payload> ref={useMergedRef(ref, parts?.trigger)} data-slot="drawer-trigger" {...props} />
23
+ }
24
+
25
+ function DrawerPortal(props: React.ComponentProps<typeof DrawerPrimitive.Portal>) {
26
+ return <DrawerPrimitive.Portal data-slot="drawer-portal" {...props} />
27
+ }
28
+
29
+ function DrawerClose(props: React.ComponentProps<typeof DrawerPrimitive.Close>) {
30
+ return <DrawerPrimitive.Close data-slot="drawer-close" {...props} />
31
+ }
32
+
33
+ function DrawerSwipeArea(props: React.ComponentProps<typeof DrawerPrimitive.SwipeArea>) {
34
+ return <DrawerPrimitive.SwipeArea data-slot="drawer-swipe-area" {...props} />
35
+ }
12
36
 
13
37
  const DrawerBackdrop = React.forwardRef<
14
38
  React.ComponentRef<typeof DrawerPrimitive.Backdrop>,
@@ -29,6 +53,7 @@ const DrawerBackdrop = React.forwardRef<
29
53
  "data-[swiping]:transition-none",
30
54
  className
31
55
  )}
56
+ data-slot="drawer-backdrop"
32
57
  {...props}
33
58
  />
34
59
  ))
@@ -46,7 +71,7 @@ const DrawerContent = React.forwardRef<
46
71
  <DrawerBackdrop />
47
72
  <DrawerPrimitive.Viewport>
48
73
  <DrawerPrimitive.Popup
49
- ref={ref}
74
+ ref={useMergedRef(ref, React.useContext(DrawerAgentContext)?.popup)}
50
75
  className={cn(
51
76
  "fixed z-50 flex flex-col gap-4 border bg-background p-6 shadow-lg",
52
77
  // A surface that travels in from an edge gets the drawer curve, which decays
@@ -74,6 +99,7 @@ const DrawerContent = React.forwardRef<
74
99
  "motion-reduce:data-[ending-style]:translate-x-0 motion-reduce:data-[ending-style]:translate-y-0 motion-reduce:data-[ending-style]:opacity-0",
75
100
  className
76
101
  )}
102
+ data-slot="drawer-content"
77
103
  {...props}
78
104
  >
79
105
  <DrawerPrimitive.Content className="size-full overflow-y-auto">
@@ -92,6 +118,7 @@ function DrawerTitle({
92
118
  return (
93
119
  <DrawerPrimitive.Title
94
120
  className={cn("text-lg font-medium leading-none tracking-tight", className)}
121
+ data-slot="drawer-title"
95
122
  {...props}
96
123
  />
97
124
  )
@@ -104,18 +131,19 @@ function DrawerDescription({
104
131
  return (
105
132
  <DrawerPrimitive.Description
106
133
  className={cn("text-sm text-muted-foreground", className)}
134
+ data-slot="drawer-description"
107
135
  {...props}
108
136
  />
109
137
  )
110
138
  }
111
139
 
112
140
  const DrawerHeader = ({ className, ...props }: React.HTMLAttributes<HTMLDivElement>) => (
113
- <div className={cn("flex flex-col gap-1.5 text-center sm:text-left", className)} {...props} />
141
+ <div className={cn("flex flex-col gap-1.5 text-center sm:text-left", className)} data-slot="drawer-header" {...props} />
114
142
  )
115
143
  DrawerHeader.displayName = "DrawerHeader"
116
144
 
117
145
  const DrawerFooter = ({ className, ...props }: React.HTMLAttributes<HTMLDivElement>) => (
118
- <div className={cn("mt-auto flex flex-col gap-2 sm:flex-row sm:justify-end", className)} {...props} />
146
+ <div className={cn("mt-auto flex flex-col gap-2 sm:flex-row sm:justify-end", className)} data-slot="drawer-footer" {...props} />
119
147
  )
120
148
  DrawerFooter.displayName = "DrawerFooter"
121
149