Context Menu

A list of actions that grows out of the exact point you right-clicked, clamped inside its surface.

or tap the label

Build a context menu that opens where the pointer is. Structure: a bounded surface with onContextMenu (preventDefault), and an absolutely positioned menu inside it. Position: convert the click to container coordinates with getBoundingClientRect, then CLAMP it: x between 4 and (containerWidth - panelWidth), y between 4 and (containerHeight - panelHeight). This clamp is the bug in almost every hand-rolled version; without it the panel opens half off the edge the moment someone right-clicks near a corner. Motion: scale from 0.9 to 1 with opacity, 150ms, transformOrigin TOP LEFT, the corner nearest the click. A panel that fades in beside the cursor loses the one thing a context menu has, which is that the actions belong to the exact spot you pressed. Keep it FAST, around 150ms. A context menu is opened mid-task by someone who already knows what they want, so anything slower is felt as lag rather than as polish. Colour the destructive row with your destructive token. It is the one row where a misclick cannot be undone by clicking again. Give it a SECOND way in: a normal tap target that opens the same menu. Right-click does not exist on touch, and long-press is not discoverable. Closing: document mousedown and Escape, both removed on cleanup, and stopPropagation on the panel's own mousedown so clicking inside does not close it. A menu pinned open at a stale pointer position is worse than no menu. Accessibility: role="menu" with an aria-label on the panel, role="menuitem" on each row. Under prefers-reduced-motion, use opacity only. The position still comes from the click, which is the part carrying the meaning. Colors as tokens: surface and border for the panel, hairline for the target zone, destructive for the one dangerous row. Build the component, not this demo of it. - Ambient motion that IS the component may loop: a drifting background, a marquee, a spinner while something is genuinely loading. - Do not add any timer, interval, loop or script that performs a person's action by itself. No auto-hover, no auto-click, no cycling through states, no pretend submitting, no replaying the interaction on a schedule. - Every state that answers a person waits for a real event from that person. - Placeholder copy and sample content are welcome. Simulated behaviour is not.

Rate it
0
0

We'd like to measure how the site gets used, with Google Analytics. It sets cookies. Everything the site needs to work runs either way. How we handle data