组件

Context Menu

The right-click menu, written on the same slip of paper as the dropdown and set down where the pointer was. Highlighter rows, drawn keycaps, pencil boxes and felt ticks, a red pencil hatch for what deletes. It opens on a right click, a long press on touch, or the menu key, and pops in where the pointer was.

安装

terminal
npx shadcn@latest add @wobko/context-menu

第一次使用?先把 registry 加进 components.json。 如何安装

示例

Right-click anywhere

The shadcn demo: back, forward and reload with keycaps, a sub-menu, ticked rows and a radio group. The slip opens where you clicked (or press the menu key on the focused note).

On a list row

asChild makes each row its own region, so the menu knows which row you clicked. The chosen row is remembered below the list; rows are focusable, so the menu key works from the keyboard.

Pen picker on a sticky note

A composed use: right-click a sticky note to choose the pen it is written with. The menu is a second sticky, in another color so it reads as its own note, taped down; the radio rows tick the choice.

API 参考

ContextMenuContent

The slip: a Paper that is also the Radix content (role=menu), opened at the pointer. A CSS sheet with no SVG unless a torn edge is asked for; rows scroll in a viewport inside it so the shadow is not cut. `max-h-*`, `overflow-*` and `overscroll-*` in `className` are applied to that viewport (shadcn code writes them on the content), the rest stays on the slip. Every Radix Content prop passes through.

属性类型默认值说明
stockStockName"slip"What it is made of: slip, paper, kraft, sticky-*, index-card ...
edgeEdgeSpec"cut"Cut (pure CSS), torn, deckle ...
tiltboolean | numbertrueSmall seeded tilt of the sheet; text stays upright.
tape"corner" | "top" | "both" | falsefalseStrips of masking tape holding the slip down.
seedstring | number-Pins the hand.

ContextMenuLabel

A heading in the typewriter.

属性类型默认值说明
insetbooleanfalseLines the text up with rows that carry a tick or an icon.

ContextMenuItem

A row; the highlighter is a CSS background, so it costs no ink.

属性类型默认值说明
insetbooleanfalseLines the text up with rows that carry a tick or an icon.
variant"default" | "destructive""default"Destructive is red, and its highlight is a red pencil hatch.
onSelect(event: Event) => void-Runs when it is chosen; `event.preventDefault()` keeps the menu open.

ContextMenuCheckboxItem

A pencil box at the start of the row, ticked in blue felt when `checked`.

属性类型默认值说明
checkedboolean | 'indeterminate'-Controlled state.
onCheckedChange(checked: boolean) => void-Called when toggled.

ContextMenuRadioItem

One choice: a pencil ring with a bold felt dot in the chosen one.

属性类型默认值说明
valuestring-The value this row stands for.

ContextMenuSubTrigger

The row that opens it, with a hand-drawn chevron; it keeps the highlighter while its sub-menu is open.

属性类型默认值说明
insetbooleanfalseLines the text up with rows that carry a tick or an icon.

无障碍

  • Radix ContextMenu: opens on a right click, a long press on touch and the ContextMenu key or Shift+F10 on the focused region; the content is role=menu with menuitem, menuitemcheckbox and menuitemradio rows.
  • Up and Down move (disabled rows are skipped), Home and End jump, typing a letter jumps to the next row that starts with it, ArrowRight opens a sub-menu and ArrowLeft closes it, Enter or Space chooses, Escape closes.
  • A context menu must never be the only way to reach an action: put the same actions on a visible control (a DropdownMenu on a button) as well.
  • The highlight follows the keyboard as well as the pointer (`data-highlighted`); a row with keyboard focus also gets a bar of blue felt at its start edge. Ticks, dots, chevrons and the pencil boxes are aria-hidden drawings; keycaps are real `kbd` elements.
  • Rows are 32 px high (28 compact and calm). Portalled content repeats the InkProvider's data-density, data-motion and data-theme; with reduced motion it appears without the slide and zoom. In forced-colors mode the highlighted row takes the system highlight colors.

源码

CLI 会复制进你项目的文件。

正在加载源码