|
1
|
--- |
|
2
|
sidebar_label: "Action Menu" |
|
3
|
--- |
|
4
|
|
|
5
|
import { TypeList } from "docs-ui" |
|
6
|
|
|
7
|
export const metadata = { |
|
8
|
title: `Action Menu - Admin Components`, |
|
9
|
} |
|
10
|
|
|
11
|
# {metadata.title} |
|
12
|
|
|
13
|
<Note title="Archived" type="warning"> |
|
14
|
|
|
15
|
Medusa exports an `ActionMenu` component from the `@medusajs/dashboard` package since [v2.21.0](https://github.com/medusajs/medusa/releases/tag/v2.21.0). Use [the exported component](../../components/action-menu/page.mdx) instead of building your own. This guide is kept for reference only. |
|
16
|
|
|
17
|
</Note> |
|
18
|
|
|
19
|
In this guide, you'll learn how to add an action menu to your Medusa Admin customizations. |
|
20
|
|
|
21
|
The Medusa Admin often provides additional actions in a dropdown shown when users click a three-dot icon. |
|
22
|
|
|
23
|
 |
|
24
|
|
|
25
|
To create a component that shows this menu in your customizations, create the file `src/admin/components/action-menu.tsx` with the following content: |
|
26
|
|
|
27
|
```tsx title="src/admin/components/action-menu.tsx" |
|
28
|
import { |
|
29
|
DropdownMenu, |
|
30
|
IconButton, |
|
31
|
clx, |
|
32
|
} from "@medusajs/ui" |
|
33
|
import { EllipsisHorizontal } from "@medusajs/icons" |
|
34
|
import { Link } from "react-router-dom" |
|
35
|
|
|
36
|
export type Action = { |
|
37
|
icon: React.ReactNode |
|
38
|
label: string |
|
39
|
disabled?: boolean |
|
40
|
} & ( |
|
41
|
| { |
|
42
|
to: string |
|
43
|
onClick?: never |
|
44
|
} |
|
45
|
| { |
|
46
|
onClick: () => void |
|
47
|
to?: never |
|
48
|
} |
|
49
|
) |
|
50
|
|
|
51
|
export type ActionGroup = { |
|
52
|
actions: Action[] |
|
53
|
} |
|
54
|
|
|
55
|
export type ActionMenuProps = { |
|
56
|
groups: ActionGroup[] |
|
57
|
} |
|
58
|
|
|
59
|
export const ActionMenu = ({ groups }: ActionMenuProps) => { |
|
60
|
return ( |
|
61
|
<DropdownMenu> |
|
62
|
<DropdownMenu.Trigger asChild> |
|
63
|
<IconButton size="small" variant="transparent"> |
|
64
|
<EllipsisHorizontal /> |
|
65
|
</IconButton> |
|
66
|
</DropdownMenu.Trigger> |
|
67
|
<DropdownMenu.Content> |
|
68
|
{groups.map((group, index) => { |
|
69
|
if (!group.actions.length) { |
|
70
|
return null |
|
71
|
} |
|
72
|
|
|
73
|
const isLast = index === groups.length - 1 |
|
74
|
|
|
75
|
return ( |
|
76
|
<DropdownMenu.Group key={index}> |
|
77
|
{group.actions.map((action, index) => { |
|
78
|
if (action.onClick) { |
|
79
|
return ( |
|
80
|
<DropdownMenu.Item |
|
81
|
disabled={action.disabled} |
|
82
|
key={index} |
|
83
|
onClick={(e) => { |
|
84
|
e.stopPropagation() |
|
85
|
action.onClick() |
|
86
|
}} |
|
87
|
className={clx( |
|
88
|
"[&_svg]:text-ui-fg-subtle flex items-center gap-x-2", |
|
89
|
{ |
|
90
|
"[&_svg]:text-ui-fg-disabled": action.disabled, |
|
91
|
} |
|
92
|
)} |
|
93
|
> |
|
94
|
{action.icon} |
|
95
|
<span>{action.label}</span> |
|
96
|
</DropdownMenu.Item> |
|
97
|
) |
|
98
|
} |
|
99
|
|
|
100
|
return ( |
|
101
|
<div key={index}> |
|
102
|
<DropdownMenu.Item |
|
103
|
className={clx( |
|
104
|
"[&_svg]:text-ui-fg-subtle flex items-center gap-x-2", |
|
105
|
{ |
|
106
|
"[&_svg]:text-ui-fg-disabled": action.disabled, |
|
107
|
} |
|
108
|
)} |
|
109
|
asChild |
|
110
|
disabled={action.disabled} |
|
111
|
> |
|
112
|
<Link to={action.to} onClick={(e) => e.stopPropagation()}> |
|
113
|
{action.icon} |
|
114
|
<span>{action.label}</span> |
|
115
|
</Link> |
|
116
|
</DropdownMenu.Item> |
|
117
|
</div> |
|
118
|
) |
|
119
|
})} |
|
120
|
{!isLast && <DropdownMenu.Separator />} |
|
121
|
</DropdownMenu.Group> |
|
122
|
) |
|
123
|
})} |
|
124
|
</DropdownMenu.Content> |
|
125
|
</DropdownMenu> |
|
126
|
) |
|
127
|
} |
|
128
|
``` |
|
129
|
|
|
130
|
The `ActionMenu` component shows a three-dots icon (or `EllipsisHorizontal`) from the [Medusa Icons package](!ui!/icons/overview) in a button. |
|
131
|
|
|
132
|
When the button is clicked, a dropdown menu is shown with the actions passed in the props. |
|
133
|
|
|
134
|
The component accepts the following props: |
|
135
|
|
|
136
|
<TypeList |
|
137
|
types={[ |
|
138
|
{ |
|
139
|
name: "groups", |
|
140
|
type: "`object[]`", |
|
141
|
optional: false, |
|
142
|
description: "Groups of actions to be shown in the dropdown. Each group is separated by a divider.", |
|
143
|
children: [ |
|
144
|
{ |
|
145
|
name: "actions", |
|
146
|
type: "`object[]`", |
|
147
|
optional: false, |
|
148
|
description: "Actions in the group.", |
|
149
|
children: [ |
|
150
|
{ |
|
151
|
name: "icon", |
|
152
|
type: "`React.ReactNode`", |
|
153
|
optional: false, |
|
154
|
description: `The icon of the action. You can use icons from the [Medusa Icons package](https://docs.medusajs.com/ui/icons/overview).` |
|
155
|
}, |
|
156
|
{ |
|
157
|
name: "label", |
|
158
|
type: "`string`", |
|
159
|
optional: false, |
|
160
|
description: "The action's text." |
|
161
|
}, |
|
162
|
{ |
|
163
|
name: "disabled", |
|
164
|
type: "`boolean`", |
|
165
|
optional: true, |
|
166
|
defaultValue: false, |
|
167
|
description: "Whether the action is shown as disabled." |
|
168
|
}, |
|
169
|
{ |
|
170
|
name: "`to`", |
|
171
|
type: "`string`", |
|
172
|
optional: true, |
|
173
|
description: "The link to take the user to when they click the action. This is required if `onClick` isn't provided." |
|
174
|
}, |
|
175
|
{ |
|
176
|
name: "`onClick`", |
|
177
|
type: "`() => void`", |
|
178
|
optional: true, |
|
179
|
description: "The function to execute when the action is clicked. This is required if `to` isn't provided." |
|
180
|
} |
|
181
|
] |
|
182
|
} |
|
183
|
] |
|
184
|
} |
|
185
|
]} |
|
186
|
/> |
|
187
|
|
|
188
|
--- |
|
189
|
|
|
190
|
## Example |
|
191
|
|
|
192
|
Use the `ActionMenu` component in any widget or UI route. |
|
193
|
|
|
194
|
For example, create the widget `src/admin/widgets/product-widget.tsx` with the following content: |
|
195
|
|
|
196
|
```tsx title="src/admin/widgets/product-widget.tsx" |
|
197
|
import { defineWidgetConfig } from "@medusajs/admin-sdk" |
|
198
|
import { Pencil } from "@medusajs/icons" |
|
199
|
import { Container } from "../components/container" |
|
200
|
import { ActionMenu } from "../components/action-menu" |
|
201
|
|
|
202
|
const ProductWidget = () => { |
|
203
|
return ( |
|
204
|
<Container> |
|
205
|
<ActionMenu groups={[ |
|
206
|
{ |
|
207
|
actions: [ |
|
208
|
{ |
|
209
|
icon: <Pencil />, |
|
210
|
label: "Edit", |
|
211
|
onClick: () => { |
|
212
|
alert("You clicked the edit action!") |
|
213
|
}, |
|
214
|
}, |
|
215
|
], |
|
216
|
}, |
|
217
|
]} /> |
|
218
|
</Container> |
|
219
|
) |
|
220
|
} |
|
221
|
|
|
222
|
export const config = defineWidgetConfig({ |
|
223
|
zone: "product.details", |
|
224
|
}) |
|
225
|
|
|
226
|
export default ProductWidget |
|
227
|
``` |
|
228
|
|
|
229
|
This widget also uses a [Container](../../guides/container/page.mdx) custom component. |
|
230
|
|
|
231
|
### Use in Header |
|
232
|
|
|
233
|
You can also use the action menu in the [Header](../../guides/header/page.mdx) component as part of its actions. |
|
234
|
|
|
235
|
For example: |
|
236
|
|
|
237
|
```tsx title="src/admin/widgets/product-widget.tsx" |
|
238
|
import { defineWidgetConfig } from "@medusajs/admin-sdk" |
|
239
|
import { Pencil } from "@medusajs/icons" |
|
240
|
import { Container } from "../components/container" |
|
241
|
import { Header } from "../components/header" |
|
242
|
|
|
243
|
const ProductWidget = () => { |
|
244
|
return ( |
|
245
|
<Container> |
|
246
|
<Header |
|
247
|
title="Product Widget" |
|
248
|
subtitle="This is my custom product widget" |
|
249
|
actions={[ |
|
250
|
{ |
|
251
|
type: "action-menu", |
|
252
|
props: { |
|
253
|
groups: [ |
|
254
|
{ |
|
255
|
actions: [ |
|
256
|
{ |
|
257
|
icon: <Pencil />, |
|
258
|
label: "Edit", |
|
259
|
onClick: () => { |
|
260
|
alert("You clicked the edit action!") |
|
261
|
}, |
|
262
|
}, |
|
263
|
], |
|
264
|
}, |
|
265
|
], |
|
266
|
}, |
|
267
|
}, |
|
268
|
]} |
|
269
|
/> |
|
270
|
</Container> |
|
271
|
) |
|
272
|
} |
|
273
|
|
|
274
|
export const config = defineWidgetConfig({ |
|
275
|
zone: "product.details", |
|
276
|
}) |
|
277
|
|
|
278
|
export default ProductWidget |
|
279
|
``` |