|
1
|
import { Prerequisites } from "docs-ui" |
|
2
|
|
|
3
|
export const metadata = { |
|
4
|
title: `${pageNumber} Custom Injection Zones`, |
|
5
|
} |
|
6
|
|
|
7
|
# {metadata.title} |
|
8
|
|
|
9
|
In this chapter, you'll learn how to add custom widget injection zones in a plugin. |
|
10
|
|
|
11
|
<Note> |
|
12
|
|
|
13
|
Custom injection zones are available since Medusa [v2.16.0](https://github.com/medusajs/medusa/releases/tag/v2.16.0). |
|
14
|
|
|
15
|
</Note> |
|
16
|
|
|
17
|
## What are Custom Injection Zones? |
|
18
|
|
|
19
|
The Medusa Admin has [pre-defined injection zones](!resources!/admin-widget-injection-zones) where you insert [widgets](../widgets/page.mdx). When you build a plugin that adds custom admin pages, you can also register custom injection zones to allow other developers to inject widgets into your plugin's pages. |
|
20
|
|
|
21
|
Adding custom injection zones in a plugin requires two steps: |
|
22
|
|
|
23
|
1. Expose the injection zones in your custom admin page using the `LayoutComposer` component. |
|
24
|
2. Register the injection zones in the `InjectionZoneRegistry` interface to enable type checking and autocompletion in `defineWidgetConfig`. |
|
25
|
|
|
26
|
--- |
|
27
|
|
|
28
|
## Step 1: Expose Injection Zones with LayoutComposer |
|
29
|
|
|
30
|
<Prerequisites |
|
31
|
items={[ |
|
32
|
{ |
|
33
|
link: "/learn/fundamentals/admin/ui-routes", |
|
34
|
text: "Custom admin page (UI route)", |
|
35
|
}, |
|
36
|
]} |
|
37
|
/> |
|
38
|
|
|
39
|
When you build a custom admin page in a plugin, use the [LayoutComposer](!resources!/admin-components/layouts/layout-composer) component from `@medusajs/dashboard/components` to structure the page's layout. This component also exposes injection zones based on the prefix you pass to its `widgetsZonePrefix` prop. |
|
40
|
|
|
41
|
### Single-Column Layout Example |
|
42
|
|
|
43
|
Single-column layouts are useful for listing pages or pages with a single main content area. For example: |
|
44
|
|
|
45
|
export const singleHighlights = [ |
|
46
|
["7", "widgetsZonePrefix", "The prefix of the injection zones to expose."], |
|
47
|
["8", "preferredLayoutId", "Use a two-column layout."], |
|
48
|
["9", "sections", "The page's sections, which are rendered in the layout's main columns."], |
|
49
|
["10", "main", "Components shown in the main column."], |
|
50
|
] |
|
51
|
|
|
52
|
```tsx title="src/admin/routes/custom/[id]/page.tsx" highlights={singleHighlights} |
|
53
|
import { LayoutComposer } from "@medusajs/dashboard/components" |
|
54
|
|
|
55
|
const BrandListPage = () => { |
|
56
|
// ... |
|
57
|
return ( |
|
58
|
<LayoutComposer |
|
59
|
widgetsZonePrefix="brand.list" |
|
60
|
preferredLayoutId="core:single-column" |
|
61
|
sections={{ |
|
62
|
main: ( |
|
63
|
<BrandListSection /> |
|
64
|
), |
|
65
|
}} |
|
66
|
/> |
|
67
|
) |
|
68
|
} |
|
69
|
|
|
70
|
export default BrandListPage |
|
71
|
``` |
|
72
|
|
|
73
|
Based on the `widgetsZonePrefix` prop, the `LayoutComposer` in the example exposes the following injection zone: |
|
74
|
|
|
75
|
- `brand.list`: Widgets rendered in the main section of the brand list page. |
|
76
|
|
|
77
|
Widgets injected into this zone appear inside the main section and can be reordered by admin users. |
|
78
|
|
|
79
|
Refer to the [LayoutComposer](!resources!/admin-components/layouts/layout-composer) reference for more details on the component's props and layouts. |
|
80
|
|
|
81
|
### Two-Column Layout Example |
|
82
|
|
|
83
|
Two-column layouts are useful for details pages or pages with a main content area and a side column. For example: |
|
84
|
|
|
85
|
export const customHighlights = [ |
|
86
|
["7", "widgetsZonePrefix", "The prefix of the injection zones to expose."], |
|
87
|
["8", "preferredLayoutId", "Use a two-column layout."], |
|
88
|
["9", "data", "The data passed to the widgets injected into the zones."], |
|
89
|
["10", "sections", "The page's sections, which are rendered in the layout's main and side columns."], |
|
90
|
["11", "main", "Components shown in the main (left) column."], |
|
91
|
] |
|
92
|
|
|
93
|
```tsx title="src/admin/routes/custom/[id]/page.tsx" highlights={customHighlights} |
|
94
|
import { LayoutComposer } from "@medusajs/dashboard/components" |
|
95
|
|
|
96
|
const BrandDetailsPage = () => { |
|
97
|
// ... |
|
98
|
return ( |
|
99
|
<LayoutComposer |
|
100
|
widgetsZonePrefix="brand.details" |
|
101
|
preferredLayoutId="core:two-column" |
|
102
|
data={brand} |
|
103
|
sections={{ |
|
104
|
main: ( |
|
105
|
<> |
|
106
|
<GeneralSection brand={brand} /> |
|
107
|
{/* ... */} |
|
108
|
</> |
|
109
|
), |
|
110
|
}} |
|
111
|
/> |
|
112
|
) |
|
113
|
} |
|
114
|
|
|
115
|
export default BrandDetailsPage |
|
116
|
``` |
|
117
|
|
|
118
|
Based on the `widgetsZonePrefix` prop, the `LayoutComposer` in the example exposes the following injection zones: |
|
119
|
|
|
120
|
- `brand.details`: Widgets rendered in the main section of the brand details page. |
|
121
|
- `brand.details.side`: Widgets rendered in the side section of the brand details page. |
|
122
|
|
|
123
|
Widgets injected into these zones appear inside the respective sections and can be reordered by admin users. The `data` prop is passed to the widgets, allowing them to access the page's main data. In this example, widgets can access the `brand` object through their `data` prop. |
|
124
|
|
|
125
|
Refer to the [LayoutComposer](!resources!/admin-components/layouts/layout-composer) reference for more details on the component's props and layouts. |
|
126
|
|
|
127
|
### Custom Zone Naming Convention |
|
128
|
|
|
129
|
Custom injection zones should follow the pattern `{resource-name}.{page-context}` for the main section, and `{resource-name}.{page-context}.{section}` for other sections, where: |
|
130
|
|
|
131
|
- `resource-name`: The name of the resource being accessed in the page. For example, `brand` in the example above. |
|
132
|
- `page-context`: The page or section context. For example, `details` for a details page or `list` for a list page. |
|
133
|
- `section`: The name of the layout section the zone belongs to, if it isn't the main one. For example, `side` for the side section of a two-column layout. |
|
134
|
|
|
135
|
<Note> |
|
136
|
|
|
137
|
Don't add a `.before` or `.after` suffix to your zone names. These suffixes are deprecated since [Medusa v2.17.2](https://github.com/medusajs/medusa/releases/tag/v2.17.2), as admin users now change a widget's placement with [Layout Configurations](!user-guide!/tips/layout-configurations). |
|
138
|
|
|
139
|
</Note> |
|
140
|
|
|
141
|
This naming convention helps avoid conflicts between different plugins and makes zones easier to identify. |
|
142
|
|
|
143
|
--- |
|
144
|
|
|
145
|
## Step 2: Register Injection Zones for Type Checking |
|
146
|
|
|
147
|
Once your page exposes injection zones, register them in the `InjectionZoneRegistry` interface. This enables type checking and autocompletion for the zones when other developers create widgets using `defineWidgetConfig`. |
|
148
|
|
|
149
|
To register the injection zones, augment the `InjectionZoneRegistry` interface through declaration merging. Create an `index.d.ts` file in your plugin's root directory with the following content: |
|
150
|
|
|
151
|
```ts title="index.d.ts" |
|
152
|
declare module "@medusajs/admin-shared" { |
|
153
|
interface InjectionZoneRegistry { |
|
154
|
"brand.details": true |
|
155
|
"brand.details.side": true |
|
156
|
"brand.list": true |
|
157
|
} |
|
158
|
} |
|
159
|
``` |
|
160
|
|
|
161
|
Then, include the declaration file in your plugin's `package.json`: |
|
162
|
|
|
163
|
```json title="package.json" |
|
164
|
{ |
|
165
|
"files": [ |
|
166
|
".medusa/server", |
|
167
|
"index.d.ts" |
|
168
|
], |
|
169
|
"exports": { |
|
170
|
".": { |
|
171
|
"types": "./index.d.ts" |
|
172
|
} |
|
173
|
} |
|
174
|
} |
|
175
|
``` |
|
176
|
|
|
177
|
--- |
|
178
|
|
|
179
|
## Use Custom Injection Zones in Widgets |
|
180
|
|
|
181
|
Once registered, users using the plugin can inject widgets into the custom zones using `defineWidgetConfig`. For example: |
|
182
|
|
|
183
|
```tsx title="src/admin/widgets/draft-order-widget.tsx" |
|
184
|
import { defineWidgetConfig } from "@medusajs/admin-sdk" |
|
185
|
|
|
186
|
const BrandListWidget = () => { |
|
187
|
return ( |
|
188
|
<Container className="divide-y p-0"> |
|
189
|
<div className="flex items-center justify-between px-6 py-4"> |
|
190
|
<Heading level="h2"> |
|
191
|
Brand Widget |
|
192
|
</Heading> |
|
193
|
</div> |
|
194
|
</Container> |
|
195
|
) |
|
196
|
} |
|
197
|
|
|
198
|
export const config = defineWidgetConfig({ |
|
199
|
zone: "brand.list", |
|
200
|
}) |
|
201
|
|
|
202
|
export default BrandListWidget |
|
203
|
``` |
|
204
|
|
|
205
|
In this example, the widget is injected into the `brand.list` zone, so it's rendered in the main section of the brand list page you added to your plugin. |
|
206
|
|
|
207
|
If you don't see `brand.list` in the autocompletion of the `zone` property, make sure your `src/admin/tsconfig.json` has the following in its `include` array: |
|
208
|
|
|
209
|
```json title="src/admin/tsconfig.json" |
|
210
|
"include": [ |
|
211
|
".", |
|
212
|
"../../.medusa/types/augmentation-refs.d.ts" |
|
213
|
] |
|
214
|
``` |
|
215
|
|
|
216
|
<Note> |
|
217
|
|
|
218
|
When you build your application, you'll see a warning that this zone is unknown. This is only a precaution since the system doesn't recognize the custom zone. Make sure to use the correct zone name. |
|
219
|
|
|
220
|
</Note> |
|
221
|
|
|
222
|
### Use Data Prop in Widgets |
|
223
|
|
|
224
|
If you pass a `data` prop to the `LayoutComposer`, widgets can access it through their props. |
|
225
|
|
|
226
|
For example, if you pass a `brand` object as data to the `LayoutComposer`, you can access it in your widget like this: |
|
227
|
|
|
228
|
```tsx title="src/admin/widgets/draft-order-widget.tsx" |
|
229
|
import { defineWidgetConfig } from "@medusajs/admin-sdk" |
|
230
|
import { Container, Heading } from "@medusajs/ui" |
|
231
|
import { |
|
232
|
DetailWidgetProps, |
|
233
|
} from "@medusajs/framework/types" |
|
234
|
|
|
235
|
type BrandDetailsData = { |
|
236
|
id: string |
|
237
|
title: string |
|
238
|
// other brand properties... |
|
239
|
} |
|
240
|
|
|
241
|
const BrandWidget = ({ |
|
242
|
data, |
|
243
|
}: DetailWidgetProps<BrandDetailsData>) => { |
|
244
|
return ( |
|
245
|
<Container className="divide-y p-0"> |
|
246
|
<div className="flex items-center justify-between px-6 py-4"> |
|
247
|
<Heading level="h2"> |
|
248
|
Brand Widget {data.title} |
|
249
|
</Heading> |
|
250
|
</div> |
|
251
|
</Container> |
|
252
|
) |
|
253
|
} |
|
254
|
|
|
255
|
export const config = defineWidgetConfig({ |
|
256
|
zone: "brand.details", |
|
257
|
}) |
|
258
|
|
|
259
|
export default BrandWidget |
|
260
|
``` |
|
261
|
|
|
262
|
In this example, the `BrandWidget` is injected into the `brand.details` zone, which is part of the brand details page you added to your plugin. Since you passed the `brand` object as data to the `LayoutComposer`, developers can access it in the widget through the `data` prop. |