Context Menu

Web

Displays a menu of actions at the pointer, opened with a right click or long press.

برای باز کردن منو کلیک راست کنید

Installation

pnpm dlx cubix-ui@latest add context-menu --base radix

Usage

Import
import {
  ContextMenu,
  ContextMenuContent,
  ContextMenuItem,
  ContextMenuSeparator,
  ContextMenuTrigger,
} from "@/components/cubix/context-menu"

const triggerClassName =
  "flex h-36 w-full max-w-xs items-center justify-center rounded-lg border border-dashed px-4 text-center text-caption text-muted-foreground"
Example
<ContextMenu dir="rtl" lang="fa">
  <ContextMenuTrigger className={triggerClassName}>
    <span className="pointer-coarse:hidden">برای باز کردن منو کلیک راست کنید</span>
    <span className="hidden pointer-coarse:inline">برای باز کردن منو لمس کنید و نگه دارید</span>
  </ContextMenuTrigger>
  <ContextMenuContent>
    <ContextMenuItem>بازگشت</ContextMenuItem>
    <ContextMenuItem disabled>جلو</ContextMenuItem>
    <ContextMenuItem>بارگذاری مجدد</ContextMenuItem>
    <ContextMenuSeparator />
    <ContextMenuItem>ذخیره صفحه به‌عنوان</ContextMenuItem>
    <ContextMenuItem>چاپ</ContextMenuItem>
  </ContextMenuContent>
</ContextMenu>

Composition

The menu reads dir and lang from the closest ancestor and carries them into the portaled content. Right click, long press, Shift+F10, or the ContextMenu key opens it at the pointer.

Structure
ContextMenu
├── ContextMenuTrigger
└── ContextMenuContent
    ├── ContextMenuItem
    ├── ContextMenuSeparator
    ├── ContextMenuCheckboxItem
    ├── ContextMenuRadioGroup
    │   └── ContextMenuRadioItem
    └── ContextMenuSub
        ├── ContextMenuSubTrigger
        └── ContextMenuSubContent
            └── ContextMenuItem

Accessibility

The trigger is focusable. The content is a menu, and checkbox and radio items announce their checked state. Focus moves into the menu when it opens and returns to the trigger when it closes.

KeyAction
Shift+F10 / ContextMenuOpens the menu at the focused trigger.
ArrowDown / ArrowUpMoves focus to the next or previous item.
Home / EndMoves focus to the first or last item.
Enter / SpaceActivates the focused item. Checkbox and radio items toggle and keep the menu open.
ArrowLeft / ArrowRightOn a submenu trigger, the key toward the inline end opens the submenu (ArrowLeft in RTL). The opposite key closes it and returns focus to the trigger.
EscapeCloses the menu and returns focus to the trigger.

Examples

Basic

Right click the trigger area, long press it on touch screens, or focus it and press Shift+F10 or the menu key. Set disabled on an item to lock it.

برای باز کردن منو کلیک راست کنید

Icons

Place an icon before the label inside ContextMenuItem or ContextMenuSubTrigger. It sits at the start of the item (the right side in RTL).

برای باز کردن منو کلیک راست کنید

Submenu

Nest menus with ContextMenuSub, ContextMenuSubTrigger, and ContextMenuSubContent. In RTL the submenu opens to the left and ArrowLeft opens it from the keyboard.

برای باز کردن منو کلیک راست کنید

Checkboxes

Use ContextMenuCheckboxItem for options that turn on and off. The menu stays open while toggling.

برای باز کردن منو کلیک راست کنید

Checkboxes with icons

Icons sit before the label; the checkbox control stays at the end of the item.

برای باز کردن منو کلیک راست کنید

Radio

Use ContextMenuRadioGroup with ContextMenuRadioItem for exclusive choices.

برای باز کردن منو کلیک راست کنید

Tick indicator

Set indicator="check" to show a plain tick instead of the Cubix Checkbox or Radio. The default is "control". On a radio group it applies to every item in the group.

برای باز کردن منو کلیک راست کنید

Destructive

Use variant="destructive" on ContextMenuItem for irreversible actions such as delete.

برای باز کردن منو کلیک راست کنید

API Reference

Note: Parts render data-slot attributes (context-menu-trigger, context-menu-content, context-menu-item, context-menu-checkbox-item, context-menu-radio-group, context-menu-radio-item, context-menu-separator, context-menu-sub-trigger, context-menu-sub-content) for targeting in tests and parent selectors. Use the same props on Base UI, React Aria, and Radix.

ContextMenu

PropTypeDefaultDescription
dir"ltr" | "rtl"-Text direction. Portaled menus inherit this so RTL layout and alignment stay correct. Defaults to the closest dir on the page.
langstring-Document language on the trigger and portaled content (e.g. "fa"). Enables IRANSans metrics and zero letter-spacing for Arab script.
openboolean-Controlled open state. Omit to use uncontrolled mode.
defaultOpenbooleanfalseInitial open state when uncontrolled (Base UI and React Aria; Radix UI has no defaultOpen).
onOpenChange(open: boolean) => void-Called when the open state changes.
childrenReact.ReactNode-A ContextMenuTrigger and a ContextMenuContent.

ContextMenuTrigger

PropTypeDefaultDescription
classNamestring-Additional Tailwind classes merged with the component styles (last one wins).
childrenReact.ReactNode-The area that opens the menu on right click, long press, Shift+F10 or the ContextMenu key. It is focusable so keyboard users can reach it.

ContextMenuContent

PropTypeDefaultDescription
dir"ltr" | "rtl"-Overrides the direction inherited from ContextMenu for this menu.
langstring-Overrides the language inherited from ContextMenu for this menu.
classNamestring-Additional Tailwind classes merged with the component styles (last one wins).
childrenReact.ReactNode-Menu items. The menu opens at the pointer and flips to stay in view.

ContextMenuItem

PropTypeDefaultDescription
insetboolean-Adds start padding to align with items that have indicators.
variant"default" | "destructive""default"Visual style of the menu item.
disabledboolean-Disables the menu item.
classNamestring-Additional Tailwind classes merged with the component styles (last one wins).

ContextMenuCheckboxItem

PropTypeDefaultDescription
checkedboolean-Controlled checked state of the checkbox item.
defaultCheckedboolean-Initial checked state when uncontrolled.
onCheckedChange(checked: boolean) => void-Called when the checked state changes.
indicator"check" | "control"-What shows when checked: the Cubix Checkbox ("control", default) or a plain tick ("check").
insetboolean-Adds start padding for alignment.
disabledboolean-Disables the checkbox item.
classNamestring-Additional Tailwind classes merged with the component styles (last one wins).

ContextMenuRadioGroup

PropTypeDefaultDescription
valuestring-Controlled value of the selected radio item.
defaultValuestring-Initial value when uncontrolled.
indicator"check" | "control"-Indicator for every item in the group: the Cubix Radio ("control", default) or a plain tick ("check"). An item's own indicator prop overrides it.
onValueChange(value: string) => void-Called when the selected value changes.

ContextMenuRadioItem

PropTypeDefaultDescription
valuestring-Value of this item. It is selected when it matches the group value.
indicator"check" | "control"-Overrides the group indicator for this item: the Cubix Radio ("control") or a plain tick ("check").
insetboolean-Adds start padding for alignment.
disabledboolean-Disables the radio item.
classNamestring-Additional Tailwind classes merged with the component styles (last one wins).