Skip to content
Source
Sign inSign up
TanStack/form
Open History to see the last commit

dropdown-menu.tsx

509 lines15.4 KB

1import { Menu as DropdownMenuPrimitive } from '@ark-ui/solid'
2import CheckIcon from 'lucide-solid/icons/check'
3import ChevronRightIcon from 'lucide-solid/icons/chevron-right'
4import { createUniqueId, splitProps } from 'solid-js'
5import type { JSX } from 'solid-js'
6import { Portal } from '@/components/ui/portal'
7import { cn } from '@/utils'
8​
9// Ported from the original shadcn/ui + Radix UI + React component.
10// This wraps the Ark UI + SolidJS primitive and preserves shadcn's Tailwind-based
11// design-system API, variants, and styling.
12// https://ui.shadcn.com/
13​
14type OptionalValueItemProps = {
15 value?: string
17​
18/**
19 * The root component that owns dropdown menu state and positioning.
20 *
21 * @example
22 * ```tsx
23 * <DropdownMenu>
24 * <DropdownMenuTrigger
25 * asChild={(triggerProps) => (
26 * <Button variant="outline" {...triggerProps()}>
27 * Open
28 * </Button>
29 * )}
30 * />
31 * <DropdownMenuContent>
32 * <DropdownMenuItem value="profile">Profile</DropdownMenuItem>
33 * </DropdownMenuContent>
34 * </DropdownMenu>
35 * ```
36 */
37function DropdownMenu(props: DropdownMenuPrimitive.RootProps) {
38 const [local, others] = splitProps(props, ['children', 'positioning'])
39​
40 return (
41 <DropdownMenuPrimitive.Root
42 {...others}
43 data-slot="dropdown-menu"
44 positioning={{
45 placement: 'bottom-start',
46 gutter: 8,
47 fitViewport: true,
48 ...local.positioning,
49 }}
50 >
51 {local.children}
52 </DropdownMenuPrimitive.Root>
53 )
55​
56/**
57 * The control that opens and closes a DropdownMenu.
58 *
59 * @example
60 * ```tsx
61 * <DropdownMenu>
62 * <DropdownMenuTrigger
63 * asChild={(triggerProps) => (
64 * <Button variant="outline" {...triggerProps()}>
65 * Open
66 * </Button>
67 * )}
68 * />
69 * <DropdownMenuContent>
70 * <DropdownMenuItem value="profile">Profile</DropdownMenuItem>
71 * </DropdownMenuContent>
72 * </DropdownMenu>
73 * ```
74 */
75function DropdownMenuTrigger(props: DropdownMenuPrimitive.TriggerProps) {
76 const [local, others] = splitProps(props, ['class'])
77​
78 return (
79 <DropdownMenuPrimitive.Trigger
80 {...others}
81 data-slot="dropdown-menu-trigger"
82 class={local.class}
83 />
84 )
86​
87/**
88 * The floating menu surface that contains DropdownMenuItem actions.
89 *
90 * @example
91 * ```tsx
92 * <DropdownMenu positioning={{ placement: "bottom-start" }}>
93 * <DropdownMenuTrigger
94 * asChild={(triggerProps) => (
95 * <Button variant="outline" {...triggerProps()}>
96 * Open
97 * </Button>
98 * )}
99 * />
100 * <DropdownMenuContent class="w-40">
101 * <DropdownMenuItem value="profile">Profile</DropdownMenuItem>
102 * <DropdownMenuItem value="settings">Settings</DropdownMenuItem>
103 * </DropdownMenuContent>
104 * </DropdownMenu>
105 * ```
106 */
107function DropdownMenuContent(props: DropdownMenuPrimitive.ContentProps) {
108 const [local, others] = splitProps(props, ['children', 'class'])
109​
110 return (
111 <Portal>
112 <DropdownMenuPrimitive.Positioner data-slot="dropdown-menu-positioner">
113 <DropdownMenuPrimitive.Content
114 {...others}
115 data-slot="dropdown-menu-content"
116 class={cn(
117 'outline-none z-50 max-h-(--available-height) min-w-[max(var(--reference-width),8rem)] origin-(--transform-origin) overflow-x-hidden overflow-y-auto rounded-lg bg-popover p-1 text-popover-foreground shadow-md ring-1 ring-foreground/10 duration-100 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 data-[state=closed]:overflow-hidden data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95',
118 local.class,
119 )}
120 >
121 {local.children}
122 </DropdownMenuPrimitive.Content>
123 </DropdownMenuPrimitive.Positioner>
124 </Portal>
125 )
127​
128/**
129 * Groups related DropdownMenuItem actions within DropdownMenuContent.
130 *
131 * @example
132 * ```tsx
133 * <DropdownMenuContent>
134 * <DropdownMenuGroup>
135 * <DropdownMenuLabel>Account</DropdownMenuLabel>
136 * <DropdownMenuItem value="profile">Profile</DropdownMenuItem>
137 * </DropdownMenuGroup>
138 * </DropdownMenuContent>
139 * ```
140 */
141function DropdownMenuGroup(props: DropdownMenuPrimitive.ItemGroupProps) {
142 const [local, others] = splitProps(props, ['class'])
143​
144 return (
145 <DropdownMenuPrimitive.ItemGroup
146 {...others}
147 data-slot="dropdown-menu-group"
148 class={local.class}
149 />
150 )
152​
153type DropdownMenuItemProps = Omit<DropdownMenuPrimitive.ItemProps, 'value'> &
154 OptionalValueItemProps & {
155 inset?: boolean
156 variant?: 'default' | 'destructive'
157 }
158​
159/**
160 * A selectable action within DropdownMenuContent.
161 *
162 * @example
163 * ```tsx
164 * <DropdownMenuContent>
165 * <DropdownMenuItem value="profile">
166 * Profile
167 * <DropdownMenuShortcut>Ctrl+P</DropdownMenuShortcut>
168 * </DropdownMenuItem>
169 * <DropdownMenuItem value="delete" variant="destructive">
170 * Delete
171 * </DropdownMenuItem>
172 * </DropdownMenuContent>
173 * ```
174 */
175function DropdownMenuItem(props: DropdownMenuItemProps) {
176 const [local, others] = splitProps(props, [
177 'class',
178 'inset',
179 'value',
180 'variant',
181 ])
182 const fallbackValue = createUniqueId()
183​
184 const variant = () => local.variant ?? 'default'
185 const value = () => local.value ?? fallbackValue
186​
187 return (
188 <DropdownMenuPrimitive.Item
189 {...others}
190 value={value()}
191 data-slot="dropdown-menu-item"
192 data-inset={local.inset ? '' : undefined}
193 data-variant={variant()}
194 class={cn(
195 "group/dropdown-menu-item relative flex cursor-default items-center gap-1.5 rounded-md px-1.5 py-1 text-sm outline-hidden select-none data-highlighted:bg-accent data-highlighted:text-accent-foreground not-data-[variant=destructive]:data-highlighted:**:text-accent-foreground data-inset:pl-7 data-[variant=destructive]:text-destructive data-[variant=destructive]:data-highlighted:bg-destructive/10 data-[variant=destructive]:data-highlighted:text-destructive dark:data-[variant=destructive]:data-highlighted:bg-destructive/20 data-disabled:pointer-events-none data-disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4 data-[variant=destructive]:*:[svg]:text-destructive",
196 local.class,
197 )}
198 />
199 )
201​
202type DropdownMenuCheckboxItemProps = Omit<
203 DropdownMenuPrimitive.CheckboxItemProps,
204 'checked' | 'value'
205> &
206 OptionalValueItemProps & {
207 checked?: boolean
208 inset?: boolean
209 }
210​
211/**
212 * A checkable menu item for toggling an option from DropdownMenuContent.
213 *
214 * @example
215 * ```tsx
216 * <DropdownMenu closeOnSelect={false}>
217 * <DropdownMenuTrigger>View</DropdownMenuTrigger>
218 * <DropdownMenuContent>
219 * <DropdownMenuCheckboxItem
220 * value="grid"
221 * checked={showGrid()}
222 * onCheckedChange={(checked) => setShowGrid(checked)}
223 * >
224 * Grid
225 * </DropdownMenuCheckboxItem>
226 * </DropdownMenuContent>
227 * </DropdownMenu>
228 * ```
229 */
230function DropdownMenuCheckboxItem(props: DropdownMenuCheckboxItemProps) {
231 const [local, others] = splitProps(props, [
232 'checked',
233 'children',
234 'class',
235 'inset',
236 'value',
237 ])
238 const fallbackValue = createUniqueId()
239​
240 const checked = () => local.checked ?? false
241 const value = () => local.value ?? fallbackValue
242​
243 return (
244 <DropdownMenuPrimitive.CheckboxItem
245 {...others}
246 checked={checked()}
247 value={value()}
248 data-slot="dropdown-menu-checkbox-item"
249 data-inset={local.inset ? '' : undefined}
250 class={cn(
251 "relative flex cursor-default items-center gap-1.5 rounded-md py-1 pr-8 pl-1.5 text-sm outline-hidden select-none data-highlighted:bg-accent data-highlighted:text-accent-foreground data-highlighted:**:text-accent-foreground data-inset:pl-7 data-disabled:pointer-events-none data-disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
252 local.class,
253 )}
254 >
255 <span
256 class="pointer-events-none absolute right-2 flex items-center justify-center"
257 data-slot="dropdown-menu-checkbox-item-indicator"
258 >
259 <DropdownMenuPrimitive.ItemIndicator>
260 <CheckIcon />
261 </DropdownMenuPrimitive.ItemIndicator>
262 </span>
263 {local.children}
264 </DropdownMenuPrimitive.CheckboxItem>
265 )
267​
268/**
269 * Groups mutually exclusive DropdownMenuRadioItem options.
270 *
271 * @example
272 * ```tsx
273 * <DropdownMenuRadioGroup
274 * value={density()}
275 * onValueChange={(details) => setDensity(details.value)}
276 * >
277 * <DropdownMenuRadioItem value="compact">Compact</DropdownMenuRadioItem>
278 * <DropdownMenuRadioItem value="comfortable">
279 * Comfortable
280 * </DropdownMenuRadioItem>
281 * </DropdownMenuRadioGroup>
282 * ```
283 */
284function DropdownMenuRadioGroup(
285 props: DropdownMenuPrimitive.RadioItemGroupProps,
286) {
287 const [local, others] = splitProps(props, ['class'])
288​
289 return (
290 <DropdownMenuPrimitive.RadioItemGroup
291 {...others}
292 data-slot="dropdown-menu-radio-group"
293 class={local.class}
294 />
295 )
297​
298type DropdownMenuRadioItemProps = DropdownMenuPrimitive.RadioItemProps & {
299 inset?: boolean
301​
302/**
303 * A radio-style menu item controlled by a DropdownMenuRadioGroup.
304 *
305 * @example
306 * ```tsx
307 * <DropdownMenuRadioGroup value="comfortable">
308 * <DropdownMenuRadioItem value="compact">Compact</DropdownMenuRadioItem>
309 * <DropdownMenuRadioItem value="comfortable">
310 * Comfortable
311 * </DropdownMenuRadioItem>
312 * </DropdownMenuRadioGroup>
313 * ```
314 */
315function DropdownMenuRadioItem(props: DropdownMenuRadioItemProps) {
316 const [local, others] = splitProps(props, ['children', 'class', 'inset'])
317​
318 return (
319 <DropdownMenuPrimitive.RadioItem
320 {...others}
321 data-slot="dropdown-menu-radio-item"
322 data-inset={local.inset ? '' : undefined}
323 class={cn(
324 "relative flex cursor-default items-center gap-1.5 rounded-md py-1 pr-8 pl-1.5 text-sm outline-hidden select-none data-highlighted:bg-accent data-highlighted:text-accent-foreground data-highlighted:**:text-accent-foreground data-inset:pl-7 data-disabled:pointer-events-none data-disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
325 local.class,
326 )}
327 >
328 <span
329 class="pointer-events-none absolute right-2 flex items-center justify-center"
330 data-slot="dropdown-menu-radio-item-indicator"
331 >
332 <DropdownMenuPrimitive.ItemIndicator>
333 <CheckIcon />
334 </DropdownMenuPrimitive.ItemIndicator>
335 </span>
336 {local.children}
337 </DropdownMenuPrimitive.RadioItem>
338 )
340​
341type DropdownMenuLabelProps = JSX.HTMLElementTags['div'] & {
342 inset?: boolean
344​
345/**
346 * A muted label for a group of dropdown menu actions.
347 *
348 * @example
349 * ```tsx
350 * <DropdownMenuContent>
351 * <DropdownMenuGroup>
352 * <DropdownMenuLabel>Account</DropdownMenuLabel>
353 * <DropdownMenuItem value="profile">Profile</DropdownMenuItem>
354 * </DropdownMenuGroup>
355 * </DropdownMenuContent>
356 * ```
357 */
358function DropdownMenuLabel(props: DropdownMenuLabelProps) {
359 const [local, others] = splitProps(props, ['class', 'inset'])
360​
361 return (
362 <div
363 {...others}
364 data-slot="dropdown-menu-label"
365 data-inset={local.inset ? '' : undefined}
366 class={cn(
367 'px-1.5 py-1 text-xs font-medium text-muted-foreground data-inset:pl-7',
368 local.class,
369 )}
370 />
371 )
373​
374/**
375 * A visual divider between groups of dropdown menu actions.
376 *
377 * @example
378 * ```tsx
379 * <DropdownMenuContent>
380 * <DropdownMenuItem value="profile">Profile</DropdownMenuItem>
381 * <DropdownMenuSeparator />
382 * <DropdownMenuItem value="logout">Log out</DropdownMenuItem>
383 * </DropdownMenuContent>
384 * ```
385 */
386function DropdownMenuSeparator(props: DropdownMenuPrimitive.SeparatorProps) {
387 const [local, others] = splitProps(props, ['class'])
388​
389 return (
390 <DropdownMenuPrimitive.Separator
391 {...others}
392 data-slot="dropdown-menu-separator"
393 class={cn('-mx-1 my-1 h-px bg-border', local.class)}
394 />
395 )
397​
398/**
399 * Right-aligned shortcut text shown inside a DropdownMenuItem.
400 *
401 * @example
402 * ```tsx
403 * <DropdownMenuItem value="profile">
404 * Profile
405 * <DropdownMenuShortcut>Ctrl+P</DropdownMenuShortcut>
406 * </DropdownMenuItem>
407 * ```
408 */
409function DropdownMenuShortcut(props: JSX.HTMLElementTags['span']) {
410 const [local, others] = splitProps(props, ['class'])
411​
412 return (
413 <span
414 {...others}
415 data-slot="dropdown-menu-shortcut"
416 class={cn(
417 'ml-auto text-xs tracking-widest text-muted-foreground group-data-highlighted/dropdown-menu-item:text-accent-foreground',
418 local.class,
419 )}
420 />
421 )
423​
424type DropdownMenuSubTriggerProps = DropdownMenuPrimitive.TriggerItemProps & {
425 inset?: boolean
427​
428/**
429 * The menu item that opens a nested dropdown menu.
430 *
431 * @example
432 * ```tsx
433 * <DropdownMenu>
434 * <DropdownMenuSubTrigger>Invite users</DropdownMenuSubTrigger>
435 * <DropdownMenuSubContent>
436 * <DropdownMenuItem value="email">Email</DropdownMenuItem>
437 * </DropdownMenuSubContent>
438 * </DropdownMenu>
439 * ```
440 */
441function DropdownMenuSubTrigger(props: DropdownMenuSubTriggerProps) {
442 const [local, others] = splitProps(props, ['children', 'class', 'inset'])
443​
444 return (
445 <DropdownMenuPrimitive.TriggerItem
446 {...others}
447 data-slot="dropdown-menu-sub-trigger"
448 data-inset={local.inset ? '' : undefined}
449 class={cn(
450 "flex cursor-default items-center gap-1.5 rounded-md px-1.5 py-1 text-sm outline-hidden select-none data-highlighted:bg-accent data-highlighted:text-accent-foreground not-data-[variant=destructive]:data-highlighted:**:text-accent-foreground data-inset:pl-7 data-open:bg-accent data-open:text-accent-foreground [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",
451 local.class,
452 )}
453 >
454 {local.children}
455 <ChevronRightIcon class="ml-auto" />
456 </DropdownMenuPrimitive.TriggerItem>
457 )
459​
460/**
461 * The floating content surface for a nested dropdown menu.
462 *
463 * @example
464 * ```tsx
465 * <DropdownMenu>
466 * <DropdownMenuSubTrigger>Invite users</DropdownMenuSubTrigger>
467 * <DropdownMenuSubContent>
468 * <DropdownMenuItem value="email">Email</DropdownMenuItem>
469 * <DropdownMenuItem value="message">Message</DropdownMenuItem>
470 * </DropdownMenuSubContent>
471 * </DropdownMenu>
472 * ```
473 */
474function DropdownMenuSubContent(props: DropdownMenuPrimitive.ContentProps) {
475 const [local, others] = splitProps(props, ['children', 'class'])
476​
477 return (
478 <Portal>
479 <DropdownMenuPrimitive.Positioner data-slot="dropdown-menu-sub-positioner">
480 <DropdownMenuPrimitive.Content
481 {...others}
482 data-slot="dropdown-menu-sub-content"
483 class={cn(
484 'z-50 min-w-24 origin-(--transform-origin) overflow-hidden rounded-lg bg-popover p-1 text-popover-foreground shadow-lg ring-1 ring-foreground/10 duration-100 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95',
485 local.class,
486 )}
487 >
488 {local.children}
489 </DropdownMenuPrimitive.Content>
490 </DropdownMenuPrimitive.Positioner>
491 </Portal>
492 )
494​
495export {
496 DropdownMenu,
497 DropdownMenuCheckboxItem,
498 DropdownMenuContent,
499 DropdownMenuGroup,
500 DropdownMenuItem,
501 DropdownMenuLabel,
502 DropdownMenuRadioGroup,
503 DropdownMenuRadioItem,
504 DropdownMenuSeparator,
505 DropdownMenuShortcut,
506 DropdownMenuSubContent,
507 DropdownMenuSubTrigger,
508 DropdownMenuTrigger,