Menu
Menus help users choose items from a list.
- To let users see and choose one thing from a list of items
- To limit user selection within groups of items
- To let users type or select options in a form, use Combobox instead
- To move between views, use Tabs instead
- To let users see and choose one thing from a list of parallel actions, use in combination with a Split button
How to use it
Default
Display a list of menu items. Uses a default Garden Button internally.
Button
Configure a custom menu trigger.
Arrow
An arrow can be added to the Menu to give the user a sense of what element is triggering it.
Danger
Use danger styling for Menu items that enable destructive action.
Disabled
Items within a Menu can be disabled to prevent a user from interacting with them.
Media items
Media elements add even more context to a Menu.
Meta text
Meta text can also be paired with Menu items for additional clarity.
Grouped items
Group items together if they are associated or to enable selection.
Nested
Menus can contain nested levels for additional categorization of menu items.
Placement
Menu placement can be oriented around a trigger element in different positions. The default
placement is bottom-start
. This example demonstrates the top
placement. Optional
fallbackPlacements
can be set for fine-tuned repositioning control.
Size
Can be default or compact in size. This reduces vertical padding on menu items.
Configuration
- Name9.0.0-next.26•View source•View on npm
- Installnpm install @zendeskgarden/react-dropdowns
- Depsnpm install react react-dom styled-components @zendeskgarden/react-theming
- Importimport { Item, ItemGroup, Menu, Separator } from '@zendeskgarden/react-dropdowns'
API
The Menu component follows this structure:
<Menu>
<Item />
<Item />
<ItemGroup>
<Item />
</ItemGroup>
{/* etc. */}
</Menu>
Use Array.map
to iterate over items.
const ITEMS = [
{ value: 'Sunflower' },
{
legend: 'More flowers',
items: [
{ value: 'Violet' },
{ value: 'Daisy' },
]
}
]
<Menu>
{ITEMS.map(item =>
item.items ? (
<ItemGroup key={item.legend} legend={item.legend}>
{item.items.map(groupItem => (
<Item key={groupItem.value} {...groupItem} />
))}
</ItemGroup>
) : (
<Item key={item.value} {...item} />
)
)}
</Menu>
Match Item
and ItemGroup
prop signatures when using wrapper components.
const ItemComponent = forwardRef((props, ref) => (
<Item {...props} ref={ref} />
))
const ItemGroupComponent = forwardRef((props, ref) => (
<ItemGroup {...props} ref={ref} />
))
<Menu>
<ItemGroupComponent legend="Flowers">
<ItemComponent value="Violet" />
<ItemComponent value="Sunflower" />
<ItemComponent value="Daisy" />
</ItemGroupComponent>
</Menu>
Menu
Extends HTMLAttributes<HTMLUListElement>
A Menu manages placement, positioning and layout of its child Item components.
Prop name | Type | Default | Description |
---|---|---|---|
appendToNode | Element | DocumentFragment | Appends the menu to the element provided | |
button | Required | Sets the menu button label or renders a provided trigger element | |
buttonProps | IButtonProps | Provides additional props to the menu Button | |
defaultExpanded | boolean | Determines default expansion in an uncontrolled menu | |
defaultFocusedValue | string | Determines focused value on menu initialization | |
fallbackPlacements | ('end' | 'start' | 'top' | 'bottom' | 'top-start' | 'top-end' | 'bottom-start' | 'bottom-end' | 'end-top' | 'end-bottom' | 'start-top' | 'start-bottom')[] | Provides a list of acceptable fallback placements | |
focusedValue | string | null | Sets the focused value in a controlled menu | |
hasArrow | boolean | Attaches an arrow that points towards the menu trigger | |
isCompact | boolean | Applies compact styling | |
isExpanded | boolean | Sets the expansion in a controlled menu | |
maxHeight | string | '400px' | Sets the max-height of the menu |
minHeight | string | null | Sets the min-height of the menu | |
onChange | Handles menu state changes Parameters changes.type The event type that triggered the changechanges.isExpanded The updated menu expansionchanges.selectedItems The updated selection valueschanges.focusedValue The updated focused value | ||
placement | 'end' | 'start' | 'auto' | 'top' | 'bottom' | 'top-start' | 'top-end' | 'bottom-start' | 'bottom-end' | 'end-top' | 'end-bottom' | 'start-top' | 'start-bottom' | 'bottom-start' | Adjusts the placement of the menu |
selectedItems | ISelectedItem[] | Sets the selected items in a controlled menu | |
zIndex | number | 1000 | Sets the z-index of the menu |
Item
Extends LiHTMLAttributes<HTMLLIElement>
Nest an Item within a Menu component. It requires a value prop.
Prop name | Type | Default | Description |
---|---|---|---|
icon | ReactElement<any, string | JSXElementConstructor<any>> | Accepts an icon to display | |
isDisabled | boolean | Indicates that the item is not interactive | |
isSelected | boolean | Determines the initial selection state for the item | |
label | string | Sets the text label of the item (defaults to value ) | |
name | string | Associates the item in a radio item group | |
type | 'next' | 'previous' | 'add' | 'danger' | Determines the item type | |
value | string | Required | Sets the unique value that is returned upon selection |
Item.Meta
Extends HTMLAttributes<HTMLDivElement>
An ItemMeta provides additional information about a menu item. Nest it within an Item component.
ItemGroup
Extends LiHTMLAttributes<HTMLLIElement>
Provides a semantic grouping to its child Item components. Optionally configures its Items as selectable.
Prop name | Type | Default | Description |
---|---|---|---|
content | ReactNode | Renders content for the item group (defaults to legend text) | |
icon | ReactElement<any, string | JSXElementConstructor<any>> | Accepts an icon to display | |
legend | string | Sets the text label of the item group | |
type | 'checkbox' | 'radio' | Configures the selection type for items within the group |
Separator
Extends LiHTMLAttributes<HTMLLIElement>
A semantic separator between Item components.