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