Skip to content

UI: Menu

View as Markdown

Menu, its items, separators, popover and trigger, and useDeliberatePress for destructive items. Import the token file once (@pithyx/ui/tokens.css) and wrap the app in UiProvider; see @pithyx/ui.

Generated from the TSDoc comments of @pithyx/ui 1.0.0-rc.9 at build time; the text below the headings is the comment in the source.

The props of MenuItem.

  • Omit<AriaMenuItemProps, "className" | "children">
optional icon?: ReactNode;

An icon before the label.

children: ReactNode;

The label; a string is also the text for type-ahead.

optional tone?: "danger" | "default";

danger for a destructive entry; default when left out.

optional shortcut?: string;

Keyboard shortcut shown at the end of the item.

const MenuTrigger: (props) => Element | null = AriaMenuTrigger;

Opens a menu from its trigger: the first child is the trigger (a Button or IconButton), the second a MenuPopover with the Menu. It is React Aria’s MenuTrigger; its props (isOpen, onOpenChange, trigger) are documented there.

Parameter Type Description
props MenuTriggerProps React Aria’s menu trigger props and the two children.

Element | null

The trigger with its menu.

function MenuPopover(props): Element;

The popover a Menu opens in, next to its trigger inside a MenuTrigger.

Parameter Type Description
props Omit<PopoverProps, "className"> & object React Aria’s popover props (placement, offset).

Element

The popover.


function Menu<T>(props): Element;

A menu of MenuItems and MenuSeparators, with keyboard and touch handling.

Type Parameter
T extends object
Parameter Type Description
props Omit<MenuProps<T>, "className"> & object React Aria’s menu props (onAction, selectionMode, items).

Element

The menu.

<MenuTrigger>
<IconButton label="More">
<IconDots size={18} />
</IconButton>
<MenuPopover>
<Menu onAction={(id) => run(String(id))}>
<MenuItem id="rename" shortcut="F2">
Rename
</MenuItem>
<MenuSeparator />
<MenuItem id="delete" tone="danger">
Delete
</MenuItem>
</Menu>
</MenuPopover>
</MenuTrigger>

function MenuItem(props): Element;

One entry of a menu, with an optional icon and key hint. It shows a check mark in a menu with a selectionMode and a chevron when it opens a submenu.

Parameter Type Description
props MenuItemProps React Aria’s menu item props (id, onAction, isDisabled, textValue) plus icon, tone and shortcut.

Element

The menu item.


function MenuSeparator(): Element;

A line between groups of menu entries.

Element

The separator.


function useDeliberatePress(): object;

React Aria runs a menu item’s action when a mouse press that began elsewhere, on the menu’s trigger, is released over the item (press, drag, release). A menu that opens under the pointer can then act on a plain click of its trigger. Destructive items take their action only from a press that started on them: spread pressProps on the item and ask confirm() in the menu’s action handler.

The props for the item and the check for the action handler.

pressProps: object;

Spread on the destructive item.

onPressStart: () => void;

Arms the item when a press starts on it.

void

confirm: () => boolean;

Whether the action comes from a press that started on the item; disarms it.

boolean

const deliberate = useDeliberatePress();
<Menu onAction={(id) => id === 'delete' && deliberate.confirm() && remove()}>
<MenuItem id="delete" tone="danger" {...deliberate.pressProps}>
Delete
</MenuItem>
</Menu>;