本文へ移動
日本語

仕組み

The menu opens to the right of and below the point, flips left or up when it would leave the area, and scales from the corner nearest the point. Coordinates are converted to layout pixels, so a scaled ancestor does not offset it. A long press shows a ring that closes under the finger and cancels if the finger moves more than 8px.

向いている場面

  • Secondary actions on items in a grid, list or canvas.
  • Desktop-like apps where users expect right-click.

向かない場面

  • The only way to reach an action: also offer a visible button.
  • Primary actions: show them directly.

使い方

import { ContextMenu } from './ContextMenu.jsx'

export function FileGrid({ files }) {
  return (
    <ContextMenu
      label="File actions"
      items={[
        { id: 'open', label: 'Open', shortcut: '↵', onSelect: open },
        { id: 'rename', label: 'Rename', onSelect: rename },
        { type: 'separator' },
        { id: 'trash', label: 'Move to trash', danger: true, onSelect: trash },
      ]}
    >
      {files.map((f) => <FileTile key={f.id} file={f} tabIndex={0} />)}
    </ContextMenu>
  )
}

プロパティ

プロパティ型既定値説明
childrenReactNode—The area that answers right-click and long press; it becomes the positioning context.
items({ id, label, icon?, shortcut?, danger?, disabled?, onSelect? } | { type: 'separator' } | { type: 'label', label })[]—Menu entries.
labelstring—Accessible name of the menu.
onOpen(target: Element) => void—Called with the element that was right-clicked or pressed, so you know what the menu is for.
onOpenChange(open: boolean) => void—Called when the menu opens or closes.
longPressnumber480Touch hold time in ms.
defaultOpenAt{ x, y } | nullnullOpen on first render at this point in the area (without taking focus).
...restHTMLAttributes<HTMLDivElement>—Other attributes go to the area.

アクセシビリティ

  • role menu with role menuitem rows and role separator; disabled items carry aria-disabled.
  • Shift+F10 or the Menu key on a focused element opens it with focus on the first item; Up, Down, Home, End and typeahead move; Enter chooses.
  • Escape or Tab closes the menu and returns focus to the element it was opened from; a click outside, scrolling or leaving the window also closes it.
  • Items are 34px high; the destructive item uses red text and an icon, not colour alone.
  • With reduced motion the menu fades without scaling and the long-press ring is shown without animating.

モーション

Menu 160ms cubic-bezier(0.2, 0.9, 0.3, 1.1) from 0.94 scale at the corner nearest the pointer; long-press ring fills linearly over the hold time.

プロジェクトに追加

プロンプトを Codex または Claude Code に貼り付けます。エージェントが公開ソースを読み、書き込む内容を示したうえで、既存のコンポーネントの隣に追加します。

ContextMenu.jsxcontext-menu.csstokens.cssREADME.md

2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。

オーバーレイ
「ボトムシート」は Pro に含まれます。解除するとプレビューが表示されます。