1 import { Prerequisites, CodeTab, CodeTabs } from "docs-ui"
2
3 export const metadata = {
4 title: `${pageNumber} Admin UI Routes`,
5 }
6
7 # {metadata.title}
8
9 In this chapter, you’ll learn how to create a UI route in the admin dashboard.
10
11 ## What is a UI Route?
12
13 The Medusa Admin dashboard is customizable, allowing you to add new pages, called UI routes. You create a UI route as a React component showing custom content that allows admin users to perform custom actions.
14
15 For example, you can add a new page to show and manage product reviews, which aren't available natively in Medusa.
16
17 You can create a UI route directly in your Medusa application, or in a [plugin](../../plugins/page.mdx) if you want to share the UI route across multiple Medusa applications.
18
19 ---
20
21 ## How to Create a UI Route?
22
23 <Prerequisites
24 items={[{
25 link: "/learn/installation",
26 text: "Medusa application installed"
27 }]}
28 />
29
30 You create a UI route in a `page.tsx` file under a sub-directory of `src/admin/routes` directory. The file's path relative to `src/admin/routes` determines its path in the dashboard. The file’s default export must be the UI route’s React component.
31
32 For example, create the file `src/admin/routes/custom/page.tsx` with the following content:
33
34 ![Example of UI route file in the application's directory structure](https://res.cloudinary.com/dza7lstvk/image/upload/v1732867243/Medusa%20Book/ui-route-dir-overview_tgju25.jpg)
35
36 ```tsx title="src/admin/routes/custom/page.tsx"
37 import { Container, Heading } from "@medusajs/ui"
38
39 const CustomPage = () => {
40 return (
41 <Container className="divide-y p-0">
42 <div className="flex items-center justify-between px-6 py-4">
43 <Heading level="h2">This is my custom route</Heading>
44 </div>
45 </Container>
46 )
47 }
48
49 export default CustomPage
50 ```
51
52 You add a new route at `http://localhost:9000/app/custom`. The `CustomPage` component holds the page's content, which currently only shows a heading.
53
54 In the route, you use [Medusa UI](!ui!), a package that Medusa maintains to allow you to customize the dashboard with the same components used to build it.
55
56 <Note title="Important" type="warning">
57
58 The UI route component must be created as an arrow function.
59
60 </Note>
61
62 ### Test the UI Route
63
64 To test the UI route, start the Medusa application:
65
66 ```bash npm2yarn
67 npm run dev
68 ```
69
70 Then, after logging into the admin dashboard, open the page `http://localhost:9000/app/custom` to see your custom page.
71
72 ---
73
74 ## Show UI Route in the Sidebar
75
76 To add a sidebar item for your custom UI route, export a configuration object in the UI route's file:
77
78 export const highlights = [
79 ["16", "label", "The label of the UI route's sidebar item."],
80 ["17", "icon", "The icon of the UI route's sidebar item."]
81 ]
82
83 ```tsx title="src/admin/routes/custom/page.tsx" highlights={highlights}
84 import { defineRouteConfig } from "@medusajs/admin-sdk"
85 import { ChatBubbleLeftRight } from "@medusajs/icons"
86 import { Container, Heading } from "@medusajs/ui"
87
88 const CustomPage = () => {
89 return (
90 <Container className="divide-y p-0">
91 <div className="flex items-center justify-between px-6 py-4">
92 <Heading level="h2">This is my custom route</Heading>
93 </div>
94 </Container>
95 )
96 }
97
98 export const config = defineRouteConfig({
99 label: "Custom Route",
100 icon: ChatBubbleLeftRight,
101 })
102
103 export default CustomPage
104 ```
105
106 The configuration object is created using `defineRouteConfig` from the Medusa Framework. It accepts the following properties:
107
108 - `label`: the sidebar item's label.
109 - `icon`: an optional React component used as an icon in the sidebar.
110 - `rank`: an optional number to order the route among sibling routes. Learn more in the [Specify UI Route Sidebar Rank](#specify-ui-route-sidebar-rank) section.
111
112 The above example adds a new sidebar item with the label `Custom Route` and an icon from the [Medusa UI Icons package](!ui!/icons/overview).
113
114 ### Specify UI Route Sidebar Rank
115
116 <Note>
117
118 UI route ranking is available starting [Medusa v2.11.4](https://github.com/medusajs/medusa/releases/tag/v2.11.4).
119
120 </Note>
121
122 By default, custom UI routes are added to the sidebar in the order their files are loaded. This applies to your custom UI routes, and UI routes defined in plugins.
123
124 You can specify the ranking of your UI route in the sidebar using the `rank` property passed to `defineRouteConfig`.
125
126 For example, consider you have the following UI routes:
127
128 <CodeTabs group="ui-routes">
129 <CodeTab label="UI Route 1" value="ui-route-1">
130
131 ```tsx title="src/admin/routes/analytics/page.tsx" highlights={[["18"]]}
132 import { defineRouteConfig } from "@medusajs/admin-sdk"
133 import { ChartBar } from "@medusajs/icons"
134 import { Container, Heading } from "@medusajs/ui"
135
136 const AnalyticsPage = () => {
137 return (
138 <Container className="divide-y p-0">
139 <div className="flex items-center justify-between px-6 py-4">
140 <Heading level="h2">Analytics Dashboard</Heading>
141 </div>
142 </Container>
143 )
144 }
145
146 export const config = defineRouteConfig({
147 label: "Analytics",
148 icon: ChartBar,
149 rank: 1,
150 })
151
152 export default AnalyticsPage
153 ```
154
155 </CodeTab>
156 <CodeTab label="UI Route 2" value="ui-route-2">
157
158 ```tsx title="src/admin/routes/reports/page.tsx" highlights={[["18"]]}
159 import { defineRouteConfig } from "@medusajs/admin-sdk"
160 import { DocumentText } from "@medusajs/icons"
161 import { Container, Heading } from "@medusajs/ui"
162
163 const ReportsPage = () => {
164 return (
165 <Container className="divide-y p-0">
166 <div className="flex items-center justify-between px-6 py-4">
167 <Heading level="h2">Reports</Heading>
168 </div>
169 </Container>
170 )
171 }
172
173 export const config = defineRouteConfig({
174 label: "Reports",
175 icon: DocumentText,
176 rank: 2,
177 })
178
179 export default ReportsPage
180 ```
181
182 </CodeTab>
183 </CodeTabs>
184
185 In the sidebar, "Analytics" with the rank `1` will be added before "Reports" with the rank `2`.
186
187 #### How are UI Routes Sorted in the Sidebar
188
189 Medusa sorts custom UI routes based on their rank:
190
191 1. UI routes that have ranks are sorted in ascending order.
192 2. UI routes without a rank are added after the ranked UI routes.
193
194 Medusa also applies the same sorting logic to UI routes at the nested level. Learn more in the [Nested UI Routes Ranking](#nested-ui-routes-ranking) section.
195
196 ### Nested UI Routes
197
198 Consider that alongside the UI route above at `src/admin/routes/custom/page.tsx` you create a nested UI route at `src/admin/routes/custom/nested/page.tsx` that also exports route configurations:
199
200 ![Example of nested UI route file in the application's directory structure](https://res.cloudinary.com/dza7lstvk/image/upload/v1732867243/Medusa%20Book/ui-route-dir-overview_tgju25.jpg)
201
202 ```tsx title="src/admin/routes/custom/nested/page.tsx"
203 import { defineRouteConfig } from "@medusajs/admin-sdk"
204 import { Container, Heading } from "@medusajs/ui"
205
206 const NestedCustomPage = () => {
207 return (
208 <Container className="divide-y p-0">
209 <div className="flex items-center justify-between px-6 py-4">
210 <Heading level="h2">This is my nested custom route</Heading>
211 </div>
212 </Container>
213 )
214 }
215
216 export const config = defineRouteConfig({
217 label: "Nested Route",
218 })
219
220 export default NestedCustomPage
221 ```
222
223 This UI route is shown in the sidebar as an item nested in the parent "Custom Route" item. Nested items are only shown when the parent sidebar items (in this case, "Custom Route") are clicked.
224
225 #### Caveats
226
227 Some caveats for nested UI routes in the sidebar:
228
229 - Nested dynamic UI routes, such as one created at `src/admin/routes/custom/[id]/page.tsx` aren't added to the sidebar as it's not possible to link to a dynamic route. If the dynamic route exports route configurations, a warning is logged in the browser's console.
230 - Nested routes in settings pages aren't shown in the sidebar to follow the admin's design conventions.
231 - The `icon` configuration is ignored for the sidebar item of nested UI routes to follow the admin's design conventions.
232
233 ### Route Under Existing Admin Route
234
235 You can add a custom UI route under an existing route. For example, you can add a route under the orders route:
236
237 ```tsx title="src/admin/routes/orders/nested/page.tsx"
238 import { defineRouteConfig } from "@medusajs/admin-sdk"
239 import { Container, Heading } from "@medusajs/ui"
240
241 const NestedOrdersPage = () => {
242 return (
243 <Container className="divide-y p-0">
244 <div className="flex items-center justify-between px-6 py-4">
245 <Heading level="h1">Nested Orders Page</Heading>
246 </div>
247 </Container>
248 )
249 }
250
251 export const config = defineRouteConfig({
252 label: "Nested Orders",
253 nested: "/orders",
254 })
255
256 export default NestedOrdersPage
257 ```
258
259 The `nested` property passed to `defineRouteConfig` specifies which route this custom route is nested under. This route will now show in the sidebar under the existing "Orders" sidebar item.
260
261 #### Nested UI Routes Ranking
262
263 Nested UI routes also accept the [rank](#specify-ui-route-sidebar-rank) configuration. It allows you to specify the order that the nested UI routes are shown in the sidebar under the parent item.
264
265 For example:
266
267 {/* eslint-disable @medusajs/ui-route-config-via-define-route-config */}
268
269 ```tsx title="src/admin/routes/orders/insights/page.tsx"
270 // In nested UI route 1 at src/admin/routes/orders/insights/page.tsx
271 export const config = defineRouteConfig({
272 label: "Order Insights",
273 nested: "/orders",
274 rank: 1, // Will appear first
275 })
276
277 // In nested UI route 2 at src/admin/routes/orders/reports/page.tsx
278 export const config = defineRouteConfig({
279 label: "Order Reports",
280 nested: "/orders",
281 rank: 2, // Will appear second
282 })
283 ```
284
285 In this example, the "Order Insights" item will appear before the "Order Reports" item under the parent "Orders" item in the sidebar.
286
287 ---
288
289 ## Create Settings Page
290
291 To create a page under the settings section of the admin dashboard, create a UI route under the path `src/admin/routes/settings`.
292
293 For example, create a UI route at `src/admin/routes/settings/custom/page.tsx`:
294
295 ![Example of settings UI route file in the application's directory structure](https://res.cloudinary.com/dza7lstvk/image/upload/v1732867435/Medusa%20Book/setting-ui-route-dir-overview_kytbh8.jpg)
296
297 ```tsx title="src/admin/routes/settings/custom/page.tsx"
298 import { defineRouteConfig } from "@medusajs/admin-sdk"
299 import { Container, Heading } from "@medusajs/ui"
300
301 const CustomSettingPage = () => {
302 return (
303 <Container className="divide-y p-0">
304 <div className="flex items-center justify-between px-6 py-4">
305 <Heading level="h1">Custom Setting Page</Heading>
306 </div>
307 </Container>
308 )
309 }
310
311 export const config = defineRouteConfig({
312 label: "Custom",
313 })
314
315 export default CustomSettingPage
316 ```
317
318 This adds a page under the path `/app/settings/custom`. An item is also added to the settings sidebar with the label `Custom`.
319
320 ---
321
322 ## Path Parameters
323
324 A UI route can accept path parameters if the name of any of the directories in its path is of the format `[param]`.
325
326 For example, create the file `src/admin/routes/custom/[id]/page.tsx` with the following content:
327
328 ![Example of UI route file with path parameters in the application's directory structure](https://res.cloudinary.com/dza7lstvk/image/upload/v1732867748/Medusa%20Book/path-param-ui-route-dir-overview_kcfbev.jpg)
329
330 ```tsx title="src/admin/routes/custom/[id]/page.tsx" highlights={[["5", "", "Retrieve the path parameter."], ["10", "{id}", "Show the path parameter."]]}
331 import { useParams } from "react-router-dom"
332 import { Container, Heading } from "@medusajs/ui"
333
334 const CustomPage = () => {
335 const { id } = useParams()
336
337 return (
338 <Container className="divide-y p-0">
339 <div className="flex items-center justify-between px-6 py-4">
340 <Heading level="h1">Passed ID: {id}</Heading>
341 </div>
342 </Container>
343 )
344 }
345
346 export default CustomPage
347 ```
348
349 You access the passed parameter using `react-router-dom`'s [useParams hook](https://reactrouter.com/api/hooks/useParams).
350
351 If you run the Medusa application and go to `http://localhost:9000/app/custom/123`, you'll see `123` printed in the page.
352
353 ---
354
355 ## Set UI Route Breadcrumbs
356
357 The Medusa Admin dashboard shows breadcrumbs at the top of each page, if specified. This allows users to navigate through your custom UI routes.
358
359 To set the breadcrumbs of a UI route, export a `handle` object with a `breadcrumb` property in the UI route's file:
360
361 ```tsx title="src/admin/routes/custom/page.tsx" highlights={[["16", "breadcrumb", "Set the breadcrumbs of the UI route."]]}
362 import { Container, Heading } from "@medusajs/ui"
363
364 const CustomPage = () => {
365 return (
366 <Container className="divide-y p-0">
367 <div className="flex items-center justify-between px-6 py-4">
368 <Heading level="h2">This is my custom route</Heading>
369 </div>
370 </Container>
371 )
372 }
373
374 export default CustomPage
375
376 export const handle = {
377 breadcrumb: () => "Custom Route",
378 }
379 ```
380
381 The `breadcrumb`'s value is a function that returns the breadcrumb label as a string, or a React JSX element.
382
383 ### Set Breadcrumbs for Nested UI Routes
384
385 If you set a breadcrumb for a nested UI route, and you open the route in the Medusa Admin, you'll see the breadcrumbs starting from its parent route to the nested route.
386
387 For example, if you have the following UI route at `src/admin/routes/custom/nested/page.tsx` that's nested under the previous one:
388
389 ```tsx title="src/admin/routes/custom/nested/page.tsx" highlights={[["16", "breadcrumb", "Set the breadcrumbs of the nested UI route."]]}
390 import { Container, Heading } from "@medusajs/ui"
391
392 const NestedCustomPage = () => {
393 return (
394 <Container className="divide-y p-0">
395 <div className="flex items-center justify-between px-6 py-4">
396 <Heading level="h2">This is my nested custom route</Heading>
397 </div>
398 </Container>
399 )
400 }
401
402 export default NestedCustomPage
403
404 export const handle = {
405 breadcrumb: () => "Nested Custom Route",
406 }
407 ```
408
409 Then, when you open the nested route at `http://localhost:9000/app/custom/nested`, you'll see the breadcrumbs as `Custom Route > Nested Custom Route`. Each breadcrumb is clickable, allowing users to navigate back to the parent route.
410
411 ### Set Breadcrumbs Dynamically
412
413 In some use cases, you may want to show a dynamic breadcrumb for a UI route. For example, if you have a UI route that displays a brand's details, you can set the breadcrumb to show the brand's name dynamically.
414
415 To do that, you can:
416
417 1. Define and export a `loader` function in the UI route file that fetches the data needed for the breadcrumb.
418 2. Receive the data in the `breadcrumb` function and return the dynamic label.
419
420 For example, create a UI route at `src/admin/routes/brands/[id]/page.tsx` with the following content:
421
422 export const dynamicBreadcrumbsHighlights = [
423 ["25", "loader", "Define a loader function to fetch the brand data."],
424 ["35", "breadcrumb", "Set the breadcrumb using the fetched brand data."],
425 ["36", "loaderData", "The data returned by the loader function is passed as props to the breadcrumb function."],
426 ]
427
428 ```tsx title="src/admin/routes/brands/[id]/page.tsx" highlights={dynamicBreadcrumbsHighlights}
429 import { Container, Heading } from "@medusajs/ui"
430 import { LoaderFunctionArgs, UIMatch, useLoaderData } from "react-router-dom"
431 import { sdk } from "../../../lib/sdk"
432
433 type BrandResponse = {
434 brand: {
435 name: string
436 }
437 }
438
439 const BrandPage = () => {
440 const { brand } = useLoaderData() as Awaited<BrandResponse>
441
442 return (
443 <Container className="divide-y p-0">
444 <div className="flex items-center justify-between px-6 py-4">
445 <Heading level="h2">{brand.name}</Heading>
446 </div>
447 </Container>
448 )
449 }
450
451 export default BrandPage
452
453 export async function loader({ params }: LoaderFunctionArgs) {
454 const { id } = params
455 const { brand } = await sdk.client.fetch<BrandResponse>(`/admin/brands/${id}`)
456
457 return {
458 brand,
459 }
460 }
461
462 export const handle = {
463 breadcrumb: (
464 { loaderData }: UIMatch<BrandResponse>
465 ) => loaderData?.brand.name || "Brand",
466 }
467 ```
468
469 In the `loader` function, you retrieve the brands from a custom API route and return them.
470
471 Then, in the `handle.breadcrumb` function, you receive the `loaderData` prop containing the brand information returned by the `loader` function. You can use this data to return a dynamic breadcrumb label.
472
473 <Note>
474
475 The `loaderData` prop replaces the `data` prop starting from [Medusa v2.19.0](https://github.com/medusajs/medusa/releases/tag/v2.19.0), which upgraded the Medusa Admin to React Router v7. In earlier versions, use the `data` prop instead.
476
477 </Note>
478
479 When you open the UI route at `http://localhost:9000/app/brands/123`, the breadcrumb will show the brand's name, such as `Acme`.
480
481 <Note title="Tip">
482
483 You also use the `useLoaderData` hook to access the data returned by the `loader` function in the UI route component. Learn more in the [Routing Customizations](../routing/page.mdx#fetch-data-with-route-loaders) chapter.
484
485 </Note>
486
487 ---
488
489 ## Set UI Route Page Title
490
491 <Note>
492
493 This feature is available starting [Medusa v2.17.2](https://github.com/medusajs/medusa/releases/tag/v2.17.2).
494
495 </Note>
496
497 By default, a UI route's page title is set to the `label` property of the route's configuration.
498
499 You can also set it to a dynamic value, which is useful for UI routes that accept path parameters. For example, if you have a brand details page, you can set the page title to the brand's name.
500
501 To set the page title of a UI route different than the `label` property, export a `handle` object with a `seo` property in the UI route's file:
502
503 export const seoHighlights = [
504 ["26", "loader", "Define a loader function to fetch the brand data."],
505 ["36", "seo", "Set the document title using the fetched brand data."],
506 ["37", "match", "The match object holds the data returned by the loader in its `loaderData` property."],
507 ]
508
509 ```tsx title="src/admin/routes/brands/[id]/page.tsx" highlights={seoHighlights}
510 import { Container, Heading } from "@medusajs/ui"
511 import { LoaderFunctionArgs, UIMatch, useLoaderData } from "react-router-dom"
512 // Path to the JS SDK
513 import { sdk } from "../../../lib/sdk"
514
515 type BrandResponse = {
516 brand: {
517 name: string
518 }
519 }
520
521 const BrandPage = () => {
522 const { brand } = useLoaderData() as Awaited<BrandResponse>
523
524 return (
525 <Container className="divide-y p-0">
526 <div className="flex items-center justify-between px-6 py-4">
527 <Heading level="h2">{brand.name}</Heading>
528 </div>
529 </Container>
530 )
531 }
532
533 export default BrandPage
534
535 export async function loader({ params }: LoaderFunctionArgs) {
536 const { id } = params
537 const { brand } = await sdk.client.fetch<BrandResponse>(`/admin/brands/${id}`)
538
539 return {
540 brand,
541 }
542 }
543
544 export const handle = {
545 seo: (match: UIMatch<BrandResponse>) => ({
546 title: match.loaderData?.brand.name || "Brand",
547 }),
548 }
549 ```
550
551 In the `loader` function, you retrieve the brand from a custom API route and return it.
552
553 Then, in the `handle.seo` function, you receive the `match` object whose `loaderData` property holds the data returned by the `loader` function. You use this data to return the dynamic title.
554
555 <Note>
556
557 The `loaderData` property replaces the `data` property starting from [Medusa v2.19.0](https://github.com/medusajs/medusa/releases/tag/v2.19.0), which upgraded the Medusa Admin to React Router v7. In earlier versions, use the `data` property instead.
558
559 </Note>
560
561 When you open the UI route at `http://localhost:9000/app/brands/123`, the browser tab's title will show the brand's name, such as `Acme - Medusa`.
562
563 <Note title="Tip">
564
565 If the `seo` function returns an empty `title` or doesn't return one, the dashboard falls back to the route's [sidebar label](#show-ui-route-in-the-sidebar), then its [breadcrumb](#set-ui-route-breadcrumbs), and finally the default `Medusa` title.
566
567 </Note>
568
569 ---
570
571 ## Admin Components List
572
573 To build admin customizations that match the Medusa Admin's designs and layouts, refer to [this guide](!resources!/admin-components) to find common components.
574
575 ---
576
577 ## More Routes Customizations
578
579 For more customizations related to routes, refer to the [Routing Customizations chapter](../routing/page.mdx).