Examples

File list

Each row has its own menu.

Preview

Example.tsx
import { ContextMenu, Stack, Text } from '@reactloom/ui';
import { useState } from 'react';

const files = ['Q3 report.pdf', 'Budget 2031.xlsx', 'Logo.svg'];

export default function Example() {
  const [log, setLog] = useState('');
  return (
    <Stack gap="2" style={{ maxInlineSize: 360 }}>
      <ul aria-label="Files" style={{ display: 'grid', gap: 6, margin: 0, padding: 0, listStyle: 'none' }}>
        {files.map((file) => (
          <li key={file}>
            <ContextMenu
              aria-label={`Actions for ${file}`}
              items={[
                { key: 'open', label: 'Open' },
                { key: 'rename', label: 'Rename', shortcut: 'F2' },
                { type: 'section', items: [{ key: 'delete', label: 'Delete', tone: 'danger' }] },
              ]}
              onAction={(key) => setLog(`${key} ${file}`)}
            >
              <button type="button" style={{ inlineSize: '100%', padding: 10, textAlign: 'start', border: '1px solid var(--pl-color-border-default)', borderRadius: 6, background: 'var(--pl-color-bg-surface)', color: 'inherit', font: 'inherit' }}>
                {file}
              </button>
            </ContextMenu>
          </li>
        ))}
      </ul>
      <Text size="sm" role="status">
        {log && `Chose: ${log}`}
      </Text>
    </Stack>
  );
}

Canvas

Disabled items and sections.

Preview
Right-click, long-press or press Shift+F10 here. Shapes: none
Example.tsx
import { ContextMenu } from '@reactloom/ui';
import { useState } from 'react';

export default function Example() {
  const [shapes, setShapes] = useState<string[]>([]);
  return (
    <ContextMenu
      aria-label="Canvas"
      items={[
        { key: 'rect', label: 'Add rectangle' },
        { key: 'circle', label: 'Add circle' },
        { type: 'section', items: [{ key: 'clear', label: 'Clear canvas', disabled: shapes.length === 0 }] },
      ]}
      onAction={(key) => setShapes((list) => (key === 'clear' ? [] : [...list, key]))}
    >
      {/* eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex -- focusable so Shift+F10 opens the menu */}
      <div tabIndex={0} role="region" aria-label={`Canvas, ${shapes.length} shapes`} style={{ minBlockSize: 140, padding: 12, border: '1px dashed var(--pl-color-border-strong)', borderRadius: 8 }}>
        Right-click, long-press or press Shift+F10 here. Shapes: {shapes.join(', ') || 'none'}
      </div>
    </ContextMenu>
  );
}

API reference

ContextMenu props
PropTypeDefault
childrenrequired
The area; make it focusable.
ReactElement—
itemsrequired
Same as Menu.
MenuEntry[]—
onActionrequired
(key) => void—
aria-labelrequired
Name of the menu.
string—
onOpenChange
(open) => void—
longPressDelay
Touch long-press in ms.
number500
disabled
boolean—

Customization

Extracted from the component’s source on every build, so it matches the code. How the layers fit together is explained in Customization.

Accessibility

  • The same menu keyboard model as Menu; focus returns to the area when it closes.
  • Shift+F10 and the Menu key open it at the focused area.
Keyboard interactions
KeyAction
Shift+F10 / Menu keyOpen
↑ / ↓Move
EnterRun
EscapeClose

Usage guidelines

Do

  • Offer the same actions in a visible menu button too.

Don’t

  • Don’t put actions only in a context menu — people don’t know it is there.