# Context Menu

- **Section:** Components
- **URL:** https://refast.fastapicloud.dev/docs/components/context-menu
- **Markdown:** https://refast.fastapicloud.dev/docs/components/context-menu.md
- **Source:** https://github.com/idling-mind/refast-docs/blob/main/docs/pages/components/buttons/context_menu.py
- **Live examples:** the widgets on this page need a browser; open the URL above to try them.
- **Applies to:** refast 0.12.2

Displays a floating menu when the user right-clicks (or long-presses on mobile) on
a target element. Supports items, separators, and checkbox items.

```python
from refast.components.shadcn.overlay import (
    ContextMenu, ContextMenuTrigger, ContextMenuContent,
    ContextMenuItem, ContextMenuSeparator, ContextMenuCheckboxItem,
)

ContextMenu(
    children=[
        ContextMenuTrigger(
            Container("Right-click here", class_name="border p-10 rounded-lg")
        ),
        ContextMenuContent(
            ContextMenuItem("Cut", icon="scissors", on_select=ctx.callback(handle_cut)),
            ContextMenuItem("Copy", icon="copy", on_select=ctx.callback(handle_copy)),
            ContextMenuItem("Paste", icon="clipboard", on_select=ctx.callback(handle_paste)),
        ),
    ]
)
```

```python
ContextMenu(
    children=[
        ContextMenuTrigger(
            Container("Right-click here", ...)
        ),
        ContextMenuContent(
            ContextMenuItem("Back", icon="arrow-left",
                           on_select=ctx.callback(handle_back)),
            ContextMenuItem("Reload", icon="refresh-cw",
                           on_select=ctx.callback(handle_reload)),
            ContextMenuSeparator(),
            ContextMenuCheckboxItem("Show Bookmarks",
                checked=show_bookmarks,
                on_checked_change=ctx.callback(handle_bookmarks)),
        ),
    ]
)
```

## Component Reference

### ContextMenu

The root wrapper. Does not accept any interaction props directly — behaviour is
derived from its `ContextMenuTrigger` and `ContextMenuContent` children.

| Prop | Type | Default | Description |
|------|------|---------|-------------|
| `children` | `list` | *(required)* | Must contain `ContextMenuTrigger` and `ContextMenuContent` |
| `class_name` | `str` | `""` | Extra Tailwind classes on the root |

### ContextMenuTrigger

Wraps the element that will show the menu on right-click.

| Prop | Type | Default | Description |
|------|------|---------|-------------|
| `children` | `Component` | *(required)* | The element the user right-clicks |
| `as_child` | `bool` | `False` | Merge props onto the child instead of wrapping |

### ContextMenuContent

The popover panel that appears after right-clicking.

| Prop | Type | Default | Description |
|------|------|---------|-------------|
| `children` | `list` | *(required)* | Menu items |
| `class_name` | `str` | `""` | Extra Tailwind classes |

### ContextMenuItem

| Prop | Type | Default | Description |
|------|------|---------|-------------|
| `label` | `str` | *(required)* | Item text |
| `icon` | `str \| None` | `None` | Lucide icon name |
| `shortcut` | `str \| None` | `None` | Keyboard shortcut hint (display only) |
| `disabled` | `bool` | `False` | Greys out and prevents selection |
| `on_select` | `Callback \| None` | `None` | Called when item is selected |

### ContextMenuSeparator

A horizontal rule with no configurable props (besides `class_name`).

### ContextMenuCheckboxItem

| Prop | Type | Default | Description |
|------|------|---------|-------------|
| `label` | `str` | *(required)* | Item text |
| `checked` | `bool` | `False` | Whether the checkbox is checked |
| `disabled` | `bool` | `False` | Prevents interaction |
| `on_checked_change` | `Callback \| None` | `None` | Called with new `bool` value |

## Common Patterns

```python
# File manager row
ContextMenu(
    children=[
        ContextMenuTrigger(
            Row(children=[Text("document.pdf"), Text("2 MB")])
        ),
        ContextMenuContent(
            ContextMenuItem("Open", icon="folder-open",
                           on_select=ctx.callback(handle_open)),
            ContextMenuItem("Rename", icon="edit",
                           on_select=ctx.callback(handle_rename)),
            ContextMenuSeparator(),
            ContextMenuItem("Move to Trash", icon="trash",
                           on_select=ctx.callback(handle_delete)),
        ),
    ]
)

# Image with context menu
ContextMenu(
    children=[
        ContextMenuTrigger(
            Container(class_name="w-64 h-40 bg-muted rounded-lg overflow-hidden")
        ),
        ContextMenuContent(
            ContextMenuItem("Save Image As…", icon="download",
                           on_select=ctx.callback(handle_save)),
            ContextMenuItem("Copy Image", icon="copy",
                           on_select=ctx.callback(handle_copy)),
        ),
    ]
)
```

---

Part of the Refast documentation: [all pages](https://refast.fastapicloud.dev/llms.txt), [everything on one page](https://refast.fastapicloud.dev/llms-full.txt), [search](https://refast.fastapicloud.dev/api/search?q=save_prop).
