1 ---
2 sidebar_label: "Route Focus Modal"
3 ---
4
5 import { Table, TypeList, Kbd } from "docs-ui"
6
7 export const metadata = {
8 title: `RouteFocusModal - Admin Components`,
9 }
10
11 # {metadata.title}
12
13 <Note>
14
15 This component is available since Medusa [v2.21.0](https://github.com/medusajs/medusa/releases/tag/v2.21.0).
16
17 </Note>
18
19 The `RouteFocusModal` component renders a full-screen modal that covers the page behind it. The Medusa Admin uses it for forms that need a lot of space, such as the form that creates a region.
20
21 ```tsx
22 import { RouteFocusModal } from "@medusajs/dashboard/components"
23 ```
24
25 ---
26
27 ## Route Modals
28
29 `RouteFocusModal` is tied to a route, not to a trigger button. You render it as the root element of a [UI route](!docs!/learn/fundamentals/admin/ui-routes) that is nested under the page it belongs to. When the user navigates to that route, the modal opens. When the user closes the modal, `RouteFocusModal` navigates back to the previous route.
30
31 So, to open the modal, link the user to the modal's route. For example, link to `/brands/create` from the brand list page.
32
33 Use `RouteFocusModal` for create forms and multi-step flows, and use the [RouteDrawer](../route-drawer/page.mdx) for smaller edit forms.
34
35 ---
36
37 ## Example
38
39 Consider a UI route at `src/admin/routes/brands/create/page.tsx` that creates a brand:
40
41 ```tsx title="src/admin/routes/brands/create/page.tsx"
42 import { Button, Heading, Input } from "@medusajs/ui"
43 import {
44 RouteFocusModal,
45 useRouteModal,
46 } from "@medusajs/dashboard/components"
47 import { useForm } from "react-hook-form"
48
49 const BrandCreate = () => {
50 return (
51 <RouteFocusModal>
52 <CreateBrandForm />
53 </RouteFocusModal>
54 )
55 }
56
57 const CreateBrandForm = () => {
58 const { handleSuccess } = useRouteModal()
59 const form = useForm({
60 defaultValues: {
61 name: "",
62 },
63 })
64
65 const handleSubmit = form.handleSubmit(async (data) => {
66 const brand = await createBrand(data)
67 handleSuccess(`../${brand.id}`)
68 })
69
70 return (
71 <RouteFocusModal.Form form={form}>
72 <form onSubmit={handleSubmit}>
73 <RouteFocusModal.Header />
74 <RouteFocusModal.Body>
75 <Heading>Create Brand</Heading>
76 <Input {...form.register("name")} />
77 </RouteFocusModal.Body>
78 <RouteFocusModal.Footer>
79 <RouteFocusModal.Close asChild>
80 <Button variant="secondary" size="small">
81 Cancel
82 </Button>
83 </RouteFocusModal.Close>
84 <Button type="submit" size="small">
85 Create
86 </Button>
87 </RouteFocusModal.Footer>
88 </form>
89 </RouteFocusModal.Form>
90 )
91 }
92
93 export default BrandCreate
94 ```
95
96 Call the `useRouteModal` hook in a child of the `RouteFocusModal`, since the modal provides its context. The hook returns `handleSuccess`, which closes the modal after a successful submission. Refer to the [Route Drawer](../route-drawer/page.mdx#useroutemodal) documentation for the hook's returned values, including `setCloseOnEscape` that controls whether the <Kbd>Escape</Kbd> key closes the modal.
97
98 ---
99
100 ## Props
101
102 `RouteFocusModal` accepts the following props, in addition to its children:
103
104 <TypeList
105 types={[
106 {
107 name: "prev",
108 type: "`string` \\| `object` \\| `number`",
109 optional: true,
110 defaultValue: "..",
111 description:
112 "The route to navigate to when the modal closes. Pass a string, such as `\"../..\"`, or an object with the path's parts. You can also pass a number, such as `-1`, to go back that many entries in the browser's history instead of navigating to a route.",
113 children: [
114 {
115 name: "pathname",
116 type: "`string`",
117 optional: true,
118 description: "The path to navigate to, such as `/brands`.",
119 },
120 {
121 name: "search",
122 type: "`string`",
123 optional: true,
124 description:
125 "The query string to append to the path, such as `?order=created_at`.",
126 },
127 {
128 name: "hash",
129 type: "`string`",
130 optional: true,
131 description: "The hash to append to the path, such as `#details`.",
132 },
133 ],
134 },
135 ]}
136 />
137
138 ---
139
140 ## Sub-components
141
142 <Table>
143 <Table.Header>
144 <Table.Row>
145 <Table.HeaderCell>
146 Component
147 </Table.HeaderCell>
148 <Table.HeaderCell>
149 Description
150 </Table.HeaderCell>
151 </Table.Row>
152 </Table.Header>
153 <Table.Body>
154 <Table.Row>
155 <Table.Cell>
156
157 `RouteFocusModal.Header`
158
159 </Table.Cell>
160 <Table.Cell>
161
162 The modal's header. It renders the close button, so render it even when it has no children.
163
164 </Table.Cell>
165 </Table.Row>
166 <Table.Row>
167 <Table.Cell>
168
169 `RouteFocusModal.Title`
170
171 </Table.Cell>
172 <Table.Cell>
173
174 The modal's title. Pass `asChild` to render your own heading element.
175
176 </Table.Cell>
177 </Table.Row>
178 <Table.Row>
179 <Table.Cell>
180
181 `RouteFocusModal.Description`
182
183 </Table.Cell>
184 <Table.Cell>
185
186 An accessible description of the modal's purpose.
187
188 </Table.Cell>
189 </Table.Row>
190 <Table.Row>
191 <Table.Cell>
192
193 `RouteFocusModal.Body`
194
195 </Table.Cell>
196 <Table.Cell>
197
198 The modal's scrollable content area.
199
200 </Table.Cell>
201 </Table.Row>
202 <Table.Row>
203 <Table.Cell>
204
205 `RouteFocusModal.Footer`
206
207 </Table.Cell>
208 <Table.Cell>
209
210 The modal's footer. Place the cancel and submit buttons in it.
211
212 </Table.Cell>
213 </Table.Row>
214 <Table.Row>
215 <Table.Cell>
216
217 `RouteFocusModal.Close`
218
219 </Table.Cell>
220 <Table.Cell>
221
222 Closes the modal when clicked, which navigates back to the `prev` route. Pass `asChild` to use your own button.
223
224 </Table.Cell>
225 </Table.Row>
226 <Table.Row>
227 <Table.Cell>
228
229 `RouteFocusModal.Form`
230
231 </Table.Cell>
232 <Table.Cell>
233
234 Wraps a [React Hook Form](https://react-hook-form.com/) 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.
235
236 </Table.Cell>
237 </Table.Row>
238 </Table.Body>
239 </Table>
240
241 ### RouteFocusModal.Form Props
242
243 <TypeList
244 types={[
245 {
246 name: "form",
247 type: "`object`",
248 optional: false,
249 description: `The form instance returned by the \`useForm\` hook of [React Hook Form](https://react-hook-form.com/). Refer to [React Hook Form's documentation](https://react-hook-form.com/docs/useform) for all of its properties.`,
250 children: [
251 {
252 name: "control",
253 type: "`object`",
254 optional: false,
255 description:
256 "The object that registers the form's fields and manages their state. Pass it to the fields you render in the form.",
257 },
258 {
259 name: "formState",
260 type: "`object`",
261 optional: false,
262 description:
263 "The form's state, including whether it has unsaved changes. The modal uses it to decide whether to show the unsaved-changes prompt.",
264 children: [
265 {
266 name: "isDirty",
267 type: "`boolean`",
268 optional: false,
269 description:
270 "Whether the user changed any of the form's fields.",
271 },
272 ],
273 },
274 {
275 name: "handleSubmit",
276 type: "`(onValid: (data: object) => unknown, onInvalid?: (errors: object) => unknown) => (event?: React.BaseSyntheticEvent) => Promise<void>`",
277 optional: false,
278 description:
279 "Creates the submit handler that you pass to the `onSubmit` prop of the `form` element. It validates the fields before running your callback.",
280 },
281 {
282 name: "register",
283 type: "`(name: string, options?: object) => object`",
284 optional: false,
285 description:
286 "Registers an input in the form. Spread its returned value on the input you render.",
287 },
288 {
289 name: "watch",
290 type: "`(name?: string) => unknown`",
291 optional: false,
292 description:
293 "Returns the current value of a field, or of the entire form if you don't pass a name.",
294 },
295 {
296 name: "setValue",
297 type: "`(name: string, value: unknown, options?: object) => void`",
298 optional: false,
299 description: "Sets a field's value programmatically.",
300 },
301 {
302 name: "reset",
303 type: "`(values?: object, options?: object) => void`",
304 optional: false,
305 description:
306 "Resets the form's fields to the specified values, or to their default values.",
307 },
308 ],
309 },
310 {
311 name: "blockSearchParams",
312 type: "`boolean`",
313 optional: true,
314 defaultValue: "false",
315 description:
316 "Whether to also show the unsaved-changes prompt when only the search parameters of the route change.",
317 },
318 {
319 name: "onClose",
320 type: "`(isSubmitSuccessful: boolean) => void`",
321 optional: true,
322 description:
323 "A function to run when the user leaves the form's route. It receives whether the form was submitted successfully.",
324 },
325 ]}
326 />
327
328 ---
329
330 ## Stacking Modals
331
332 To open another modal above a `RouteFocusModal`, such as a table for the user to select items from, use the [StackedFocusModal](../stacked-focus-modal/page.mdx) or the [StackedDrawer](../stacked-drawer/page.mdx) components. Both are controlled with the `useStackedModal` hook.