RouteDrawer - Admin Components
The RouteDrawer component renders a drawer that slides in from the side of the screen. The Medusa Admin uses it for edit forms, such as the form that edits a customer's details.
Route Modals#
RouteDrawer is tied to a route, not to a trigger button. You render it as the root element of a UI route that is nested under the page it edits. When the user navigates to that route, the drawer opens. When the user closes the drawer, RouteDrawer navigates back to the previous route.
So, to open the drawer, link the user to the drawer's route. For example, link to /brands/:id/edit from the brand's details page.
Example#
Consider a UI route at src/admin/routes/brands/[id]/edit/page.tsx that edits a brand:
1import { Heading } from "@medusajs/ui"2import { RouteDrawer } from "@medusajs/dashboard/components"3import { useParams } from "react-router-dom"4 5const BrandEdit = () => {6 const { id } = useParams()7 8 return (9 <RouteDrawer>10 <RouteDrawer.Header>11 <RouteDrawer.Title asChild>12 <Heading>Edit Brand</Heading>13 </RouteDrawer.Title>14 </RouteDrawer.Header>15 <EditBrandForm id={id!} />16 </RouteDrawer>17 )18}19 20export default BrandEdit
EditBrandForm renders the form's fields, as explained in the useRouteModal section.
Props#
RouteDrawer accepts the following props, in addition to its children:
Sub-components#
Component | Description |
|---|---|
| The drawer's header. Place the title and description in it. |
| The drawer's title. Pass |
| An accessible description of the drawer's purpose. |
| The drawer's scrollable content area. |
| The drawer's footer. Place the cancel and submit buttons in it. |
| Closes the drawer when clicked, which navigates back to the |
| Wraps a React Hook Form form. It provides the form's context to the fields, and shows a confirmation prompt if the user leaves the route while the form has unsaved changes. |
RouteDrawer.Form Props#
useRouteModal#
Use the useRouteModal hook in components rendered inside a RouteDrawer or a RouteFocusModal. It gives you access to the modal's route context.
The hook throws an error if you use it outside of a route modal.
Returned Values#
Example#
The form component uses handleSuccess to close the drawer after the brand is updated:
1import { Button, Input, toast } from "@medusajs/ui"2import {3 RouteDrawer,4 useRouteModal,5} from "@medusajs/dashboard/components"6import { useForm } from "react-hook-form"7 8const EditBrandForm = ({ id }: { id: string }) => {9 const { handleSuccess } = useRouteModal()10 const form = useForm({11 defaultValues: {12 name: "",13 },14 })15 16 const handleSubmit = form.handleSubmit(async (data) => {17 await updateBrand(id, data)18 toast.success("Brand updated")19 handleSuccess()20 })21 22 return (23 <RouteDrawer.Form form={form}>24 <form onSubmit={handleSubmit}>25 <RouteDrawer.Body>26 <Input {...form.register("name")} />27 </RouteDrawer.Body>28 <RouteDrawer.Footer>29 <RouteDrawer.Close asChild>30 <Button variant="secondary" size="small">31 Cancel32 </Button>33 </RouteDrawer.Close>34 <Button type="submit" size="small">35 Save36 </Button>37 </RouteDrawer.Footer>38 </form>39 </RouteDrawer.Form>40 )41}