vael-ui v0.5.7

Navigation & menus

Layout

Menu

A dropdown list of actions that opens from a trigger, with full keyboard navigation. Use it for a set of actions behind one button.

Install

import { Menu } from 'vael-ui'

Playground

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

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

<template>
  <Menu :items="items" v-model:open="open">
    <template #trigger>
      <Button>Trigger</Button>
    </template>
  </Menu>
</template>

Props

NameTypeDefaultDescription
itemsreadonly MenuEntry<T>[]Data-driven rows. Ignored when the default slot renders custom markup instead.
triggerElTriggerRefExternal ref for a trigger that can't live in the #trigger slot.
side'top' | 'right' | 'bottom' | 'left''bottom'Which side of the trigger the panel opens on.
align'start' | 'end' | 'center''start'How the panel aligns against the trigger along that side.
sideOffsetnumber8Gap between the trigger 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 it moves: false for instantly, { spring } to retime the built-in motion, your own initial and exit for the panel, or targets for the rows (select: '.ui-menu-item'). 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.
maxPanelHeightnumberCaps the panel's height in pixels; the item list scrolls past it. Unset, only the viewport limits it.
panelWidthPanelWidthThe panel's width: 'trigger' for at least its trigger's width, as a select's is (longer rows still widen it), or a width in px or any CSS length ('20rem'). Unset, it fits its rows. Submenus keep their own width.
openPathreadonly string[]Row path to reveal on open, as each row's value (or label): every submenu along it opens and its last row takes focus instead of the first, e.g. to show the current selection.
uiPartial<Record<'positioner' | 'panel' | 'header' | 'footer' | 'icon' | 'chevron', UiPartValue>>Class and style overrides for each part.
openbooleanfalseWhether the menu is open.

Events

NameTypeDescription
active(value: boolean)Fires when the pointer enters or leaves any submenu row or panel in this subtree; a parent menu uses it to stay open.
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.
collapse()Fires on ArrowLeft inside the menu (ArrowRight right to left); a parent menu uses it to close this submenu.
update:open(value: boolean)Fires when open changes (v-model:open).

Slots

NameTypeDescription
trigger{ open: boolean; }Trigger markup. Menu wires the click and anchors the panel to it, so render only the button.
headeranyContent above the item list, outside its scroll region; it stays put while items scrolls.
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, shortcut and submenu chevron stay.
default{ close: () => void; open: boolean; isClosing: boolean; cancelClose: () => void; panelEl: HTMLElement | null; placement: string; maxHeight: number | null; }Fully custom menu content. Render your own role="menuitem" markup; Menu ignores items.

Exposed

NameTypeDescription
panelElHTMLElement | nullPanel element (null while closed).
positionerElHTMLElement | nullPositioning wrapper around the panel (null while closed).
listElHTMLElement | nullItem list element (null while closed).
highlightElHTMLElement | nullThe hover and keyboard highlight, sliding between rows (null while closed). Under a ConfigProvider with motion: false it moves at once, for your own animation.
placementPlacementResolved placement after flipping, e.g. 'bottom-start'.
positionerStyleRecord<string, string>Inline positioning style applied to the positioner.
isClosingbooleantrue while a beforeClose close is pending.
open() => voidOpens the menu.
toggle(event: MouseEvent) => voidOpens or closes the menu.
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.

<Menu
  :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 Button pattern from the WAI-ARIA Authoring Practices Guide.

KeyAction
EnterSpaceOpens the menu from the trigger and focuses the first item.
ArrowDownOpens the menu from the trigger and focuses the first item.
ArrowUpOpens the menu from the trigger and focuses the last item.
ArrowDownArrowUpMoves focus to the next or previous item, wrapping at the ends.
HomeEndMoves focus to the first or last item.
EnterSpaceActivates the focused item and closes the menu, or opens its submenu.
SpaceOn a checkbox row, toggles it and keeps the menu open.
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, and returns focus to its trigger.
TabCloses the menu and moves focus to the element after the trigger.
  • The trigger gets aria-haspopup="menu", aria-expanded, and aria-controls while open. The list has role="menu" and is named by its trigger, and a submenu by its row.
  • Rows are role="menuitem", or role="menuitemcheckbox" with aria-checked when checked is set. Submenu rows add aria-haspopup and aria-expanded.
  • Items use a roving tabindex, and disabled items are skipped. The row under the mouse takes focus, so Enter activates the row you see highlighted. With the default slot, render your own role="menuitem" elements.
  • Right to left, ArrowLeft and ArrowRight swap, and submenus open to the left.