Menu

Menus in Space Web provide an opportunity to nest various hierarchical navigation, features, or settings. Each menu item has an option to include an icon by default, but this can be removed. The menus themselves share the functionality and display of select menus.

When to use

  • When you want to display a navigational list.
  • When you want to display a list of actions.

Keybindings

  • KeyDown / KeyUp navigates down / up in the menu list, highlighting items as needed.
  • ArrowRight / Space Opens nested menu, if it exists
  • ArrowLeft / Esc Closes nested menu and shifts focus to parent menu
  • Enter / Space selects the currently highlighted item. If no item is highlighted, select the first item in the menu list.

Accessibility

  • role="listbox" is provided to indicate ul as a composite widget.
  • currently focused element of ul is refernced by aria-activedescendant attribute, which value is the id of selected li.

Examples

Menu will be focused(keyboard navigation will start working) as soon as it mounts. ListItem is selected if selected prop is true. Navigation will start with first selected item highlighted.

with disableNavigationOnMount set true, navigation will be off initially. It can be enabled by hovering over Menu, or when Menu is focused.

Virtualization

Setting virtual property for the Menu can enable virtualization. maxHeight and overflow must be provided to menu or its container

Check this example in list if overflow cannot be handled on Menu component.

maxHeight can be provided to Menu to make it scrollable.

Also, notice when triggerType is hover in StatefulPopover, focusWhenNavigationEnabled should be marked as false, otherwise as soon as user navigates anchor-button with keyboard, Button will be blurred when Menu mounts which will close popover-content(Menu) immediately.

Reducer can be used to alter the state of Menu. Here, reducer is used to set initially highlighted item.

Action Menu

Configurable action menu can be created using Menu.

Nested Menu with Popover

To use Nested Menu, Wrap Menu with NestedMenu Context and pass getChildMenu prop to MenuItem.

Ref can be used to turn on/off navigation of the Menu.

API

NameTypeDefaultDescription
$showFocusOutline
boolean
Adds outline on focus to the list
captureScroll
boolean
falseWhen the user reaches top/bottom of the menu, prevents scroll to the body/scroll-parent
children
react node
Menu items
className
string
Classes to apply on List Root
disableAutoHighlight
boolean
falseIf true, Menu will not highlight item any item initially. By default Menu highlights selected item/1st highlightable item on mount.
disableNavigationOnMount
boolean
falseBy default keyboard navigation starts as soon as Menu mounts. Flag to opt-out of this behaviour.
estimateItemSize
number
34Default size for items of virtual list
focusWhenNavigationEnabled
boolean
trueWhether Items container be focused in DOM, when keyboard navigation is enabled.In Select, you would probably want it to be false, so that focus remains on Input of Selectbox.
overrides
custom
Lets you customize all aspects of the component.
overscanCount
number
5Set the number of over scanned children of the virtualized menu
resetMenuHighlightOnMouseLeave
boolean
falseIf true, Any highlighted MenuItem will unhighlight as soon as mouse/focus leaves Menu.
selectOnTab
boolean
falseWhen enabled allows to click on highlighted item on Tab key press.
virtual
boolean
falseAdd virtualization to menu

You can check the implementation of the component for more details.

You can import this module like so:

import {Menu} from 'spaceweb/menu'

It exports the following components or utility functions:

  • Menu
  • MenuItem
  • NestedMenuProvider