Menu
The Menu is an UI component that displays a list of items vertically. It's commonly used to group related options or actions together. You can use it within various components like Dropdown, Drawer, and Split Button to organize content or actions in a straightforward and accessible way.
By default, a Menu will handle automatically the active menu item based on user interaction thanks to the enableAutoActive prop set by default to true.
There are two ways to use this component:
- by passing elements as children.
- by using the
itemsprop to define the list of items directly.
All menu items and submenu items must have an id attribute for the features to work properly.
Using children
You can pass elements like MenuItem, SubmenuItem, and their associated components as children to construct a MenuItemList. This provides enhanced control over the layout and content, allowing for more tailored and flexible menu designs.
Single level menu
<MenuItemList>
<MenuItem id="menu1">
<MenuItemLabel>
<Icon icon="account_circle"></Icon>
<Typography>Menu Item</Typography>
</MenuItemLabel>
</MenuItem>
<MenuItem id="menu2">
<MenuItemLabel>
<Icon icon="account_circle"></Icon>
<Typography>Menu Item</Typography>
</MenuItemLabel>
</MenuItem>
<MenuItem id="menu3">
<MenuItemLabel>
<Icon icon="account_circle"></Icon>
<Typography>Menu Item</Typography>
</MenuItemLabel>
</MenuItem>
</MenuItemList>
Multi-level menu
For menu item with submenu items, a collapsed icon should also be displayed on the right side of menu item label. To select a suitable icon, please refer to the Icon documentation.
<MenuItemList>
<MenuItem id="menu1">
<MenuItemLabel>
<Icon icon="account_circle"></Icon>
<Typography>Menu Item</Typography>
</MenuItemLabel>
</MenuItem>
<MenuItem id="menu2">
<MenuItemLabel>
<Icon icon="account_circle"></Icon>
<Typography>Menu Item</Typography>
</MenuItemLabel>
</MenuItem>
<MenuItem id="menu3">
<MenuItemLabel>
<Icon icon="account_circle"></Icon>
<Typography>Menu Item</Typography>
<Icon icon="keyboard_arrow_up"></Icon>
</MenuItemLabel>
<SubmenuItemList>
<SubmenuItem id="submenu1">Submenu Item</SubmenuItem>
<SubmenuItem id="submenu2">Submenu Item</SubmenuItem>
<SubmenuItem id="submenu3">Submenu Item</SubmenuItem>
</SubmenuItemList>
</MenuItem>
</MenuItemList>
Using items prop
You can also use the items prop to generate the menu item list dynamically. This prop has the following format:
type MenuItemType = {
label: string,
id: string,
icon?: string | IconProps,
children?: [
{
label: string,
id: string,
}
],
};
Single level menu
const items=[
{ label: 'Menu item', id: 'menu1', icon: 'account_circle' },
{ label: 'Menu item', id: 'menu2', icon: 'account_circle' },
{ label: 'Menu item', id: 'menu3', icon: 'account_circle' },
]
<MenuItemList items={items}></MenuItemList>
Multi-level menu
For menu items that contain submenu items, a collapsed icon will be automatically displayed on the right side of the menu label. When a menu item with a submenu is clicked, the icon will dynamically expand or collapse.
const items=[
{label: "Menu item", id: "menu1", icon: "account_circle"},
{label: "Menu item", id: "menu2", icon: "account_circle"},
{
label: "Menu item", id: "menu3", icon: "account_circle"},
children: [
{ label: 'Submenu item', id: 'submenu1' },
{ label: 'Submenu item', id: 'submenu2' },
{ label: 'Submenu item', id: 'submenu3' },
],
},
]
<MenuItemList items={items}></MenuItemList>
Multi-level menu with custom icon
To select an appropriate icon, please consult the Icon component documentation.
You can include an icon in the items property by adding either the icon name or an icon object with the following interface:
interface IconProps = {
icon: string,
classes?: string,
lib?: IconLibType,
size?: IconSizeType,
variant?: IconVariantType,
};
const items=[
{label: "Menu item", id: "menu1", icon: <Icon icon="applied_settings" lib="business" />},
{label: "Menu item", id: "menu2", icon: <Icon icon="applied_settings" lib="business" />},
{
label: "Menu item", id: "menu3",
icon: <Icon icon="applied_settings" lib="business" />,
children: [
{ label: 'Submenu item', id: 'submenu1' },
{ label: 'Submenu item', id: 'submenu2' },
{ label: 'Submenu item', id: 'submenu3' },
],
},
]
<MenuItemList items={items}></MenuItemList>
Sizes
The menu item lists come in three size: small, medium (default) and large.
Small
Medium
Large
const items=[{label: "Menu item", id: "menu1", icon: "account_circle"},...]
<MenuItemList size="small" items={items}></MenuItemList>
<MenuItemList size="medium" items={items}></MenuItemList>
<MenuItemList size="large" items={items}></MenuItemList>
Managing items
All the props listed below can be applied directly to the MenuItemList component, either using the children or using the items prop method.
enableAutoActive
Setting the enableAutoActive prop to true (default value) will automatically activate the active state of both menu items and submenu items when they are clicked or when the activeId prop value has changed.
On the other hand, setting it to false will require manual management of the active state.
When enableAutoActive is set to false, handling the active state is not compatible with the items prop usage.
For a SubmenuItem to be active, its MenuItem parent must also have the active prop.
<MenuItemList enableAutoActive={false}>
<MenuItem id="menu1">
<MenuItemLabel>
<Icon icon="account_circle"></Icon>
<Typography>Menu Item</Typography>
</MenuItemLabel>
</MenuItem>
<MenuItem id="menu2">
<MenuItemLabel>
<Icon icon="account_circle"></Icon>
<Typography>Menu Item</Typography>
</MenuItemLabel>
</MenuItem>
<MenuItem id="menu3" active>
<MenuItemLabel>
<Icon icon="account_circle"></Icon>
<Typography>Menu Item</Typography>
<Icon icon="keyboard_arrow_up"></Icon>
</MenuItemLabel>
<SubmenuItemList>
<SubmenuItem id="submenu1">Submenu Item</SubmenuItem>
<SubmenuItem id="submenu2" active>Submenu Item</SubmenuItem>
<SubmenuItem id="submenu3">Submenu Item</SubmenuItem>
</SubmenuItemList>
</MenuItem>
</MenuItemList>
activeId
Set the activeId value to the active menu or submenu item id by default.
const items=[{label: "Menu item", id: "menu1", icon: "account_circle"},...]
<MenuItemList items={items} activeId="menu2"></MenuItemList>
disabledIds
Set the disabledIds value to a list of ids of disabled menu items or disabled submenu items.
const items=[{label: "Menu item", id: "menu1", icon: "account_circle"},...]
<MenuItemList
items={items}
disabledIds={['menu1', 'submenu2']}
></MenuItemList>
collapsedIds
Set the collapsedIds value to a list of menu items that don't show their own nested submenu items by default.
const items=[{label: "Menu item", id: "menu1", icon: "account_circle"},...]
<MenuItemList items={items} collapsedIds={['menu3']}></MenuItemList>
Scrolling
scrollable
If there are more menu items than can be displayed at once, you can make the menu item list scrollable by adding the scrollable attribute to this component. By default, the scrollable menu item list displays the first five menu items and half of the sixth one.
const items=[{label: "Menu item", id: "menu1", icon: "account_circle"},...]
<MenuItemList items={items} scrollable></MenuItemList>
nbVisibleItems
If you want to change the number of visible items when the menu item list is scrollable, you can change value of the nbVisibleItems prop.
const items=[{label: "Menu item", id: "menu1", icon: "account_circle"},...]
<MenuItemList items={items} scrollable nbVisibleItems={2}></MenuItemList>
Customization
Add your own classes with the classes prop. This way you will be able to override or extend the styles applied to the component (you can use available utility classes by importing @qtm/css/dist/utilities.css).
const items=[{label: "Menu item", id: "menu1", icon: "account_circle"},...]
<MenuItemList items={items} classes="bg-primary-100"></MenuItemList>
API
MenuItem
The MenuItem component accepts all the props a standard <div> does in React (HTML, ARIA, data attributes, event handlers, and children). Its props are typed as React.HTMLAttributes<HTMLDivElement>. See more about supported HTML props in React's documentation.
MenuItemLabel
The MenuItemLabel component accepts all the props a standard <div> does in React (HTML, ARIA, data attributes, event handlers, and children). Its props are typed as React.HTMLAttributes<HTMLDivElement>. See more about supported HTML props in React's documentation.
MenuItemList
The MenuItemList component accepts all the props a standard <div> does in React (HTML, ARIA, data attributes, event handlers, and children). Its props are typed as React.HTMLAttributes<HTMLDivElement>. See more about supported HTML props in React's documentation.
SubmenuItem
The SubmenuItem component accepts all the props a standard <div> does in React (HTML, ARIA, data attributes, event handlers, and children). Its props are typed as React.HTMLAttributes<HTMLDivElement>. See more about supported HTML props in React's documentation.
SubmenuItemList
The SubmenuItemList component accepts all the props a standard <div> does in React (HTML, ARIA, data attributes, event handlers, and children). Its props are typed as React.HTMLAttributes<HTMLDivElement>. See more about supported HTML props in React's documentation.