Skip to content
Esc
↑↓navigate↵open⌘Jpreview
On this page

Context Menu for Astro

Astro menus that open from a right click or a long press. Built with the popover component, with support for keyboard navigation, multiple selection, and more.

Installation

bunx --bun shadcn@latest add @gnostic/context-menu

Usage

---
import {
  ContextMenu,
  ContextMenuCheckboxItem,
  ContextMenuContent,
  ContextMenuGroup,
  ContextMenuItem,
  ContextMenuLabel,
  ContextMenuRadioGroup,
  ContextMenuRadioItem,
  ContextMenuSeparator,
  ContextMenuShortcut,
  ContextMenuTrigger,
} from "@/components/ui/context-menu"
---
<ContextMenu>
  <ContextMenuTrigger>Right click here</ContextMenuTrigger>
  <ContextMenuContent>
    <ContextMenuGroup>
      <ContextMenuLabel>My Account</ContextMenuLabel>
      <ContextMenuItem>Profile</ContextMenuItem>
      <ContextMenuItem>Billing</ContextMenuItem>
    </ContextMenuGroup>
    <ContextMenuSeparator />
    <ContextMenuGroup>
      <ContextMenuCheckboxItem value="email">
        Email notifications
      </ContextMenuCheckboxItem>
      <ContextMenuRadioGroup>
        <ContextMenuRadioItem value="pro">Pro plan</ContextMenuRadioItem>
      </ContextMenuRadioGroup>
    </ContextMenuGroup>
  </ContextMenuContent>
</ContextMenu>

Composition

Arrange the parts as shown for a ContextMenu:

ContextMenu
├── ContextMenuTrigger
└── ContextMenuContent
    ├── ContextMenuGroup
    │   ├── ContextMenuLabel
    │   ├── ContextMenuItem
    │   └── ContextMenuItem
    ├── ContextMenuSeparator
    └── ContextMenuGroup
        ├── ContextMenuCheckboxItem
        ├── ContextMenuRadioGroup
        │   └── ContextMenuRadioItem
        └── ContextMenuItem
            └── ContextMenuShortcut

ContextMenuContent already renders the menu portal. Use ContextMenuPortal only when you build a custom positioner. Do not wrap ContextMenuContent in a second portal.

Visual styles follow the Coss context menu. Item layout, checkbox marks, and the popup surface use the same recipes as Dropdown Menu. There is no @data-slot/context-menu package. This component binds the right click, then opens the dropdown menu at that point.

Examples

Basic

A right-click target with account actions.

With Shortcuts

Add ContextMenuShortcut to show keyboard hints. The hint is a label. It does not register a shortcut.

With Icons

Combine icons with labels. The menu recipe sizes the icons.

With Checkboxes

Use ContextMenuCheckboxItem for toggles. Set closeOnSelect={false} when several choices should stay in the menu.

With Radio Group

Use ContextMenuRadioItem for one selection. Store the current choice on ContextMenu with defaultValue. ContextMenuRadioGroup groups the choices. The menu still has one radio value.

With Destructive Items

Use variant="destructive" for irreversible actions.

Sides

The popup opens below the pointer by default, centered, with a 4px offset. Set side, align, and sideOffset on ContextMenuContent to override that placement.

API Reference

ContextMenuProps

PropType
side?DropdownMenuProps['side']

Preferred side of the pointer or trigger.

TypeDropdownMenuProps['side']
Default"bottom"
align?DropdownMenuProps['align']

Preferred alignment against the pointer or trigger.

TypeDropdownMenuProps['align']
Default"center"
sideOffset?number

Distance in pixels from the pointer or trigger.

Typenumber
Default4
alignOffset?number

Offset in pixels from the start or end alignment.

Typenumber
Default0

ContextMenuContentProps

ContextMenuItemProps

PropType
variant?'default' | 'destructive'

Visual style of the action. `destructive` uses the destructive foreground color.

Type'default' | 'destructive'
Default"default"
inset?boolean

Indent the label so it lines up with checkbox and radio labels.

Typeboolean
Defaultfalse

ContextMenuCheckboxItemProps

PropType
valuestring

Value committed when the item is checked.

Typestring
defaultChecked?boolean

Check this item when the menu root has no `defaultValues`.

Typeboolean
Defaultfalse
variant?'default' | 'destructive'

Visual style of the action.

Type'default' | 'destructive'
Default"default"
inset?boolean

Indent the label.

Typeboolean
Defaultfalse

ContextMenuRadioItemProps

PropType
valuestring

Value committed when this choice is selected.

Typestring
defaultChecked?boolean

Select this choice when the menu root has no `defaultValue`.

Typeboolean
Defaultfalse
variant?'default' | 'destructive'

Visual style of the choice.

Type'default' | 'destructive'
Default"default"
inset?boolean

Indent the label.

Typeboolean
Defaultfalse

ContextMenuLabelProps

PropType
inset?boolean

Indent the label so it lines up with checkbox and radio labels.

Typeboolean
Defaultfalse

Events

Dispatch inbound events on the context menu root (data-slot="context-menu"). The root emits the outbound events.

Outbound events

Event Detail
context-menu:open-change { open: boolean, previousOpen: boolean, source: "pointer" | "keyboard" | "programmatic" | "restore" | "init", reason: "trigger" | "select" | "outside" | "escape" | "tab" | "programmatic" | "init" }
context-menu:highlight-change { value: string | null, previousValue: string | null, item: HTMLElement | null, previousItem: HTMLElement | null, source: "pointer" | "keyboard" | "programmatic" | "restore" }
context-menu:select { value: string, item: HTMLElement, itemType: "item" | "radio" | "checkbox", source: "pointer" | "keyboard", checked?: boolean }
context-menu:value-change { value: string | null, previousValue: string | null, item: HTMLElement | null, previousItem: HTMLElement | null, source: "pointer" | "keyboard" | "programmatic" | "restore" }
context-menu:values-change { values: string[], previousValues: string[], changedValue: string | null, checked: boolean | null, item: HTMLElement | null, source: "pointer" | "keyboard" | "programmatic" | "restore" }

context-menu:select is cancelable. Call event.preventDefault() to stop the selection.

Inbound events

Event Detail
context-menu:set { open?: boolean, value?: string | null, values?: string[], highlightedValue?: string | null, source?: "programmatic" | "restore" }

context-menu:set with open: true opens the menu against the trigger bounds. A right click opens it at the pointer instead.

Read the installed source for the complete prop types. See the dropdown menu data-slot docs for item behavior. create() in context-menu.ts binds the pointer and forwards events with on and emit from @data-slot/core.

When to use Context Menu

Use a context menu for actions tied to one object, such as a file, a row, or a card. Keep the same actions available from a visible control. A pointer user can miss a right click, and a touch user needs the long press.

Use Dropdown Menu when a button should open the menu.

Keyboard and behavior

Right click or long press the trigger. Focus the trigger and press the context-menu key or Shift+F10 to open it from the trigger bounds. Arrow keys move the highlight. Enter and Space activate the highlighted item. Escape closes the menu.

The popup uses the dropdown menu controller. Test activation, outside pointer, and focus return. Page scroll locks while the menu is open. Pass lockScroll={false} to leave the page free.

This port has no submenu parts. Coss ContextMenuSub and the shadcn submenu are not available, because the dropdown menu package has no submenu. A nested list is not a submenu. Coss checkbox variant="switch" is not available. Render a link with as="a" on ContextMenuItem.

Theme and layout

Item padding, separators, and popup shadows use the shared dropdown menu recipes. A long menu can scroll. Keep destructive wording explicit and check disabled items in both modes.