The padding required to prevent the arrow from reaching the very edge of the popper.
Menu
An accessible dropdown menu for the common dropdown menu button design pattern.
Menu uses roving tabIndex for focus management.
Import#
Chakra UI exports 8 components for rendering menus:
Menu: The wrapper component provides context, state, and focus management.MenuList: The wrapper for the menu items. Must be a direct child ofMenu.MenuButton: The trigger for the menu list. Must be a direct child ofMenu.MenuItem: The trigger that handles menu selection. Must be a direct child of aMenuList.MenuGroup: A wrapper to group related menu items.MenuDivider: A visual separator for menu items and groups.MenuOptionGroup: A wrapper for checkable menu items (radio and checkbox).MenuItemOption: The checkable menu item, to be used withMenuOptionGroup.
import {Menu,MenuButton,MenuList,MenuItem,MenuItemOption,MenuGroup,MenuOptionGroup,MenuIcon,MenuCommand,MenuDivider,} from "@chakra-ui/react"
Usage#
Accessing the internal state#
To access the internal state of the Menu, use a function as children
(commonly known as a render prop). You'll get access to the internal state
isOpen and method onClose.
Customizing the button#
The default MenuButton can be styled using the usual styled-system props, but
it starts off plainly styled.
Using the as prop of the MenuButton, you can render a custom component
instead of the default MenuButton. For instance, you can use Chakra's Button
component, or your own custom component.
Custom components must take a
refprop which is assigned to the React component that triggers the menu opening. This is so that theMenuListpopover can be positioned correctly. Without this, theMenuListwill render in an undefined position.
Letter Navigation#
When focus is on the MenuButton or within the MenuList and you type a letter
key, a search begins. Focus will move to the first MenuItem that starts with
the letter you typed.
Open the menu, try and type any letter, (say "S") to see the focus movement.
Just another example#
Adding icons and commands#
You can add icon to each MenuItem by passing the icon prop. To add a
commands (or hotkeys) to menu items, you can use the command prop.
Lazily mounting MenuItem#
By default, the Menu component renders all children of MenuList to the DOM,
meaning that invisible menu items are still rendered but are hidden by styles.
If you want to defer rendering of each children of MenuList until that menu is
open, you can use the isLazy prop. This is useful if your Menu needs to be
extra performant, or make network calls on mount that should only happen when
the component is displayed.
Rendering menu in a portal#
To render menus in a portal, import the Portal component and wrap the
MenuList within the Portal.
MenuGroup#
To group related MenuItems, use the MenuGroup component and pass it a
title for the group name.
Menu option groups#
You can compose a menu for table headers to help with sorting and filtering
options. Use the MenuOptionGroup and MenuItemOption components.
Accessibility#
Keyboard Interaction#
| Key | Action |
|---|---|
Enter or Space | When MenuButton receives focus, opens the menu and places focus on the first menu item. |
ArrowDown | When MenuButton receives focus, opens the menu and moves focus to the first menu item. |
ArrowUp | When MenuButton receives focus, opens the menu and moves focus to the last menu item. |
Escape | When the menu is open, closes the menu and sets focus to the MenuButton. |
Tab | no effect |
Home | When the menu is open, moves focus to the first item. |
End | When the menu is open, moves focus to the last item. |
A-Z or a-z | When the menu is open, moves focus to the next menu item with a label that starts with the typed character if such an menu item exists. |
ARIA roles#
For MenuButton:
roleis set tobutton.aria-haspopupis set tomenu.- When the menu is displayed,
aria-expandedis set totrue. aria-controlsis set to theidof theMenuList.
For MenuList:
roleis set tomenu.aria-orientationis set tovertical.
For MenuItem:
roleis set tomenuitem.- Gets one of these roles
menuitem/menuitemradio/menuitemcheckbox.
Props#
Menu Props#
arrowPadding
arrowPaddingnumber8autoSelect
autoSelectIf true, the first enabled menu item will receive focus and be selected
when the menu opens.
booleantrueboundary
boundaryThe boundary area for the popper. Used within the preventOverflow modifier
HTMLElement | "clippingParents" | "scrollParent""clippingParents"closeOnBlur
closeOnBlurIf true, the menu will close when you click outside
the menu list
booleantruecloseOnSelect
closeOnSelectIf true, the menu will close when a menu item is
clicked
booleantruecolorScheme
colorSchemeColor Schemes for Menu are not implemented in the default theme. You can extend the theme to implement them.
"whiteAlpha" | "blackAlpha" | "gray" | "red" | "orange" | "yellow" | "green" | "teal" | "blue" | "cyan" | "purple" | "pink" | "linkedin" | "facebook" | "messenger" | "whatsapp" | "twitter" | "telegram"computePositionOnMount
computePositionOnMountIf true, the menu will be positioned when it mounts
(even if it's not open).
Note 🚨: We don't recommend using this in a menu/popover intensive UI or page
as it might affect scrolling performance.
booleandefaultIsOpen
defaultIsOpenbooleanenabled
enabledWhether the popper.js should be enabled
booleaneventListeners
eventListenersIf provided, determines whether the popper will reposition itself on scroll
and resize of the window.
boolean | { scroll?: boolean; resize?: boolean | undefined; } | undefinedflip
flipIf true, the popper will change its placement and flip when it's
about to overflow its boundary area.
booleantruegutter
gutterThe distance or margin between the reference and popper.
It is used internally to create an offset modifier.
NB: If you define offset prop, it'll override the gutter.
number8id
idstringisLazy
isLazyPerformance 🚀:
If true, the MenuItem rendering will be deferred
until the menu is open.
booleanisOpen
isOpenbooleanlazyBehavior
lazyBehaviorPerformance 🚀: The lazy behavior of menu's content when not visible. Only works when `isLazy={true}` - "unmount": The menu's content is always unmounted when not open. - "keepMounted": The menu's content initially unmounted, but stays mounted when menu is open.
"unmount" | "keepMounted""unmount"matchWidth
matchWidthIf true, the popper will match the width of the reference at all times.
It's useful for autocomplete, `date-picker` and select patterns.
booleanmodifiers
modifiersArray of popper.js modifiers. Check the docs to see the list of possible modifiers you can pass. @see Docs https://popper.js.org/docs/v2/modifiers/
Partial<Modifier<string, any>>[]offset
offsetThe main and cross-axis offset to displace popper element from its reference element.
[crossAxis: number, mainAxis: number]onClose
onClose(() => void)onOpen
onOpen(() => void)placement
placementThe placement of the popper relative to its reference.
"bottom" | "left" | "right" | "top" | "auto" | "auto-start" | "auto-end" | "top-start" | "top-end" | "bottom-start" | "bottom-end" | "right-start" | "right-end" | "left-start" | "left-end""bottom"preventOverflow
preventOverflowIf true, will prevent the popper from being cut off and ensure
it's visible within the boundary area.
booleantruesize
sizeSizes for Menu are not implemented in the default theme. You can extend the theme to implement them.
stringstrategy
strategyThe CSS positioning strategy to use.
"fixed" | "absolute""absolute"variant
variantVariants for Menu are not implemented in the default theme. You can extend the theme to implement them.
stringMenuButton Props#
MenuButton composes Box so you can pass all Box props to
change its style.
MenuList Props#
MenuList composes Box so you can pass all Box props to
change its style.
MenuItem Props#
closeOnSelect
closeOnSelectOverrides the parent menu's closeOnSelect prop.
booleancommand
commandRight-aligned label text content, useful for displaying hotkeys.
stringcommandSpacing
commandSpacingThe spacing between the command and menu item's label.
SystemProps["ml"]icon
iconThe icon to render before the menu item's label.
React.ReactElementiconSpacing
iconSpacingThe spacing between the icon and menu item's label.
SystemProps["mr"]isDisabled
isDisabledIf true, the menuitem will be disabled
booleanisFocusable
isFocusableIf true and the menuitem is disabled, it'll
remain keyboard-focusable
booleanMenuGroup Props#
MenuGroup composes Box so you can pass all Box props to
change its style.
MenuOptionGroup Props#
defaultValue
defaultValuestring | string[]onChange
onChange((value: string | string[]) => void)type
type"checkbox" | "radio"value
valuestring | string[]MenuItemOption Props#
MenuItemOption composes Box so you can pass all box props
in addition to these:
closeOnSelect
closeOnSelectOverrides the parent menu's closeOnSelect prop.
booleancommand
commandRight-aligned label text content, useful for displaying hotkeys.
stringcommandSpacing
commandSpacingThe spacing between the command and menu item's label.
SystemProps["ml"]icon
iconThe icon to render before the menu item's label.
React.ReactElementiconSpacing
iconSpacingThe spacing between the icon and menu item's label.
SystemProps["mr"]isChecked
isCheckedbooleanisDisabled
isDisabledIf true, the menuitem will be disabled
booleanisFocusable
isFocusableIf true and the menuitem is disabled, it'll
remain keyboard-focusable
booleantype
type"checkbox" | "radio"value
valuestring