Raw
1 import { Prerequisites } from "docs-ui"
2
3 export const metadata = {
4 title: `${pageNumber} Admin Widgets`,
5 }
6
7 # {metadata.title}
8
9 In this chapter, you’ll learn about widgets and how to use them.
10
11 ## What is an Admin Widget?
12
13 The Medusa Admin's pages are customizable for inserting widgets of custom content in pre-defined injection zones. For example, you can add a widget on the product details page that allows admin users to sync products to a third-party service.
14
15 You create these widgets as React components that render the content and functionality of the widget.
16
17 You can create an admin widget directly in your Medusa application, or in a [plugin](../../plugins/page.mdx) if you want to share the widget across multiple Medusa applications.
18
19 ---
20
21 ## How to Create a Widget?
22
23 <Prerequisites
24 items={[{
25 link: "/learn/installation",
26 text: "Medusa application installed"
27 }]}
28 />
29
30 You create a widget in a `.tsx` file under the `src/admin/widgets` directory. The file must export:
31
32 1. A React component that renders the widget. This will be the file's default export.
33 2. The widget’s configurations indicating where to insert the widget.
34
35 For example, create the file `src/admin/widgets/product-widget.tsx` with the following content:
36
37 ![Example of widget file in the application's directory structure](https://res.cloudinary.com/dza7lstvk/image/upload/v1732867137/Medusa%20Book/widget-dir-overview_dqsbct.jpg)
38
39 export const widgetHighlights = [
40 ["5", "ProductWidget", "The React component of the product widget."],
41 ["17", "zone", "The zone to inject the widget into."]
42 ]
43
44 ```tsx title="src/admin/widgets/product-widget.tsx" highlights={widgetHighlights}
45 import { defineWidgetConfig } from "@medusajs/admin-sdk"
46 import { Container, Heading } from "@medusajs/ui"
47
48 // The widget
49 const ProductWidget = () => {
50 return (
51 <Container className="divide-y p-0">
52 <div className="flex items-center justify-between px-6 py-4">
53 <Heading level="h2">Product Widget</Heading>
54 </div>
55 </Container>
56 )
57 }
58
59 // The widget's configurations
60 export const config = defineWidgetConfig({
61 zone: "product.details",
62 })
63
64 export default ProductWidget
65 ```
66
67 In the example above, the widget is injected at the end of a product’s details page.
68
69 You export the `ProductWidget` component, which displays the heading `Product Widget`. In the widget, you use [Medusa UI](!ui!) to customize the dashboard with the same components used to build it.
70
71 To export the widget's configuration, you use `defineWidgetConfig` from the Admin Extension SDK. It accepts an object as a parameter with the following properties:
72
73 - `zone`: A string or array of strings indicating the injection zone(s) to inject the widget into.
74 - `id` (optional): A stable string identifier for the widget. When omitted, Medusa derives an ID from the widget file's path at build time. You can set this explicitly to keep layout customizations working if you rename or move the file. Plugin authors should prefix the ID to avoid collisions. For example, `"my-plugin:brand-widget"`.
75
76 <Note title="Important" type="warning">
77
78 The widget component must be created as an arrow function.
79
80 </Note>
81
82 ### Test the Widget
83
84 To test out the widget, start the Medusa application:
85
86 ```bash npm2yarn
87 npm run dev
88 ```
89
90 Then, open a product’s details page. You’ll find your custom widget at the end of the page.
91
92 ---
93
94 ## Widget Placement
95
96 Prior to Medusa v2.17.2, widget zones ended with `.before` or `.after` to indicate whether the widget should be placed at the beginning or end of the zone. For example, `product.details.before` would place the widget at the top of the product details page, while `product.details.after` would place it at the bottom.
97
98 As of Medusa v2.17.2, the `.before` and `.after` suffixes have been deprecated in favor of [Layout Configurations](!user-guide!/tips/layout-configurations). The login page is the only exception. It isn't part of the layout configurations, so its `login.before` and `login.after` zones still place the widget before or after the login form.
99
100 Widgets are now placed at the end of the zone they're injected into. For example, if you inject a widget into the `product.details` zone, it will be placed at the end of the product details page. Then, users can customize the placement of the widget in the Medusa Admin dashboard using [Layout Configurations](!user-guide!/tips/layout-configurations). The widget can be moved to the top of the page or in between core components of the page.
101
102 If you're still using the `.before` or `.after` suffixes, you can continue to do so, but it's recommended to migrate to the new system. `.before` and `.after` (aside from the login page) have no effect on the placement of widgets in Medusa v2.17.2 and later, and may be removed in future versions.
103
104 ---
105
106 ## Props Passed to Widgets on Detail Pages
107
108 Widgets that are injected into a detail page receive a `data` prop, which is the main data of the details page.
109
110 For example, a widget injected into the `product.details` zone receives the product's details in the `data` prop:
111
112 export const detailHighlights = [
113 ["10", "data", "Receive the data as a prop."],
114 ["11", "AdminProduct", "Pass the expected type of `data` as a type argument."],
115 ["16", "data.title", "Show the product's title."]
116 ]
117
118 ```tsx title="src/admin/widgets/product-widget.tsx" highlights={detailHighlights}
119 import { defineWidgetConfig } from "@medusajs/admin-sdk"
120 import { Container, Heading } from "@medusajs/ui"
121 import {
122 DetailWidgetProps,
123 AdminProduct,
124 } from "@medusajs/framework/types"
125
126 // The widget
127 const ProductWidget = ({
128 data,
129 }: DetailWidgetProps<AdminProduct>) => {
130 return (
131 <Container className="divide-y p-0">
132 <div className="flex items-center justify-between px-6 py-4">
133 <Heading level="h2">
134 Product Widget {data.title}
135 </Heading>
136 </div>
137 </Container>
138 )
139 }
140
141 // The widget's configurations
142 export const config = defineWidgetConfig({
143 zone: "product.details",
144 })
145
146 export default ProductWidget
147 ```
148
149 The props type is `DetailWidgetProps`, and it accepts as a type argument the expected type of `data`. For the product details page, it's `AdminProduct`.
150
151 ---
152
153 ## Injection Zones List
154
155 Refer to the [Admin Widget Injection Zones](!resources!/admin-widget-injection-zones) reference for the full list of injection zones and their props.
156
157 ---
158
159 ## Custom Injection Zones
160
161 Plugins can register custom injection zones to extend the widget system beyond the core zones. This is useful when creating custom admin pages in a plugin and you want to allow other developers to inject widgets into your custom pages.
162
163 Refer to the [Custom Injection Zones](../custom-injection-zones/page.mdx) chapter to learn how to add custom injection zones in a plugin.
164
165 ---
166
167 ## Admin Components List
168
169 While the Medusa Admin uses the [Medusa UI](!ui!) components, it also expands on them for styling and design purposes.
170
171 To build admin customizations that match the Medusa Admin's designs and layouts, refer to the [Admin Components](!resources!/admin-components) guide. You'll find components like `Header`, `JSON View`, and more that match the Medusa Admin's design.
172
173 ---
174
175 ## Show Widgets Conditionally
176
177 In some cases, you may want to show a widget only if certain conditions are met. For example, you may want to show a widget only if the product has a brand.
178
179 To prevent the widget from showing, return an empty fragment from the widget component:
180
181 ```tsx title="src/admin/widgets/product-widget.tsx"
182 import { defineWidgetConfig } from "@medusajs/admin-sdk"
183 import { Container, Heading } from "@medusajs/ui"
184 import {
185 DetailWidgetProps,
186 AdminProduct,
187 } from "@medusajs/framework/types"
188
189 // The widget
190 const ProductWidget = ({
191 data,
192 }: DetailWidgetProps<AdminProduct>) => {
193 if (!data.metadata?.brand) {
194 return <></> // Don't show the widget if the product has no brand
195 }
196
197 return (
198 <Container className="divide-y p-0">
199 <div className="flex items-center justify-between px-6 py-4">
200 <Heading level="h2">
201 Brand: {data.metadata.brand}
202 </Heading>
203 </div>
204 </Container>
205 )
206 }
207
208 // The widget's configurations
209 export const config = defineWidgetConfig({
210 zone: "product.details",
211 })
212
213 export default ProductWidget
214 ```
215
216 In the above example, you return an empty fragment if the product has no brand. Otherwise, you show the brand name in the widget.