Context Menu
The <x-wirekit:: component creates a right-click triggered dropdown menu. It follows the same keyboard navigation and ARIA patterns as Dropdown but uses the browser's contextmenu event as its trigger mechanism.
On touch devices — which have no right-click — the menu also opens on a long-press (touch-and-hold for half a second) anywhere on the trigger area. A scroll or drag gesture cancels the press, so the menu never fights with scrolling.
Usage
Basic Setup
<x-wirekit::context-menu>
<x-slot:trigger>
<div class="p-8 border-2 border-dashed rounded-lg">
Right-click here
</div>
</x-slot:trigger>
<x-wirekit::context-menu.item icon="edit" wire:click="edit({{ $item->id }})">
Edit
</x-wirekit::context-menu.item>
<x-wirekit::context-menu.item icon="eye" href="/items/{{ $item->id }}">
View Details
</x-wirekit::context-menu.item>
<x-wirekit::context-menu.separator />
<x-wirekit::context-menu.item icon="trash" :danger="true" wire:click="delete({{ $item->id }})">
Delete
</x-wirekit::context-menu.item>
</x-wirekit::context-menu>
Icons
Items can display icons using WireKit's icon alias system:
<x-wirekit::context-menu.item icon="edit">Edit</x-wirekit::context-menu.item>
<x-wirekit::context-menu.item icon="download">Download</x-wirekit::context-menu.item>
<x-wirekit::context-menu.item icon="trash" :danger="true">Delete</x-wirekit::context-menu.item>
Icons require the Icon system to be configured with a preset and the blade-icons package installed.
Danger Items
Use the danger prop for destructive actions. These items use the danger color tokens and signal caution to the user:
<x-wirekit::context-menu.item :danger="true" icon="trash" wire:click="delete">
Delete permanently
</x-wirekit::context-menu.item>
Without Livewire, the item submits a form. Give it type="submit" and put the <form> inside the context menu, around the item: the menu's panel is moved to the end of the page when it opens, so a form around the context menu no longer contains its items, and pressing one submits nothing. route('projects. names the same resource route as the alert dialog's form.
{{-- 1. Inside the context menu: the form around the one item that submits it --}}
<form method="POST" action="{{ route('projects.destroy', $project) }}">
@csrf
@method('DELETE')
<x-wirekit::context-menu.item type="submit" :danger="true" icon="trash">
Delete permanently
</x-wirekit::context-menu.item>
</form>
Disabled Items
<x-wirekit::context-menu.item :disabled="true" icon="download">
Export (no permission)
</x-wirekit::context-menu.item>
Disabled items are visually muted, not focusable via keyboard navigation, and have aria-disabled="true".
On the Rows of a List
A common pattern is attaching a context menu to each record in a list. Right-click any row to open the menu — only one context menu is ever visible at a time, so opening a new one automatically closes the previous one:
Ada Lovelace
ada@example.com
Alan Turing
alan@example.com
Grace Hopper
grace@example.com
@foreach($users as $user)
<x-wirekit::context-menu :label="'Actions for '.$user->name" style="display: block;">
<x-slot:trigger>
<x-wirekit::band :edge="$loop->last ? 'none' : 'bottom'" padding="md">
<x-wirekit::row justify="between" align="center" gap="sm">
<x-wirekit::stack gap="none">
<x-wirekit::text weight="medium">{{ $user->name }}</x-wirekit::text>
<x-wirekit::text size="sm" intent="muted">{{ $user->email }}</x-wirekit::text>
</x-wirekit::stack>
<x-wirekit::badge>{{ $user->role }}</x-wirekit::badge>
</x-wirekit::row>
</x-wirekit::band>
</x-slot:trigger>
<x-wirekit::context-menu.item icon="eye" href="/users/{{ $user->id }}">
View profile
</x-wirekit::context-menu.item>
<x-wirekit::context-menu.item icon="edit" wire:click="edit({{ $user->id }})">
Edit user
</x-wirekit::context-menu.item>
<x-wirekit::context-menu.separator />
<x-wirekit::context-menu.item icon="trash" :danger="true" wire:click="delete({{ $user->id }})">
Delete
</x-wirekit::context-menu.item>
</x-wirekit::context-menu>
@endforeach
The example builds its rows as a list rather than a <table> because <x-wirekit:: renders a wrapper element, and a wrapper between <tbody> and <tr> is invalid HTML. In a real table, put the context menu inside a cell, around what that cell shows: a wrapper inside a <td> is valid.
Submenus
Nest a <x-wirekit:: to add a flyout sub-menu. The parent item opens a child panel beside it (on hover, click, or ArrowRight); the child holds ordinary <x-wirekit::s. Selecting a leaf item closes the whole menu.
Pass a label for plain parent text, or a <x-slot:label> for rich content. The parent carries aria-haspopup="menu" + aria-expanded; the child panel is its own role="menu". Submenus nest to any depth technically, but two levels is the comfortable maximum for a reader — see Pitfalls.
Behavior
- Right-click (
contextmenuevent) opens the menu at the cursor position - Floating UI (bundled) positions the menu at the click coordinates with flip and shift; in a short window the menu moves up until it fits, and only a menu taller than the window scrolls
- Click outside closes the menu automatically
- Teleport moves the panel to the end of the page (the
teleportprop). A form around the context menu no longer contains its items, so the<form>goes inside the menu, around the item that submits it (Danger Items) - Livewire SPA navigation (
wire:navigate) automatically closes open menus - Transitions use scale + opacity for smooth open/close animation
- The native browser context menu is prevented only inside the trigger area -- right-clicking elsewhere on the page behaves normally
Building the Menu When It Is Needed
A context menu builds its menu with the page, and the menu starts its script, though a list with a context menu on every row opens few of them. With lazy, a context menu keeps its menu in a <template> until it first opens, by a right click, a long press or Shift+F10. Once built, the menu stays when it closes again, and it opens at the pointer and takes the focus as a menu built with the page does.
{{-- 1. The menu is built the first time it opens --}}
<x-wirekit::context-menu label="Invoice actions" lazy>
<x-slot:trigger>
<div>Invoice 1001</div>
</x-slot:trigger>
{{-- 2. The menu's items --}}
<x-wirekit::context-menu.item>Duplicate</x-wirekit::context-menu.item>
<x-wirekit::context-menu.item>Archive</x-wirekit::context-menu.item>
</x-wirekit::context-menu>
To build every context menu this way, set it once in the components array of config/:
// config/wirekit.php
'components' => [
'context-menu' => ['lazy' => true],
],
Until a context menu has opened, its menu is not part of the page: a script that looks for an item in it, and a browser test that reads it before a right click, find nothing. Open the menu first, or leave lazy off for a menu whose items something has to find while it is closed.
Props
<x-wirekit::context-menu>
| Prop | Type | Default | Description |
|---|---|---|---|
label |
string|null |
null |
Names the menu panel. A role="menu" with no name is announced as an unnamed menu, and the panel teleports out of the trigger's subtree — so the trigger is no longer adjacent and the name is the only thing identifying what opened. Falls back to a translated default |
teleport |
bool |
true |
Teleport the panel to <body> so it escapes every ancestor stacking context, overflow: hidden, and contain: layout container — the same pattern Modal, Drawer, Alert-Dialog, Tooltip, and Command-Palette use. Set to false only when the panel must stay inside the component root (rare; almost always an anti-pattern). |
lazy |
bool |
false |
Keep the menu out of the page until it first opens. components.context-menu.lazy in config/wirekit.php sets the default. See Building the Menu When It Is Needed |
scope |
string|null |
null |
Scoped personalization key |
<x-wirekit::context-menu.item>
| Prop | Type | Default | Description |
|---|---|---|---|
href |
string|null |
null |
URL (renders <a> instead of <button>) |
danger |
bool |
false |
Destructive/danger styling |
disabled |
bool |
false |
Disabled state |
icon |
string|null |
null |
WireKit icon alias (e.g. 'trash', 'edit') |
scope |
string|null |
null |
Scoped personalization key |
<x-wirekit::context-menu.submenu>
| Prop | Type | Default | Description |
|---|---|---|---|
label |
string|null |
null |
Parent item text (or pass a <x-slot:label> for rich content) |
icon |
string|null |
null |
Optional leading icon (WireKit icon alias) |
placement |
string|null |
null |
Floating UI placement of the child panel; without one it opens toward the end of the row, right-start, or left-start on a right-to-left page; one you set keeps its side on either page |
offset |
int |
0 |
Distance in px between the parent item and the child panel |
disabled |
bool |
false |
Disabled state |
scope |
string|null |
null |
Scoped personalization key |
Slots
| Slot | Purpose |
|---|---|
trigger |
The area that responds to right-click |
| default | Menu items and separators |
Sub-Components
| Component | Purpose |
|---|---|
context-menu.item |
A single menu entry (link or action) |
context-menu.submenu |
A nested flyout sub-menu opened from a parent item |
context-menu.separator |
Visual divider between item groups |
Accessibility
- An
aria-labeloraria-labelledbyof yours names the menu in place oflabel - Without a control in the trigger slot, the trigger area itself is the tab stop, so
Shift+F10has an element to fire on. It is agroupdescribed as "Shift+F10 opens options", so a screen reader says a menu is there - Menu panel:
role="menu", uniqueid(auto-generated) - Items:
role="menuitem",tabindex="-1" - Disabled items:
aria-disabled="true", skipped by keyboard navigation - Focus management: the first enabled item is focused when the menu opens — right-click and long-press included, not just the keyboard path. On close, focus returns to the first focusable element inside your
triggerslot (a button or link). If the trigger area holds no focusable element there is nothing to return focus to, so give a right-clickable region one whenever the action it exposes matters - Click outside closes the menu
- Escape closes the menu
- Icons:
aria-hidden="true"(decorative) - The native browser context menu is suppressed only within the trigger area
Context menus are a progressive enhancement. Every action available in the context menu must also be reachable through another UI path (button, dropdown menu, etc.) — keyboard and assistive-technology users never discover a right-click-only menu.
Keyboard Interaction
| Key | Action |
|---|---|
Shift+F10 (or the platform context-menu key) on the trigger |
Open the context menu (keyboard equivalent of right-click) |
ArrowUp / ArrowDown |
Move between items |
Home / End |
Jump to first / last item |
Enter / Space |
Activate the focused item |
ArrowRight (on a submenu parent) |
Open the submenu and focus its first item |
ArrowLeft / Escape (inside a submenu) |
Close the submenu and return focus to the parent item |
Escape |
Close the menu and return focus to the first focusable element in the trigger slot |
Tab |
Close the menu and continue at the next focusable element after the trigger. Shift+Tab closes it and continues at the one before |
On a right-to-left page a submenu opens to the left, with its chevron pointing there: Left Arrow opens it and Right Arrow closes it. A placement you set keeps its side, its chevron and its keys on either page.
Pitfalls
- Don't use context-menu as a primary action surface. Right-click and long-press are not discoverable: nothing on screen says the menu is there, and a keyboard-only reader has no gesture for it at all. Mirror every context-menu action in a visible button or
<x-wirekit::so non-mouse users can reach it.dropdown> - Keep submenus to one level. The component imposes no depth limit, but a third level is hard to reach with a pointer and long to walk with arrow keys.
Design Tokens
| Element | Token |
|---|---|
| Menu background | --color-wk-bg-elevated |
| Menu border | --color-wk-border / --border-wk-width |
| Menu radius | --radius-wk-md |
| Menu shadow | --shadow-wk-md |
| Menu padding, top and bottom | --padding-wk-y-xs |
| Item text | --color-wk-text |
| Item hover bg | --color-wk-bg-hover |
| Item padding | --padding-wk-x-md / --padding-wk-y-sm |
| Danger text | --color-wk-danger-text |
| Danger hover bg | --color-wk-bg-hover, as every item |
| Icon color | The item's own text color |
| Separator color | --color-wk-border-subtle |
| Font family | --font-wk-sans |
| Font size | --text-wk-md |
| Disabled opacity | --opacity-wk-disabled |
Personalization
This component reads no config/ keys — every option is a prop. Style it through the design tokens above, or scope a variant with the scope prop.
An empty 'context-menu' => [] block in the config is not an override point: pasting one changes nothing.
Scoped Personalization
<x-wirekit::context-menu scope="file-manager">
...
</x-wirekit::context-menu>
// In a service provider's boot() method. A closure receives the shipped classes, so the
// component keeps them; a plain string would replace them outright.
WireKit::scope('file-manager', [
'context-menu' => ['classes' => [
'panel' => fn (string $vendor): string => str_replace('min-w-48', 'min-w-[14rem]', $vendor),
]],
]);
Further Reading
- WAI-ARIA Menu Pattern -- the authoring pattern this component implements
- Floating UI -- positioning engine (bundled)
- MDN:
role="menu" - MDN:
role="menuitem" - MDN:
contextmenuevent