Skip to main content

Starwind UI v3.0 is now available! Migration guide

Context Menu

Installation

Usage

---
import {
ContextMenu,
ContextMenuCheckboxItem,
ContextMenuContent,
ContextMenuGroup,
ContextMenuItem,
ContextMenuSeparator,
ContextMenuShortcut,
ContextMenuSub,
ContextMenuSubContent,
ContextMenuSubTrigger,
ContextMenuTrigger,
} from "@/components/starwind/context-menu";
---
<ContextMenu>
<ContextMenuTrigger class="flex aspect-video w-full min-w-40 max-w-xs items-center justify-center rounded-xl border border-dashed text-sm">
<span class="hidden pointer-fine:inline-block">Right click here</span>
<span class="hidden pointer-coarse:inline-block">Long press here</span>
</ContextMenuTrigger>
<ContextMenuContent class="w-48">
<ContextMenuGroup>
<ContextMenuItem>
Back
<ContextMenuShortcut>⌘[</ContextMenuShortcut>
</ContextMenuItem>
<ContextMenuItem disabled>
Forward
<ContextMenuShortcut>⌘]</ContextMenuShortcut>
</ContextMenuItem>
<ContextMenuItem>
Reload
<ContextMenuShortcut>⌘R</ContextMenuShortcut>
</ContextMenuItem>
<ContextMenuSub>
<ContextMenuSubTrigger>More Tools</ContextMenuSubTrigger>
<ContextMenuSubContent class="w-44">
<ContextMenuGroup>
<ContextMenuItem>Save Page...</ContextMenuItem>
<ContextMenuItem>Create Shortcut...</ContextMenuItem>
<ContextMenuItem>Name Window...</ContextMenuItem>
</ContextMenuGroup>
<ContextMenuSeparator />
<ContextMenuGroup>
<ContextMenuItem>Developer Tools</ContextMenuItem>
</ContextMenuGroup>
<ContextMenuSeparator />
<ContextMenuGroup>
<ContextMenuItem class="text-destructive data-highlighted:text-destructive focus:text-destructive">Delete</ContextMenuItem>
</ContextMenuGroup>
</ContextMenuSubContent>
</ContextMenuSub>
</ContextMenuGroup>
<ContextMenuSeparator />
<ContextMenuGroup>
<ContextMenuCheckboxItem defaultChecked>Show Bookmarks</ContextMenuCheckboxItem>
<ContextMenuCheckboxItem>Show Full URLs</ContextMenuCheckboxItem>
</ContextMenuGroup>
</ContextMenuContent>
</ContextMenu>

Composition

Use the following composition to build a ContextMenu:

ContextMenu
├── ContextMenuTrigger
└── ContextMenuContent
├── ContextMenuGroup
│ ├── ContextMenuLabel
│ ├── ContextMenuItem
│ └── ContextMenuCheckboxItem
├── ContextMenuSeparator
└── ContextMenuSub
├── ContextMenuSubTrigger
└── ContextMenuSubContent
└── ContextMenuGroup
├── ContextMenuItem
└── ContextMenuItem

Basic

A simple context menu with a few actions.

Use ContextMenuSub, ContextMenuSubTrigger, and ContextMenuSubContent to nest secondary actions.

Shortcuts

Add ContextMenuShortcut to show keyboard hints.

With Icons

Combine icons with labels for quick scanning.

Checkboxes

Use ContextMenuCheckboxItem for toggle actions.

API Reference

Styled Component API

These props are added or materially changed by the installed styled component. Standard HTML attributes remain available through the inherited interfaces noted below. Expand a prop to see named type definitions and framework-specific imports. Follow the Primitive and Runtime links for lower-level behavior props.

Context Menu Content

Inherits div attributes.

Contains the following additional props:

Prop Type Default Toggle details
disablePortal boolean false
Description
Keeps the public Portal wrapper inline instead of moving it to a target.
Classification
Wrapper prop
portalContainer string
Description
Sets the CSS selector for the public Portal wrapper target.
Classification
Wrapper prop

Context Menu Item

Inherits div attributes.

Contains the following additional props:

Prop Type Default Toggle details
inset boolean false
Description
Adds leading inset spacing for visual alignment.
Classification
Wrapper prop

Context Menu Checkbox Item

Inherits div attributes. Omits `aria-checked` and `role`.

Contains the following additional props:

Prop Type Default Toggle details
closeOnClick boolean false
Description
Closes the containing surface after the item is activated.
Classification
Wrapper prop
indicatorClass string
Description
Adds classes to the generated selection indicator.
Classification
Wrapper prop
inset boolean false
Description
Adds leading inset spacing for visual alignment.
Classification
Wrapper prop
showIndicator boolean true
Description
Shows the generated selection indicator.
Classification
Wrapper prop

Context Menu Radio Item

Inherits div attributes. Omits `aria-checked` and `role`.

Contains the following additional props:

Prop Type Default Toggle details
closeOnClick boolean false
Description
Closes the containing surface after the item is activated.
Classification
Wrapper prop
indicatorClass string
Description
Adds classes to the generated selection indicator.
Classification
Wrapper prop
inset boolean false
Description
Adds leading inset spacing for visual alignment.
Classification
Wrapper prop
showIndicator boolean true
Description
Shows the generated selection indicator.
Classification
Wrapper prop
value Required string
Description
Controls or identifies the component value.
Classification
Wrapper prop

Context Menu Label

Inherits div attributes.

Contains the following additional props:

Prop Type Default Toggle details
inset boolean false
Description
Adds leading inset spacing for visual alignment.
Classification
Wrapper prop

Context Menu Sub Trigger

Inherits div attributes.

Contains the following additional props:

Prop Type Default Toggle details
inset boolean false
Description
Adds leading inset spacing for visual alignment.
Classification
Wrapper prop

Context Menu Sub Content

Inherits div attributes.

Contains the following additional props:

Prop Type Default Toggle details
disablePortal boolean false
Description
Keeps the public Portal wrapper inline instead of moving it to a target.
Classification
Wrapper prop
portalContainer string
Description
Sets the CSS selector for the public Portal wrapper target.
Classification
Wrapper prop
side "top" | "right" | "bottom" | "left" "right"
Description
Selects the preferred side for floating content.
Classification
Primitive override
Primitive prop
context-menu.Popup.side
sideOffset number 0
Description
Sets the distance in pixels between floating content and its anchor.
Classification
Primitive override
Primitive prop
context-menu.Popup.sideOffset

Primitive And Runtime API

Use these references when you need the lower-level behavior APIs behind Context Menu.

Runtime API

Context Menu primitive
createContextMenu from @starwind-ui/runtime/context-menu

Changelog

View version history v2.1.0 3 releases

v2.1.0

  • Added portalContainer and disablePortal to ContextMenuContent and ContextMenuSubContent for custom portal targets and inline rendering.
  • Kept touch-opened parent menus active while focus moves into a submenu, which keeps mobile submenus anchored to their triggers.

v2.0.1

  • Named the generated aggregate default export so React and Astro tooling can identify the installed component cleanly.

v2.0.0

  • Rebuilt Context Menu on Starwind Runtime for opening, keyboard navigation, submenus, and checked items.
  • See the Context Menu Primitive for the underlying unstyled anatomy and behavior API.