Raw
1 import { DropdownMenu, IconButton, clx } from "@medusajs/ui"
2
3 import { EllipsisHorizontal } from "@medusajs/icons"
4 import { PropsWithChildren, ReactNode } from "react"
5 import { Link } from "react-router-dom"
6 import { ConditionalTooltip } from "./conditional-tooltip"
7
8 export type Action = {
9 icon: ReactNode
10 label: string
11 disabled?: boolean
12 /**
13 * Optional tooltip to display when a disabled action is hovered.
14 */
15 disabledTooltip?: string | ReactNode
16 } & (
17 | {
18 to: string
19 onClick?: never
20 }
21 | {
22 onClick: () => void
23 to?: never
24 }
25 )
26
27 export type ActionGroup = {
28 actions: Action[]
29 }
30
31 type ActionMenuProps = PropsWithChildren<{
32 groups: ActionGroup[]
33 variant?: "transparent" | "primary"
34 }>
35
36 export const ActionMenu = ({
37 groups,
38 variant = "transparent",
39 children,
40 }: ActionMenuProps) => {
41 const inner = children ?? (
42 <IconButton size="small" variant={variant}>
43 <EllipsisHorizontal />
44 </IconButton>
45 )
46
47 return (
48 <DropdownMenu>
49 <DropdownMenu.Trigger asChild>{inner}</DropdownMenu.Trigger>
50 <DropdownMenu.Content>
51 {groups.map((group, index) => {
52 if (!group.actions.length) {
53 return null
54 }
55
56 const isLast = index === groups.length - 1
57
58 return (
59 <DropdownMenu.Group key={index}>
60 {group.actions.map((action, index) => {
61 const Wrapper = action.disabledTooltip
62 ? ({ children }: { children: ReactNode }) => (
63 <ConditionalTooltip
64 showTooltip={action.disabled}
65 content={action.disabledTooltip}
66 side="right"
67 >
68 <div>{children}</div>
69 </ConditionalTooltip>
70 )
71 : "div"
72
73 if (action.onClick) {
74 return (
75 <Wrapper key={index}>
76 <DropdownMenu.Item
77 disabled={action.disabled}
78 onClick={(e) => {
79 e.stopPropagation()
80 action.onClick()
81 }}
82 className={clx(
83 "[&_svg]:text-ui-fg-subtle flex items-center gap-x-2",
84 {
85 "[&_svg]:text-ui-fg-disabled": action.disabled,
86 }
87 )}
88 >
89 {action.icon}
90 <span>{action.label}</span>
91 </DropdownMenu.Item>
92 </Wrapper>
93 )
94 }
95
96 return (
97 <Wrapper key={index}>
98 <DropdownMenu.Item
99 className={clx(
100 "[&_svg]:text-ui-fg-subtle flex items-center gap-x-2",
101 {
102 "[&_svg]:text-ui-fg-disabled": action.disabled,
103 }
104 )}
105 asChild
106 disabled={action.disabled}
107 >
108 <Link to={action.to} onClick={(e) => e.stopPropagation()}>
109 {action.icon}
110 <span>{action.label}</span>
111 </Link>
112 </DropdownMenu.Item>
113 </Wrapper>
114 )
115 })}
116 {!isLast && <DropdownMenu.Separator />}
117 </DropdownMenu.Group>
118 )
119 })}
120 </DropdownMenu.Content>
121 </DropdownMenu>
122 )
123 }