Skip to content

Context Menu ​

A menu that appears at the pointer on right click or long press.

Right click here
vue
<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>
css
.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);
}
vue
<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:

vue
<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.

Right click here
vue
<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>
css
.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);
}
vue
<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

PropTypeDefaultDescription
defaultOpenboolean | undefinedfalseThe menu is open at the mount of an uncontrolled root.
disabledboolean | undefinedfalseThe user cannot open the menu or use its items.
highlightItemOnHoverboolean | undefinedtrueA move of the pointer highlights its item. With false, CSS :hover and [data-highlighted] differ.
loopFocusboolean | undefinedtrueAn arrow key at the last item goes to the first item, and the other way.
openboolean | undefinedThe menu is open. v-model:open binds it.
orientationMenuRootOrientation | undefined'vertical'The axis of the arrow keys that move the highlight.

Slots

SlotSlot propsDescription
defaultThe trigger and the portal of the menu.

Events

EventArgumentsDescription
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

MemberTypeDescription
close() => voidCloses 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

PropTypeDefaultDescription
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA 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

SlotSlot propsDescription
default
state: ContextMenuTriggerState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: ContextMenuTriggerState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[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

PropTypeDefaultDescription
deferboolean | undefinedfalseMoves the content after the other content of the same update mounted.
disabledboolean | undefinedfalseRenders the content in place, also on the server.
keepMountedboolean | undefinedfalseThe closed popup stays in the DOM, with hidden and inert.
toPortalTarget | undefinedThe 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

SlotSlot propsDescription
defaultThe content of the part.

ContextMenu.Backdrop ​

An overlay under the menu. Renders a <div> element.

Props

PropTypeDefaultDescription
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA 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

SlotSlot propsDescription
default
state: MenuBackdropState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: MenuBackdropState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[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

PropTypeDefaultDescription
alignAlign | undefined'center'The alignment of the popup on the side.
alignOffsetnumber | OffsetFunction | undefined0The offset on the align axis in pixels, or a function that gives it.
anchorAnchor | (() => 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.
arrowPaddingnumber | undefined5The smallest space between the arrow and an inner edge of its nearest positioned ancestor, in pixels.
collisionAvoidanceCollisionAvoidance | undefinedHow the popup avoids the edge of the boundary.
collisionBoundaryBoundary | undefined'clipping-ancestors'The area that holds the popup: an element, elements, or a rectangle.
collisionPaddingPadding | undefined5The space between the popup and the edge of the boundary, in pixels.
disableAnchorTrackingboolean | undefinedfalseThe popup does not follow a move of its anchor.
sideSide | 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.
sideOffsetnumber | OffsetFunction | undefined0The distance from the anchor in pixels, or a function that gives it.
stickyboolean | undefinedfalseThe popup stays in the view after its anchor scrolls out of it.
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA 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

SlotSlot propsDescription
default
state: ContextMenuPositionerState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: ContextMenuPositionerState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[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

VariableDescription
--anchor-widthThe width of the anchor, in pixels.
--anchor-heightThe height of the anchor, in pixels.
--available-widthThe width from the anchor to the edge of the collision boundary, in pixels.
--available-heightThe height from the anchor to the edge of the collision boundary, in pixels.
--transform-originThe 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

PropTypeDefaultDescription
finalFocusFocusTarget | undefinedThe 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.
idstring | undefinedThe trigger names the popup by this id in aria-controls, when the menu has no Menu.List. With no value, a generated id.
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA 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

SlotSlot propsDescription
default
state: MenuPopupState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: MenuPopupState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[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

VariableDescription
--popup-widthThe width of the popup, in pixels: the old width, then the new width, while Menu.Viewport moves to another trigger.
--popup-heightThe 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

PropTypeDefaultDescription
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA 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

SlotSlot propsDescription
default
state: MenuArrowState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: MenuArrowState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[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

PropTypeDefaultDescription
closeOnClickboolean | undefinedtrueA click closes the menu.
disabledboolean | undefinedfalseThe item keeps the focus, and does no action.
idstring | undefinedMenu.Input names the highlighted item by this id in aria-activedescendant. With no value, a generated id.
labelstring | undefinedThe text that the typeahead and the filter read. With no value, the text of the item.
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA 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

SlotSlot propsDescription
default
state: MenuItemState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: MenuItemState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[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

PropTypeDefaultDescription
closeOnClickboolean | undefinedfalseA click closes the menu.
idstring | undefinedMenu.Input names the highlighted item by this id in aria-activedescendant. With no value, a generated id.
labelstring | undefinedThe text that the typeahead and the filter read. With no value, the text of the item.
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA 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

SlotSlot propsDescription
default
state: MenuLinkItemState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: MenuLinkItemState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[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

PropTypeDefaultDescription
closeParentOnEscboolean | undefinedfalseEscape in the submenu also closes its parent menu.
defaultOpenboolean | undefinedfalseThe menu is open at the mount of an uncontrolled root.
disabledboolean | undefinedfalseThe user cannot open the menu or use its items.
highlightItemOnHoverboolean | undefinedtrueA move of the pointer highlights its item. With false, CSS :hover and [data-highlighted] differ.
loopFocusboolean | undefinedtrueAn arrow key at the last item goes to the first item, and the other way.
openboolean | undefinedThe menu is open. v-model:open binds it.
orientationMenuRootOrientation | undefined'vertical'The axis of the arrow keys that move the highlight.

Slots

SlotSlot propsDescription
defaultThe trigger and the portal of the submenu.

Events

EventArgumentsDescription
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

MemberTypeDescription
close() => voidCloses the submenu.

ContextMenu.SubmenuTrigger ​

An item that opens a submenu. Renders a <div> element.

Props

PropTypeDefaultDescription
closeDelaynumber | undefined0The time in milliseconds before the close, after the pointer moved to another item.
delaynumber | undefined100The time in milliseconds from the first move on the item to the open.
disabledboolean | undefinedfalseThe item keeps the focus, and does not open the submenu.
idstring | undefinedMenu.Input names the highlighted item by this id in aria-activedescendant. With no value, a generated id.
labelstring | undefinedThe text that the typeahead and the filter read. With no value, the text of the item.
openOnHoverboolean | undefinedtrueA mouse that moves on the item opens the submenu.
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA 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

SlotSlot propsDescription
default
state: MenuSubmenuTriggerState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: MenuSubmenuTriggerState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[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

PropTypeDefaultDescription
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA 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

SlotSlot propsDescription
default
state: MenuGroupState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: MenuGroupState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[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

PropTypeDefaultDescription
idstring | undefinedThe group names the label by this id in aria-labelledby. With no value, a generated id.
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA 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

SlotSlot propsDescription
default
state: MenuGroupLabelState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: MenuGroupLabelState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe 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

PropTypeDefaultDescription
defaultValueValue | undefinedThe value of the checked item at the mount of an uncontrolled group.
disabledboolean | undefinedfalseThe user cannot change the value.
modelValueValue | undefinedThe value of the checked item. v-model binds it.
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA 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

SlotSlot propsDescription
default
state: MenuRadioGroupState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: MenuRadioGroupState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Events

EventArgumentsDescription
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

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[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

PropTypeDefaultDescription
closeOnClickboolean | undefinedfalseA click closes the menu.
disabledboolean | undefinedfalseThe item keeps the focus, and the user cannot choose it.
idstring | undefinedMenu.Input names the highlighted item by this id in aria-activedescendant. With no value, a generated id.
labelstring | undefinedThe text that the typeahead and the filter read. With no value, the text of the item.
valueValueRequiredThe value that the group gets at a press of the item.
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA 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

SlotSlot propsDescription
default
state: MenuRadioItemState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: MenuRadioItemState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[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

PropTypeDefaultDescription
closeOnClickboolean | undefinedfalseA click closes the menu.
defaultCheckedboolean | undefinedfalseThe item is checked at the mount of an uncontrolled item.
disabledboolean | undefinedfalseThe item keeps the focus, and the user cannot change it.
idstring | undefinedMenu.Input names the highlighted item by this id in aria-activedescendant. With no value, a generated id.
labelstring | undefinedThe text that the typeahead and the filter read. With no value, the text of the item.
modelValueboolean | undefinedThe item is checked. v-model binds it.
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA 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

SlotSlot propsDescription
default
state: MenuCheckboxItemState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: MenuCheckboxItemState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Events

EventArgumentsDescription
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

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[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

PropTypeDefaultDescription
orientation"horizontal" | "vertical" | undefined'horizontal'The orientation of the separator.
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA 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

SlotSlot propsDescription
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) => void
state: SeparatorState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLDivElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[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 UIVase UI
ContextMenu.Root prop onOpenChangeComplete@after-enter and @after-leave of a <Transition> around the popup run after its animations.
ContextMenu.Root prop actionsRefA 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 renderThe 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.PositionerWith 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 nativeButtonThe 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 actionsRefA 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 nativeButtonThe part reads the element that it renders, so it has no nativeButton.
ContextMenu.RadioGroupThe change event of the part has the one reason that it sends, item-press.
ContextMenu.RadioItem prop nativeButtonThe part reads the element that it renders, so it has no nativeButton.
ContextMenu.RadioItemIndicatorThe 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.CheckboxItemThe 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.CheckboxItemIndicatorThe 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 ​

PartMemberWhy
ContextMenu.Portaldisabled propA disabled portal renders at its source, also on the server (VUE-128).
ContextMenu.Portaldefer propThe portal is a <Teleport>, and it takes the props of <Teleport>: defer waits for the other content of the same update.

Behavior ​

Vase UIEvidence
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.