Context Menu
A menu that appears at the pointer on right click or long press.
<script setup lang="ts">
import { ContextMenu } from '@vase-ui/vue/context-menu';
import styles from './index.module.css';
const popupHiddenClass = styles.PopupHidden;
</script>
<template>
<ContextMenu.Root>
<ContextMenu.Trigger :class="styles.Trigger">Right click here</ContextMenu.Trigger>
<ContextMenu.Portal>
<ContextMenu.Positioner :class="styles.Positioner">
<Transition :enter-from-class="popupHiddenClass" :leave-to-class="popupHiddenClass">
<ContextMenu.Popup :class="styles.Popup">
<ContextMenu.Item :class="styles.Item">Add to Library</ContextMenu.Item>
<ContextMenu.Item :class="styles.Item">Add to Playlist</ContextMenu.Item>
<ContextMenu.Separator :class="styles.Separator" />
<ContextMenu.Item :class="styles.Item">Play Next</ContextMenu.Item>
<ContextMenu.Item :class="styles.Item">Play Last</ContextMenu.Item>
<ContextMenu.Separator :class="styles.Separator" />
<ContextMenu.Item :class="styles.Item">Favorite</ContextMenu.Item>
<ContextMenu.Item :class="styles.Item">Share</ContextMenu.Item>
</ContextMenu.Popup>
</Transition>
</ContextMenu.Positioner>
</ContextMenu.Portal>
</ContextMenu.Root>
</template>.Trigger {
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: center;
width: 100%;
max-width: 16rem;
aspect-ratio: 5 / 3;
outline: 0;
border: 1px solid oklch(14.5% 0 0deg);
border-radius: 0;
background-color: white;
font-size: 0.875rem;
line-height: 1.25rem;
color: oklch(14.5% 0 0deg);
-webkit-user-select: none;
user-select: none;
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
}
}
.Positioner {
outline: 0;
}
.Popup {
box-sizing: border-box;
outline: 0;
padding-block: 0.25rem;
border: 1px solid oklch(14.5% 0 0deg);
border-radius: 0;
background-color: white;
color: oklch(14.5% 0 0deg);
box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);
transform-origin: var(--transform-origin);
transition:
transform 100ms ease-out,
opacity 100ms ease-out;
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
box-shadow: none;
}
}
.Item {
outline: 0;
cursor: default;
-webkit-user-select: none;
user-select: none;
padding-block: 0.5rem;
padding-left: 1rem;
padding-right: 2rem;
display: flex;
font-size: 0.875rem;
line-height: 1rem;
&[data-highlighted] {
z-index: 0;
position: relative;
color: white;
@media (prefers-color-scheme: dark) {
color: oklch(14.5% 0 0deg);
}
}
&[data-highlighted]::before {
content: '';
z-index: -1;
position: absolute;
inset-block: 0;
inset-inline: 0.25rem;
background-color: oklch(14.5% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: white;
}
}
&[aria-disabled="true"] {
color: oklch(55.6% 0 0deg);
@media (prefers-color-scheme: dark) {
color: oklch(70.8% 0 0deg);
}
}
}
.Separator {
margin: 0.25rem;
height: 1px;
background-color: oklch(14.5% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: white;
}
}
/* The enter-from and leave-to class of the `<Transition>` around the popup. */
.PopupHidden {
opacity: 0;
transform: scale(0.98);
}<script setup lang="ts">
import { ContextMenu } from '@vase-ui/vue/context-menu';
const itemClass =
"flex cursor-default py-2 pr-8 pl-4 text-sm leading-4 outline-hidden select-none data-highlighted:relative data-highlighted:z-0 data-highlighted:text-white data-highlighted:before:absolute data-highlighted:before:inset-x-1 data-highlighted:before:inset-y-0 data-highlighted:before:z-[-1] data-highlighted:before:bg-neutral-950 data-highlighted:before:content-[''] aria-disabled:text-neutral-500 dark:data-highlighted:text-neutral-950 dark:data-highlighted:before:bg-white dark:aria-disabled:text-neutral-400";
const popupHiddenClass = 'scale-[0.98] opacity-0';
</script>
<template>
<ContextMenu.Root>
<ContextMenu.Trigger
class="flex aspect-5/3 w-full max-w-64 items-center justify-center rounded-none border border-neutral-950 bg-white text-neutral-950 select-none text-sm dark:border-white dark:bg-neutral-950 dark:text-white"
>Right click here</ContextMenu.Trigger
>
<ContextMenu.Portal>
<ContextMenu.Positioner class="outline-hidden">
<Transition :enter-from-class="popupHiddenClass" :leave-to-class="popupHiddenClass">
<ContextMenu.Popup
class="origin-[var(--transform-origin)] border border-neutral-950 bg-white py-1 text-neutral-950 shadow-[0.25rem_0.25rem_0] shadow-black/12 outline-hidden transition-[scale,opacity] duration-100 ease-out dark:border-white dark:bg-neutral-950 dark:text-white dark:shadow-none"
>
<ContextMenu.Item :class="itemClass">Add to Library</ContextMenu.Item>
<ContextMenu.Item :class="itemClass">Add to Playlist</ContextMenu.Item>
<ContextMenu.Separator class="mx-1 my-1 h-px bg-neutral-950 dark:bg-white" />
<ContextMenu.Item :class="itemClass">Play Next</ContextMenu.Item>
<ContextMenu.Item :class="itemClass">Play Last</ContextMenu.Item>
<ContextMenu.Separator class="mx-1 my-1 h-px bg-neutral-950 dark:bg-white" />
<ContextMenu.Item :class="itemClass">Favorite</ContextMenu.Item>
<ContextMenu.Item :class="itemClass">Share</ContextMenu.Item>
</ContextMenu.Popup>
</Transition>
</ContextMenu.Positioner>
</ContextMenu.Portal>
</ContextMenu.Root>
</template>Usage guidelines
- Use context menus as an enhancement: Do not make a context menu the only way to do an action. Users may not find or open a context menu, especially on touch devices or with assistive technology. Always show visible controls for the actions of the context menu.
Anatomy
Import the component and assemble its parts:
<script setup lang="ts">
import { ContextMenu } from '@vase-ui/vue/context-menu';
</script>
<template>
<ContextMenu.Root>
<ContextMenu.Trigger />
<ContextMenu.Portal>
<ContextMenu.Backdrop />
<ContextMenu.Positioner>
<ContextMenu.Popup>
<ContextMenu.Arrow />
<ContextMenu.Item />
<ContextMenu.LinkItem />
<ContextMenu.Separator />
<ContextMenu.SubmenuRoot>
<ContextMenu.SubmenuTrigger />
</ContextMenu.SubmenuRoot>
<ContextMenu.Group>
<ContextMenu.GroupLabel />
</ContextMenu.Group>
<ContextMenu.RadioGroup>
<ContextMenu.RadioItem value="" />
</ContextMenu.RadioGroup>
<ContextMenu.CheckboxItem />
</ContextMenu.Popup>
</ContextMenu.Positioner>
</ContextMenu.Portal>
</ContextMenu.Root>
</template>Examples
Menu has more demos. Many of them apply to the context menu too.
Nested menu
To make a submenu, put a ContextMenu.SubmenuRoot in the parent context menu. Use ContextMenu.SubmenuTrigger for the item that opens the nested menu.
<script setup lang="ts">
import { h } from 'vue';
import type { FunctionalComponent } from 'vue';
import { ContextMenu } from '@vase-ui/vue/context-menu';
import styles from './index.module.css';
const CaretRightIcon: FunctionalComponent = () =>
h(
'svg',
{
width: '16',
height: '16',
viewBox: '0 0 16 16',
fill: 'currentColor',
style: { display: 'block' },
},
[h('path', { d: 'M6 12V4l4.5 4z' })],
);
const popupHiddenClass = styles.PopupHidden;
</script>
<template>
<ContextMenu.Root>
<ContextMenu.Trigger :class="styles.Trigger">Right click here</ContextMenu.Trigger>
<ContextMenu.Portal>
<ContextMenu.Positioner :class="styles.Positioner">
<Transition :enter-from-class="popupHiddenClass" :leave-to-class="popupHiddenClass">
<ContextMenu.Popup :class="styles.Popup">
<ContextMenu.Item :class="styles.Item">Add to Library</ContextMenu.Item>
<ContextMenu.SubmenuRoot>
<ContextMenu.SubmenuTrigger :class="styles.SubmenuTrigger">
Add to Playlist
<CaretRightIcon />
</ContextMenu.SubmenuTrigger>
<ContextMenu.Portal>
<ContextMenu.Positioner
:class="styles.Positioner"
:align-offset="-4"
:side-offset="-4"
>
<Transition
:enter-from-class="popupHiddenClass"
:leave-to-class="popupHiddenClass"
>
<ContextMenu.Popup :class="styles.SubmenuPopup">
<ContextMenu.Item :class="styles.Item">Get Up!</ContextMenu.Item>
<ContextMenu.Item :class="styles.Item">Inside Out</ContextMenu.Item>
<ContextMenu.Item :class="styles.Item">Night Beats</ContextMenu.Item>
<ContextMenu.Separator :class="styles.Separator" />
<ContextMenu.Item :class="styles.Item">New playlist…</ContextMenu.Item>
</ContextMenu.Popup>
</Transition>
</ContextMenu.Positioner>
</ContextMenu.Portal>
</ContextMenu.SubmenuRoot>
<ContextMenu.Separator :class="styles.Separator" />
<ContextMenu.Item :class="styles.Item">Play Next</ContextMenu.Item>
<ContextMenu.Item :class="styles.Item">Play Last</ContextMenu.Item>
<ContextMenu.Separator :class="styles.Separator" />
<ContextMenu.Item :class="styles.Item">Favorite</ContextMenu.Item>
<ContextMenu.Item :class="styles.Item">Share</ContextMenu.Item>
</ContextMenu.Popup>
</Transition>
</ContextMenu.Positioner>
</ContextMenu.Portal>
</ContextMenu.Root>
</template>.Trigger {
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: center;
width: 100%;
max-width: 16rem;
aspect-ratio: 5 / 3;
outline: 0;
border: 1px solid oklch(14.5% 0 0deg);
border-radius: 0;
background-color: white;
font-size: 0.875rem;
line-height: 1.25rem;
color: oklch(14.5% 0 0deg);
-webkit-user-select: none;
user-select: none;
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
}
}
.Positioner {
outline: 0;
}
.Popup,
.SubmenuPopup {
box-sizing: border-box;
outline: 0;
padding-block: 0.25rem;
border: 1px solid oklch(14.5% 0 0deg);
border-radius: 0;
background-color: white;
color: oklch(14.5% 0 0deg);
box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);
transform-origin: var(--transform-origin);
transition:
transform 100ms ease-out,
opacity 100ms ease-out;
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
box-shadow: none;
}
}
.Arrow {
display: flex;
&[data-side='top'] {
bottom: -8px;
rotate: 180deg;
}
&[data-side='bottom'] {
top: -8px;
rotate: 0deg;
}
&[data-side='left'] {
right: -13px;
rotate: 90deg;
}
&[data-side='right'] {
left: -13px;
rotate: -90deg;
}
}
.ArrowFill {
fill: white;
@media (prefers-color-scheme: dark) {
fill: oklch(14.5% 0 0deg);
}
}
.ArrowOuterStroke {
fill: oklch(14.5% 0 0deg);
@media (prefers-color-scheme: dark) {
fill: white;
}
}
.ArrowInnerStroke {
fill: white;
@media (prefers-color-scheme: dark) {
fill: oklch(14.5% 0 0deg);
}
}
.Item,
.SubmenuTrigger {
outline: 0;
cursor: default;
-webkit-user-select: none;
user-select: none;
padding-block: 0.5rem;
padding-left: 1rem;
padding-right: 1.5rem;
display: flex;
font-size: 0.875rem;
line-height: 1rem;
&[data-highlighted] {
z-index: 0;
position: relative;
color: white;
@media (prefers-color-scheme: dark) {
color: oklch(14.5% 0 0deg);
}
}
&[data-highlighted]::before {
content: '';
z-index: -1;
position: absolute;
inset-block: 0;
inset-inline: 0.25rem;
background-color: oklch(14.5% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: white;
}
}
&[aria-disabled="true"] {
color: oklch(55.6% 0 0deg);
@media (prefers-color-scheme: dark) {
color: oklch(70.8% 0 0deg);
}
}
}
.SubmenuTrigger {
align-items: center;
justify-content: space-between;
gap: 1rem;
padding-right: 0.5rem;
&[data-popup-open] {
z-index: 0;
position: relative;
}
&[data-popup-open]::before {
content: '';
z-index: -1;
position: absolute;
inset-block: 0;
inset-inline: 0.25rem;
background-color: oklch(97% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(26.9% 0 0deg);
}
}
&[data-highlighted][data-popup-open]::before {
background-color: oklch(14.5% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: white;
}
}
}
.Separator {
margin: 0.25rem;
height: 1px;
background-color: oklch(14.5% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: white;
}
}
/* The enter-from and leave-to class of the `<Transition>` around the popup. */
.PopupHidden {
opacity: 0;
transform: scale(0.98);
}<script setup lang="ts">
import { h } from 'vue';
import type { FunctionalComponent } from 'vue';
import { ContextMenu } from '@vase-ui/vue/context-menu';
const CaretRightIcon: FunctionalComponent = () =>
h(
'svg',
{
width: '16',
height: '16',
viewBox: '0 0 16 16',
fill: 'currentColor',
style: { display: 'block' },
},
[h('path', { d: 'M6 12V4l4.5 4z' })],
);
const popupClass =
'origin-[var(--transform-origin)] border border-neutral-950 bg-white py-1 text-neutral-950 shadow-[0.25rem_0.25rem_0] shadow-black/12 outline-hidden transition-[scale,opacity] duration-100 ease-out dark:border-white dark:bg-neutral-950 dark:text-white dark:shadow-none';
const itemClass =
"flex cursor-default py-2 pr-6 pl-4 text-sm leading-4 outline-hidden select-none data-highlighted:relative data-highlighted:z-0 data-highlighted:text-white data-highlighted:before:absolute data-highlighted:before:inset-x-1 data-highlighted:before:inset-y-0 data-highlighted:before:z-[-1] data-highlighted:before:bg-neutral-950 data-highlighted:before:content-[''] aria-disabled:text-neutral-500 dark:data-highlighted:text-neutral-950 dark:data-highlighted:before:bg-white dark:aria-disabled:text-neutral-400";
const submenuTriggerClass =
"flex cursor-default items-center justify-between gap-4 py-2 pr-2 pl-4 text-sm leading-4 outline-hidden select-none data-popup-open:relative data-popup-open:z-0 data-popup-open:before:absolute data-popup-open:before:inset-x-1 data-popup-open:before:inset-y-0 data-popup-open:before:z-[-1] data-popup-open:before:bg-neutral-100 data-popup-open:before:content-[''] data-highlighted:relative data-highlighted:z-0 data-highlighted:text-white data-highlighted:before:absolute data-highlighted:before:inset-x-1 data-highlighted:before:inset-y-0 data-highlighted:before:z-[-1] data-highlighted:before:bg-neutral-950 data-highlighted:before:content-[''] data-highlighted:data-popup-open:before:bg-neutral-950 aria-disabled:text-neutral-500 dark:data-popup-open:before:bg-neutral-800 dark:data-highlighted:text-neutral-950 dark:data-highlighted:before:bg-white dark:data-highlighted:data-popup-open:before:bg-white dark:aria-disabled:text-neutral-400";
const popupHiddenClass = 'scale-[0.98] opacity-0';
</script>
<template>
<ContextMenu.Root>
<ContextMenu.Trigger
class="flex aspect-5/3 w-full max-w-64 items-center justify-center rounded-none border border-neutral-950 bg-white text-neutral-950 select-none text-sm dark:border-white dark:bg-neutral-950 dark:text-white"
>Right click here</ContextMenu.Trigger
>
<ContextMenu.Portal>
<ContextMenu.Positioner class="outline-hidden">
<Transition :enter-from-class="popupHiddenClass" :leave-to-class="popupHiddenClass">
<ContextMenu.Popup :class="popupClass">
<ContextMenu.Item :class="itemClass">Add to Library</ContextMenu.Item>
<ContextMenu.SubmenuRoot>
<ContextMenu.SubmenuTrigger :class="submenuTriggerClass">
Add to Playlist
<CaretRightIcon />
</ContextMenu.SubmenuTrigger>
<ContextMenu.Portal>
<ContextMenu.Positioner class="outline-hidden" :align-offset="-4" :side-offset="-4">
<Transition
:enter-from-class="popupHiddenClass"
:leave-to-class="popupHiddenClass"
>
<ContextMenu.Popup :class="popupClass">
<ContextMenu.Item :class="itemClass">Get Up!</ContextMenu.Item>
<ContextMenu.Item :class="itemClass">Inside Out</ContextMenu.Item>
<ContextMenu.Item :class="itemClass">Night Beats</ContextMenu.Item>
<ContextMenu.Separator class="mx-1 my-1 h-px bg-neutral-950 dark:bg-white" />
<ContextMenu.Item :class="itemClass">New playlist…</ContextMenu.Item>
</ContextMenu.Popup>
</Transition>
</ContextMenu.Positioner>
</ContextMenu.Portal>
</ContextMenu.SubmenuRoot>
<ContextMenu.Separator class="mx-1 my-1 h-px bg-neutral-950 dark:bg-white" />
<ContextMenu.Item :class="itemClass">Play Next</ContextMenu.Item>
<ContextMenu.Item :class="itemClass">Play Last</ContextMenu.Item>
<ContextMenu.Separator class="mx-1 my-1 h-px bg-neutral-950 dark:bg-white" />
<ContextMenu.Item :class="itemClass">Favorite</ContextMenu.Item>
<ContextMenu.Item :class="itemClass">Share</ContextMenu.Item>
</ContextMenu.Popup>
</Transition>
</ContextMenu.Positioner>
</ContextMenu.Portal>
</ContextMenu.Root>
</template>API reference
ContextMenu.Root
Groups the parts of a menu that opens at the pointer. Renders no element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
defaultOpen | boolean | undefined | false | The menu is open at the mount of an uncontrolled root. |
disabled | boolean | undefined | false | The user cannot open the menu or use its items. |
highlightItemOnHover | boolean | undefined | true | A move of the pointer highlights its item. With false, CSS :hover and
[data-highlighted] differ. |
loopFocus | boolean | undefined | true | An arrow key at the last item goes to the first item, and the other way. |
open | boolean | undefined | The menu is open. v-model:open binds it. | |
orientation | MenuRootOrientation | undefined | 'vertical' | The axis of the arrow keys that move the highlight. |
Slots
| Slot | Slot props | Description |
|---|---|---|
default | The trigger and the portal of the menu. |
Events
| Event | Arguments | Description |
|---|---|---|
openChange | [open: boolean, details: ContextMenuRootChangeEventDetails] | The menu opens or closes. details.cancel() keeps it as it was. |
update:open | [open: boolean] | Comes after openChange, unless a listener cancels the change. v-model:open uses it. |
itemHighlighted | [element: HTMLElement | undefined, details: ContextMenuRootHighlightEventDetails] | The highlighted item changes. It gets the element of the item, or undefined. |
Exposed
| Member | Type | Description |
|---|---|---|
close | () => void | Closes the menu. |
ContextMenu.Trigger
The area that opens the menu at a right press or at a long press. A long press of a touch or a
pen opens the menu after 500 ms, also where the browser sends no contextmenu event. A key
opens the menu only when the browser sends a contextmenu event for it.
Renders a <div> element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A tag or a component that the part renders in place of its own element, with the same
attributes and content. The element slot wins over it. |
Slots
| Slot | Slot props | Description |
|---|---|---|
default | state: ContextMenuTriggerState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: ContextMenuTriggerState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
[data-popup-open] | The menu is open. |
[data-pressed] | The menu is open, as with [data-popup-open]. |
ContextMenu.Portal
Moves its content to another place of the DOM, <body> by default.
Renders a visually hidden <span> in place while the moved popup is open. Its aria-owns names
the popup, so a screen reader reads the popup after its trigger. In a menu or a menubar, the
<span> has the role group.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
defer | boolean | undefined | false | Moves the content after the other content of the same update mounted. |
disabled | boolean | undefined | false | Renders the content in place, also on the server. |
keepMounted | boolean | undefined | false | The closed popup stays in the DOM, with hidden and inert. |
to | PortalTarget | undefined | The target of the content. With no value, the target of the nearest portal above, else
body. While it is null, the portal renders nothing. |
Slots
| Slot | Slot props | Description |
|---|---|---|
default | The content of the part. |
ContextMenu.Backdrop
An overlay under the menu.
Renders a <div> element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A tag or a component that the part renders in place of its own element, with the same
attributes and content. The element slot wins over it. |
Slots
| Slot | Slot props | Description |
|---|---|---|
default | state: MenuBackdropState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: MenuBackdropState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
[hidden] | The menu is closed, and keepMounted of the portal keeps the backdrop in the DOM. |
ContextMenu.Positioner
Places the popup at the pointer.
Renders a <div> element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
align | Align | undefined | 'center' | The alignment of the popup on the side. |
alignOffset | number | OffsetFunction | undefined | 0 | The offset on the align axis in pixels, or a function that gives it. |
anchor | Anchor | (() => Anchor) | The element to place the popup against: an element, a virtual element, a template ref of a part, or a function that gives one. With no value, the active trigger. | |
arrowPadding | number | undefined | 5 | The smallest space between the arrow and an inner edge of its nearest positioned ancestor, in pixels. |
collisionAvoidance | CollisionAvoidance | undefined | How the popup avoids the edge of the boundary. | |
collisionBoundary | Boundary | undefined | 'clipping-ancestors' | The area that holds the popup: an element, elements, or a rectangle. |
collisionPadding | Padding | undefined | 5 | The space between the popup and the edge of the boundary, in pixels. |
disableAnchorTracking | boolean | undefined | false | The popup does not follow a move of its anchor. |
side | Side | undefined | 'bottom' | The side of the anchor for the popup. It can change to avoid a collision. inline-start and
inline-end follow the CSS direction of the positioner: give the positioner dir when the
target of its portal has another direction. |
sideOffset | number | OffsetFunction | undefined | 0 | The distance from the anchor in pixels, or a function that gives it. |
sticky | boolean | undefined | false | The popup stays in the view after its anchor scrolls out of it. |
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A tag or a component that the part renders in place of its own element, with the same
attributes and content. The element slot wins over it. |
Slots
| Slot | Slot props | Description |
|---|---|---|
default | state: ContextMenuPositionerState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: ContextMenuPositionerState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
[data-side] | The side of the anchor that the popup is on: top, bottom, left, right, inline-start or inline-end. A logical side shows with a logical side prop, and for a submenu with no side prop. |
[data-align] | The alignment of the popup on its side: start, center or end. |
[data-anchor-hidden] | The anchor is out of view. |
CSS variables
| Variable | Description |
|---|---|
--anchor-width | The width of the anchor, in pixels. |
--anchor-height | The height of the anchor, in pixels. |
--available-width | The width from the anchor to the edge of the collision boundary, in pixels. |
--available-height | The height from the anchor to the edge of the collision boundary, in pixels. |
--transform-origin | The point that the popup grows from, for the transform-origin of the popup. It is at the arrow, on the edge of the anchor. With no arrow, it is at the aligned edge of the popup or at the center of the anchor. |
ContextMenu.Popup
The container of the items of the menu.
Renders a <div> element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
finalFocus | FocusTarget | undefined | The element that gets the focus at the close. By default: the trigger, else the element that had the focus before the open. After an open by code, that element comes before the trigger. | |
id | string | undefined | The trigger names the popup by this id in aria-controls, when the menu has no Menu.List.
With no value, a generated id. | |
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A tag or a component that the part renders in place of its own element, with the same
attributes and content. The element slot wins over it. |
Slots
| Slot | Slot props | Description |
|---|---|---|
default | state: MenuPopupState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: MenuPopupState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
[data-side] | The side of the anchor that the popup is on, as on the positioner. |
[data-align] | The alignment of the popup on its side: start, center or end. |
[data-instant] | An animation should not run: click after a key on the trigger, dismiss after Escape, group at a move between the menus of a menubar, or trigger-change after a move to another trigger. |
[hidden] | The menu is closed, and keepMounted of the portal keeps the popup in the DOM. |
CSS variables
| Variable | Description |
|---|---|
--popup-width | The width of the popup, in pixels: the old width, then the new width, while Menu.Viewport moves to another trigger. |
--popup-height | The height of the popup, in pixels: the old height, then the new height, while Menu.Viewport moves to another trigger. |
ContextMenu.Arrow
An element that points to the center of the anchor.
Renders a <div> element.
Its place is in the padding box of its nearest positioned ancestor: the popup when the popup has a position, else the positioner.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A tag or a component that the part renders in place of its own element, with the same
attributes and content. The element slot wins over it. |
Slots
| Slot | Slot props | Description |
|---|---|---|
default | state: MenuArrowState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: MenuArrowState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
[data-side] | The side of the anchor that the popup is on, as on the positioner. |
[data-align] | The alignment of the popup on its side: start, center or end. |
[data-uncentered] | The arrow cannot point to the center of the anchor. |
ContextMenu.Item
An item of the menu.
Renders a <div> element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
closeOnClick | boolean | undefined | true | A click closes the menu. |
disabled | boolean | undefined | false | The item keeps the focus, and does no action. |
id | string | undefined | Menu.Input names the highlighted item by this id in aria-activedescendant. With no value, a
generated id. | |
label | string | undefined | The text that the typeahead and the filter read. With no value, the text of the item. | |
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A tag or a component that the part renders in place of its own element, with the same
attributes and content. The element slot wins over it. |
Slots
| Slot | Slot props | Description |
|---|---|---|
default | state: MenuItemState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: MenuItemState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
[data-highlighted] | The item has the highlight. |
[aria-disabled="true"] | The item or the menu is disabled. |
[hidden] | The item does not match the query of the filter. |
ContextMenu.LinkItem
A link in the menu.
Renders an <a> element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
closeOnClick | boolean | undefined | false | A click closes the menu. |
id | string | undefined | Menu.Input names the highlighted item by this id in aria-activedescendant. With no value, a
generated id. | |
label | string | undefined | The text that the typeahead and the filter read. With no value, the text of the item. | |
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A tag or a component that the part renders in place of its own element, with the same
attributes and content. The element slot wins over it. |
Slots
| Slot | Slot props | Description |
|---|---|---|
default | state: MenuLinkItemState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: MenuLinkItemState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
[data-highlighted] | The link has the highlight. |
[hidden] | The link does not match the query of the filter. |
ContextMenu.SubmenuRoot
Groups the parts of a submenu. Renders no element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
closeParentOnEsc | boolean | undefined | false | Escape in the submenu also closes its parent menu. |
defaultOpen | boolean | undefined | false | The menu is open at the mount of an uncontrolled root. |
disabled | boolean | undefined | false | The user cannot open the menu or use its items. |
highlightItemOnHover | boolean | undefined | true | A move of the pointer highlights its item. With false, CSS :hover and
[data-highlighted] differ. |
loopFocus | boolean | undefined | true | An arrow key at the last item goes to the first item, and the other way. |
open | boolean | undefined | The menu is open. v-model:open binds it. | |
orientation | MenuRootOrientation | undefined | 'vertical' | The axis of the arrow keys that move the highlight. |
Slots
| Slot | Slot props | Description |
|---|---|---|
default | The trigger and the portal of the submenu. |
Events
| Event | Arguments | Description |
|---|---|---|
openChange | [open: boolean, details: MenuRootChangeEventDetails] | The submenu opens or closes. details.cancel() keeps it as it was. |
update:open | [open: boolean] | Comes after openChange, unless a listener cancels the change. v-model:open uses it. |
itemHighlighted | [element: HTMLElement | undefined, details: MenuRootHighlightEventDetails] | The highlighted item changes. It gets the element of the item, or undefined. |
Exposed
| Member | Type | Description |
|---|---|---|
close | () => void | Closes the submenu. |
ContextMenu.SubmenuTrigger
An item that opens a submenu.
Renders a <div> element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
closeDelay | number | undefined | 0 | The time in milliseconds before the close, after the pointer moved to another item. |
delay | number | undefined | 100 | The time in milliseconds from the first move on the item to the open. |
disabled | boolean | undefined | false | The item keeps the focus, and does not open the submenu. |
id | string | undefined | Menu.Input names the highlighted item by this id in aria-activedescendant. With no value, a
generated id. | |
label | string | undefined | The text that the typeahead and the filter read. With no value, the text of the item. | |
openOnHover | boolean | undefined | true | A mouse that moves on the item opens the submenu. |
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A tag or a component that the part renders in place of its own element, with the same
attributes and content. The element slot wins over it. |
Slots
| Slot | Slot props | Description |
|---|---|---|
default | state: MenuSubmenuTriggerState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: MenuSubmenuTriggerState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
[data-popup-open] | The submenu is open. |
[data-highlighted] | The item has the highlight. |
[aria-disabled="true"] | The item, the submenu or the parent menu is disabled. |
[hidden] | The item does not match the query of the filter. |
ContextMenu.Group
Groups related items.
Renders a <div> element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A tag or a component that the part renders in place of its own element, with the same
attributes and content. The element slot wins over it. |
Slots
| Slot | Slot props | Description |
|---|---|---|
default | state: MenuGroupState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: MenuGroupState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
[hidden] | No item of the group matches the query of the filter. |
ContextMenu.GroupLabel
A label that names its group.
Renders a <div> element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
id | string | undefined | The group names the label by this id in aria-labelledby. With no value, a generated id. | |
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A tag or a component that the part renders in place of its own element, with the same
attributes and content. The element slot wins over it. |
Slots
| Slot | Slot props | Description |
|---|---|---|
default | state: MenuGroupLabelState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: MenuGroupLabelState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLElement | null | The element of the part, or null while it is not mounted. |
ContextMenu.RadioGroup
Groups radio items, of which one is checked.
Renders a <div> element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
defaultValue | Value | undefined | The value of the checked item at the mount of an uncontrolled group. | |
disabled | boolean | undefined | false | The user cannot change the value. |
modelValue | Value | undefined | The value of the checked item. v-model binds it. | |
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A tag or a component that the part renders in place of its own element, with the same
attributes and content. The element slot wins over it. |
Slots
| Slot | Slot props | Description |
|---|---|---|
default | state: MenuRadioGroupState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: MenuRadioGroupState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Events
| Event | Arguments | Description |
|---|---|---|
valueChange | [value: Value, details: MenuRadioGroupChangeEventDetails] | The user checks an item. details.cancel() keeps the value as it was. |
update:modelValue | [value: Value] | Comes after valueChange, unless a listener cancels the change. v-model uses it. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
[aria-disabled="true"] | The group is disabled. |
[hidden] | No item of the group matches the query of the filter. |
ContextMenu.RadioItem
An item of a radio group.
Renders a <div> element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
closeOnClick | boolean | undefined | false | A click closes the menu. |
disabled | boolean | undefined | false | The item keeps the focus, and the user cannot choose it. |
id | string | undefined | Menu.Input names the highlighted item by this id in aria-activedescendant. With no value, a
generated id. | |
label | string | undefined | The text that the typeahead and the filter read. With no value, the text of the item. | |
value | Value | Required | The value that the group gets at a press of the item. |
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A tag or a component that the part renders in place of its own element, with the same
attributes and content. The element slot wins over it. |
Slots
| Slot | Slot props | Description |
|---|---|---|
default | state: MenuRadioItemState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: MenuRadioItemState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
[aria-checked="true"] | The item is checked. |
[data-highlighted] | The item has the highlight. |
[aria-disabled="true"] | The item, its group or the menu is disabled. |
[hidden] | The item does not match the query of the filter. |
ContextMenu.CheckboxItem
An item that the user checks and unchecks.
Renders a <div> element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
closeOnClick | boolean | undefined | false | A click closes the menu. |
defaultChecked | boolean | undefined | false | The item is checked at the mount of an uncontrolled item. |
disabled | boolean | undefined | false | The item keeps the focus, and the user cannot change it. |
id | string | undefined | Menu.Input names the highlighted item by this id in aria-activedescendant. With no value, a
generated id. | |
label | string | undefined | The text that the typeahead and the filter read. With no value, the text of the item. | |
modelValue | boolean | undefined | The item is checked. v-model binds it. | |
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A tag or a component that the part renders in place of its own element, with the same
attributes and content. The element slot wins over it. |
Slots
| Slot | Slot props | Description |
|---|---|---|
default | state: MenuCheckboxItemState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: MenuCheckboxItemState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Events
| Event | Arguments | Description |
|---|---|---|
checkedChange | [checked: boolean, details: MenuCheckboxItemChangeEventDetails] | The user checks or unchecks the item. details.cancel() keeps it as it was. |
update:modelValue | [checked: boolean] | Comes after checkedChange, unless a listener cancels the change. v-model uses it. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
[aria-checked="true"] | The item is checked. |
[data-highlighted] | The item has the highlight. |
[aria-disabled="true"] | The item or the menu is disabled. |
[hidden] | The item does not match the query of the filter. |
ContextMenu.Separator
A separator element accessible to screen readers.
Renders a <div> element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
orientation | "horizontal" | "vertical" | undefined | 'horizontal' | The orientation of the separator. |
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A tag or a component that the part renders in place of its own element, with the same
attributes and content. The element slot wins over it. |
Slots
| Slot | Slot props | Description |
|---|---|---|
default | state: SeparatorState | Content, such as a glyph between two groups. Assistive technology does not read it. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: SeparatorState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLDivElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
[aria-orientation] | The orientation of the separator: horizontal or vertical. |
Differences from Base UI
Vase UI ports Context Menu from Base UI 1.9.0.
API
| Base UI | Vase UI |
|---|---|
ContextMenu.Root prop onOpenChangeComplete | @after-enter and @after-leave of a <Transition> around the popup run after its animations. |
ContextMenu.Root prop actionsRef | A template ref of the root has close(). With :css="false", the leave hook of a <Transition> keeps the popup until it calls done, so the root has no unmount action. |
ContextMenu.Portal prop render | The portal is a <Teleport>: its content goes to the target, and its only element in place is the visually hidden element that owns the popup. So it has no render, as and element. |
ContextMenu.Backdrop | <Transition> animates the part, so the state has no transitionStatus, and the part has no data-starting-style and data-ending-style. A closed part is out of the DOM, or it has hidden with keepMounted of the portal, so CSS selects [hidden] and the part has no data-open and data-closed. |
ContextMenu.Positioner | With a Vue default, the part cannot find out that a prop is absent, and the positioner of a submenu shares the props. side, align, sideOffset, alignOffset and arrowPadding thus have no Vue default, and their default follows the place of the menu, as in Base UI. side is 'inline-end' for a submenu, else 'bottom'. align is 'start'. A context menu with no side and an align that is not center opens over the pointer, with sideOffset -5 and alignOffset 2, else both are 0. arrowPadding is 0 for a context menu, else 5. |
ContextMenu.Popup | <Transition> animates the part, so the state has no transitionStatus, and the part has no data-starting-style and data-ending-style. A closed part is out of the DOM, or it has hidden with keepMounted of the portal, so CSS selects [hidden] and the part has no data-open and data-closed. |
ContextMenu.Item prop nativeButton | The part reads the element that it renders, so it has no nativeButton. |
ContextMenu.SubmenuRoot prop onOpenChangeComplete | @after-enter and @after-leave of a <Transition> around the popup run after its animations. |
ContextMenu.SubmenuRoot prop actionsRef | A template ref of the root has close(). With :css="false", the leave hook of a <Transition> keeps the popup until it calls done, so the root has no unmount action. |
ContextMenu.SubmenuTrigger prop nativeButton | The part reads the element that it renders, so it has no nativeButton. |
ContextMenu.RadioGroup | The change event of the part has the one reason that it sends, item-press. |
ContextMenu.RadioItem prop nativeButton | The part reads the element that it renders, so it has no nativeButton. |
ContextMenu.RadioItemIndicator | The default slot of the item gives state.checked, so the developer writes v-if or v-show on an element of their own, with <Transition> for an enter and a leave. CSS alone selects [aria-checked="true"]. The family thus has no indicator part. |
ContextMenu.CheckboxItem | The change event of the part has the one reason that it sends, item-press. The part reads the element that it renders, so it has no nativeButton. |
ContextMenu.CheckboxItemIndicator | The default slot of the item gives state.checked, so the developer writes v-if or v-show on an element of their own, with <Transition> for an enter and a leave. CSS alone selects [aria-checked="true"]. The family thus has no indicator part. |
Added in Vase UI
| Part | Member | Why |
|---|---|---|
ContextMenu.Portal | disabled prop | A disabled portal renders at its source, also on the server (VUE-128). |
ContextMenu.Portal | defer prop | The portal is a <Teleport>, and it takes the props of <Teleport>: defer waits for the other content of the same update. |
Behavior
| Vase UI | Evidence |
|---|---|
| A long press with a stylus opens the Context Menu. | |
Shift+Tab in the popup of a menu or a select asks for the close with the reason focus-out. When a handler cancels the close, the popup stays open, and the focus still goes to the trigger. In a submenu, the focus goes to the submenu trigger, or to the input of a filterable parent menu. From the input of a filterable menu, the focus goes to the trigger. A root context menu has no trigger element, so a canceled close for Tab or Shift+Tab keeps the focus in its modal popup. In Base UI, Shift+Tab does nothing when the close is canceled: the list stops the key and moves no focus. Tab still leaves the popup, so the focus can leave in one direction only. |
|
A touch outside an open popup closes it only at a tap. A tap ends with pointerup in less than 1 s and moves 8 px or less. Chromium on Android sends click for a move of 8 px and not for 8.1 px, the smallest limit of the measured browsers. A touch that scrolls the page ends with pointercancel and closes nothing, as a native popover stays open in Chromium and WebKit. A drag of more than 8 px on an element that does not scroll also closes nothing. A tap on a backdrop, or on the element that a modal popup puts over the page, closes the popup and gives the page under it no click. In Base UI, a touch that scrolls the page closes a Popover, a Menu or a Select. | |
An arrow points at the center of its anchor. Its place is in the padding box of its nearest positioned ancestor, so the borders of a popup with a position do not move it. arrowPadding keeps the same space from each inner edge of that box. In the Popover demo of Base UI, the popup has position: relative and a 1 px border, and the arrow is 1.5 px off the center of its trigger. | |
--transform-origin is in the pixels of the popup, from its border box: the arrow or the aligned edge or the center of the anchor, on the edge of the anchor. A margin of the popup, a padding of the positioner, a margin that collapses through the popup and a zoom of the popup do not move it. A Select popup over the trigger grows from the middle of the value, also after it grows while the user scrolls the list. In the Popover demo of Base UI, a 16 px margin on the popup moves the origin 16 px off the edge of the trigger. | |
An arrow in a popup with a CSS zoom points at the center of its anchor. Its place and arrowPadding are in the pixels of its nearest positioned ancestor. A Select popup over the trigger with a zoom puts the text of the selected item over the text of the value. In the Popover demo of Base UI, a zoom: 2 on the popup moves the arrow 225 px off the center of the trigger. |
The handbook gives the differences that all components share: Styling, Animation, Composition, Customization, Forms and TypeScript.