Files
medusa-store/www/apps/book/app/advanced-development/admin/widgets/page.mdx
Shahed Nasser c1db40b564 docs: added customer storefront guides (#7685)
* added customer guides

* fixes to sidebar

* remove old customer registration guide

* fix build error

* generate files

* run linter
2024-06-13 12:21:54 +03:00

139 lines
3.7 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { Table } from "docs-ui"
export const metadata = {
title: `${pageNumber} Admin Widgets`,
}
# {metadata.title}
In this chapter, youll learn more about widgets and how to use them.
## What is an Admin Widget?
Admin widgets are React components you inject into predetermined injection zones in the Medusa Admin dashboard.
For example, you can add a widget on the order details page that shows payment details retrieved from Stripe.
---
## How to Create a Widget?
A widget is created in a file under the `src/admin/widgets` directory. The files default export must be the widget, which is the React component. The file must also export the widgets configurations.
For example, create the file `src/admin/widgets/product-widget.tsx` with the following content:
export const widgetHighlights = [
["4", "ProductWidget", "The React component of the product widget."],
["14", "zone", "The zone to inject the widget to."]
]
```tsx title="src/admin/widgets/product-widget.tsx" highlights={widgetHighlights}
import { defineWidgetConfig } from "@medusajs/admin-shared"
// The widget
const ProductWidget = () => {
return (
<div>
<h2>Product Widget</h2>
</div>
)
}
// The widget's configurations
export const config = defineWidgetConfig({
zone: "product.details.after",
})
export default ProductWidget
```
The widget only shows the heading `Product Widget`.
Use the `defineWidgetConfig` function imported from `@medusajs/admin-shared` to create and export the widget's configurations.
The function accepts as a parameter an object with the following property:
- `zone`: A string or an array of strings, each being the name of the zone to inject the widget into.
In the example above, the widget is injected after a products details.
### Test the Widget
To test out the widget, start the Medusa application:
```bash npm2yarn
npm run dev
```
Then, open a products details page. Youll find your custom widget at the bottom of the page.
---
## Detail Widget Props
Widgets that are injected into a details page (for example, `product.details.after`) receive a `data` prop, which is the main data of the details page (for example, the product object).
For example:
```tsx title="src/admin/widgets/product-widget.tsx" highlights={[["5"]]}
import { defineWidgetConfig } from "@medusajs/admin-shared"
import { DetailWidgetProps, AdminProduct } from "@medusajs/types"
const ProductWidget = ({
data,
}: DetailWidgetProps<AdminProduct>) => {
return (
<div>
<h2>Product Widget {data.title}</h2>
</div>
)
}
export const config = defineWidgetConfig({
zone: "product.details.after",
})
export default ProductWidget
```
Notice that the type of the props is `DetailWidgetProps`, which accepts as a type argument the expected type of the data.
---
## Using UI Components
Its highly recommended that you use the [Medusa UI package](https://docs.medusajs.com/ui) to match your widgets design with the rest of the Medusa Admin.
For example, you can rewrite the above component to the following:
```tsx title="src/admin/widgets/product-widget.tsx"
import { defineWidgetConfig } from "@medusajs/admin-shared"
import { Container, Heading } from "@medusajs/ui"
const ProductWidget = () => {
return (
<Container>
<Heading level="h2">Product Widget</Heading>
</Container>
)
}
export const config: WidgetConfig = defineWidgetConfig({
zone: "product.details.after",
})
export default ProductWidget
```
<Note title="Tip">
Admin Widgets also support [Tailwind CSS](https://tailwindcss.com/) out of the box.
</Note>
---
## Injection Zone
Refer to [this reference](!resources!/admin-widget-injection-zones) for the full list of injection zones and their props.