Command palette

@yasinsensoy/web

Web · Components

Menu

Action menu opened from a button (edit, copy, delete…).

9 exports

Live example

Preview

Code

import { Menu, Button } from "@yasinsensoy/web";

<Menu
  trigger={<Button variant="outline">Actions</Button>}
  items={[
    { label: "Dosya", items: [
      { label: "Edit", icon: "pencil", shortcut: "Mod+E", onSelect: duzenle },
      { label: "Kopyala", icon: "copy", shortcut: "Mod+C", onSelect: kopyala },
      { label: "Share", icon: "share", disabled: true, tooltip: "Drafts can't be shared" },
    ] },
    "separator",
    { label: "Sil", icon: "delete", tone: "danger", shortcut: "Delete", onSelect: remove },
  ]}
/>

Events

  1. Events appear here when you interact with the preview.

IconButton trigger: its name also shows as a tooltip

Import

import { ContextMenu, Menu } from "@yasinsensoy/web";

ContextMenu

componentClient component

Menu that takes over the right-click: right-clicking an area (long press on touch) opens your own items instead of the browser's menu. Items are given with the same items array as Menu.

  • Mouse: The menu opens at the cursor; if it doesn't fit on screen it flips left/up. It closes on an outside click, with Esc, on scroll or when leaving the window. Right-clicking elsewhere while open moves the menu there.
  • Keyboard: While the area (or an element inside) is focused, Shift+F10 or the menu key opens the menu below the focused element; ↑/↓, Home/End, type-ahead, Enter and Esc work like Menu. Focus returns on close.
  • Shortcuts: Items' shortcut values work; by default only while focus is inside the area (see ContextMenuShortcutScope).
  • Mobile: Opens on long press, and becomes a bottom action sheet on small screens (closed by pulling down and with the back button). Scrolling and normal taps aren't broken; the click following the press that opened the menu is swallowed. iOS's link preview bubble is disabled in this area.
  • Performance: The menu panel is added to the DOM on the first right-click, long press or focus; wrapping every table row is cheap.
  • In nested areas the innermost menu opens. If you call event.preventDefault() in your own onContextMenu handler, the menu doesn't open.
ContextMenu({ onOpenChange, ...props }: ContextMenuProps): JSX.Element

Examples

<ContextMenu
  items={[
    { label: "Open", onSelect: open },
    { label: "Rename", shortcut: "F2", onSelect: rename },
    { label: "Copy", shortcut: "Mod+C", onSelect: copy },
    "separator",
    { label: "Delete", icon: "trash", tone: "danger", shortcut: "Delete", onSelect: remove },
  ]}
>
  <Card title="report.pdf" tabIndex={0} />
</ContextMenu>

When ContextMenu item shortcuts work.

  • "area": While focus is inside the area (e.g. a table row is selected) or the menu is open. This is the right one if the same menu appears many times on the page (in every row).
  • "global": Anywhere on the page.
  • "open": Only while the menu is open.
  • "none": Shortcuts are only displayed.
type ContextMenuShortcutScope = "area" | "global" | "open" | "none"

Props of the ContextMenu component.

NameTypeDefaultDescription
childrenReactElementArea to right-click: a single element (card, table row, file icon…). Props are passed to this element; no extra wrapper is added.
itemsreadonly MenuEntry[]Items, groups and separators; same as Menu.
disabled?booleanfalseIf true, the browser's own context menu opens.
longPressDelay?number500Long press duration to open on touch screens (ms).
shortcutScope?ContextMenuShortcutScope"area"When item shortcuts work. See ContextMenuShortcutScope.
onOpenChange?(open: boolean) => voidCalled with the new state when it opens or closes.
mobileSheet?booleantrueOpen from the bottom like an action sheet on small screens (below 640 px)?
label?stringScreen reader name of the menu.
className?stringClasses added to the menu panel (e.g. w-56).
effect?EffectPropEffect or effects of the items (e.g. "ripple", "shine"). If omitted, UIProvider appearance.defaults.menu.effect. See BuiltinEffect.