vael-ui v0.5.7

Overlays

Layout

ContextMenu

A menu that opens at the pointer on right-click, or on a long press on touch screens. Use it for actions on the item under the pointer.

Keyboard users open the menu with Shift+F10 or the Menu key, which fire contextmenu on the focused element. Make the wrapped target focusable (tabindex="0") and give it an accessible name.

Install

import { ContextMenu } from 'vael-ui'

Playground

Right-click here

Placement, behaviour and animation

Usage
<script setup lang="ts">
import { ref } from 'vue'
import { ContextMenu } from 'vael-ui'

const items = [
  { label: 'Apple', value: 'apple' },
  { label: 'Banana', value: 'banana' },
  { label: 'Cherry', value: 'cherry' },
]
const open = ref(false)
</script>

<template>
  <ContextMenu :items="items" v-model:open="open">
    <div>Right-click here</div>
  </ContextMenu>
</template>

Props

NameTypeDefaultDescription
itemsreadonly MenuEntry<T>[]Data-driven rows, in the same shape as Menu's items.
disabledbooleanfalseDisables both the right-click and long-press triggers.
longPressbooleantrueAdds a touch long-press trigger alongside the native contextmenu event.
longPressDelaynumber500How long you hold a touch press, in ms, before the menu opens.
side'top' | 'right' | 'bottom' | 'left''bottom'Which side of the cursor point the panel opens on.
align'start' | 'end' | 'center''start'How the panel aligns against the cursor point along that side.
sideOffsetnumber2Gap between the cursor point and the panel, in pixels.
alignOffsetnumber0Shifts the panel along the alignment axis, in pixels.
closeOnEscbooleantrueEscape key closes the panel.
closeOnOutsidebooleantrueClicking outside the panel closes it.
beforeCloseBeforeClose<PopoverCloseReason>Runs before the menu closes. Return true to close it, false to keep it open, or a promise of either, like () => confirmAction(…).result. The menu stays open while the promise is pending.
forceMountbooleanfalseKeeps it mounted while closed, toggled with v-show, so its content keeps its state. vael-ui then leaves its motion to you.
motionMenuMotionHow the menu moves, like Menu's. false opens and closes it at once. An object moves it your way: values for the panel part, { spring }, or enter / leave hooks. Unset, it follows the nearest ConfigProvider's style.
teleportTostring | HTMLElement'body'Teleport target: a CSS selector or element.
scrollFadebooleantrueMasks the panel's top/bottom edge as its content scrolls under it, signaling there's more.
uiPartial<Record<'positioner' | 'panel' | 'header' | 'footer', UiPartValue>>Class and style overrides for each part.
openbooleanfalseWhether the menu is open.

Events

NameTypeDescription
open-change(value: boolean, details: PopoverOpenChangeDetails)Fires before the menu closes, with the reason; details.cancel() keeps it open.
select(item: T)Fires when a data-driven row is activated, including rows in submenus.
update:open(value: boolean)Fires when open changes (v-model:open).

Slots

NameTypeDescription
default{ open: boolean; }Wrapped content, such as a card, row or image. Right-clicking it, or long-pressing it on touch, opens the menu.
headeranyForwarded to Menu's own #header.
item{ item: T; }Override one data-driven row's content while keeping its behavior.
label{ item: T; }Override a row's text only; its icon and shortcut stay. Forwarded to Menu's own #label.

Exposed

NameTypeDescription
wrapperElHTMLElement | nullElement wrapping the default slot; listens for right-click and long-press.
anchorElHTMLElement | nullZero-size element placed at the open point; the panel anchors to it.
panelElHTMLElement | nullPanel element (null while closed).
positionerElHTMLElement | nullPositioning wrapper around the panel (null while closed).
listElHTMLElement | nullItem list element (null while closed).
isClosingbooleantrue while a beforeClose close is pending.
openAt(x: number, y: number) => Promise<void>Opens the menu at a viewport point, for example from a custom "⋮" button instead of a right-click.
close() => voidCloses the menu, running @open-change and beforeClose first.
cancelClose() => voidCancels a close pending in beforeClose and keeps the menu open.

Motion New

Menu's motion, and ContextMenu's and SplitButton's: values or hooks for positioner and panel, and how it opens and closes. false turns it off.

<ContextMenu
  :motion="{
    initial: { panel: { opacity: 0, y: 8, scale: 0.98 } },
    spring: { damping: 0.85, response: 0.35 },
  }"
/>
NameTypeDescription
initialexitpositionerpanelSets where each part comes in from and leaves to: any CSS property, plus x, y and blur. A plain number is px, or degrees for rotate.
enterleave(parts, context) => animationAnimates the parts with GSAP, motion or element.animate(). Return the animation and the component waits for it, and stops it if interrupted.
morphlinkedexitOrder'trigger' | Record<string, LinkedMotion> | 'reverse'morph: 'trigger' grows it out of the element that opened it. linked moves other elements with it, such as the page receding behind it. exitOrder: 'reverse' closes its children first.
springdurationeasing{ damping, response } | number | stringTimes all of it: a spring, or a duration in ms with an easing. Never both.
falsebooleanTurns its motion off, for an animation of your own on its exposed elements.

Accessibility

Follows the Menu pattern from the WAI-ARIA Authoring Practices Guide.

KeyAction
ArrowDownArrowUpMoves focus to the next or previous item, wrapping at the ends.
HomeEndMoves focus to the first or last item.
EnterSpaceActivates the focused item, or opens its submenu.
ArrowRightOpens the focused item’s submenu and focuses its first item.
ArrowLeftCloses the submenu and returns focus to its parent item.
A letterMoves focus to the next item whose label starts with the typed letters.
EscapeCloses the open submenu, or the menu.
  • It opens on the native contextmenu event, and the panel is a Menu with the same roles and keys.
  • Browsers also fire contextmenu from the keyboard on a focused element, so put something focusable inside. To open it from your own button, call the exposed openAt(x, y). Pass aria-label to name the menu.
  • Focus moves to the first item on open. On close, it returns to what had it, or to the element you right-clicked.
  • Right to left, ArrowLeft and ArrowRight swap, and submenus open to the left.