Documentation menu
morphui Documentation

MorphContextMenu

Actions that come out of the row they act on.

Live preview

Click, right-click, long-press, or press Shift+F10.

Right-click the row, or press Shift+F10.

Example

The same component used in the preview. Works in JSX and TSX.

MorphContextMenu example
import { useState } from 'react';
import { MorphContextMenu, type MorphMenuItem } from '@lestradavaz/morph-ui';
import './demo.css';

export default function ContextMenuDemo() {
  const [name, setName] = useState('Project brief');
  const [copies, setCopies] = useState(0);
  const [favourite, setFavourite] = useState(false);
  const [notice, setNotice] = useState('Click, right-click, long-press, or press Shift+F10.');

  const items: MorphMenuItem[] = [
    {
      id: 'rename',
      label: 'Rename',
      icon: '✎',
      onSelect: () => {
        const next = name === 'Project brief' ? 'Project brief (edited)' : 'Project brief';
        setName(next);
        setNotice(`Renamed to ${next}.`);
      },
    },
    {
      id: 'duplicate',
      label: 'Duplicate',
      icon: '▣',
      onSelect: () => {
        setCopies(count => count + 1);
        setNotice('A copy was created.');
      },
    },
    {
      id: 'favourite',
      label: favourite ? 'Favourite' : 'Add to favourites',
      icon: favourite ? '★' : '☆',
      checked: favourite,
      onSelect: () => {
        setFavourite(value => !value);
        setNotice(favourite ? 'Removed from favourites.' : 'Added to favourites.');
      },
    },
    { id: 'details', label: 'Show details', icon: 'ⓘ', onSelect: () => setNotice('Last edited today · Shared with 3 people.') },
  ];

  return (
    <div className="demo-stack demo-context">
      <MorphContextMenu
        label="File actions"
        items={items}
        trigger={
          <div className="demo-file" tabIndex={0} role="button" aria-label={`Actions for ${name}`}>
            <span className="demo-file-icon" aria-hidden="true">▤</span>
            <span>
              <strong>{name}</strong>
              <small>Document · {copies ? `${copies} ${copies === 1 ? 'copy' : 'copies'}` : 'No copies'}</small>
            </span>
            <span className="demo-file-favourite" aria-hidden="true">{favourite ? '★' : '⋯'}</span>
          </div>
        }
      />
      <p className="demo-notice" aria-live="polite">{notice}</p>
    </div>
  );
}
Preview stylesheet demo.css
demo.css
.demo-primary,.demo-chip,.demo-card,.demo-close { font:inherit; cursor:pointer; border:0; }
.demo-primary { display:inline-flex; align-items:center; justify-content:center; min-height:48px; padding:0 25px; border-radius:999px; background:var(--morph-accent); color:var(--morph-on-accent); font-weight:500; font-size:15px; white-space:nowrap; }
.demo-chip { padding:14px 22px; border-radius:16px; background:var(--morph-chip); color:var(--morph-text); font-size:15px; }
.demo-primary:active,.demo-chip:active { transform:scale(.98); }
.demo-dialog-panel { width:min(460px,100%); height:min(650px,100%); border-radius:34px; }
.demo-window-panel { width:min(530px,100%); height:min(570px,100%); border-radius:34px; }
.demo-sheet { padding:60px 36px 32px; display:flex; flex-direction:column; gap:20px; }
.demo-sheet h2 { font-size:30px; line-height:1.2; letter-spacing:-.025em; margin:0; font-weight:550; }
.demo-sheet p { margin:0; color:var(--morph-muted); font-size:15px; line-height:1.65; }
.demo-sheet form { display:flex; flex-direction:column; gap:20px; }
.demo-sheet label { display:flex; flex-direction:column; gap:8px; font-size:14px; }
.demo-sheet input { width:100%; height:48px; padding:0 14px; color:var(--morph-text); background:var(--morph-surface); border:1px solid var(--morph-border); border-radius:10px; font:inherit; }
.demo-sheet input::placeholder { color:var(--morph-muted); }
.demo-emblem { display:grid; place-items:center; width:44px; height:44px; border-radius:16px; background:var(--morph-accent-soft); color:var(--morph-accent); font-size:26px; font-weight:600; }
.demo-close { position:absolute; top:18px; right:18px; display:grid; place-items:center; width:38px; height:38px; border-radius:50%; background:var(--morph-raised); color:var(--morph-text); font-size:23px; line-height:1; z-index:2; }
.demo-note { font-size:12px; color:var(--morph-muted); }
.demo-detail { display:flex; justify-content:space-between; padding:12px 0; border-bottom:1px solid var(--morph-border); font-size:14px; }
.demo-success { display:flex; flex-direction:column; gap:24px; }
.demo-success h3 { margin:0; font-size:24px; }
.demo-card { width:240px; padding:0; overflow:hidden; border-radius:24px; background:var(--morph-bg); color:var(--morph-text); text-align:left; box-shadow:0 8px 28px rgb(0 0 0 / .06); }
.demo-art { display:block; width:100%; height:200px; background:radial-gradient(circle at 66% 74%,var(--morph-accent) 0 26%,transparent 26.4%),radial-gradient(circle at 66% 74%,transparent 0 36%,color-mix(in srgb,var(--morph-accent) 34%,transparent) 36.4% 37.6%,transparent 38%),radial-gradient(circle at 26% 26%,var(--morph-muted) 0 7%,transparent 7.4%),var(--morph-raised); }
.demo-card-caption { display:flex; align-items:center; justify-content:space-between; padding:17px 19px; font-size:13px; }
.demo-card-caption span { font-size:18px; }
.demo-art-wide { height:46dvh; }
.demo-story { max-width:680px; margin:0 auto; padding:40px 28px 80px; }
.demo-story h2 { font-size:clamp(32px,5vw,52px); line-height:1.1; letter-spacing:-.03em; margin:18px 0 24px; }
.demo-story p:not(.demo-note) { color:var(--morph-muted); font-size:17px; line-height:1.7; margin:0 0 24px; }
.demo-feature { min-height:100%; }
@media(max-width:480px) { .demo-sheet { padding:66px 24px 28px; } .demo-dialog-panel { height:min(640px,100%); } }

/* Anchored surfaces: the trigger sits in the page, the surface comes out of it. */
.demo-stack { display:flex; flex-direction:column; align-items:center; gap:18px; width:100%; }
.demo-row { display:flex; flex-wrap:wrap; align-items:center; justify-content:center; gap:12px; }
/* An opaque trigger, because a surface borrows the trigger's fill for its first
   beat: opening out of a transparent ghost would start it invisible. */
.demo-popover-trigger { background:var(--morph-bg); }
.morph-popover__row input[type='checkbox'] { width:18px; height:18px; accent-color:var(--morph-accent); cursor:pointer; }
.demo-small { height:30px; padding:0 13px; border:0; border-radius:8px; background:var(--morph-accent); color:var(--morph-on-accent); font:inherit; font-size:12px; cursor:pointer; }
.demo-context { width:min(320px,100%); }
.demo-file { display:flex; align-items:center; gap:12px; width:100%; min-height:72px; padding:10px 14px; border:1px solid var(--morph-border); border-radius:var(--morph-radius-sm); background:var(--morph-bg); color:var(--morph-text); cursor:context-menu; user-select:none; -webkit-user-select:none; touch-action:manipulation; }
.demo-file:focus-visible { outline:2px solid var(--morph-accent); outline-offset:2px; }
.demo-file > span:nth-child(2) { display:grid; gap:2px; flex:1; }
.demo-file strong { font-size:14px; font-weight:500; }
.demo-file small { color:var(--morph-muted); font-size:11px; }
.demo-file-icon { display:grid; place-items:center; width:38px; height:38px; border-radius:10px; background:var(--morph-accent-soft); color:var(--morph-on-accent-soft); font-size:22px; }
.demo-file-favourite { color:var(--morph-muted); font-size:21px; }
.demo-notice { min-height:18px; margin:0; color:var(--morph-muted); font-size:11px; text-align:center; }

/* A control and its visible text side by side: the switch and the checkbox each
   decide their own text, so only the pair's layout lives here. */
.demo-label { font-size:15px; font-weight:500; color:var(--morph-text); cursor:pointer; -webkit-user-select:none; user-select:none; }
.demo-label[data-disabled] { color:var(--morph-muted); cursor:not-allowed; }
/* Two fields stacked at a readable measure: a text field at the width of the
   stage is a field nobody can scan. */
.demo-fields { display:grid; gap:16px; width:min(100%,340px); }
.demo-copy { margin:0; max-width:52ch; color:var(--morph-muted); font-size:14px; }

Usage

Pass the element the menu belongs to, and the items it offers. The menu grows out of that element rather than out of the cursor: the row unfolding is what makes the menu legible as belonging to it, and a panel pasted beside a pointer belongs to nothing.

<MorphContextMenu
  label="File actions"
  items={[
    { id: 'rename', label: 'Rename', icon: '✎', onSelect: rename },
    { id: 'duplicate', label: 'Duplicate', icon: '▣', onSelect: duplicate },
    { id: 'favourite', label: 'Favourite', icon: '★', checked: favourite, onSelect: toggleFavourite },
  ]}
  trigger={<div className="file-row" tabIndex={0}>Project brief</div>}
/>

A click on the trigger opens the menu too, at a point inside it, so the same element works as a menu button.

Opening it

Right-click, a click, a long press on touch, or Shift+F10 and the context-menu key. A long press is read off the trigger itself, so nothing is wrapped around your element and your layout is left alone. Drift of more than ten pixels cancels the press, which is what stops a scroll from being mistaken for a hold.

The trigger needs to be reachable by keyboard — tabIndex={0} on a div, or a real button — and it gets aria-haspopup="menu" and aria-expanded from the component.

Items

checked turns an item into a checkbox: it is rendered with role="menuitemcheckbox" and a tick, and aria-checked reports its state. disabled blocks selection and reports it. icon is a glyph or a small element, drawn before the label.

{ id: 'favourite', label: favourite ? 'Favourite' : 'Add to favourites', checked: favourite, onSelect: toggle }

onSelect runs before the menu closes, so a handler that changes the page and one that changes the menu’s own state work the same way.

Keyboard

Once open, the arrow keys move through the items, Home and End go to the ends, Enter chooses, and typing a letter moves to the first item that starts with it. Focus moves with the selection, and the item in focus is the item that looks active — a menu where the highlight and the focus disagree is a menu that lies to the person using it.

Escape closes and returns focus to the trigger. A menu that can only be driven by a pointer is a menu half the people using it cannot operate.

API reference

MorphContextMenu props

PropType / defaultDescription
triggerReactElementRequiredThe element the menu belongs to. Right-click, click, long-press or Shift+F10.
itemsMorphMenuItem[]Requiredid, label, and optionally icon, checked, disabled and onSelect.
labelstringRequiredAn accessible name for the menu, such as “File actions”.
widthnumbermeasuredThe resting width of the menu. Left out, it is measured as it lays itself out.
heightnumbermeasuredThe resting height of the menu. Pass both when the content can change size.
onOpenChange(open: boolean) => void—Notification when the surface is asked to open, and when it has closed.

Once open, the arrow keys, Home, End, Enter and first-letter movement all work. A menu that can only be driven by a pointer is a menu half the people using it cannot operate.