|
1
|
--- |
|
2
|
sidebar_label: "LayoutComposer" |
|
3
|
--- |
|
4
|
|
|
5
|
import { TypeList } from "docs-ui" |
|
6
|
|
|
7
|
export const metadata = { |
|
8
|
title: `LayoutComposer - Admin Components`, |
|
9
|
} |
|
10
|
|
|
11
|
# {metadata.title} |
|
12
|
|
|
13
|
The `LayoutComposer` component provides a flexible layout composition system for building admin pages with widget support. It manages the rendering of widgets in specific sections and supports different layout structures. |
|
14
|
|
|
15
|
The `LayoutComposer` is useful for plugins that want to create custom admin pages with a consistent layout and custom widget injection zones. |
|
16
|
|
|
17
|
<Note> |
|
18
|
|
|
19
|
This component is available since Medusa [v2.16.0](https://github.com/medusajs/medusa/releases/tag/v2.16.0). |
|
20
|
|
|
21
|
</Note> |
|
22
|
|
|
23
|
## Basic Example |
|
24
|
|
|
25
|
In the following example, the `LayoutComposer` is used to create a two-column layout for a custom brand details page: |
|
26
|
|
|
27
|
```tsx |
|
28
|
import { LayoutComposer } from "@medusajs/dashboard/components" |
|
29
|
|
|
30
|
const BrandDetailsPage = () => { |
|
31
|
// retrieve brand... |
|
32
|
return ( |
|
33
|
<LayoutComposer |
|
34
|
widgetsZonePrefix="brand.details" |
|
35
|
preferredLayoutId="core:two-column" |
|
36
|
data={brand} |
|
37
|
sections={{ |
|
38
|
main: ( |
|
39
|
<> |
|
40
|
<BrandGeneralSection brand={brand} /> |
|
41
|
<BrandVariantsSection brand={brand} /> |
|
42
|
</> |
|
43
|
), |
|
44
|
side: ( |
|
45
|
<> |
|
46
|
<BrandMediaSection brand={brand} /> |
|
47
|
<BrandStatusSection brand={brand} /> |
|
48
|
</> |
|
49
|
), |
|
50
|
}} |
|
51
|
/> |
|
52
|
) |
|
53
|
} |
|
54
|
``` |
|
55
|
|
|
56
|
The `widgetsZonePrefix` prop determines the widget injection zones for the page. In this example, the UI route exposes the following zones: |
|
57
|
|
|
58
|
- `brand.details`: Widgets rendered in the main section of the brand details page. |
|
59
|
- `brand.details.side`: Widgets rendered in the side section of the brand details page. |
|
60
|
|
|
61
|
### Single Column Layout |
|
62
|
|
|
63
|
The `LayoutComposer` also supports single-column layouts, which is useful for listing pages or pages with a single main content area. For example: |
|
64
|
|
|
65
|
```tsx |
|
66
|
import { LayoutComposer } from "@medusajs/dashboard/components" |
|
67
|
|
|
68
|
const BrandListPage = () => { |
|
69
|
return ( |
|
70
|
<LayoutComposer |
|
71
|
widgetsZonePrefix="brand.list" |
|
72
|
preferredLayoutId="core:single-column" |
|
73
|
sections={{ |
|
74
|
main: ( |
|
75
|
<BrandListSection /> |
|
76
|
), |
|
77
|
}} |
|
78
|
/> |
|
79
|
) |
|
80
|
} |
|
81
|
``` |
|
82
|
|
|
83
|
The `widgetsZonePrefix` prop determines the widget injection zones for the page. In this example, the UI route exposes the following zone: |
|
84
|
|
|
85
|
- `brand.list`: Widgets rendered in the main section of the brand list page. |
|
86
|
|
|
87
|
--- |
|
88
|
|
|
89
|
## Props |
|
90
|
|
|
91
|
<TypeList |
|
92
|
types={[ |
|
93
|
{ |
|
94
|
name: "widgetsZonePrefix", |
|
95
|
type: "`string`", |
|
96
|
optional: false, |
|
97
|
description: "The prefix used to determine widget injection zones for the page. For a two-column layout, the component exposes `{prefix}` (main section) and `{prefix}.side` (side section). For a single-column layout, it exposes `{prefix}` only.", |
|
98
|
}, |
|
99
|
{ |
|
100
|
name: "preferredLayoutId", |
|
101
|
type: "`string`", |
|
102
|
optional: false, |
|
103
|
description: "The ID of the preferred layout to use. By default, accepted values are `\"core:single-column\"`, `\"core:single-row\"`, `\"core:two-column\"`, and `\"core:settings-sidebar\"`.", |
|
104
|
}, |
|
105
|
{ |
|
106
|
name: "sections", |
|
107
|
type: "`object`", |
|
108
|
optional: false, |
|
109
|
description: "The content of each section of the layout. The key is the section's name, and the value is a `ReactNode`. The available sections depend on the value of `preferredLayoutId`.", |
|
110
|
children: [ |
|
111
|
{ |
|
112
|
name: "main", |
|
113
|
type: "`ReactNode`", |
|
114
|
optional: false, |
|
115
|
description: "The main section. Available in the `core:single-column`, `core:single-row`, and `core:two-column` layouts.", |
|
116
|
}, |
|
117
|
{ |
|
118
|
name: "side", |
|
119
|
type: "`ReactNode`", |
|
120
|
optional: false, |
|
121
|
description: "The side section. Available in the `core:two-column` layout only.", |
|
122
|
}, |
|
123
|
{ |
|
124
|
name: "general", |
|
125
|
type: "`ReactNode`", |
|
126
|
optional: false, |
|
127
|
description: "The general section. Available in the `core:settings-sidebar` layout only.", |
|
128
|
}, |
|
129
|
{ |
|
130
|
name: "developer", |
|
131
|
type: "`ReactNode`", |
|
132
|
optional: false, |
|
133
|
description: "The developer section. Available in the `core:settings-sidebar` layout only.", |
|
134
|
}, |
|
135
|
{ |
|
136
|
name: "myAccount", |
|
137
|
type: "`ReactNode`", |
|
138
|
optional: false, |
|
139
|
description: "The account section. Available in the `core:settings-sidebar` layout only.", |
|
140
|
}, |
|
141
|
{ |
|
142
|
name: "extensions", |
|
143
|
type: "`ReactNode`", |
|
144
|
optional: false, |
|
145
|
description: "The extensions section. Available in the `core:settings-sidebar` layout only.", |
|
146
|
}, |
|
147
|
], |
|
148
|
}, |
|
149
|
{ |
|
150
|
name: "data", |
|
151
|
type: "`unknown`", |
|
152
|
optional: true, |
|
153
|
description: "Data passed from the UI route to the `LayoutComposer`. The component passes this data to the widgets injected into the zones, allowing them to access the page's main data.", |
|
154
|
}, |
|
155
|
{ |
|
156
|
name: "hasOutlet", |
|
157
|
type: "`boolean`", |
|
158
|
optional: true, |
|
159
|
defaultValue: "true", |
|
160
|
description: "Whether to render an outlet after the layout, which nested routes use to show modals such as drawers and dialogs.", |
|
161
|
}, |
|
162
|
{ |
|
163
|
name: "disableWidgets", |
|
164
|
type: "`boolean`", |
|
165
|
optional: true, |
|
166
|
defaultValue: "false", |
|
167
|
description: "Whether to disable the widget injection zones of this layout.", |
|
168
|
}, |
|
169
|
]} |
|
170
|
/> |
|
171
|
|
|
172
|
--- |
|
173
|
|
|
174
|
## LayoutComposer.Entry |
|
175
|
|
|
176
|
Merchants can reorder the sections of a page, and Medusa saves their preference using each entry's component name. Wrap a section in `LayoutComposer.Entry` with a stable `id` so that the preference survives renaming or minifying the component. |
|
177
|
|
|
178
|
```tsx |
|
179
|
import { LayoutComposer } from "@medusajs/dashboard/components" |
|
180
|
|
|
181
|
const BrandDetailsPage = () => { |
|
182
|
return ( |
|
183
|
<LayoutComposer |
|
184
|
widgetsZonePrefix="brand.details" |
|
185
|
preferredLayoutId="core:two-column" |
|
186
|
sections={{ |
|
187
|
main: ( |
|
188
|
<> |
|
189
|
<LayoutComposer.Entry id="brand-general"> |
|
190
|
<BrandGeneralSection brand={brand} /> |
|
191
|
</LayoutComposer.Entry> |
|
192
|
<LayoutComposer.Entry id="brand-variants"> |
|
193
|
<BrandVariantsSection brand={brand} /> |
|
194
|
</LayoutComposer.Entry> |
|
195
|
</> |
|
196
|
), |
|
197
|
}} |
|
198
|
/> |
|
199
|
) |
|
200
|
} |
|
201
|
``` |
|
202
|
|
|
203
|
## Custom Layouts |
|
204
|
|
|
205
|
You can create custom layouts by adding a layout file under `src/admin/layouts/` in your plugin. A layout file must have a default export (the React component) and a named `config` export created with `defineLayoutConfig`. |
|
206
|
|
|
207
|
For example, create the file `src/admin/layouts/three-column.tsx` with the following content: |
|
208
|
|
|
209
|
```tsx title="src/admin/layouts/three-column.tsx" |
|
210
|
import { defineLayoutConfig } from "@medusajs/admin-sdk" |
|
211
|
import type { |
|
212
|
LayoutComponentProps, |
|
213
|
} from "@medusajs/dashboard/components" |
|
214
|
|
|
215
|
const ThreeColumnLayout = ({ |
|
216
|
sections, |
|
217
|
}: LayoutComponentProps) => ( |
|
218
|
<div className="grid grid-cols-3 gap-4"> |
|
219
|
<div>{sections.main}</div> |
|
220
|
<div>{sections.side}</div> |
|
221
|
<div>{sections.extra}</div> |
|
222
|
</div> |
|
223
|
) |
|
224
|
|
|
225
|
export const config = defineLayoutConfig({ |
|
226
|
id: "my-plugin:three-column", |
|
227
|
sections: [ |
|
228
|
{ id: "main", ordering: "list" }, |
|
229
|
{ id: "side", ordering: "list" }, |
|
230
|
{ id: "extra", ordering: "list" }, |
|
231
|
], |
|
232
|
}) |
|
233
|
|
|
234
|
export default ThreeColumnLayout |
|
235
|
``` |
|
236
|
|
|
237
|
To get type-safe section names when using your custom layout, augment the `LayoutSectionRegistry` interface in `@medusajs/admin-shared`: |
|
238
|
|
|
239
|
```ts title="index.d.ts" |
|
240
|
declare module "@medusajs/admin-shared" { |
|
241
|
interface LayoutSectionRegistry { |
|
242
|
"my-plugin:three-column": "main" | "side" | "extra" |
|
243
|
} |
|
244
|
} |
|
245
|
``` |
|
246
|
|
|
247
|
You can then use the custom layout in the `LayoutComposer`: |
|
248
|
|
|
249
|
```tsx |
|
250
|
import { LayoutComposer } from "@medusajs/dashboard/components" |
|
251
|
|
|
252
|
const BrandPage = () => { |
|
253
|
return ( |
|
254
|
<LayoutComposer |
|
255
|
widgetsZonePrefix="brand.list" |
|
256
|
preferredLayoutId="my-plugin:three-column" |
|
257
|
sections={{ |
|
258
|
main: <BrandListSection />, |
|
259
|
side: <BrandFiltersSection />, |
|
260
|
extra: <BrandStatsSection />, |
|
261
|
}} |
|
262
|
/> |
|
263
|
) |
|
264
|
} |
|
265
|
``` |