ContextMenu
ActionsA menu opened by right-clicking (or long-pressing) an area: <ContextMenu.Root> holds a ContextMenu.Trigger (the area) and a ContextMenu.Content at the pointer. It takes the same items as DropdownMenu: Item (variant="error", Shortcut), CheckboxItem, RadioGroup with RadioItems, Group with GroupHeading, Label, Separator and submenus. Arrow keys move, Enter chooses, Escape closes.
import { ContextMenu } from '@limerdev/zest'; DropdownMenu or buttons.Examples
A row
Right-click (or long-press) the card. Offer the same actions somewhere visible too.
Chose: nothing yet
<script lang="ts">
import { ContextMenu } from '@limerdev/zest';
let last = $state('nothing yet');
let pinned = $state(false);
</script>
<ContextMenu.Root>
<ContextMenu.Trigger
class="grid h-36 w-72 place-content-center rounded-box border-(length:--border) border-dashed border-border text-sm text-muted"
>
Right-click order 1042
</ContextMenu.Trigger>
<ContextMenu.Content class="w-52">
<ContextMenu.Item onSelect={() => (last = 'Open')}>
Open
<ContextMenu.Shortcut>↵</ContextMenu.Shortcut>
</ContextMenu.Item>
<ContextMenu.Item onSelect={() => (last = 'Duplicate')}>Duplicate</ContextMenu.Item>
<ContextMenu.CheckboxItem bind:checked={pinned}>Pinned</ContextMenu.CheckboxItem>
<ContextMenu.Separator />
<ContextMenu.Item variant="error" onSelect={() => (last = 'Delete')}>Delete</ContextMenu.Item>
</ContextMenu.Content>
</ContextMenu.Root>
<p class="text-sm text-muted">Chose: {last}{pinned ? ', pinned' : ''}</p> Props
Every part also takes class (merged over its defaults), ref (bindable: the element), children, and child (a snippet that
renders your own element, with props to spread on it).
ContextMenu.Root
| Prop | Type | Default | Description |
|---|---|---|---|
| dir | "ltr" | "rtl" | "ltr" | The direction of the site. |
| onOpenChange | OnChangeFn<boolean> | – | A callback that is called when the menu is opened or closed. |
| onOpenChangeComplete | OnChangeFn<boolean> | – | A callback that is called when the menu is opened or closed. |
| open bindable | boolean | false | The open state of the menu. |
ContextMenu.Trigger
| Prop | Type | Default | Description |
|---|---|---|---|
| disabled | boolean | – | Whether the context menu trigger is disabled. If disabled, the trigger will not open the menu when right-clicked. |
ContextMenu.Content
| Prop | Type | Default | Description |
|---|---|---|---|
| align | "start" | "center" | "end" | – | The preferred alignment of the anchor to render against when open. This may change when collisions occur. |
| alignOffset | number | – | An offset in pixels from the "start" or "end" alignment options. |
| arrowPadding | number | – | This describes the padding between the arrow and the edges of the floating element. If your floating element has border-radius, this will prevent it from overflowing the corners. |
| avoidCollisions | boolean | true | When true, overrides the side and align options to prevent collisions with the boundary edges. |
| collisionBoundary | Arrayable<Boundary> | – | A boundary element or array of elements to check for collisions against. |
| collisionPadding | number | Partial<Record<"top" | "right" | "bottom" | "left", number>> | 8 | The amount in pixels of virtual padding around the viewport edges to check for overflow which will cause a collision. |
| customAnchor | string | HTMLElement | Measurable | null | – | Use an element other than the trigger to anchor the content to. If provided, the content will be anchored to the provided element instead of the trigger. You can pass a selector string or an HTMLElement. |
| dir | "ltr" | "rtl" | – | The text direction of the content. |
| escapeKeydownBehavior | "close" | "defer-otherwise-close" | "defer-otherwise-ignore" | "ignore" | close | Escape behavior type. close: Closes the element immediately. defer-otherwise-close: Delegates the action to its parent component that has an escape keydown handler. If no parent is found, it closes the element. defer-otherwise-ignore: Delegates the action to the parent element. If no parent is found, nothing is done. ignore: Prevents the element from closing and also blocks the parent element from closing in response to an escape key press. |
| forceMount | boolean | – | Whether to force mount the component. |
| hideWhenDetached | boolean | – | |
| interactOutsideBehavior | "close" | "defer-otherwise-close" | "defer-otherwise-ignore" | "ignore" | close | Interact outside behavior type. close: Closes the element immediately. defer-otherwise-close: Delegates the action to the parent element. If no parent is found, it closes the element. defer-otherwise-ignore: Delegates the action to the parent element. If no parent is found, nothing is done. ignore: Prevents the element from closing and also blocks the parent element from closing in response to an outside interaction. |
| loop | boolean | false | When true, the menu will loop through items when navigating with the keyboard. |
| onCloseAutoFocus | EventCallback | – | Event handler called when auto-focusing on close. Can be prevented. |
| onEscapeKeydown | ((e: KeyboardEvent) => void) | – | Callback fired when escape is pressed. |
| onFocusOutside | ((event: FocusEvent) => void) | – | Callback fired when focus leaves the dismissible layer. |
| onInteractOutside | InteractOutsideEventHandler | – | Callback fired when an outside interaction event completes, which is either a pointerup, mouseup, or touchend event, depending on the user's input device. |
| onOpenAutoFocus | EventCallback | – | Event handler called when auto-focusing on open. Can be prevented. |
| portalProps | PortalProps | – | Props for the portal the content renders in: to (the element to render in, the body by default) and disabled (render in place). |
| preventOverflowTextSelection | boolean | true | Passing true will prevent the overflow of text selection outside the element, provided the element is the top layer. |
| preventScroll | boolean | true | Whether to prevent scrolling the body when the content is open. Whether to prevent body scrolling when the content is open. |
| side | "top" | "right" | "bottom" | "left" | – | The preferred side of the anchor to render against when open. Will be reversed when collisions occur. |
| sideOffset | number | – | The distance in pixels from the anchor to the floating element. |
| sticky | "partial" | "always" | – | |
| strategy | "absolute" | "fixed" | – | The positioning strategy to use for the floating element. |
| trapFocus | boolean | false | Whether focus is trapped within the focus scope. |
| updatePositionStrategy | "always" | "optimized" | "optimized" | "optimized" will only update the position when necessary, while "always" will update the position on each animation frame, which is useful if the floating content is following something like a mouse cursor. |
ContextMenu.Portal
| Prop | Type | Default | Description |
|---|---|---|---|
| disabled | boolean | false | Disable portalling and render the component inline |
| to | PortalTarget | document.body | Where to portal the content to. |
ContextMenu.Item
| Prop | Type | Default | Description |
|---|---|---|---|
| closeOnSelect | boolean | true | Whether or not the menu item should close when selected. |
| disabled | boolean | false | When true, the user will not be able to interact with the menu item. |
| inset | boolean | – | Indent the text to line up with items that show an icon, check or dot. |
| onSelect | ((event: Event) => void) | – | A callback fired when the menu item is selected. Prevent default behavior of selection with event.preventDefault(). |
| textValue | string | undefined | Optional text to use for typeahead filtering. By default, typeahead will use the .textContent of the menu item. When the content is more complex, you can provide a string here instead. |
| variant | "error" | "default" | 'default' | error colours the item for deleting. |
ContextMenu.CheckboxItem
| Prop | Type | Default | Description |
|---|---|---|---|
| checked bindable | boolean | false | The checked state of the checkbox. It can be one of: - true for checked - false for unchecked |
| closeOnSelect | boolean | false | Whether or not the menu item should close when selected. |
| disabled | boolean | false | When true, the user will not be able to interact with the menu item. |
| indeterminate bindable | boolean | false | Whether the checkbox is in an indeterminate state or not. |
| onCheckedChange | OnChangeFn<boolean> | – | A callback that is fired when the checked state changes. |
| onIndeterminateChange | OnChangeFn<boolean> | – | A callback function called when the indeterminate state changes. |
| onSelect | ((event: Event) => void) | – | A callback fired when the menu item is selected. Prevent default behavior of selection with event.preventDefault(). |
| textValue | string | undefined | Optional text to use for typeahead filtering. By default, typeahead will use the .textContent of the menu item. When the content is more complex, you can provide a string here instead. |
| value | string | – | The value of the checkbox item when used in a checkbox group. |
ContextMenu.CheckboxGroup
| Prop | Type | Default | Description |
|---|---|---|---|
| onValueChange | OnChangeFn<string[]> | – | A callback that is fired when the selected checkbox items change. |
| value bindable | string[] | [] | The values of the selected checkbox items. Supports two-way binding with bind:value. |
ContextMenu.RadioGroup
| Prop | Type | Default | Description |
|---|---|---|---|
| onValueChange | OnChangeFn<string> | – | A callback that is fired when the selected radio item changes. |
| value bindable | string | '' | The value of the selected radio item. Supports two-way binding with bind:value. |
ContextMenu.RadioItem
| Prop | Type | Default | Description |
|---|---|---|---|
| closeOnSelect | boolean | false | Whether or not the menu item should close when selected. |
| disabled | boolean | false | When true, the user will not be able to interact with the menu item. |
| onSelect | ((event: Event) => void) | – | A callback fired when the menu item is selected. Prevent default behavior of selection with event.preventDefault(). |
| textValue | string | undefined | Optional text to use for typeahead filtering. By default, typeahead will use the .textContent of the menu item. When the content is more complex, you can provide a string here instead. |
| value required | string | – | The value of the radio item. |
ContextMenu.Group
No props of its own beyond the common ones.
ContextMenu.GroupHeading
| Prop | Type | Default | Description |
|---|---|---|---|
| inset | boolean | – | Indent the text to line up with items that show an icon, check or dot. |
ContextMenu.Label
| Prop | Type | Default | Description |
|---|---|---|---|
| inset | boolean | – | Indent the text to line up with items that show an icon, check or dot. |
ContextMenu.Separator
No props of its own beyond the common ones.
ContextMenu.Shortcut
No props of its own beyond the common ones.
ContextMenu.Sub
| Prop | Type | Default | Description |
|---|---|---|---|
| onOpenChange | OnChangeFn<boolean> | – | A callback that is called when the menu is opened or closed. |
| onOpenChangeComplete | OnChangeFn<boolean> | – | A callback that is called when the menu finishes opening/closing animations. |
| open bindable | boolean | false | The open state of the menu. |
ContextMenu.SubTrigger
| Prop | Type | Default | Description |
|---|---|---|---|
| disabled | boolean | false | When true, the user will not be able to interact with the menu item. |
| inset | boolean | – | Indent the text to line up with items that show an icon, check or dot. |
| onSelect | ((event: Event) => void) | – | A callback fired when the menu item is selected. Prevent default behavior of selection with event.preventDefault(). |
| openDelay | number | 100 | The amount of time in ms from when the mouse enters the subtrigger until the submenu opens. This is useful for preventing the submenu from opening as a user is moving their mouse through the menu without a true intention to open that submenu. To disable the behavior, set it to 0. |
| textValue | string | undefined | Optional text to use for typeahead filtering. By default, typeahead will use the .textContent of the menu item. When the content is more complex, you can provide a string here instead. |
ContextMenu.SubContent
| Prop | Type | Default | Description |
|---|---|---|---|
| align | "start" | "center" | "end" | – | The preferred alignment of the anchor to render against when open. This may change when collisions occur. |
| alignOffset | number | – | An offset in pixels from the "start" or "end" alignment options. |
| arrowPadding | number | – | This describes the padding between the arrow and the edges of the floating element. If your floating element has border-radius, this will prevent it from overflowing the corners. |
| avoidCollisions | boolean | true | When true, overrides the side and align options to prevent collisions with the boundary edges. |
| collisionBoundary | Arrayable<Boundary> | – | A boundary element or array of elements to check for collisions against. |
| collisionPadding | number | Partial<Record<"top" | "right" | "bottom" | "left", number>> | 8 | The amount in pixels of virtual padding around the viewport edges to check for overflow which will cause a collision. |
| customAnchor | string | HTMLElement | Measurable | null | – | Use an element other than the trigger to anchor the content to. If provided, the content will be anchored to the provided element instead of the trigger. You can pass a selector string or an HTMLElement. |
| dir | "ltr" | "rtl" | – | The text direction of the content. |
| escapeKeydownBehavior | "close" | "defer-otherwise-close" | "defer-otherwise-ignore" | "ignore" | close | Escape behavior type. close: Closes the element immediately. defer-otherwise-close: Delegates the action to its parent component that has an escape keydown handler. If no parent is found, it closes the element. defer-otherwise-ignore: Delegates the action to the parent element. If no parent is found, nothing is done. ignore: Prevents the element from closing and also blocks the parent element from closing in response to an escape key press. |
| forceMount | boolean | – | Whether to force mount the component. |
| hideWhenDetached | boolean | – | |
| interactOutsideBehavior | "close" | "defer-otherwise-close" | "defer-otherwise-ignore" | "ignore" | close | Interact outside behavior type. close: Closes the element immediately. defer-otherwise-close: Delegates the action to the parent element. If no parent is found, it closes the element. defer-otherwise-ignore: Delegates the action to the parent element. If no parent is found, nothing is done. ignore: Prevents the element from closing and also blocks the parent element from closing in response to an outside interaction. |
| loop | boolean | false | When true, the menu will loop through items when navigating with the keyboard. |
| onCloseAutoFocus | EventCallback | – | Event handler called when auto-focusing on close. Can be prevented. |
| onEscapeKeydown | ((e: KeyboardEvent) => void) | – | Callback fired when escape is pressed. |
| onFocusOutside | ((event: FocusEvent) => void) | – | Callback fired when focus leaves the dismissible layer. |
| onInteractOutside | InteractOutsideEventHandler | – | Callback fired when an outside interaction event completes, which is either a pointerup, mouseup, or touchend event, depending on the user's input device. |
| onOpenAutoFocus | EventCallback | – | Event handler called when auto-focusing on open. Can be prevented. |
| preventOverflowTextSelection | boolean | true | Passing true will prevent the overflow of text selection outside the element, provided the element is the top layer. |
| side | "top" | "right" | "bottom" | "left" | – | The preferred side of the anchor to render against when open. Will be reversed when collisions occur. |
| sideOffset | number | – | The distance in pixels from the anchor to the floating element. |
| sticky | "partial" | "always" | – | |
| strategy | "absolute" | "fixed" | – | The positioning strategy to use for the floating element. |
| trapFocus | boolean | false | Whether focus is trapped within the focus scope. |
| updatePositionStrategy | "always" | "optimized" | "optimized" | "optimized" will only update the position when necessary, while "always" will update the position on each animation frame, which is useful if the floating content is following something like a mouse cursor. |