Tonic UI
Getting StartedColor ModeColor StyleVersionsContributing
THEME
BordersBreakpointsColorsOutlinesPositionsSpacingSizesShadowsTypography
BUILDING BLOCKS
BoxControlBox
UTILITIES

Menu

An accessible dropdown menu for the common dropdown menu button design pattern. Menu uses roving tabIndex for focus management.

Import

  • Menu: The component that provides the menu functionality.
  • MenuButton: A button that composes Button, MenuToggle, and MenuToggleIcon to create a menu button.
  • MenuDivider: A divider that separates menu items.
  • MenuGroup: A component that groups menu items.
  • MenuItem: The individual menu items.
  • MenuList: A styled MenuContent that wraps the menu items.
  • MenuToggle: The toggle that opens the menu. This is usually a button or link.
  • MenuToggleIcon: An indicator that appears next to the toggle. This is usually a chevron or arrow.
  • Submenu: The component that provides the submenu functionality.
  • SubmenuList: The list of menu items that appear in the submenu.
  • SubmenuToggle: The toggle that opens the submenu when the menu item is hovered over.

Usage

Basic menu with an anchor

EDITABLE EXAMPLE

Basic menu with MenuButton

EDITABLE EXAMPLE

Cascading submenus

The following example shows a menu with cascading submenus.

EDITABLE EXAMPLE

Group

EDITABLE EXAMPLE

Divider

EDITABLE EXAMPLE

Disabled

Add disabled property on MenuItem

EDITABLE EXAMPLE

Set the minimum width of the MenuList to the width of the MenuButton

EDITABLE EXAMPLE

Menu with icon

EDITABLE EXAMPLE

Dropup Menu

Add the placement property with top-start

EDITABLE EXAMPLE

Menu with checkbox

Need to set closeOnSelect to false on Menu

EDITABLE EXAMPLE

Menu with label inline

EDITABLE EXAMPLE

Offset position

Add skidding or distance to customize menu offset position

EDITABLE EXAMPLE

Accessibility

Keyboard Interaction

KeyAction
Enter or SpaceWhen MenuButton receives focus, opens the menu and places focus on the first menu item
ArrowDownWhen MenuButton receives focus, opens the menu and moves focus to the first menu item
ArrowUpWhen MenuButton receives focus, opens the menu and moves focus to the last menu item
EscapeWhen the menu is open, closes the menu and sets focus to the MenuButton

ARIA roles

For MenuButton:

  • It has role set to button.
  • It has aria-haspopup set to either menu.
  • When the menu is displayed, MenuButton has aria-expanded set to true.
  • MenuButton has aria-controls set to the id of the MenuList.

For MenuList:

  • It contains the MenuItem has role menu.

Props

Menu

NameTypeDefaultDescription
childrenReactNodeThe children of the menu must be MenuButton or MenuList
isOpenbooleanIf true, the menu will be opened
autoSelectbooleanfalseThe menu will select the first enabled item when it opens, the trigger item must be MenuButton
closeOnBlurbooleantrueIf true, the menu will close on outside click or blur
closeOnSelectbooleantrueIf true, the menu will close on menu item select
placementPopperJS.placementbottom-startThe placement of the MenuList. One of: 'top', 'bottom', 'right', 'left', 'top-start', 'top-end', 'bottom-start', 'bottom-end', 'right-start', 'right-end', 'left-start', 'left-end'

MenuButton

NameType
childrenReactNode
onClickReact.MouseEventHandler
onKeyDownReact.KeyboardEventHandler

MenuDivider

NameTypeDefaultDescription
childrenReactNode

MenuGroup

NameTypeDefaultDescription
childrenReactNodeThe content of the menu group, should be the MenuItem component
titlestringThe title of the menu group

MenuItem

NameTypeDefaultDescription
disabledbooleanIf true, the menu item will be disabled
onClickReact.MouseEventHandlerFunction that is called on click and enter/space keypress
onKeyDownReact.KeyboardEventHanderFunction that is called on keydown

MenuList

NameTypeDefaultDescription
childrenReactNodeThe content of the MenuList, should be the MenuItem component
onClickReact.MouseEventHandlerFunction to be called when you click on the menu item
onBlurReact.FocusEventHandlerFunction to be called when you blur out of the menu list
skiddingnumber0Displaces the menu (in pixels) along the reference element. Used by popper.js
distancenumber0Displaces the menu (in pixels) away from, or toward, the reference. Used by popper.js

MenuToggle

NameTypeDefaultDescription
childrenReactNode | ({ getMenuToggleProps }) => ReactNode
disabledbooleanWhether the menu toggle is disabled.
onClickfunctionCallback when the menu toggle is clicked.
onKeyDownfunctionCallback when the user presses a key.

MenuToggleIcon

NameTypeDefaultDescription
appearbooleanfalseBy default the child component does not perform the enter transition when it first mounts, regardless of the value of in. If you want this behavior, set both appear and in to true.
childrenReactNode | (state, props) => ReactNodeA function child can be used instead of a React element. This function is called with the current transition state ('entering', 'entered', 'exiting', 'exited'), ref, style, and context specific props for a component.
disabledbooleanWhether the menu toggle icon is disabled.
easingstring | { enter?: string, exit?: string }{ enter: easing.easeInOut, exit: easing.easeInOut }The timing function that describes how intermediate values are calculated during a transition. You may specify a single timing function for all transitions, or individually with an object.
inbooleanIf true, the component will transition in.
mountOnEnterbooleanIf true, it will "lazy mount" the component on the first in={true}. After the first enter transition the component will stay mounted, even on the 'exited' state, unless you also specify unmountOnExit. By default the child component is mounted immediately along with the parent transition component.
timeoutnumber | { appear?: number, enter?: number, exit?: number }{ enter: duration.enterScreen, exit: duration.levingScreen }The duration for the transition, in milliseconds. You may specify a single timeout for all transitions, or individually with an object.
unmountOnExitbooleanIf true, it will unmount the child component when in={false} and the animation has finished. By default the child component stays mounted after it reaches the 'exited' state.

Submenu

NameTypeDefaultDescription
childrenReactNode
defaultIsOpenbooleanfalseWhether the submenu is open by default.
isOpenbooleanWhether the submenu is open.
onClosefunctionCallback when the submenu is closed.
onOpenfunctionCallback when the submenu is opened.
placementstring'right-start'The placement of the submenu. One of: 'right-start', 'right-end', 'left-start', 'left-end'

SubmenuList

NameTypeDefaultDescription
childrenReactNode

SubmenuToggle

NameTypeDefaultDescription
childrenReactNode | ({ getSubmenuToggleProps }) => ReactNode
disabledbooleanWhether the submenu toggle is disabled.