Context Menu
Replaces the browser's right-click menu inside one element with a menu of your own items.
<arc-context-menu>Overview
ContextMenu replaces the browser's right-click menu inside one element. It listens for contextmenu on its parent, so a right-click anywhere in the parent opens the menu at the pointer, flipped away from the viewport edges. Items are <arc-menu-item> children with an optional icon, shortcut hint and disabled. <arc-menu-divider> separates them and <arc-menu-label> heads a named group.
Choosing an item fires arc-select with its value, or its label if it has none, and closes the menu. Escape or a click outside also closes it. arc-open fires on every right-click, including one that only moves an open menu. arc-close fires when it closes and is cancelable.
The menu takes focus when it opens. Arrow Down and Arrow Up move through the items, skipping dividers and disabled ones, Home and End jump to the first and last, and Enter or Space chooses. Closing from the keyboard returns focus to where it was. A click elsewhere leaves focus where you clicked.
Guidelines
Do
- Make it a direct child of the element that should get the menu
- Group related items with `<arc-menu-divider>` or `<arc-menu-label>`, such as clipboard actions and then navigation
- Show shortcut hints on items that also have a key binding
- Disable items that don't apply right now instead of removing them, so people learn what the menu offers
- Give items a `value` when two of them could share a label
- Move long lists of actions to a CommandPalette
Don't
- Don't put it on `<body>`. That replaces the browser's menu everywhere on the page
- Don't put forms or other complex UI in an item. The menu renders its own copy of each item, so only the label, icon and shortcut survive
Features
- A second right-click while it's open moves the menu to the new spot
- The `arc-select` detail also has `index`, and an `item` object with the item's `label`, `shortcut`, `icon` and `value`
- Opens with a scale-in animation
Preview
Usage
This component needs JavaScript. Use the Web Component or a framework wrapper.
<arc-context-menu>
<div slot="trigger">Right-click me</div>
<arc-menu-item shortcut="⌘C">Copy</arc-menu-item>
<arc-menu-item shortcut="⌘V">Paste</arc-menu-item>
</arc-context-menu>import { ContextMenu, MenuItem } from '@arclux/arc-ui-react';
export default function Example() {
return (
<ContextMenu>
<div slot="trigger">Right-click me</div>
<MenuItem shortcut="⌘C">Copy</MenuItem>
<MenuItem shortcut="⌘V">Paste</MenuItem>
</ContextMenu>
);
}<script setup>
import { ContextMenu, MenuItem } from '@arclux/arc-ui-vue';
</script>
<template>
<ContextMenu>
<div slot="trigger">Right-click me</div>
<MenuItem shortcut="⌘C">Copy</MenuItem>
<MenuItem shortcut="⌘V">Paste</MenuItem>
</ContextMenu>
</template><script>
import { ContextMenu, MenuItem } from '@arclux/arc-ui-svelte';
</script>
<ContextMenu>
<div slot="trigger">Right-click me</div>
<MenuItem shortcut="⌘C">Copy</MenuItem>
<MenuItem shortcut="⌘V">Paste</MenuItem>
</ContextMenu>import { Component } from '@angular/core';
import { ContextMenu, MenuItem } from '@arclux/arc-ui-angular';
@Component({
imports: [ContextMenu, MenuItem],
template: `
<arc-context-menu>
<div slot="trigger">Right-click me</div>
<arc-menu-item shortcut="⌘C">Copy</arc-menu-item>
<arc-menu-item shortcut="⌘V">Paste</arc-menu-item>
</arc-context-menu>
`,
})
export class MyComponent {}import { ContextMenu, MenuItem } from '@arclux/arc-ui-solid';
export default function Example() {
return (
<ContextMenu>
<div slot="trigger">Right-click me</div>
<MenuItem shortcut="⌘C">Copy</MenuItem>
<MenuItem shortcut="⌘V">Paste</MenuItem>
</ContextMenu>
);
}import { ContextMenu, MenuItem } from '@arclux/arc-ui-preact';
export default function Example() {
return (
<ContextMenu>
<div slot="trigger">Right-click me</div>
<MenuItem shortcut="⌘C">Copy</MenuItem>
<MenuItem shortcut="⌘V">Paste</MenuItem>
</ContextMenu>
);
}API
openbooleanfalse- Controls the visibility of the context menu. Set to true when the contextmenu event fires; set to false when the user selects an item, clicks the backdrop, or presses Escape.
Events
arc-open- Fired when the context menu opens
arc-close- Fired when the context menu closes
arc-select- Fired when a menu item is selected