Menu API
Import
Section titled “Import”Cimpress UI exports five menu-related components:
MenuRoot: component that connects a trigger button with its associated menuMenu: component that displays a collapsible list of options and actions that users can choose from.MenuItem: component that renders a single interactive item within a menu.MenuSection: component that groups related menu items together under a section title.Divider: component that adds a horizontal line between menu items to visually separate them.
import { Divider, Menu, MenuItem, MenuRoot, MenuSection } from '@cimpress-ui/react';Accessibility notes
Section titled “Accessibility notes”Menu trigger button follows the ARIA menu button pattern. See the linked page for a list of available keyboard interactions.
Collapsible menu list follows the ARIA menu pattern. See the linked page for a list of available keyboard interactions.
Menu trigger button requires a textual label to remain accessible to assistive technologies. See our accessibility guide for more details.
Triggering a menu
Section titled “Triggering a menu”To open a menu when a trigger button is pressed, wrap both the trigger button and the menu component with a MenuRoot component. This will automatically associate the button with the menu, without any manual setup.
import { Button, Menu, MenuItem, MenuRoot } from '@cimpress-ui/react';
export default function Demo() { return ( <MenuRoot> <Button>Open menu</Button> <Menu aria-label="Menu"> <MenuItem>A menu can be opened by an automatically attached trigger button.</MenuItem> </Menu> </MenuRoot> );}Selectable items
Section titled “Selectable items”Menu items can be made selectable. Both single and multiple selection is supported. Each menu section has its own selection state.
import { Button, Menu, MenuItem, MenuRoot, MenuSection, type Selection } from '@cimpress-ui/react';import { IconChevronDown, IconTextAlignmentCenter, IconTextAlignmentLeft, IconTextAlignmentRight, IconTextBold, IconTextItalic, IconTextStrikethrough, IconTextUnderline,} from '@cimpress-ui/react/icons';import { useState } from 'react';
const TEXT_STYLE_ITEMS = [ { id: 'bold', label: 'Bold', icon: <IconTextBold /> }, { id: 'italic', label: 'Italic', icon: <IconTextItalic /> }, { id: 'underline', label: 'Underline', icon: <IconTextUnderline /> }, { id: 'strikethrough', label: 'Strikethrough', icon: <IconTextStrikethrough /> },];
const TEXT_ALIGNMENT_ITEMS = [ { id: 'left', label: 'Left', icon: <IconTextAlignmentLeft /> }, { id: 'center', label: 'Center', icon: <IconTextAlignmentCenter /> }, { id: 'right', label: 'Right', icon: <IconTextAlignmentRight /> },];
export default function Demo() { const [selectedTextStyles, setSelectedTextStyles] = useState<Selection>(() => new Set(['bold', 'italic'])); const [selectedTextAlignment, setSelectedTextAlignment] = useState<Selection>(() => new Set(['left']));
return ( <MenuRoot> <Button iconEnd={<IconChevronDown />}>Text formatting</Button> <Menu> <MenuSection title="Style" items={TEXT_STYLE_ITEMS} selectionMode="multiple" selectedKeys={selectedTextStyles} onSelectionChange={setSelectedTextStyles} > {(item) => <MenuItem icon={item.icon}>{item.label}</MenuItem>} </MenuSection> <MenuSection title="Alignment" items={TEXT_ALIGNMENT_ITEMS} selectionMode="single" selectedKeys={selectedTextAlignment} onSelectionChange={setSelectedTextAlignment} > {(item) => <MenuItem icon={item.icon}>{item.label}</MenuItem>} </MenuSection> </Menu> </MenuRoot> );}Collections
Section titled “Collections”Menu is a collection component. See our collection components guide to learn how to work with collections.
API reference
Section titled “API reference”MenuRoot
Section titled “MenuRoot”Encapsulates a menu trigger and its associated menu. The trigger can be any Cimpress UI button, and the menu will be displayed when the trigger is activated.
MenuRootProps
- ReactNode
children *
Section titled “ children * ” -
The menu trigger with its associated menu. Provide the trigger as the first child, and the menu as the second child.
- boolean
isOpen
Section titled “ isOpen ” -
Whether the overlay is open by default (controlled).
- boolean
defaultOpen
Section titled “ defaultOpen ” -
Whether the overlay is open by default (uncontrolled).
- (isOpen: boolean) => void
onOpenChange
Section titled “ onOpenChange ” -
Handler that is called when the overlay's open state changes.
Displays a collapsible list of options and actions that users can choose from.
MenuProps<T extends CollectionItem = CollectionItem>
- 'end' | 'start'
align
Section titled “ align ” - Defaults to 'start' .
The alignment of the menu overlay relative to its trigger.
- (key: Key) => void
onAction
Section titled “ onAction ” -
Handler that is called when an item is selected. When using
onAction, all items must have anidprop defined. - UIEventHandler<HTMLDivElement>
onScroll
Section titled “ onScroll ” -
Handler that is called when the list of items is scrolled.
- ReactNode | ((item: T) => ReactNode)
children
Section titled “ children ” -
The contents of the collection.
- Iterable<T, any, any>
items
Section titled “ items ” -
The items to display in the collection.
- SelectionMode
selectionMode
Section titled “ selectionMode ” -
The type of selection that is allowed in the collection.
- 'all' | (Iterable<Key, any, any>)
selectedKeys
Section titled “ selectedKeys ” -
The currently selected keys in the collection (controlled).
- (keys: Selection) => void
onSelectionChange
Section titled “ onSelectionChange ” -
Handler that is called when the selection changes.
MenuItem
Section titled “MenuItem”Renders a single list item within Menu.
MenuItemProps
- StringLikeChildren
children *
Section titled “ children * ” -
The content to display as the label.
- Key
-
The ID of the item. Has to be unique across all sections and items.
- string
description
Section titled “ description ” -
Additional description for the menu item.
- ReactNode
-
An icon representing the menu item.
- string
-
A URL to link to.
- undefined
routerOptions
Section titled “ routerOptions ” -
Options for the configured client side router.
- string
hrefLang
Section titled “ hrefLang ” -
Hints at the human language of the linked URL. SeeMDN.
- HTMLAttributeAnchorTarget
target
Section titled “ target ” -
The target window for the link. See MDN.
- string
-
The relationship between the linked resource and the current page. See MDN.
- string | boolean
download
Section titled “ download ” -
Causes the browser to download the linked URL. A string may be provided to suggest a file name. See MDN.
- string
-
A space-separated list of URLs to ping when the link is followed. See MDN.
- HTMLAttributeReferrerPolicy
referrerPolicy
Section titled “ referrerPolicy ” -
How much of the referrer to send when following the link. See MDN.
- (e: HoverEvent) => void
onHoverStart
Section titled “ onHoverStart ” -
Handler that is called when a hover interaction starts.
- (e: HoverEvent) => void
onHoverEnd
Section titled “ onHoverEnd ” -
Handler that is called when a hover interaction ends.
- boolean
isDisabled
Section titled “ isDisabled ” -
Whether the item is disabled.
- () => void
onAction
Section titled “ onAction ” -
Handler that is called when the item is selected.
MenuSection
Section titled “MenuSection”Groups list items within Menu into a section.
MenuSectionProps<T extends CollectionItem = CollectionItem>
- string
title *
Section titled “ title * ” -
The content to display as the section title.
- Key
-
The ID of the section. Has to be unique across all sections and items.
- ReactNode | ((item: T) => ReactNode)
children
Section titled “ children ” -
The contents of the collection.
- Iterable<T, any, any>
items
Section titled “ items ” -
The items to display in the collection.
- SelectionMode
selectionMode
Section titled “ selectionMode ” -
The type of selection that is allowed in the collection.
- 'all' | (Iterable<Key, any, any>)
selectedKeys
Section titled “ selectedKeys ” -
The currently selected keys in the collection (controlled).
- (keys: Selection) => void
onSelectionChange
Section titled “ onSelectionChange ” -
Handler that is called when the selection changes.
Divider
Section titled “Divider”Renders a divider between adjacent content.
- Ref<HTMLDivElement>
-
The
reftype for this component.
DividerProps
- 'vertical' | 'horizontal'
orientation
Section titled “ orientation ” - Defaults to 'horizontal' .
The orientation of the divider (horizontal or vertical).
- string
-
The element's unique identifier. See MDN.
- boolean
data-cim-style-root
Section titled “ data-cim-style-root ” -
Use this attribute to "claim" the component tree for exclusive Cimpress UI usage.
- string
UNSAFE_className
Section titled “ UNSAFE_className ” -
Sets the CSS className for the element. Only use as a last resort. Use style props instead.
See styling guide.
- CSSProperties
UNSAFE_style
Section titled “ UNSAFE_style ” -
Sets the CSS style for the element. Only use as a last resort. Use style props instead.
See styling guide.
StyleProps
- Responsive<Spacing | "auto">
margin
Section titled “ margin ” -
The amount of margin applied to all edges of this component.
- Responsive<Spacing | "auto">
marginX
Section titled “ marginX ” -
The amount of margin applied to the left and right edges of this component. Takes priority over
margin. - Responsive<Spacing | "auto">
marginY
Section titled “ marginY ” -
The amount of margin applied to the top and bottom edges of this component. Takes priority over
margin. - Responsive<Spacing | "auto">
marginTop
Section titled “ marginTop ” -
The amount of margin applied to the top edge of this component. Takes priority over
marginYandmargin. - Responsive<Spacing | "auto">
marginRight
Section titled “ marginRight ” -
The amount of margin applied to the right edge of this component. Takes priority over
marginXandmargin. - Responsive<Spacing | "auto">
marginBottom
Section titled “ marginBottom ” -
The amount of margin applied to the bottom edge of this component. Takes priority over
marginYandmargin. - Responsive<Spacing | "auto">
marginLeft
Section titled “ marginLeft ” -
The amount of margin applied to the left edge of this component. Takes priority over
marginXandmargin.