Routing and Menu
The menu is the core of the layout component. In antd-layout, menus must be manually configured based on the routing structure, and routing must use react-router.
Route Lazy Loading
If you need route lazy loading, you should use the Data Mode routing configuration. Combined with the LazyPage helper provided by antd-layout, you can assemble lazy-loaded routes.
import { LazyPage } from "@adminui-dev/antd-layout"
import { createBrowserRouter, redirect } from "react-router-dom"
const DashboardLayout = LazyPage(()=>import("@/layouts"))
const OrderPage = LazyPage(()=>import("@/pages/transaction/order"))
const ProductPage = LazyPage(()=>import("@/pages/transaction/product"))
const WelcomePage = LazyPage(()=>import("@/pages/welcome"))
const routers = createBrowserRouter(
[
{ path:"/", Component:DashboardLayout, children: [
{ index:true, loader:()=>redirect("/welcome") },
{ path:"welcome", Component:WelcomePage },
{ path:"transaction", children:[
{ index:true, loader:()=>redirect("/transaction/order") },
{ path:"order", Component:OrderPage },
{ path:"product", Component:ProductPage }
] }
] }
]
)
export { routers }Then mount the router at the application root. Note that you must include the nprogress.css style. Lazy loading uses a top progress bar indicator.
import { Suspense } from 'react'
import './App.css'
import 'nprogress/nprogress.css';
import { RouterProvider } from 'react-router'
import { routers } from './routes'
function App() {
return (
<>
<Suspense>
<RouterProvider router={routers} />
</Suspense>
</>
)
}
export default AppFor more routing configuration details, please refer to the react-router
Configuring the Menu
Menus must be configured according to the routing structure and then passed into the antd-layout component.
import { AntdLayout } from "@adminui-dev/antd-layout"
import type { LayoutConfig, MenuData } from "@adminui-dev/antd-layout"
const menuData:MenuData = {
name:"",
path:"/",
label:"Dashboard",
children:[
{ name:"welcome", path:"welcome", label:"Welcome" },
{ name:"transaction", path:"transaction", label:"Transaction", children:[
{ name:"order", path:"order", label:"Order" },
{ name:"product", path:"product", label:"Product" },
]}
]
}
export default function(){
// layout config
const layoutConfig:LayoutConfig = {
disabledLocale:true,
largeBrand:true,
}
return(
<>
<AntdLayout layoutConfig={layoutConfig} menuData={menuData} />
</>
)
}
Note: The root node of the menu returns an
object, not anarray.
Icons
The icon parameter field is icon, which accepts a Component object. You will need to install the icon libraries yourself; all major icon libraries are supported. We recommend using the official @ant-design/icons or lucide-react. Native img elements and svg objects are also supported. It is best to stick to a single icon set to ensure a consistent visual style.
- Example using an icon library:
import type { MenuData } from "@adminui-dev/antd-layout"
import { DashboardOutlined, SmileOutlined } from "@ant-design/icons"
export const menuData:MenuData = {
name:"",
path:"/",
label:"Dashboard",
icon:<DashboardOutlined />
children:[
{name:"welcome", path:"welcome", label:"Welcome", icon:<SmileOutlined />}
]
}
- Example using
imgorsvg:
import type { MenuData } from "@adminui-dev/antd-layout"
import reactIcon from "@/assets/react.svg"
export const menuData:MenuData = {
name:"",
path:"/",
label:"Dashboard",
icon:<img src={reactIcon} />
children:[
{name:"welcome", path:"welcome", label:"Welcome", icon:<img src="/smile.png">}
]
}
Alternatively:
{ icon:<svg /> }
Note: Do not set width or height styles on the
icon. Its dimensions should be dynamically controlled byLayoutConfig.menuIconSize
❗Incorrect usage:
{ icon:<img src="vite.svg" style="width:20px" />}
// or
{ icon:<SmileOutlined size={20} />}
Menu localization
Menu i18n is enabled in sync with the layout component’s localeMessages. the key is automatically generated based on path and name, All menu i18n keys must start with menu., The root menu uses menu., These keys are also used in breadcrumbs. Menu i18n data is usually stored in a separate file, such as .menus.ts. Example:
export default {
"menu.":"Dashboard",
"menu.login":"Login",
"menu.welcome":"Welcome",
"menu.workbench":"Workbench",
"menu.workbench.notifications":"Notifications",
"menu.workbench.monitoring":"Monitoring",
"menu.workbench.report":"Report",
"menu.workbench.report.visit":"Visit",
"menu.workbench.report.sales":"Sales",
"menu.workbench.logs":"Logs",
"menu.workbench.logs.login":"Login logs",
"menu.workbench.logs.operation":"Operation logs"
}export default {
"menu.":"仪表盘",
"menu.login":"登录",
"menu.welcome":"欢迎页",
"menu.workbench":"工作台",
"menu.workbench.notifications":"我的消息",
"menu.workbench.monitoring":"数据监控",
"menu.workbench.report":"统计报表",
"menu.workbench.report.visit":"访问统计",
"menu.workbench.report.sales":"销售统计",
"menu.workbench.logs":"日志记录",
"menu.workbench.logs.login":"登录日志",
"menu.workbench.logs.operation":"操作日志"
}To disable menu i18n, set
disabledLocaletotruein the layout configuration。When i18n is disabled, menu names will fall back to the
labelfield.
Menu Properties
MenuData
| Name | Type | Default | Description |
|---|---|---|---|
| name | string | - | Unique name,corresponding to a route node |
| label | string | - | Display name |
| path | string | - | Navigation route |
| layout | boolean | true | Use the Layout menu |
| icon | ReactNode | - | Icon component |
| selectedIcon | ReactNode | - | Select the icon component |
| extra | ReactNode | - | Extra UI such as <Brage>,<Tag> |
| params | Record<string,string> | - | Route parameters |
| children | MenuData[] | - | Child menus |
- It is not recommended for menus to exceed 3 levels. The structure should remain clear, concise, and intuitive.
- When
layout=false, the page corresponding to the current menu path will have its layout styles and menu removed, while the theme/skin background effect will be retained.