Searching Puzzle Pieces

Type to search Puzzle Pieces.

Context Menu

A right-click menu that opens at the pointer. It is a compound family composed over the shared Dropdown Panel base, running the web component in menu mode with trigger="contextmenu". The pointer placement, the viewport clamp, the touch long-press, the scroll dismissal and the whole keyboard model are the component's.

Rows are Dropdown Menu's, re-exported rather than duplicated — a context-menu row is a dropdown-menu row, and in a copy-in registry that means you edit one Item.pzl and both menus follow. Only the root and the panel differ, and they differ for real: a pointer trigger with no button, and a panel the component places and clamps at the pointer instead of aligning to a trigger.

Right-click anywhere
in this box
File
Share with

Last selection: nothing yet

Installation

Copy the family into your app. It pulls dropdown-menu — whose rows it re-exports — and the shared dropdown-panel base along with it:

puzzle add piece context-menu

That base has a runtime dependency — the web component it wraps. Menu mode and the pointer trigger are 2.1.0's, so the range matters:

npm install @magic-spells/dropdown-panel

Then merge its stylesheets into your app's style entry. The first is required; the second adds the entrance effects:

/* app/styles/styles.css */
@import "tailwindcss";
@import "@magic-spells/dropdown-panel/css" layer(components);         /* required */
@import "@magic-spells/dropdown-panel/css/effects" layer(components); /* optional */

Requires the pieces.css tokens merged into your app styles (see Introduction).

Usage

import ContextMenu from '@/components/ui/ContextMenu';
<ContextMenu @select={ onSelect }>
  <div class="h-40 w-72 rounded-xl border-2 border-dashed border-border">
    Right-click anywhere in here
  </div>

  <ContextMenu.Content>
    <ContextMenu.Item value="open" @press={ run }>Open</ContextMenu.Item>
    <ContextMenu.Separator />
    <ContextMenu.Item value="delete" variant="danger" @press={ run }>
      Delete
    </ContextMenu.Item>
  </ContextMenu.Content>
</ContextMenu>

There is no trigger element. The surface is everything you put inside the root apart from the Content — a right-click or a 500 ms touch long-press anywhere in it opens the panel at the pointer, clamped to an 8px margin and flipped left or up rather than clipped.

A right-click menu is an enhancement, never the only route to an action: there is no keyboard gesture for it and no discoverability. Put the same choices in a Dropdown Menu or a toolbar as well.

Disabled

Menu disabled — the browser's own menu comes back

Keyboard

Opening focuses the first row. From there it is menu mode's model, identical to Dropdown Menu's, and none of it lives in this piece:

ArrowDown / ArrowUpMove row focus, wrapping at the ends and skipping disabled rows, separators and labels.
Home / EndFirst / last row.
a–zTypeahead: jump to the next row whose text starts with what you type. The buffer clears after 500 ms.
Enter / SpaceActivate the focused row. Space clicks a Link too, which Space alone would never follow.
ArrowRight / ArrowLeftOpen a Sub's panel and focus its first row; ArrowLeft closes it again.
EscapeClose one level. Closing the last one restores focus to whatever was focused before the menu opened — the body, for a mouse-opened menu, since a right-click moves no focus; the previously focused element after a keyboard or long-press open.
TabCloses the menu and moves on.

Reference

ContextMenu

The root, and the right-clickable surface. Everything inside it except the Content is the surface.

disabledThe menu is inert and the browser's own context menu comes back. It works by setting no trigger mode at all, so expect one "requires <dropdown-trigger>" console warning per disabled instance.
effect'fade' | 'slide' | 'scale' | 'blur' | 'swing'. Needs the effects stylesheet.
classMerged onto the host, after `inline-block`.
openNOT a prop, on purpose — the coordinates only exist at the moment of the gesture. To summon one yourself, call showAt(x, y) on a DropdownPanel.
@show / @hideReport-only: the menu opened or closed.
@selectValue-first: select(value, event). Fires once per activation, submenus included. Return false to cancel the close.

ContextMenu.Content

The panel. Placed at the pointer and clamped by the component, so there is no align to give it.

flipDefault true, and inert on the root panel — the component places and flips a pointer-positioned panel itself. It matters for a .Sub's downward panel inside a context menu. Pass flip={ false } to opt out.
effect'fade' | 'slide' | 'scale' | 'blur' | 'swing'.
classMerged onto the panel. Merging is not precedence: use the ! suffix (mt-1!) to override a default. Never overflow-hidden — it clips a Sub.

Rows — Item · Link · Group · Label · Separator · Shortcut · Sub · Trigger

Re-exported from the DropdownMenu family, so ContextMenu.Item and DropdownMenu.Item are the same component. See the Dropdown Menu reference for their props; Trigger comes along for a Sub's row.

valueItem and Link: passed to @press and reported by the root @select.
variant'default' | 'danger'.
disabledGreys the row out and takes it out of the keyboard walk.
@pressValue-first per-row hook, alongside the root @select.