Menus within react-md can be created by using the following components:
DropdownMenu - a nice default implementation that renders a <Button> and a
<Menu> together and handles the menu's visibilityMenuItem - one of the actions inside of the <Menu>MenuItemLink - a <Link> specifically to be used inside of a <Menu>MenuItemSeparator - a <Divider> specifically to be used inside of a
<Menu>MenuItemGroup - Render a <ul role="group" aria-label="Accessible Label">
when rendering a group of related menu items together. This is generally used
with the MenuItemRadio component.MenuItemRadio - Render a <Radio> as a MenuItem. Requires the @react-md/form
package.MenuItemCheckbox - Render a <Checkbox> as a MenuItem. Requires the @react-md/form
package.MenuItemSwitch - Render a <Switch> as a MenuItem. Requires the @react-md/form
package.MenuItemFileInput - Render a <FileInput> as a MenuItem. Requires the
@react-md/form package.MenuItemTextField - Render a <TextField> as a MenuItem. Requires the
@react-md/form package.In addition, menus have some built-in keyboard behavior:
ArrowDown/ArrowUp keys will focus the next/previous menu itemHome/End keys will focus the first/last menu item.Escape/Tab keys will close the menuThe example below will showcase a simple DropdownMenu with MenuItems and a
MenuItemSeparator.
Starting with react-md@v5.0.0, menus can be conditionally rendered as a @react-md/sheet
using a new renderAsSheet configuration. This is generally helpful for small
devices like phones since there is a lack of screen space. This functionality
can be enabled by either:
Configuration component from the @react-md/layout package to have
menuConfiguration: { renderAsSheet: "phone" } or { renderAsSheet: true }DropdownMenu or Menu component in a
MenuConfigurationProvider and setting renderAsSheet={true} or
renderAsSheet="phone"renderAsSheet={true} or renderAsSheet="phone" directly on the
DropdownMenu or Menu componentSetting
renderAsSheet="phone"will only render menus as sheets when theAppSizeisphone.
You can configure additional sheet behavior in those three places like:
sheetHeader - Normally an AppBar or a DialogHeader that would appear
above the list of MenuItemsheetFooter - Normally an AppBar or a DialogFooter that would appear
below the list of MenuItemsheetPosition - The sheet's position within the viewport. This defaults to
"below"sheetVerticalSize - The sheet's vertical size. This defaults to "touch"The example below will allow you to configure the renderAsSheet behavior so
you can play around with the default implementation. In addition, this example
will add a custom header and footer to the sheet that can also close the sheet
using the useMenuVisibility hook.