NOCTA UI

Context Menu

A context menu component that appears on right-click with full keyboard navigation and submenu support

Installation

Install the Context Menu component using the nocta-ui CLI:

Terminal
npx nocta-ui add context-menu

Then import the component:

Import
import { 
  ContextMenu, 
  ContextMenuTrigger, 
  ContextMenuContent,
  ContextMenuItem,
  ContextMenuSeparator
} from '@/components/ui/context-menu';

Basic Usage

Simple Context Menu

Right-click on the area below to open the context menu
Right-click here

With Separators

Use ContextMenuSeparator to group related menu items.

Context menu with separators
Right-click for menu with separators

With Submenu

Create nested menus using ContextMenuSub, ContextMenuSubTrigger, and ContextMenuSubContent.

Context menu with submenu
Right-click for submenu example

Disabled State

Both the entire context menu and individual items can be disabled.

Disabled context menu and items
Context menu disabled
Some items disabled

File Manager Example

A real-world example showing a file manager with context menus.

File manager context menu example
document.pdf
2.4 MB
image.jpg
1.8 MB
project.zip
15.2 MB

Positioning

The context menu automatically positions itself relative to the cursor position and avoids viewport collisions.

<ContextMenuContent
  side="bottom"        // 'top' | 'right' | 'bottom' | 'left'
  align="start"        // 'start' | 'center' | 'end'
  sideOffset={4}       // Distance from cursor
  alignOffset={0}      // Alignment offset
  avoidCollisions={true} // Prevent viewport overflow
>
  {/* Menu items */}
</ContextMenuContent>

Keyboard Navigation

The Context Menu component supports full keyboard navigation:

  • Right-click: Open context menu
  • Arrow Keys: Navigate between menu items
  • Enter: Activate selected item
  • Escape: Close menu
  • Tab: Navigate through focusable elements
  • Home/End: Jump to first/last item

Accessibility

The Context Menu component is built with accessibility in mind:

  • ARIA Attributes: Proper role="menu", aria-expanded, aria-haspopup
  • Keyboard Navigation: Full keyboard support with arrow keys
  • Focus Management: Proper focus handling and restoration
  • Screen Reader Support: Announces menu state and items
  • Portal Rendering: Renders outside component tree to avoid z-index issues

Props

ContextMenu

PropTypeDefaultDescription
childrenReact.ReactNodeThe trigger and content components
classNamestringAdditional CSS classes

ContextMenuTrigger

PropTypeDefaultDescription
childrenReact.ReactNodeContent that triggers the menu on right-click
disabledbooleanfalseDisable the context menu trigger
classNamestringAdditional CSS classes

ContextMenuContent

PropTypeDefaultDescription
childrenReact.ReactNodeMenu items and separators
side'top' | 'right' | 'bottom' | 'left''bottom'Preferred side relative to cursor
align'start' | 'center' | 'end''start'Alignment relative to cursor
sideOffsetnumber0Distance from cursor
alignOffsetnumber0Offset for alignment
avoidCollisionsbooleantruePrevent viewport overflow
onEscapeKeyDown(event: KeyboardEvent) => voidEscape key handler
`