|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: "HorizontalMenu" |
| 4 | +--- |
| 5 | + |
| 6 | +# `<HorizontalMenu>` |
| 7 | + |
| 8 | +This [Enterprise Edition](https://react-admin-ee.marmelab.com)<img class="icon" src="./img/premium.svg" /> renders a horizontal menu component, alternative to react-admin's `<Menu>`, to be used in the AppBar of the [`<ContainerLayout>`](./ContainerLayout.md). |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +`<HorizontalMenu>` is part of the [ra-navigation](https://react-admin-ee.marmelab.com/documentation/ra-navigation#containerlayout) package. |
| 13 | + |
| 14 | +## Usage |
| 15 | + |
| 16 | +Create a menu component based on `<HorizontalMenu>` and `<HorizontalMenu.Item>` (or `<HorizontalMenu.DashboardItem>`) children. |
| 17 | + |
| 18 | +Each child should have a `value` corresponding to the [application location](https://react-admin-ee.marmelab.com/documentation/ra-navigation#concepts) of the target, and can have a `to` prop corresponding to the target location if different from the app location. |
| 19 | + |
| 20 | +```jsx |
| 21 | +import { HorizontalMenu } from '@react-admin/ra-navigation'; |
| 22 | + |
| 23 | +export const Menu = () => ( |
| 24 | + <HorizontalMenu> |
| 25 | + <HorizontalMenu.DashboardItem label="Dashboard" value="" /> |
| 26 | + <HorizontalMenu.Item label="Songs" to="/songs" value="songs" /> |
| 27 | + <HorizontalMenu.Item label="Artists" to="/artists" value="artists" /> |
| 28 | + <HorizontalMenu.Item label="Business" value="business"> |
| 29 | + <HorizontalMenu.Item label="Producers" to="/producers" value="producers" /> |
| 30 | + <HorizontalMenu.Item label="Label" to="/label" value="label" /> |
| 31 | + </HorizontalMenu.Item> |
| 32 | + <HorizontalMenu.Item label="Custom" to="/custom" value="custom" /> |
| 33 | + </HorizontalMenu> |
| 34 | +); |
| 35 | +``` |
| 36 | + |
| 37 | +Then pass it to ta custom layout based on `<ContainerLayout>`, and make it the `<Admin layout>`: |
| 38 | + |
| 39 | +```jsx |
| 40 | +import { Admin, Resource } from 'react-admin'; |
| 41 | +import { ContainerLayout } from '@react-admin/ra-navigation'; |
| 42 | + |
| 43 | +import { Menu } from './Menu'; |
| 44 | + |
| 45 | +const MyLayout = ({ children }) => ( |
| 46 | + <ContainerLayout menu={<Menu />}> |
| 47 | + {children} |
| 48 | + </ContainerLayout> |
| 49 | +); |
| 50 | + |
| 51 | +const App = () => ( |
| 52 | + <Admin dataProvider={dataProvider} layout={MyLayout}> |
| 53 | + ... |
| 54 | + </Admin> |
| 55 | +); |
| 56 | +``` |
| 57 | + |
| 58 | +## Props |
| 59 | + |
| 60 | +`<HorizontalMenu>` accepts the following props: |
| 61 | + |
| 62 | +| Prop | Required | Type | Default | Description | |
| 63 | +| -------------- | -------- | --------- | ----------- | ---------------------------------------------------------------------------------------- | |
| 64 | +| `children` | Optional | | | The menu items to display. | |
| 65 | +| `hasDashboard` | Optional | Boolean | | Display an `<HorizontalMenu.DashboardItem>` with your resources if no children specified | |
| 66 | + |
| 67 | +It also accept the props of [MUI Tabs](https://mui.com/material-ui/api/tabs/#props). |
| 68 | + |
| 69 | +## `children` |
| 70 | + |
| 71 | +When you use `<HorizontalMenu>` without any child, it automatically adds one menu item per resource. |
| 72 | + |
| 73 | +If you want to customize the menu items, pass them as children to the `<HorizontalMenu>`. Each child should be a [`<HorizontalMenu.Item>`](#horizontalmenuitem) or a [`<HorizontalMenu.DashboardItem>`](#horizontalmenudashboarditem). |
| 74 | + |
| 75 | +```jsx |
| 76 | +import { HorizontalMenu } from '@react-admin/ra-navigation'; |
| 77 | + |
| 78 | +export const Menu = () => ( |
| 79 | + <HorizontalMenu> |
| 80 | + <HorizontalMenu.DashboardItem label="Dashboard" value="" /> |
| 81 | + <HorizontalMenu.Item label="Songs" to="/songs" value="songs" /> |
| 82 | + <HorizontalMenu.Item label="Artists" to="/artists" value="artists" /> |
| 83 | + <HorizontalMenu.Item label="Business" value="business"> |
| 84 | + <HorizontalMenu.Item label="Producers" to="/producers" value="producers" /> |
| 85 | + <HorizontalMenu.Item label="Label" to="/label" value="label" /> |
| 86 | + </HorizontalMenu.Item> |
| 87 | + <HorizontalMenu.Item label="Custom" to="/custom" value="custom" /> |
| 88 | + </HorizontalMenu> |
| 89 | +); |
| 90 | +``` |
| 91 | + |
| 92 | +## `hasDashboard` |
| 93 | + |
| 94 | +This prop lets you add a dashboard item when using `<HorizontalMenu>` with no children. |
| 95 | + |
| 96 | +```tsx |
| 97 | +import { ContainerLayout, HorizontalMenu } from '@react-admin/ra-navigation'; |
| 98 | + |
| 99 | +const MyLayout = ({ children }) => ( |
| 100 | + <ContainerLayout menu={<HorizontalMenu hasDashboard />}> |
| 101 | + {children} |
| 102 | + </ContainerLayout> |
| 103 | +); |
| 104 | +``` |
| 105 | + |
| 106 | +## `<HorizontalMenu.Item>` |
| 107 | + |
| 108 | +An item for the `<HorizontalMenu>` component. Used to define access to a list view for a resource, or a custom route. |
| 109 | + |
| 110 | +```tsx |
| 111 | +<HorizontalMenu> |
| 112 | + <HorizontalMenu.DashboardItem label="Home" /> |
| 113 | + <HorizontalMenu.Item label="Artists" to="/artists" value="artists" /> |
| 114 | + <HorizontalMenu.Item label="Songs" to="/songs" value="songs" /> |
| 115 | + <HorizontalMenu.Item label="Labels" to="/labels" value="labels" /> |
| 116 | +</HorizontalMenu> |
| 117 | +``` |
| 118 | + |
| 119 | +### Props |
| 120 | + |
| 121 | +| Prop | Required | Type | Default | Description | |
| 122 | +| --------------- | -------- | --------- | ----------- | -------------------------------------------------------------------------------------------------------------------- | |
| 123 | +| `value` | Required | string | | The value of the Tab and the default route to use if no `to` is provided | |
| 124 | +| `label` | Optional | string | | The text to display | |
| 125 | +| `to` | Optional | string | | The route to which the item redirects | |
| 126 | +| `TabProps` | Optional | [TabProps](https://mui.com/material-ui/api/tabs/#props) | | Additional props of the Tab | |
| 127 | +| `MenuProps` | Optional | [MenuProps](https://mui.com/material-ui/api/menu/#props) | | Additional props of the Menu (`HorizontalMenu.Item` with children) | |
| 128 | +| `MenuItemProps` | Optional | [MenuItemProps](https://mui.com/material-ui/api/menu-item/#props) | | Additional props of the MenuItem (children of a `HorizontalMenu.Item`) | |
| 129 | + |
| 130 | +### `label` |
| 131 | + |
| 132 | +You can customize the label by setting the `label` prop. It is inferred from the `value` prop by default. |
| 133 | + |
| 134 | +`<HorizontalMenu.Item>` uses the i18n layer, so you can translate the label. Check [the Translation chapter](./TranslationTranslating.md) for more information. |
| 135 | + |
| 136 | +```tsx |
| 137 | +<HorizontalMenu> |
| 138 | + <HorizontalMenu.Item label="Artists" value="artists" /> |
| 139 | + <HorizontalMenu.Item label="ra.custom.path.resource.song" value="songs" /> |
| 140 | +</HorizontalMenu> |
| 141 | +``` |
| 142 | + |
| 143 | +### `MenuProps` |
| 144 | + |
| 145 | +Additional props passed to the [Menu](https://mui.com/material-ui/api/menu/#props) (item displayed if it has children). |
| 146 | + |
| 147 | +{% raw %} |
| 148 | + |
| 149 | +```tsx |
| 150 | +<HorizontalMenu> |
| 151 | + <HorizontalMenu.Item |
| 152 | + value="songs" |
| 153 | + MenuProps={{ open: true, autoFocus: true }} |
| 154 | + > |
| 155 | + <HorizontalMenu.Item value="albums" /> |
| 156 | + <HorizontalMenu.Item value="singles" /> |
| 157 | + </HorizontalMenu.Item> |
| 158 | +</HorizontalMenu> |
| 159 | +``` |
| 160 | + |
| 161 | +{% endraw %} |
| 162 | + |
| 163 | +### `MenuItemProps` |
| 164 | + |
| 165 | +Additional props passed to the [MenuItem](https://mui.com/material-ui/api/menu-item/#props) (item displayed in a sub-menu). |
| 166 | + |
| 167 | +{% raw %} |
| 168 | + |
| 169 | +```tsx |
| 170 | +<HorizontalMenu> |
| 171 | + <HorizontalMenu.Item value="songs"> |
| 172 | + <HorizontalMenu.Item |
| 173 | + value="albums" |
| 174 | + MenuItemProps={{ |
| 175 | + divider: true, |
| 176 | + selected: isSelected(), |
| 177 | + }} |
| 178 | + /> |
| 179 | + <HorizontalMenu.Item value="singles" /> |
| 180 | + </HorizontalMenu.Item> |
| 181 | +</HorizontalMenu> |
| 182 | +``` |
| 183 | + |
| 184 | +{% endraw %} |
| 185 | + |
| 186 | +### `TabProps` |
| 187 | + |
| 188 | +Additional props passed to the [Tab](https://mui.com/material-ui/api/tabs/#props). |
| 189 | + |
| 190 | +{% raw %} |
| 191 | + |
| 192 | +```tsx |
| 193 | +import { HorizontalMenu } from '@react-admin/ra-navigation'; |
| 194 | +import MusicNoteIcon from '@mui/icons-material/MusicNote'; |
| 195 | + |
| 196 | +const Menu = () => ( |
| 197 | + <HorizontalMenu> |
| 198 | + <HorizontalMenu.Item |
| 199 | + value="songs" |
| 200 | + TabProps={{ icon: <MusicNoteIcon />, iconPosition: 'start' }} |
| 201 | + /> |
| 202 | + </HorizontalMenu> |
| 203 | +); |
| 204 | +``` |
| 205 | + |
| 206 | +{% endraw %} |
| 207 | + |
| 208 | +### `to` |
| 209 | + |
| 210 | +You can customize the link of your resource by setting the `to` prop. It is inferred from the `value` prop by default as ``/${value}``. |
| 211 | + |
| 212 | +```tsx |
| 213 | +<HorizontalMenu> |
| 214 | + <HorizontalMenu.Item to="/artists" value="artists" /> |
| 215 | + <HorizontalMenu.Item to="/musics" value="songs" /> |
| 216 | +</HorizontalMenu> |
| 217 | +``` |
| 218 | + |
| 219 | +### `value` |
| 220 | + |
| 221 | +The `value` passed to the [MUI `Tab`](https://mui.com/material-ui/react-tabs/): |
| 222 | + |
| 223 | +```tsx |
| 224 | +<HorizontalMenu> |
| 225 | + <HorizontalMenu.Item value="artists" /> |
| 226 | + <HorizontalMenu.Item value="songs" /> |
| 227 | +</HorizontalMenu> |
| 228 | +``` |
| 229 | + |
| 230 | +## `<HorizontalMenu.DashboardItem>` |
| 231 | + |
| 232 | +This component adds a menu item that redirects to the `/` route. It accepts the same props as [`<HorizontalMenu.Item>`](#horizontalmenuitem). |
| 233 | + |
| 234 | +```tsx |
| 235 | +<HorizontalMenu.DashboardItem value="" /> |
| 236 | +``` |
| 237 | + |
| 238 | +## Adding Sub-Menus |
| 239 | + |
| 240 | +<video controls autoplay playsinline muted loop> |
| 241 | + <source src="https://react-admin-ee.marmelab.com/assets/horizontal-menu-submenu.mp4" type="video/mp4"/> |
| 242 | + Your browser does not support the video tag. |
| 243 | +</video> |
| 244 | + |
| 245 | +`<HorizontalMenu.Item>` creates a menu item for a given path. But you can also add `<HorizontalMenu.Item>` components as a child to create a submenu. |
| 246 | + |
| 247 | +```jsx |
| 248 | +<HorizontalMenu> |
| 249 | + <HorizontalMenu.DashboardItem label="Home" /> |
| 250 | + <HorizontalMenu.Item label="artists" to="/artists" value="artists" /> |
| 251 | + <HorizontalMenu.Item label="Business" value="business"> |
| 252 | + <HorizontalMenu.Item label="Producers" to="/producers" value="producers" /> |
| 253 | + <HorizontalMenu.Item label="Label" to="/label" value="label" /> |
| 254 | + </HorizontalMenu.Item> |
| 255 | + <HorizontalMenu.Item label="songs" to="/songs" value="songs" /> |
| 256 | +</HorizontalMenu> |
| 257 | +``` |
0 commit comments