Menu

Responsable :
NikitaCG
GitHub
size
groupLabel
itemChildren
itemTheme
itemDisabled
itemSelected
itemHref

Menu

The Menu component displays list of choices in the Popup when interacting with its trigger, typically a button.

There are a collection of related components:

  • Menu - the container of the menu
  • MenuItem - an option to select from the menu
  • MenuDivider - a divider for grouping options
  • MenuTrigger - a built-in default trigger, Button with ellipsis icon

Basic usage

import {Menu, MenuItem, MenuDivider, MenuTrigger} from '@gravity-ui/uikit';

function BasicMenu() {
  return (
    <Menu trigger={<MenuTrigger />}>
      <MenuItem>Copy</MenuItem>
      <MenuItem>Move</MenuItem>
      <MenuDivider />
      <MenuItem theme="danger">Delete</MenuItem>
    </Menu>
  );
}

Custom trigger

You can render any kind of component as a trigger if it accepts basic HTMLAttributes as props and a ref for HTMLElement. For more complex components you can use a function variant of trigger prop that have triggerProps as the first argument and triggerRef as the second argument which you should pass to your component.

Context menu

To implement context menu pattern you should use "virtual element" as a trigger:

import {Menu, MenuItem, MenuDivider, MenuTrigger} from '@gravity-ui/uikit';

function ContextMenu() {
  const [trigger, setTrigger] = React.useState(null);

  React.useEffect(() => {
    const handleContextMenu = (event) => {
      event.preventDefault();
      setTrigger({
        getBoundingClientRect() {
          return {
            width: 0,
            height: 0,
            x: event.clientX,
            y: event.clientY,
            top: event.clientY,
            right: event.clientX,
            bottom: event.clientY,
            left: event.clientX,
          };
        },
      });
    };
    document.addEventListener('contextmenu', handleContextMenu);
    return () => document.removeEventListener('contextmenu', handleContextMenu);
  }, []);

  return (
    <Menu trigger={trigger}>
      <MenuItem>Copy</MenuItem>
      <MenuItem>Move</MenuItem>
      <MenuDivider />
      <MenuItem theme="danger">Delete</MenuItem>
    </Menu>
  );
}

Inline mode

By default Menu is rendered inside the Popup. But you can render it inline using inline prop in your own container.

Properties

NameDescriptionTypeDefault
classNameHTML class attributestring
styleHTML style attributeReact.CSSProperties
aria-labelAccessible name for the menustring
aria-labelledbyID of an element that names the menustring
qaTest ID (data-qa attribute)string
openControlled state for openboolean
defaultOpenUncontrolled state for openboolean
childrenMenu related components (items, dividers, etc.)React.ReactNode
disabledDisabled statebooleanfalse
inlineRenders the menu inlinebooleanfalse
triggerTrigger element which opens the menuReact.ReactElement (triggerProps, triggerRef) => React.ReactElement VirtualElement null
placementPopup placementPopupPlacement"bottom-start"
onOpenChangeCallback for open state change(open: boolean, event: Event, reason: OpenChangeReason) => void
sizeThe Menu size"s" "m" "l" "xl""m"

When an Icon without an explicit size is passed to the icon property, its size is selected automatically according to the Menu size. Set the Icon size, width, or height explicitly to override it.

MenuItem accepts any valid button or a element props in addition to these:

NameDescriptionTypeDefault
qaTest ID (data-qa attribute)string
themeThe MenuItem theme"normal" "info" "success" "warning" "danger" "utility""normal"
selectedSelected state (menuitemcheckbox)boolean
disabledDisabled statebooleanfalse
iconRender slot for an iconReact.ReactElement
arrowRender slot for a nested menu arrowReact.ReactElement
childrenContentReact.ReactNode
componentCustom root componentReact.ElementType

MenuTrigger accepts any Button component props in addition to these:

NameDescriptionTypeDefault
iconType of icon to display"horizontal" "vertical""horizontal"