ContextMenu
A context menu component that appears on right-click with customizable menu items and actions
Installation
Terminal
Import
Component.tsx
Basic
Basic: The simplest usage of ContextMenu.
Features:
- Right-click triggered context menu
- Virtual positioning at mouse cursor location
- Keyboard navigation support
- Automatic dismissal on click outside
Implementation notes:
- Uses floating-ui/react for positioning and interactions
- Fully reuses Dropdown sub-components (Content, Item, etc.)
- Supports all standard menu features like dividers and variants
Right click me to open context menu
With Disabled
WithDisabled: Demonstrates disabled ContextMenu functionality.
Features:
- Shows how to disable the entire context menu
- Right-click events are prevented when disabled=true
- Visual feedback through data attributes for styling
- Useful for conditional menu availability
- Supports both ContextMenu.Trigger and triggerRef approaches
Implementation:
- Set disabled={true} to prevent context menu activation
- Can use CSS selectors like [data-disabled] for styling
- Both Trigger and triggerRef approaches support disabled state
Right click me
Right click me
With Disabled Items
WithDisabledItems: Demonstrates disabled menu items.
Features:
- Visual disabled state
- Prevents interaction with disabled items
- Maintains keyboard navigation flow
- Useful for conditional menu states
Right click for menu with disabled items
Light Variant
LightVariant: Demonstrates ContextMenu with light variant styling.
Features:
- Light variant visual style
- Standard menu items
- Divider separation
- Danger variant item support
Right click for light variant context menu
With Labels And Dividers
Shows complex menu structure with labels and dividers.
Features:
- Section labels for grouping
- Visual dividers for separation
- Different item variants (danger, highlight)
- Hierarchical menu organization
Use cases:
- File manager context menus
- Complex application menus
- Settings and configuration menus
Right click for complex menu
With Selection
WithSelection: Demonstrates selection functionality with visual indicators.
Features:
- Visual selection indicator (check mark)
- Controlled selection state
- Selection persists between menu opens
- Automatic menu closure on selection
Use cases:
- Radio button-like selection in menus
- Theme or preference selection
- Single-choice menu scenarios
Right click me for selection menu
With Trigger Ref
WithTriggerRef: Demonstrates using triggerRef to avoid component wrapping conflicts.
This approach solves complex nesting scenarios by:
- Using a ref to directly bind the context menu to any DOM element
- Avoiding wrapper components that might interfere with other libraries
- Providing maximum flexibility for integration
Features:
- Left-click opens Dropdown normally
- Right-click opens ContextMenu via triggerRef
- No component wrapping conflicts
- Clean separation of concerns
With Trigger Selector
WithTriggerSelector: Demonstrates using triggerSelector (CSS selector) instead of triggerRef.
Features:
- Supports any valid CSS selector (#id, .class, [data-*], etc.)
- Same functionality as triggerRef
- triggerRef takes priority if both are provided
Use cases:
- When you cannot access the element via ref (e.g., third-party components)
- When the trigger element is rendered elsewhere in the DOM
- When you prefer a simpler, selector-based approach
Using #id selector
Using .class selector
Using [data-*] selector
Right click me (data-* selector)
Empty
Empty: Demonstrates empty state display when no items are available.
Features:
- Show custom empty message when menu has no items
- Useful for dynamic menu content scenarios
Right click me to open context menu
API reference
| ContextMenuProps | Type | Default |
|---|---|---|
disabled | boolean |undefined | - |
disabledNested | boolean |undefined | - |
focusManagerProps | FloatingFocusManagerProps |undefined | - |
offset | number |undefined | - |
onOpenChange | ((open: boolean) => void) |undefined | - |
open | boolean |undefined | - |
placement | undefined |"top" |"right" |"bottom" |"left" |"top-start" |"top-end" |"right-start" |"right-end" |"bottom-start" |"bottom-end" |"left-start" |"left-end" | - |
portalId | string |undefined | - |
readOnly | boolean |undefined | - |
root | HTMLElement |null |undefined | - |
selection | boolean |undefined | - |
triggerRef | RefObject<HTMLElement> |undefined | - |
triggerSelector | string |undefined | - |
variant | undefined |"default" |"light" |"reset" | - |