# Context Menu

> Source: https://design.nanatec.co.ke/components/context-menu/
> A menu of commands that replaces the browser's own when someone right-clicks, long-presses or presses the Menu key over an area.

The context menu puts the commands for a thing where the pointer already is: right-click a
file, a table row or a canvas and choose what to do with it. It replaces the browser's own
menu over the area it wraps, and nowhere else. People who never right-click never find it,
so every command in it must also be reachable another way, such as a
[menu button](/components/menu-button/) on the row.

## Preview

Right-click the area, long-press it on a touch screen, or focus the button and press
Shift+F10.

## Installation

The content is the area; children with `slot="menu"` are the items.

```html
<nana-context-menu label="File actions">
  <div class="file-row">report.pdf</div>
  <nana-menu-item slot="menu" value="open">Open</nana-menu-item>
  <nana-menu-item slot="menu" value="history" href="/files/report/history">History</nana-menu-item>
  <hr slot="menu" />
  <nana-menu-item slot="menu" value="delete" destructive>Delete</nana-menu-item>
</nana-context-menu>

<script type="module">
  document.querySelector('nana-context-menu').addEventListener('nana-select', (event) => {
    console.log(event.detail.value);
  });
</script>
```

Items are [menu items](/components/menu-item/), with their icons, shortcuts and states.
`nana-select` fires for the chosen one, and `nana-open` and `nana-close` as the menu
opens and closes.

## Opening and closing

- A right-click, or a long press on a touch screen, opens the menu at the pointer. A
  second right-click moves it there.
- Shift+F10 or the Menu key opens it below the focused element, on its first item.
- Near the window's edges it opens upwards or moves in, so it is never cut off.
- Choosing an item, Escape, Tab, a press outside, scrolling or resizing the window closes
  it.
- Set `disabled` to give the area the browser's own menu back.

## Keyboard

| Key | Does |
| --- | --- |
| Shift+F10 or Menu | Opens the menu on the first item |
| Up and Down arrows | Move between items, skipping disabled ones |
| Home and End | Go to the first or last item |
| A letter | Goes to the next item starting with it |
| Enter or Space | Chooses the item |
| Escape | Closes the menu and returns focus |
| Tab | Closes the menu and moves on |

## Accessibility

- The menu is named by `label`, "Actions" by default; name it after the thing it acts
  on, such as "File actions".
- Focus goes back to where it was when the menu closes.
- A context menu is never the only way to a command. Screen reader and keyboard users
  rarely look for one, and many touch users never long-press.

## API
