---
title: "Context Menu"
description: "Context Menu with accessible presentation and local state."
---

# Context Menu

Context Menu with accessible presentation and local state.

## Installation

Install the shared package once in your Exact app. This version is a beta.

```sh
bun add --exact @rightui/ui@0.4.0-beta.4
```

Or choose another package manager:

```sh
npm install --save-exact @rightui/ui@0.4.0-beta.4
pnpm add --save-exact @rightui/ui@0.4.0-beta.4
yarn add --exact @rightui/ui@0.4.0-beta.4
```

Exact compilation uses Bun 1.4.2. Register Geist and Newsreader, declare the font marker and add the root provider using [Installation](/docs/installation).

### Manual source setup

Install an editable copy of the complete package:

```sh
mkdir -p vendor/rightui vendor/rightui-ui
npm pack @rightui/ui@0.4.0-beta.4 --pack-destination vendor/rightui
tar -xzf ./vendor/rightui/rightui-ui-0.4.0-beta.4.tgz -C vendor/rightui-ui --strip-components=1
bun add file:./vendor/rightui-ui
```

Keep package.json, the Contract files, ui.tamber, assets and licenses together.

### Import

```contract
use ContextMenu, ContextMenuAdvanced from "@rightui/ui"
```

## Usage

Import ContextMenu, ContextMenuAdvanced from @rightui/ui. Store its value in state and update it through the provided action callback.

## Example

```contract
use FontWeights from "@rightui/ui"
use uiFontFamily, ContextMenuAdvanced, AdvancedMenuEntry, AdvancedMenuSelection from "@rightui/ui"

component Demo
  props
    width: number
    reduced: bool
  inject
    uiWeights: FontWeights
    uiFont: string
  derive narrow = width < 600
  state checks: string[] = ["status", "bookmarks"]
  state radios: AdvancedMenuSelection[] = [AdvancedMenuSelection(group="position", value="bottom"), AdvancedMenuSelection(group="person", value="pedro"), AdvancedMenuSelection(group="zoom", value="100")]
  action choose(value: string)
    chosen = value
  action check(id: string, value: bool)
    checks = value ? checks.filter(item => item != id).concat([id]) : checks.filter(item => item != id)
  action radio(group: string, value: string)
    radios = radios.filter(item => item.group != group).concat([AdvancedMenuSelection(group=group, value=value)])
  state chosen = "No action"
  view
    column gap="ui.space-16" min-width=0 max-width="100%"
      ContextMenuAdvanced(id="parity-context", label="Right click here", entries=[AdvancedMenuEntry(id="back", parent="", label="Back", kind="item", shortcut="⌘[", disabled=false, group="", value=""), AdvancedMenuEntry(id="forward", parent="", label="Forward", kind="item", shortcut="⌘]", disabled=true, group="", value=""), AdvancedMenuEntry(id="reload", parent="", label="Reload", kind="item", shortcut="⌘R", disabled=false, group="", value=""), AdvancedMenuEntry(id="tools", parent="", label="More Tools", kind="submenu", shortcut="", disabled=false, group="", value=""), AdvancedMenuEntry(id="save", parent="tools", label="Save Page As…", kind="item", shortcut="⇧⌘S", disabled=false, group="", value=""), AdvancedMenuEntry(id="shortcut", parent="tools", label="Create Shortcut…", kind="item", shortcut="", disabled=false, group="", value=""), AdvancedMenuEntry(id="developer", parent="tools", label="Developer Tools", kind="item", shortcut="⌥⌘I", disabled=false, group="", value=""), AdvancedMenuEntry(id="context-separator", parent="", label="", kind="separator", shortcut="", disabled=false, group="", value=""), AdvancedMenuEntry(id="bookmarks", parent="", label="Show Bookmarks Bar", kind="checkbox", shortcut="⇧⌘B", disabled=false, group="", value=""), AdvancedMenuEntry(id="urls", parent="", label="Show Full URLs", kind="checkbox", shortcut="", disabled=false, group="", value=""), AdvancedMenuEntry(id="people-separator", parent="", label="", kind="separator", shortcut="", disabled=false, group="", value=""), AdvancedMenuEntry(id="people-label", parent="", label="People", kind="label", shortcut="", disabled=false, group="", value=""), AdvancedMenuEntry(id="people", parent="", label="People", kind="group", shortcut="", disabled=false, group="", value=""), AdvancedMenuEntry(id="pedro", parent="people", label="Pedro Duarte", kind="radio", shortcut="", disabled=false, group="person", value="pedro"), AdvancedMenuEntry(id="colm", parent="people", label="Colm Tuite", kind="radio", shortcut="", disabled=false, group="person", value="colm")], checks=checks, radios=radios, reduced=reduced, choose=choose, check=check, radio=radio)
      text "Destructive action" font-weight=uiWeights.semibold font-family=uiFontFamily(uiFont) data-rightui-font="body"
      ContextMenuAdvanced(id="context-destructive", label="Right click to manage project", entries=[AdvancedMenuEntry(id="delete-project", parent="", label="Delete project", kind="destructive", shortcut="", disabled=false, group="", value="")], checks=[], radios=[], reduced=reduced, choose=choose, check=check, radio=radio)

```

## API Reference

| Property | Type |
| --- | --- |
| ContextMenuAdvanced.id | string |
| ContextMenuAdvanced.label | string |
| ContextMenuAdvanced.entries | AdvancedMenuEntry[] |
| ContextMenuAdvanced.checks | string[] |
| ContextMenuAdvanced.radios | AdvancedMenuSelection[] |
| ContextMenuAdvanced.reduced | bool |
| ContextMenuAdvanced.choose | action |
| ContextMenuAdvanced.check | action |
| ContextMenuAdvanced.radio | action |
| ContextMenu.id | string |
| ContextMenu.label | string |
| ContextMenu.entries | MenuEntry[] |
| ContextMenu.reduced | bool |
| ContextMenu.choose | action |

## Source

File: packages/ui/menu.contract

```contract
use FontWeights from "./typography-context.contract"
use uiFontFamily from "./typography-context.contract"
use Control from "./styles.contract"
use uiPopupIn, uiPopupOut from "./popup-motion.contract"

shape MenuEntry
  id: string
  label: string
  shortcut: string
  disabled: bool
  destructive: bool

component DropdownMenuPanel
  props
    panelId: string
    switching: bool
    id: string
    label: string
    entries: MenuEntry[]
    bar: bool
    firstGroup: bool
    centered: bool
    circular: bool
    separateDestructive: bool
    reduced: bool
    changed: action
    closeMenu: action
    pick: action
    itemKeys: action
  inject
    uiWeights: FontWeights
    uiFont: string
  action menuKeys(k: string)
    if k == "Escape"
      preventDefault()
      stopPropagation()
      closeMenu()
    else if k == "Tab"
      closeMenu()
  view
    column id=panelId testId=(id + "-menu") popover="auto" toggle=changed keycapture=menuKeys
      role="menu" aria-label=label tabindex=-1
      left="auto" top="auto" position-area=(centered ? "bottom" : "bottom span-right") position-try-fallbacks="flip-inline, flip-block, flip-block flip-inline"
      width=(circular ? "ui.avatar-menu-width" : "14rem") min-width="0px" max-width="calc(100vw - ui.space-32)" max-height="min(28rem, 70vh)" overflow="auto" box-sizing="border-box"
      margin-top=(bar ? "ui.space-9" : "ui.space-4") margin-bottom=(bar ? "ui.space-9" : "ui.space-4") margin-left=(firstGroup ? "ui.space-negative-5" : centered ? "ui.space-4" : "0px") margin-right=(centered ? "ui.space-4" : "0px") padding="ui.space-4" gap="0px" border-width="ui.border-width" border-style="solid" border-color="ui.border" border-radius="ui.radius-menu" background-color="ui.surface" box-shadow="ui.shadow-menu"
      -exact-layout-transition=(bar and not reduced ? "ui.duration-normal ui.easing" : "none")
      animation=(reduced or switching ? "none" : "uiPopupIn ui.duration-fast ui.easing both")
      -exact-exit-animation=(reduced ? "none" : "uiPopupOut ui.duration-fast ui.easing both")
      column role="none" align-items="stretch" gap="0px" min-width="0px"
        for item, index in entries key=item.id
          if separateDestructive and item.destructive and index > 0 and entries.slice(0, index).filter(previous => previous.destructive).length == 0
            view role="separator" height="ui.border-width" margin="ui.space-4 ui.space-negative-4" background-color="ui.border"
          button id=(id + "-item-" + item.id) testId=(id + "-item-" + item.id) class=Control
            role="menuitem" tabindex=-1 disabled=item.disabled press=pick(item) key=itemKeys(item.id)
            display="flex" width="100%" box-sizing="border-box" align-items="center" flex-wrap="wrap" gap="ui.space-8" min-height="ui.space-32" padding="ui.space-6 ui.space-8" border="0" border-radius="ui.radius-item" font-weight=uiWeights.regular background-color="transparent"
            color=(item.destructive ? "ui.danger" : "ui.foreground")
            :hover
              background-color="ui.muted"
            :focus-visible
              background-color="ui.muted"
            text item.label flex="1 1 6rem" min-width="0px" text-align="left" font-family=uiFontFamily(uiFont) data-rightui-font="body"
            if item.shortcut != ""
              text item.shortcut min-width="0px" hyphens="auto" font-size="ui.text-xs" letter-spacing="ui.tracking-wide" color="ui.muted-foreground" aria-hidden=true font-family=uiFontFamily(uiFont) data-rightui-font="body"

component ActionMenuTrigger
  props
    id: string
    label: string
    entries: MenuEntry[]
    contextual: bool
    circular: bool
    separateDestructive: bool
    reduced: bool
    choose: action
  inject
    uiWeights: FontWeights
    uiFont: string
  slot
  state expanded = false
  state openEpoch = 0
  state contextFocus = false
  derive enabled = entries.filter(item => not item.disabled)
  action open
    contextFocus = false
    openEpoch = openEpoch + 1
    expanded = true
    showPopover(id + "-menu", source=id)
    match first(enabled)
      case some(item)
        focus(id + "-item-" + item.id)
      case none
        focus(id + "-menu")
  action closeMenu
    expanded = false
    focus(id)
  action toggle
    if expanded
      closeMenu()
    else
      open()
  action context
    if contextual
      openEpoch = openEpoch + 1
      expanded = true
      contextFocus = true
  action changed(epoch: number, e: ToggleEvent)
    if epoch == openEpoch
      expanded = e.newState == "open"
      if e.newState == "open" and contextFocus
        match first(enabled)
          case some(item)
            focus(id + "-item-" + item.id)
          case none
            focus(id + "-menu")
      contextFocus = false

  action pick(item: MenuEntry)
    if not item.disabled
      closeMenu()
      choose(item.id)
  action triggerKeys(k: string, e: KeyboardEvent)
    if k == "ArrowDown" or k == "ArrowUp" or k == "ContextMenu" or (k == "F10" and e.shiftKey)
      preventDefault()
      open()
      if k == "ArrowUp"
        match enabled.at(enabled.length - 1)
          case some(item)
            focus(id + "-item-" + item.id)
          case none
            focus(id + "-menu")
  action itemKeys(itemId: string, k: string, e: KeyboardEvent)
    if not e.isComposing
      let index = enabled.map(item => item.id).indexOf(itemId)
      if k == "ArrowDown" or k == "ArrowUp" or k == "Home" or k == "End"
        preventDefault()
        let next = (k == "Home" ? 0 : k == "End" ? enabled.length - 1 : (index + (k == "ArrowDown" ? 1 : enabled.length - 1)) % max(1, enabled.length))
        match enabled.at(next)
          case some(item)
            focus(id + "-item-" + item.id)
          case none
            focus(id + "-menu")
      else if k == "Enter" or k == " "
        preventDefault()
        match enabled.at(index)
          case some(item)
            pick(item)
          case none
            closeMenu()
      else if k.length == 1 and not e.metaKey and not e.ctrlKey and not e.altKey
        match first(enabled.filter(item => item.label.toLowerCase().startsWith(k.toLowerCase())))
          case some(item)
            preventDefault()
            focus(id + "-item-" + item.id)
          case none
            stopPropagation()
  view
    column min-width="0px" align-items="flex-start"
      button id=id testId=id class=Control font-weight=uiWeights.medium press=toggle contextmenu=context key=triggerKeys
        contextPopover=(contextual ? id + "-menu" : "")
        aria-label=label aria-haspopup="menu" aria-expanded=expanded aria-controls=(id + "-menu")
        display="flex" align-items="center" justify-content="center"
        width=(circular ? "ui.avatar-size" : "auto") height=(circular ? "ui.avatar-size" : "auto") min-height=(circular ? "ui.avatar-size" : "ui.control")
        padding=(circular ? "0px" : contextual ? "ui.space-24 ui.space-16" : "ui.space-8 ui.space-16") border-width=(circular ? "0px" : "ui.border-width") border-style=(contextual ? "dashed" : "solid") border-color="ui.border" border-radius=(circular ? "ui.avatar-radius" : "ui.radius")
        background-color=(circular ? "transparent" : "ui.surface") color="ui.foreground"
        children
      if expanded
        for epoch in [openEpoch] key=epoch
          DropdownMenuPanel(id=id, panelId=id + "-menu", switching=false, label=label, entries=entries, bar=false, firstGroup=false, centered=(contextual or circular), circular=circular, separateDestructive=separateDestructive, reduced=reduced, changed=changed(epoch), closeMenu=closeMenu, pick=pick, itemKeys=itemKeys)

component ActionMenu
  props
    id: string
    label: string
    entries: MenuEntry[]
    contextual: bool
    reduced: bool
    choose: action
  inject
    uiFont: string
  view
    ActionMenuTrigger(id=id, label=label, entries=entries, contextual=contextual, circular=false, separateDestructive=false, reduced=reduced, choose=choose)
      text label text-align="left" font-family=uiFontFamily(uiFont) data-rightui-font="body"

component DropdownMenu
  props
    id: string
    label: string
    entries: MenuEntry[]
    reduced: bool
    choose: action
  view
    ActionMenu(id=id, label=label, entries=entries, contextual=false, reduced=reduced, choose=choose)

component ContextMenu
  props
    id: string
    label: string
    entries: MenuEntry[]
    reduced: bool
    choose: action
  view
    ActionMenu(id=id, label=label, entries=entries, contextual=true, reduced=reduced, choose=choose)

```
