Context Menu

A menu of contextual actions opened from a pointer or keyboard target.

Installation

Install the component package, shared coss-svelte theme, and Bits UI peer.

npm install coss-svelte @coss-svelte/theme bits-ui

Usage

Import the Svelte component exports directly from the package.

import { ContextMenu, ContextMenuCheckboxItem, ContextMenuGroup, ContextMenuGroupLabel, ContextMenuItem, ContextMenuLinkItem, ContextMenuPopup, ContextMenuRadioGroup, ContextMenuRadioItem, ContextMenuSeparator, ContextMenuShortcut, ContextMenuSub, ContextMenuSubPopup, ContextMenuSubTrigger, ContextMenuTrigger } from "coss-svelte";

Anatomy

  • ContextMenu
  • ContextMenuCheckboxItem
  • ContextMenuGroup
  • ContextMenuGroupLabel
  • ContextMenuItem
  • ContextMenuLinkItem
  • ContextMenuPopup
  • ContextMenuRadioGroup
  • ContextMenuRadioItem
  • ContextMenuSeparator
  • ContextMenuShortcut
  • ContextMenuSub
  • ContextMenuSubPopup
  • ContextMenuSubTrigger
  • ContextMenuTrigger

API Reference

ContextMenu

A menu of contextual actions opened from a pointer or keyboard target.

Signatures

{ open?: boolean; onOpenChange?: OnChangeFn<boolean>; onOpenChangeComplete?: OnChangeFn<boolean>; dir?: Direction }
PropTypeDefaultDescription
bind:openbooleanfalseOpen state for the popup or disclosure. Bind with `bind:open`.
dirDirection"ltr"Sets logical keyboard navigation, placement, and chevron direction.
children : Snippet<[]>

Inherits from Bits UI ContextMenu.Root.

ContextMenuCheckboxItem

Renders a bindable checkbox action with either a checkmark or switch presentation.

Signatures

{ checked?: boolean; onCheckedChange?: OnChangeFn<boolean>; indeterminate?: boolean; onIndeterminateChange?: OnChangeFn<boolean> }
PropTypeDefaultDescription
bind:checkedbooleanfalseChecked state for the control. Bind with `bind:checked` when supported.
bind:indeterminatebooleanfalseDisplays the checkbox in an indeterminate state.
variantContextMenuCheckboxVariant"default"Visual variant for the component.
disabledbooleanfalseDisables interaction with the control.
closeOnSelectbooleantrueCloses the context menu after the item is selected.
textValuestring-Provides plain text for keyboard typeahead when item content is complex.
children : Snippet<[MenuCheckboxItemSnippetProps]>bind:ref : HTMLElement | null

Inherits from Bits UI ContextMenu.CheckboxItem.

ContextMenuGroup

Groups related contextual actions without adding another interactive layer.

children : Snippet<[]>bind:ref : HTMLElement | null

Inherits from Bits UI ContextMenu.Group.

ContextMenuGroupLabel

Labels a related group of contextual actions.

PropTypeDefaultDescription
insetbooleanfalseAdds leading space so the item aligns with rows that render an indicator.
children : Snippet<[]>bind:ref : HTMLElement | null

Inherits from Bits UI ContextMenu.GroupHeading.

ContextMenuItem

Renders a selectable contextual action with default or destructive styling.

PropTypeDefaultDescription
variantContextMenuItemVariant"default"Visual variant for the component.
insetbooleanfalseAdds leading space so the item aligns with rows that render an indicator.
disabledbooleanfalseDisables interaction with the control.
closeOnSelectbooleantrueCloses the context menu after the item is selected.
textValuestring-Provides plain text for keyboard typeahead when item content is complex.
children : Snippet<[]>child : Snippet<[{ props: Record<string, unknown> }]>bind:ref : HTMLElement | null

Inherits from Bits UI ContextMenu.Item.

ContextMenuLinkItem

Renders a menu item as a semantic anchor while preserving menu keyboard behavior.

PropTypeDefaultDescription
hrefstring-Renders the component as a link target when provided.
targetHTMLAttributeAnchorTarget | null-Chooses the browsing context used by a link item.
relstring | null-Describes the relationship between a link item and its destination.
downloadstring | boolean-Forwards the anchor download attribute to a link item.
hreflangstring | null-Declares the language of a link item's destination.
referrerpolicyReferrerPolicy | null-Controls referrer information sent when a link item is followed.
variantContextMenuItemVariant"default"Visual variant for the component.
insetbooleanfalseAdds leading space so the item aligns with rows that render an indicator.
disabledbooleanfalseDisables interaction with the control.
closeOnSelectbooleantrueCloses the context menu after the item is selected.
textValuestring-Provides plain text for keyboard typeahead when item content is complex.
children : Snippet<[]>bind:ref : HTMLAnchorElement | null

Inherits from Bits UI ContextMenu.Item.

ContextMenuPopup

Portals and positions the contextual menu surface at the pointer or keyboard anchor.

PropTypeDefaultDescription
side"top" | "right" | "bottom" | "left""bottom"Side from which the surface appears.
sideOffsetnumber4Sets the distance between the pointer anchor and popup in pixels.
align"center" | "start" | "end""center"Aligns the popup with the virtual pointer anchor.
alignOffsetnumber0Offsets the popup from its preferred alignment in pixels.
collisionPaddingnumber | Partial<Record<"top" | "right" | "bottom" | "left", number>>8Keeps the popup inset from viewport collision boundaries.
loopbooleantrueWraps keyboard focus from the last menu item to the first and back.
preventScrollbooleantrueLocks document scrolling while the root popup is open.
forceMountboolean-Keeps content mounted while closed so transitions can complete.
escapeKeydownBehaviorEscapeBehaviorType"defer-otherwise-close"Defers to an open submenu Escape layer before closing the root menu.
interactOutsideBehaviorInteractOutsideBehaviorType"close"Controls how this floating layer responds to an outside pointer interaction.
portalProps{ disabled?: boolean; to?: PortalTarget }{}Configures the exact Bits Portal target (`to`) or renders inline when `disabled`.
children : Snippet<[]>bind:ref : HTMLElement | null

Inherits from Bits UI ContextMenu.Content.

ContextMenuRadioGroup

Coordinates one selected value across related radio menu items.

Signatures

{ value?: string; onValueChange?: OnChangeFn<string> }
PropTypeDefaultDescription
bind:valuestring""Selected radio item value.
children : Snippet<[]>bind:ref : HTMLElement | null

Inherits from Bits UI ContextMenu.RadioGroup.

ContextMenuRadioItem

Renders one bindable single-choice action within a radio group.

PropTypeDefaultDescription
valuestring-Value selected by this radio item.
disabledbooleanfalseDisables interaction with the control.
closeOnSelectbooleantrueCloses the context menu after the item is selected.
textValuestring-Provides plain text for keyboard typeahead when item content is complex.
children : Snippet<[MenuRadioItemSnippetProps]>bind:ref : HTMLElement | null

Inherits from Bits UI ContextMenu.RadioItem.

ContextMenuSeparator

Visually separates groups of contextual actions.

children : Snippet<[]>bind:ref : HTMLElement | null

Inherits from Bits UI ContextMenu.Separator.

ContextMenuShortcut

Displays a semantic keyboard shortcut hint aligned to an item.

children : Snippet<[]>bind:ref : HTMLElement | null

Inherits from Svelte <kbd> attributes.

ContextMenuSub

Provides independent open state for a nested contextual menu.

Signatures

{ open?: boolean; onOpenChange?: OnChangeFn<boolean>; onOpenChangeComplete?: OnChangeFn<boolean> }
PropTypeDefaultDescription
bind:openbooleanfalseOpen state for the popup or disclosure. Bind with `bind:open`.
children : Snippet<[]>

Inherits from Bits UI ContextMenu.Sub.

ContextMenuSubPopup

Portals and positions the floating surface for a nested menu.

PropTypeDefaultDescription
side"top" | "right" | "bottom" | "left""right" in LTR; "left" in RTLPlaces the submenu at logical inline-end unless explicitly overridden.
sideOffsetnumber0Sets the distance between the pointer anchor and popup in pixels.
align"center" | "start" | "end""start"Aligns the submenu with its trigger.
alignOffsetnumber-5 (0 when align is center)Offsets non-centered submenu alignment toward its parent row.
collisionPaddingnumber | Partial<Record<"top" | "right" | "bottom" | "left", number>>8Keeps the submenu inset from viewport collision boundaries.
loopbooleantrueWraps keyboard focus from the last menu item to the first and back.
forceMountboolean-Keeps content mounted while closed so transitions can complete.
escapeKeydownBehaviorEscapeBehaviorType"close"Closes this submenu before the root menu and restores focus to its trigger.
interactOutsideBehaviorInteractOutsideBehaviorType"defer-otherwise-close"Controls how this floating layer responds to an outside pointer interaction.
portalProps{ disabled?: boolean; to?: PortalTarget }{}Configures the exact Bits Portal target (`to`) or renders inline when `disabled`.
children : Snippet<[]>bind:ref : HTMLElement | null

Inherits from Bits UI ContextMenu.SubContent.

ContextMenuSubTrigger

Opens a nested contextual menu with pointer or keyboard input.

PropTypeDefaultDescription
insetbooleanfalseAdds leading space so the item aligns with rows that render an indicator.
disabledbooleanfalseDisables interaction with the control.
textValuestring-Provides plain text for keyboard typeahead when item content is complex.
openDelaynumber100Delays pointer-triggered submenu opening by this many milliseconds.
children : Snippet<[]>child : Snippet<[{ props: Record<string, unknown> }]>bind:ref : HTMLElement | null

Inherits from Bits UI ContextMenu.SubTrigger.

ContextMenuTrigger

Defines the right-click target and opens from Shift+F10 or the Context Menu key.

PropTypeDefaultDescription
disabledbooleanfalseDisables interaction with the control.
children : Snippet<[]>child : Snippet<[{ props: Record<string, unknown> }]>bind:ref : HTMLElement | null

Inherits from Bits UI ContextMenu.Trigger.

Implementation Details

Status
Stable
Foundation
bits
Category
Overlays & Popups
Particles
8