# Context Menu Primitive

## Objective

Offer right-click actions as a shortcut to controls that are also visible elsewhere.

A contextual menu bound to right-click and a keyboard equivalent, scoped to the item under the pointer.

## Before You Begin

This feature is being added to an application that already exists and already
works. Do not scaffold a new project, and do not assume a blank slate.

Inspect the codebase first and establish:

- The existing application structure and where code of this kind already lives.
- The framework and version in use.
- The existing design system — colours, spacing, typography, and component conventions.
- Existing UI components you can reuse instead of writing new ones.
- The existing database structure, if this feature needs to persist anything.
- The existing authentication and authorization system, if this feature is user-scoped.
- Dependencies already installed, so you don't add a library that duplicates one.
- The existing test setup and conventions.

Only start writing code once you understand the above. If the application
already implements part of this feature, extend it rather than replacing it.

## Implementation Instructions

1. Identify the surfaces where a contextual menu genuinely helps — table rows, file and item grids, canvas objects, tree nodes — and leave the rest of the app to the browser's own menu.
2. Treat the context menu as an accelerator, never as the only route to an action. Every item in it must also be reachable from a visible control, because a right-click menu is undiscoverable and unavailable to many users.
3. Bind a keyboard equivalent that opens the menu for the currently focused item and positions it against that item rather than against the last pointer coordinates.
4. Build the menu's contents from the item actually targeted, and filter by the current user's permissions on that specific record before the menu opens.
5. Item navigation, typeahead, disabled handling, submenus, and dismissal are owned by the Dropdown Menu Primitive; this feature owns only the trigger, the target resolution, and the positioning at a point. Do not fork a second menu implementation.

## UI and UX Requirements

Match the application's existing design system exactly. Reuse its components,
spacing, and typography. This feature should look like it was always there.

## Responsive Requirements

Works on mobile, tablet, and desktop. Touch targets are large enough to hit on a
phone, and nothing overflows horizontally at 320px.

## Accessibility Requirements

- Fully keyboard navigable.
- Correct semantic elements and ARIA roles.
- Visible focus states.
- Meets WCAG AA contrast.
- Dynamic changes are announced to screen readers.
- Respects prefers-reduced-motion.

## Edge Cases

- There must be a keyboard route to the same menu — the context menu key and Shift-F10 on the focused item — otherwise the actions exist only for pointer users.
- The browser's own menu is the right answer in places, and suppressing it universally is hostile. Leave it in text inputs, on selected text, on links, and on images, where copy, paste, spellcheck, and save-image are what the user actually wants.
- Opened at the bottom or right edge of the window, the menu must flip back over the click point rather than extending beyond the viewport, and must never open partly off-screen on a second monitor arrangement.
- The menu must reflect permissions on the specific record targeted, not on the collection. A row the user may view but not delete must not offer Delete and then fail.
- Right-clicking a row inside a multi-selection should act on the whole selection, and right-clicking outside it should reset the selection to the clicked item — pick one rule and apply it everywhere.
- Long-press is the touch equivalent, and if it is implemented it must not fire during a scroll gesture or a drag.
- The menu must close when the underlying item is deleted or moved while it is open, rather than acting on a record that is no longer there.

## Testing

Exercise the feature end to end in the running application. Cover every edge case
above, then run the existing test suite and confirm nothing regressed.

## Acceptance Criteria

- [ ] Right-click opens a contextual menu only on the surfaces where it was deliberately added.
- [ ] Every action in a context menu is also reachable from a visible control.
- [ ] A keyboard shortcut opens the same menu for the focused item.
- [ ] The native browser menu still appears in text fields, on selected text, on links, and on images.
- [ ] The menu stays fully within the viewport when opened near any edge.
- [ ] Menu items reflect the current user's permissions on the targeted record.
- [ ] Item behaviour and dismissal are delegated to the dropdown menu primitive.
- [ ] The feature matches the existing design system.
- [ ] No existing functionality is broken.

## Adaptation Rules

- Match the existing design system. Do not introduce a new colour palette,
  spacing scale, or component library.
- Reuse existing components and utilities wherever they fit.
- Follow the naming, file layout, and code style already present.
- Do not upgrade, replace, or remove existing dependencies to make this
  feature fit. Adapt the feature to the app, not the app to the feature.
- Do not break existing functionality. If a change is genuinely required in
  existing code, make the smallest one that works and say so.
- If something in these instructions conflicts with how the application is
  built, follow the application and explain the deviation.

## Final Verification

Before you report the work as done:

1. Re-read the acceptance criteria above and check each one against what you
   actually built.
2. Run the application and exercise the feature end to end.
3. Run the existing test suite and confirm you have broken nothing.
4. Check the feature on mobile, tablet, and desktop widths.
5. Check keyboard navigation and focus handling.
6. Summarize what changed: files added, files modified, and anything you
   deliberately did differently because of how this application is built.

If any acceptance criterion is unmet, fix it before reporting completion.
