ContextMenu

Das ContextMenu öffnet ein Popover mit Aktionen oder Links, meist über einen Button.
import {
  Button,
  ContextMenu,
  ContextMenuTrigger,
  MenuItem,
} from "@mittwald/flow-react-components";

<ContextMenuTrigger>
  <Button>Trigger</Button>
  <ContextMenu
    onAction={(id) => {
      alert(id);
    }}
  >
    <MenuItem id="1">Item 1</MenuItem>
    <MenuItem id="2">Item 2</MenuItem>
    <MenuItem id="3">Item 3</MenuItem>
  </ContextMenu>
</ContextMenuTrigger>

Best Practices

  • Sammle Aktionen in einem ContextMenu, um die Oberfläche aufgeräumt zu halten.
  • Zeige nur kontextrelevante Inhalte.
  • Benenne die Aktionen klar und verständlich.
  • Stelle häufig genutzte Aktionen nach oben, damit sie leicht erreichbar sind.
  • Gruppiere bei Bedarf mit einer Section oder einem Separator. Das erhöht die Übersichtlichkeit.

Einfach-Auswahl

Durch selectionMode="single" verhalten sich die Menüeinträge des ContextMenu wie eine RadioGroup, sodass nur ein Element ausgewählt werden kann.


Mehrfach-Auswahl

Durch selectionMode="multiple" verhalten sich die Menüeinträge des ContextMenu wie Checkboxen, sodass mehrere Optionen ausgewählt werden können.


Mit Switch

Ein Switch im ContextMenu ermöglicht es, Aktionen direkt zu aktivieren oder deaktivieren.


Mit Icon

Verwende Icons innerhalb vom <MenuItem />, um Aktionen schneller verständlich zu machen oder bekannte Funktionen klarer zu vermitteln.


Mit Separator

Ein Separator gruppiert logisch zusammengehörige Aktionen.


Mit ContextMenuSections

Eine ContextMenuSection sorgt im ContextMenu für klare Struktur, indem sie eine Heading und definierte Abstände verwendet.


Mit fester Breite

Lege eine Breite für dein ContextMenu fest, um beispielsweise zu verhindern, dass das ContextMenu zu breit wird oder die Breite des ContextMenus beim Nachladen von Daten springt.


Mit Selection Mode

Setze selectionMode direkt auf einer ContextMenuSection, um mehrere Auswahlgruppen mit jeweils eigenem Auswahlverhalten zu kombinieren.


Mit Avatar

Verwende das ContextMenu als Profilmenü, indem innerhalb ein Avatar und eine Heading platziert werden.


States

Ein MenuItem kann unterschiedliche States annehmen, die anzeigen, dass aktuell keine Interaktion möglich ist.

  • Disabled – Verwende den Disabled-State, wenn das MenuItem keine Aktion oder kein Ereignis auslösen soll.
  • Pending, Succeeded und Failed – signalisieren dem Nutzer, dass im Hintergrund etwas passiert, nachdem er auf ein MenuItem geklickt hat. Verwende das MenuItem zusammen mit der Component Action, um die States zu steuern.

Kombiniere mit …

Modal

Aktionen im ContextMenu können Modals als Overlay öffnen, wenn zusätzliche Informationen erfasst, bestätigt oder bearbeitet werden müssen.


Properties

PropertyTypeDefaultDescription
UNSTABLE_portalContainerElementdocument.bodyThe container element in which the overlay portal will be placed. This may have unknown behavior depending on where it is portalled to. @deprecated - Use a parent UNSAFE_PortalProvider to set your portal container instead.
arrowBoundaryOffsetnumber0The minimum distance the arrow's edge should be from the edge of the overlay element.
arrowRefRefObject<Element>-A ref for the popover arrow element.
boundaryElementElementdocument.bodyElement that that serves as the positioning boundary.
childrenReactNode-
classNameClassNameOrFunction<PopoverRenderProps>'react-aria-Popover'The CSS [className](https://developer.mozilla.org/en-US/docs/Web/API/Element/className) for the element. A function may be provided to compute the class based on component state.
containerPaddingnumber12The placement padding that should be applied between the element and its surrounding container.
controllerOverlayController-An overlay controller to control the popover state.
crossOffsetnumber0The additional offset applied along the cross axis between the element and its anchor element.
defaultOpenboolean-Whether the overlay is open by default (uncontrolled).
defaultSelectedKeys"all" | Iterable<Key>-The initial selected keys in the collection (uncontrolled).
dirstring-
disabledKeysIterable<Key>-The item keys that are disabled. These items cannot be selected, focused, or otherwise interacted with.
getTargetRect((target: Element) => DOMRect)target.getBoundingClientRect()Overrides the target element's bounding rectangle. Useful for positioning relative to a specific point such as the mouse cursor (e.g. context menus) or text selection. @param target - The target element.
hiddenboolean-
inertboolean-
isDialogContentboolean-Whether the popover contains a dialog.
isEnteringboolean-Whether the popover is currently performing an entry animation.
isExitingboolean-Whether the popover is currently performing an exit animation.
isKeyboardDismissDisabledbooleanfalseWhether pressing the escape key to close the popover should be disabled. Most popovers should not use this option. When set to true, an alternative way to close the popover with a keyboard must be provided.
isNonModalboolean-Whether the popover is non-modal, i.e. elements outside the popover may be interacted with by assistive technologies. Most popovers should not use this option as it may negatively impact the screen reader experience. Only use with components such as combobox, which are designed to handle this situation carefully.
isOpenboolean-Whether the overlay is open by default (controlled).
langstring-
maxHeightnumber-The maxHeight specified for the overlay element. By default, it will take all space up to the current viewport height.
offsetnumber8The additional offset applied along the main axis between the element and its anchor element.
placementPlacement'bottom'The placement of the element with respect to its anchor element.
renderDOMRenderFunction<"div", TooltipRenderProps>-Overrides the default DOM element with a custom render function. This allows rendering existing components with built-in styles and behaviors such as router links, animation libraries, and pre-styled components. Requirements: - You must render the expected element type (e.g. if `<button>` is expected, you cannot render an `<a>`). - Only a single root DOM element can be rendered (no fragments). - You must pass through props and ref to the underlying DOM element, merging with your own prop as appropriate.
renderEmptyState(() => ReactNode)-Provides content to display when there are no items in the list.
scrollRefRefObject<Element>overlayRefA ref for the scrollable region within the overlay.
selectedKeys"all" | Iterable<Key>-The currently selected keys in the collection (controlled).
selectionModeContextMenuSelectionMode-The type of selection that is allowed in the context menu.
shouldCloseOnInteractOutside((element: Element) => boolean)-When user interacts with the argument element outside of the popover ref, return true if onClose should be called. This gives you a chance to filter out interaction with elements that should not dismiss the popover. By default, onClose will always be called on interaction outside the popover ref.
shouldFlipbooleantrueWhether the element should flip its orientation (e.g. top to bottom or left to right) when there is insufficient room for it to render completely.
shouldUpdatePositionbooleantrueWhether the overlay should update its position automatically.
slotstring-A slot name for the component. Slots allow the component to receive props from a parent component. An explicit `null` value indicates that the local props completely override all props received from a parent.
styleStyleOrFunction<TooltipRenderProps>-The inline [style](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style) for the element. A function may be provided to compute the style based on component state.
translate"yes" | "no"-
triggerstring-The name of the component that triggered the popover. This is reflected on the element as the `data-trigger` attribute, and can be used to provide specific styles for the popover depending on which element triggered it.
triggerRefRefObject<Element>-The ref for the element which the popover positions itself with respect to. When used within a trigger component such as DialogTrigger, MenuTrigger, Select, etc., this is set automatically. It is only required when used standalone.
widthstring | number-Sets the context menu to a fixed width.
wrapWithReactElement<unknown, string | JSXElementConstructor<any>>-A React element the component is wrapped with. The element is cloned and receives the component as its only child — useful to render the component inside a link, a tooltip trigger or any other wrapper without changing the surrounding markup.

Events

PropertyTypeDefaultDescription
onAction((key: Key, value: MenuItemProps) => void)-Handler that is called when an item is selected.
onClickMouseEventHandler<HTMLDivElement>-
onOpenChange((isOpen: boolean) => void)-Handler that is called when the overlay's open state changes.
onSelectionChange((keys: Selection) => void)-Handler that is called when the selection changes.

Accessibility

PropertyTypeDefaultDescription
aria-describedbystring-Identifies the element (or elements) that describes the object.
aria-detailsstring-Identifies the element (or elements) that provide a detailed, extended description for the object.
aria-labelstring-Defines a string value that labels the current element.
aria-labelledbystring-Identifies the element (or elements) that labels the current element.

Auf dieser Seite