menumenu_openMDC

Context Menu

descriptionREADMEwidgetsDemo

A Material Design 3 Expressive Context Menu component, modeled primarily after Android (right-click, touch/pen long-press with 8px touch-slop cancellation, and Shift+F10 / ContextMenu keyboard trigger) and Flutter (ContextMenuController global exclusivity).

Architecture (Low-Coupling Layers)

  • Data / State (internal/context-menu-state.ts): Pure, framework-agnostic functions for roving focus navigation, typeahead matching, immutable checkbox/radio group toggling, and keyboard anchor coordinate calculation.
  • Behavior (internal/context-trigger-controller.ts + PopoverBehaviorController): Decoupled Android-first trigger controller (contextmenu, touch long-press, keyboard) paired with the shared floating surface behavior controller (exclusiveGroup: 'mdc-context-menu').
  • UI (context-menu.ts, context-menu.style.ts, context-menu.definition.ts): Dual-mode renderer supporting declarative items (item, checkbox, radio, divider, label, submenu, destructive) or custom <slot name="menu"> content.

Usage

<mdc-context-menu id="ctx">
    <div style="padding: 32px; border: 1px dashed currentColor;">
        Right-click or long-press here
    </div>
</mdc-context-menu>

Items

Code
<mdc-context-menu id="demo-context-menu">
    <mdc-card variant="outlined" style="max-width: 380px; cursor: context-menu;">
        <div style="padding: 20px; display: flex; align-items: center; gap: 16px;">
            <mdc-icon style="--mdc-icon-size: 28px; color: var(--md-sys-color-primary);">description</mdc-icon>
            <div>
                <div style="font-weight: 500; font-size: 16px;">Q4_Architecture_Spec.pdf</div>
                <div style="font-size: 13px; opacity: 0.72;">Right-click, long-press (touch), or press Shift+F10</div>
            </div>
        </div>
    </mdc-card>
</mdc-context-menu>

<script type="module">
    const root = document.currentScript?.getRootNode?.() ?? document
    const menu = root.getElementById?.('demo-context-menu') ?? document.getElementById('demo-context-menu')
    menu.items = [
        { id: 'open', label: 'Open preview', icon: 'visibility', shortcut: 'Enter' },
        { id: 'copy', label: 'Copy link', icon: 'content_copy', shortcut: 'Ctrl+C' },
        { id: 'rename', label: 'Rename', icon: 'edit', shortcut: 'F2' },
        { type: 'divider' },
        { id: 'pin', type: 'checkbox', label: 'Pin to workspace', checked: true },
        { id: 'offline', type: 'checkbox', label: 'Available offline', checked: false },
        { type: 'divider' },
        {
            id: 'share',
            type: 'submenu',
            label: 'Share & export',
            icon: 'share',
            children: [
                { id: 'share-team', label: 'Share with team', icon: 'group' },
                { id: 'export-md', label: 'Export Markdown', icon: 'download' },
            ],
        },
        { type: 'divider' },
        { id: 'delete', label: 'Move to trash', icon: 'delete', destructive: true, shortcut: 'Del' },
    ]
</script>

Variant

Code
<mdc-context-menu id="demo-context-menu-vibrant" variant="vibrant">
    <mdc-card variant="filled" style="max-width: 380px; cursor: context-menu;">
        <div style="padding: 20px; display: flex; align-items: center; gap: 16px;">
            <mdc-icon style="--mdc-icon-size: 28px;">palette</mdc-icon>
            <div>
                <div style="font-weight: 500; font-size: 16px;">Vibrant Expressive Surface</div>
                <div style="font-size: 13px; opacity: 0.72;">Right-click, long-press, or press Shift+F10</div>
            </div>
        </div>
    </mdc-card>
</mdc-context-menu>

<script type="module">
    const root = document.currentScript?.getRootNode?.() ?? document
    const menu = root.getElementById?.('demo-context-menu-vibrant') ?? document.getElementById('demo-context-menu-vibrant')
    menu.items = [
        { id: 'lbl-actions', type: 'label', label: 'Sort Order' },
        { id: 'sort-name', type: 'radio', label: 'Name (A–Z)', checked: true, group: 'sort' },
        { id: 'sort-date', type: 'radio', label: 'Last modified', checked: false, group: 'sort' },
        { type: 'divider' },
        { id: 'duplicate', label: 'Duplicate', icon: 'file_copy', shortcut: 'Ctrl+D' },
        {
            id: 'more',
            type: 'submenu',
            label: 'More options',
            icon: 'more_horiz',
            children: [
                { id: 'inspect', label: 'Inspect tokens', icon: 'code' },
                { id: 'history', label: 'Version history', icon: 'history' },
            ],
        },
    ]
</script>