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 existsArrowLeft
/Esc
Closes nested menu and shifts focus to parent menuEnter
/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 indicateul
as a composite widget.- currently focused element of
ul
is refernced byaria-activedescendant
attribute, which value is the id of selectedli
.
Examples
Menu Basic
- Item One
- Item Two
- Item Five
- Item Six
- Item Seven
- Item Eight
- Item Nine
- Item Ten
- Item Eleven
- Item Twelve
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.
Menu disableNavigationOnMount
- Item One
- Item Two
- Item Five
- Item Six
- Item Seven
- Item Eight
- Item Nine
- Item Ten
- Item Eleven
- Item Twelve
with disableNavigationOnMount
set true, navigation will be off initially. It can be enabled by hovering over Menu, or when Menu is focused.
Virtualization
- item number: 1
- item number: 2
- item number: 3
- item number: 4
- item number: 5
- item number: 6
- item number: 7
- item number: 8
- item number: 9
- item number: 10
- item number: 11
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.
Configurable action menu can be created using Menu.
To use Nested Menu, Wrap Menu with NestedMenu
Context and pass getChildMenu prop to MenuItem.
Navigation control with ref
- Item One
- Item Two
- Item Five
- Item Six
- Item Seven
- Item Eight
- Item Nine
- Item Ten
- Item Eleven
- Item Twelve
Ref can be used to turn on/off navigation of the Menu.
API
Menu props
Name | Type | Default | Description |
---|---|---|---|
$showFocusOutline | boolean | Adds outline on focus to the list | |
captureScroll | boolean | false | When 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 | false | If true, Menu will not highlight item any item initially. By default Menu highlights selected item/1st highlightable item on mount. |
disableNavigationOnMount | boolean | false | By default keyboard navigation starts as soon as Menu mounts. Flag to opt-out of this behaviour. |
estimateItemSize | number | 34 | Default size for items of virtual list |
focusWhenNavigationEnabled | boolean | true | Whether 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 | 5 | Set the number of over scanned children of the virtualized menu |
resetMenuHighlightOnMouseLeave | boolean | false | If true, Any highlighted MenuItem will unhighlight as soon as mouse/focus leaves Menu. |
selectOnTab | boolean | false | When enabled allows to click on highlighted item on Tab key press. |
virtual | boolean | false | Add virtualization to menu |
You can check the implementation of the component for more details.
Menu exports
You can import this module like so:
import {Menu} from 'spaceweb/menu'
It exports the following components or utility functions:
- Menu
- MenuItem
- NestedMenuProvider