|
5 | 5 | *
|
6 | 6 | * Copyright Oxide Computer Company
|
7 | 7 | */
|
| 8 | + |
8 | 9 | import {
|
9 |
| - Content, |
10 |
| - Item, |
11 |
| - Portal, |
12 |
| - Root, |
13 |
| - Trigger, |
14 |
| - type DropdownMenuContentProps, |
15 |
| - type DropdownMenuItemProps, |
16 |
| -} from '@radix-ui/react-dropdown-menu' |
| 10 | + Menu, |
| 11 | + MenuButton, |
| 12 | + MenuItem, |
| 13 | + MenuItems, |
| 14 | + type MenuItemsProps, |
| 15 | +} from '@headlessui/react' |
17 | 16 | import cn from 'classnames'
|
18 |
| -import { forwardRef, type ForwardedRef } from 'react' |
| 17 | +import { forwardRef, type ForwardedRef, type ReactNode } from 'react' |
19 | 18 | import { Link } from 'react-router-dom'
|
20 | 19 |
|
21 |
| -type DivRef = ForwardedRef<HTMLDivElement> |
22 |
| - |
23 |
| -// remove possibility of disabling links for now. if we put it back, make sure |
24 |
| -// to forwardRef on LinkItem so the disabled tooltip can work |
25 |
| -type LinkitemProps = Omit<DropdownMenuItemProps, 'disabled'> & { to: string } |
26 |
| - |
27 |
| -export const DropdownMenu = { |
28 |
| - Root, |
29 |
| - Trigger, |
30 |
| - Portal, |
31 |
| - // don't need to forward ref here for a particular reason but Radix gives a |
32 |
| - // big angry warning if we don't |
33 |
| - Content: forwardRef(({ className, ...props }: DropdownMenuContentProps, ref: DivRef) => ( |
34 |
| - <Content |
35 |
| - {...props} |
36 |
| - // prevents focus ring showing up on trigger when you close the menu |
37 |
| - onCloseAutoFocus={(e) => e.preventDefault()} |
38 |
| - className={cn('DropdownMenuContent', className)} |
39 |
| - ref={ref} |
40 |
| - /> |
41 |
| - )), |
42 |
| - // need to forward ref because of tooltips on disabled menu buttons |
43 |
| - Item: forwardRef(({ className, ...props }: DropdownMenuItemProps, ref: DivRef) => ( |
44 |
| - <Item {...props} className={cn('DropdownMenuItem ox-menu-item', className)} ref={ref} /> |
45 |
| - )), |
46 |
| - LinkItem: ({ className, children, to, ...props }: LinkitemProps) => ( |
47 |
| - <Item {...props} className={cn('DropdownMenuItem ox-menu-item', className)} asChild> |
48 |
| - <Link to={to}>{children}</Link> |
49 |
| - </Item> |
50 |
| - ), |
| 20 | +export const Root = Menu |
| 21 | + |
| 22 | +export const Trigger = MenuButton |
| 23 | + |
| 24 | +type ContentProps = { |
| 25 | + className?: string |
| 26 | + children: ReactNode |
| 27 | + anchor?: MenuItemsProps['anchor'] |
| 28 | + /** Spacing in px, passed as --anchor-gap */ |
| 29 | + gap?: 8 |
51 | 30 | }
|
| 31 | + |
| 32 | +export function Content({ className, children, anchor = 'bottom end', gap }: ContentProps) { |
| 33 | + return ( |
| 34 | + <MenuItems |
| 35 | + anchor={anchor} |
| 36 | + // goofy gap because tailwind hates string interpolation |
| 37 | + className={cn('DropdownMenuContent', gap === 8 && `[--anchor-gap:8px]`, className)} |
| 38 | + // necessary to turn off scroll locking so the scrollbar doesn't pop in |
| 39 | + // and out as menu closes and opens |
| 40 | + modal={false} |
| 41 | + > |
| 42 | + {children} |
| 43 | + </MenuItems> |
| 44 | + ) |
| 45 | +} |
| 46 | + |
| 47 | +type LinkItemProps = { className?: string; to: string; children: ReactNode } |
| 48 | + |
| 49 | +export function LinkItem({ className, to, children }: LinkItemProps) { |
| 50 | + return ( |
| 51 | + <MenuItem> |
| 52 | + <Link className={cn('DropdownMenuItem ox-menu-item', className)} to={to}> |
| 53 | + {children} |
| 54 | + </Link> |
| 55 | + </MenuItem> |
| 56 | + ) |
| 57 | +} |
| 58 | + |
| 59 | +type ButtonRef = ForwardedRef<HTMLButtonElement> |
| 60 | +type ItemProps = { |
| 61 | + className?: string |
| 62 | + onSelect?: () => void |
| 63 | + children: ReactNode |
| 64 | + disabled?: boolean |
| 65 | +} |
| 66 | + |
| 67 | +// need to forward ref because of tooltips on disabled menu buttons |
| 68 | +export const Item = forwardRef( |
| 69 | + ({ className, onSelect, children, disabled }: ItemProps, ref: ButtonRef) => ( |
| 70 | + <MenuItem disabled={disabled}> |
| 71 | + <button |
| 72 | + type="button" |
| 73 | + className={cn('DropdownMenuItem ox-menu-item', className)} |
| 74 | + ref={ref} |
| 75 | + onClick={onSelect} |
| 76 | + > |
| 77 | + {children} |
| 78 | + </button> |
| 79 | + </MenuItem> |
| 80 | + ) |
| 81 | +) |
0 commit comments