Menu
Menu
is a component that displays a common dropdown menu.
Import
import {Menu,MenuButton,MenuList,MenuItem,MenuGroup,MenuOptionItem,MenuOptionGroup,MenuDivider,} from "@yamada-ui/react"
Menu
: A wrapper component that controls the state of the menu.MenuButton
: A component that toggles the menu open or closed.MenuList
: A wrapper component that controls the items in the menu.MenuItem
: A component for the items in the menu.MenuGroup
: A wrapper component that groups items in the menu.MenuOptionItem
: A component for selectable menu items (radio and checkbox).MenuOptionGroup
: A wrapper component that groups selectable menu items (radio and checkbox).MenuDivider
: A component that separates items in the menu.
Usage
Editable example
<Menu> <MenuButton as={Button} rightIcon={<ChevronDown fontSize="xl" />}> Menu </MenuButton> <MenuList> <MenuItem>Set status</MenuItem> <MenuItem>Edit Profile</MenuItem> <MenuItem>Preferences</MenuItem> </MenuList> </Menu>
Accessing Internal State
children
is provided with methods such as isOpen
and onClose
. You can use these to access the internal state.
Editable example
<Menu> {({ isOpen }) => ( <> <MenuButton as={Button} rightIcon={<ChevronDown fontSize="xl" />}> {isOpen ? "Close" : "Open"} Menu </MenuButton> <MenuList> <MenuItem>Set status</MenuItem> <MenuItem>Edit Profile</MenuItem> <MenuItem>Preferences</MenuItem> </MenuList> </> )} </Menu>
Displaying Commands in Items
To display commands in items, set a string to command
.
Editable example
<Menu> <MenuButton as={IconButton} icon={<MenuIcon fontSize="2xl" />} variant="outline" /> <MenuList> <MenuItem command="⌘T">New Tab</MenuItem> <MenuItem command="⌘N">New Window</MenuItem> <MenuItem command="⌘O">Open File</MenuItem> </MenuList> </Menu>
Displaying Icons in Items
To display icons in items, set a ReactElement
to icon
.
Editable example
<Menu> <MenuButton as={IconButton} icon={<MenuIcon fontSize="2xl" />} variant="outline" /> <MenuList> <MenuItem icon={<Plus fontSize="lg" />} command="⌘T"> New Tab </MenuItem> <MenuItem icon={<ExternalLink fontSize="lg" />} command="⌘N"> New Window </MenuItem> <MenuItem icon={<FilePenLine fontSize="lg" />} command="⌘O"> Open File </MenuItem> </MenuList> </Menu>
Focusing on Items
When opening the menu, if you want to focus on a specific item, set the target ref
to initialFocusRef
.
Editable example
const ref = useRef<HTMLDivElement>(null) return ( <Menu initialFocusRef={ref}> <MenuButton as={Button} rightIcon={<ChevronDown fontSize="xl" />}> Menu </MenuButton> <MenuList> <MenuItem>Set status</MenuItem> <MenuItem ref={ref}>Edit Profile</MenuItem> <MenuItem>Preferences</MenuItem> </MenuList> </Menu> )
Changing the Display Position
To change the display position, set placement
to top
, left-start
, etc. By default, bottom-start
is set.
Editable example
<Menu placement="top"> <MenuButton as={Button} rightIcon={<ChevronDown fontSize="xl" />}> Menu </MenuButton> <MenuList> <MenuItem>Set status</MenuItem> <MenuItem>Edit Profile</MenuItem> <MenuItem>Preferences</MenuItem> </MenuList> </Menu>
Changing the Animation
To change the show/hide animation, set animation
to top
, left
, etc. By default, scale
is set.
Editable example
<Menu animation="top"> <MenuButton as={Button} rightIcon={<ChevronDown fontSize="xl" />}> Menu </MenuButton> <MenuList> <MenuItem>Set status</MenuItem> <MenuItem>Edit Profile</MenuItem> <MenuItem>Preferences</MenuItem> </MenuList> </Menu>
Changing the Offset
Depending on the size of the element or the situation, you may want to change the position of the menu. In that case, adjust the position with gutter
or offset
.
gutter
allows you to set the difference with simple elements, and offset
allows you to set [horizontal, vertical]
.
Editable example
<Wrap gap="md"> <Menu gutter={32}> <MenuButton as={Button} rightIcon={<ChevronDown fontSize="xl" />}> Menu </MenuButton> <MenuList> <MenuItem>Set status</MenuItem> <MenuItem>Edit Profile</MenuItem> <MenuItem>Preferences</MenuItem> </MenuList> </Menu> <Menu offset={[16, 16]}> <MenuButton as={Button} rightIcon={<ChevronDown fontSize="xl" />}> Menu </MenuButton> <MenuList> <MenuItem>Set status</MenuItem> <MenuItem>Edit Profile</MenuItem> <MenuItem>Preferences</MenuItem> </MenuList> </Menu> </Wrap>
Changing the Duration
To change the duration, set a numerical value (seconds) to duration
.
Editable example
<Menu duration={0.4}> <MenuButton as={Button} rightIcon={<ChevronDown fontSize="xl" />}> Menu </MenuButton> <MenuList> <MenuItem>Set status</MenuItem> <MenuItem>Edit Profile</MenuItem> <MenuItem>Preferences</MenuItem> </MenuList> </Menu>
Adding Dividers Between Items
By setting MenuDivider
between MenuItem
s, you can add dividers.
Editable example
<Menu> <MenuButton as={Button} rightIcon={<ChevronDown fontSize="xl" />}> Menu </MenuButton> <MenuList> <MenuItem>Set status</MenuItem> <MenuItem>Edit Profile</MenuItem> <MenuItem>Preferences</MenuItem> <MenuDivider /> <MenuItem>Sign out</MenuItem> </MenuList> </Menu>
Grouping Items
To group items together, use MenuGroup
.
Editable example
<Menu> <MenuButton as={Button} rightIcon={<ChevronDown fontSize="xl" />}> Menu </MenuButton> <MenuList> <MenuGroup label="account"> <MenuItem>Set status</MenuItem> <MenuItem>Edit Profile</MenuItem> <MenuItem>Preferences</MenuItem> </MenuGroup> <MenuDivider /> <MenuGroup label="action"> <MenuItem>Sign out</MenuItem> </MenuGroup> </MenuList> </Menu>
Selectable Items
You can set selectable items like radios or checkboxes by using MenuOptionGroup
and MenuOptionItem
.
Editable example
<Menu> <MenuButton as={Button} rightIcon={<ChevronDown fontSize="xl" />}> Menu </MenuButton> <MenuList> <MenuOptionGroup label="order" type="radio"> <MenuOptionItem value="asc">Ascending</MenuOptionItem> <MenuOptionItem value="desc">Descending</MenuOptionItem> </MenuOptionGroup> <MenuDivider /> <MenuOptionGroup label="display" type="checkbox"> <MenuOptionItem value="gender">gender</MenuOptionItem> <MenuOptionItem value="email">email</MenuOptionItem> <MenuOptionItem value="phone">phone</MenuOptionItem> </MenuOptionGroup> </MenuList> </Menu>
Nested Menus
To construct nested menus, insert a Menu
within a MenuItem
.
Editable example
<Menu> <MenuButton as={Button} rightIcon={<ChevronDown fontSize="xl" />}> Menu </MenuButton> <MenuList> <MenuItem> <Menu> <MenuItemButton>Settings</MenuItemButton> <MenuList> <MenuItem>Extensions</MenuItem> <MenuItem> <Menu> <MenuItemButton>Theme</MenuItemButton> <MenuList> <MenuItem>Color Theme</MenuItem> <MenuItem>File Icon Theme</MenuItem> <MenuItem>Product Icon Theme</MenuItem> </MenuList> </Menu> </MenuItem> <MenuItem>User Tasks</MenuItem> </MenuList> </Menu> </MenuItem> <MenuItem>Edit Profile</MenuItem> <MenuItem>Preferences</MenuItem> </MenuList> </Menu>
Keeping the Menu Open When an Item is Selected
If you do not want the menu to close when an item is selected, set closeOnSelect
to false
.
Editable example
<Menu closeOnSelect={false}> <MenuButton as={Button} rightIcon={<ChevronDown fontSize="xl" />}> Menu </MenuButton> <MenuList> <MenuItem>Set status</MenuItem> <MenuItem closeOnSelect={true}>Edit Profile</MenuItem> <MenuItem>Preferences</MenuItem> </MenuList> </Menu>
Keeping the Menu Open When Focus is Lost
If you do not want the menu to close when focus is lost, set closeOnSelect
to false
.
Editable example
<Menu closeOnBlur={false}> <MenuButton as={Button} rightIcon={<ChevronDown fontSize="xl" />}> Menu </MenuButton> <MenuList> <MenuItem>Set status</MenuItem> <MenuItem>Edit Profile</MenuItem> <MenuItem>Preferences</MenuItem> </MenuList> </Menu>
Disabling Items
To disable items, set isDisabled
to true
.
Editable example
<Menu> <MenuButton as={Button} rightIcon={<ChevronDown fontSize="xl" />}> Menu </MenuButton> <MenuList> <MenuItem>Set status</MenuItem> <MenuItem isDisabled>Edit Profile</MenuItem> <MenuItem>Preferences</MenuItem> </MenuList> </Menu>
Allowing Focus on Disabled Items
To allow focus on disabled items, set isFocusable
to true
.
Editable example
<Menu> <MenuButton as={Button} rightIcon={<ChevronDown fontSize="xl" />}> Menu </MenuButton> <MenuList> <MenuItem>Set status</MenuItem> <MenuItem isDisabled isFocusable> Edit Profile </MenuItem> <MenuItem>Preferences</MenuItem> </MenuList> </Menu>
Lazy Rendering
By default, it is rendered in the DOM regardless of being active. In other words, it is rendered but hidden by style.
If you want to delay rendering until it becomes active, set isLazy
to true
.
Editable example
<Menu isLazy> <MenuButton as={Button} rightIcon={<ChevronDown fontSize="xl" />}> Menu </MenuButton> <MenuList> <MenuItem>Set status</MenuItem> <MenuItem>Edit Profile</MenuItem> <MenuItem>Preferences</MenuItem> </MenuList> </Menu>
Controlling
Editable example
const { isOpen, onOpen, onClose } = useDisclosure() return ( <Menu isOpen={isOpen} onOpen={onOpen} onClose={onClose}> <MenuButton as={Button} rightIcon={<ChevronDown fontSize="xl" />}> Menu </MenuButton> <MenuList> <MenuItem>Set status</MenuItem> <MenuItem>Edit Profile</MenuItem> <MenuItem>Preferences</MenuItem> </MenuList> </Menu> )
Edit this page on GitHub