Used with a component · Menu
useMenu
The keyboard engine behind <Menu>, <MenuList> and the SpeedDial actions: roving focus, wrapping arrow keys, Home and End, and typeahead. It finds items by role="menuitem", menuitemcheckbox, menuitemradio or a data-menu-item attribute, and skips disabled ones. It listens for clicks on the list, so you don't bind @click on each item. Enter and Space click the focused item, so its own click listeners run once. The composable never closes anything: onSelect fires, and you decide what happens next.
Install
import { useMenu } from 'vael-ui'Examples
<script setup lang="ts">
import { onMounted, useTemplateRef } from 'vue'
import { useMenu } from 'vael-ui'
const listEl = useTemplateRef<HTMLElement>('list')
const { onKeydown, initRoving } = useMenu({
listEl,
onSelect: (itemEl) => console.log('run', itemEl.dataset.action),
})
// An always-visible list needs one Tab stop without taking focus on mount.
onMounted(initRoving)
</script>
<template>
<div ref="list" role="menu" aria-label="File" @keydown="onKeydown">
<button role="menuitem" data-action="copy">Copy</button>
<button role="menuitem" data-action="rename">Rename</button>
<button role="menuitem" data-action="delete" disabled>Delete</button>
</div>
</template>