Menu
Usage
Label and items
Use the label prop to set the text of the button, which also names the menu for screen readers, and the items prop to fill the menu.
<KMenu
label="Options"
:items="[{ label: 'Edit' }, { label: 'Duplicate' }, { separator: true }, { label: 'Delete', color: 'danger' }]"
/>
Trigger
Use the trigger prop to style the button with the props of Button: variant, color, size, leadingIcon, trailingIcon, pill, square and disabled. The button looks selected while the menu is open.
<script setup lang="ts">
import type { MenuEntry } from '@kuina-ui/vue'
import { ChevronDown, Download, Ellipsis, Eye, Flag, Pencil, Wrench } from 'lucide-vue-next'
const transactionActions: MenuEntry[] = [
{ label: 'Edit', icon: Pencil },
{ label: 'Download receipt', icon: Download },
]
const rowActions: MenuEntry[] = [
{ label: 'View details', icon: Eye },
{ label: 'Download receipt', icon: Download },
{ separator: true },
{ label: 'Report a problem', icon: Flag },
]
</script>
<template>
<KMenu label="Options" :items="transactionActions" :trigger="{ variant: 'outline', trailingIcon: ChevronDown }" />
<KMenu label="Tools" :items="transactionActions" :trigger="{ variant: 'ghost', leadingIcon: Wrench }" />
<KMenu
label="More actions"
:items="rowActions"
:trigger="{ variant: 'ghost', color: 'gray', leadingIcon: Ellipsis, square: true }"
align="end"
width="sm"
/>
</template>
Custom trigger
Use the trigger slot to open the menu from your own element, like a profile card. Bind triggerAttributes on it: they open the menu on click and with the arrow keys, and tell screen readers that the element opens a menu.
<script setup lang="ts">
import type { MenuEntry } from '@kuina-ui/vue'
import { ChevronsUpDown, CircleUser, LogOut, Settings } from 'lucide-vue-next'
const profileItems: MenuEntry[] = [
{ label: 'Your profile', icon: CircleUser, href: '/profile' },
{ label: 'Settings', icon: Settings },
{ separator: true },
{ label: 'Log out', icon: LogOut, color: 'danger' },
]
</script>
<template>
<KMenu label="Avery Collins" :items="profileItems" width="trigger">
<template #trigger="{ triggerAttributes }">
<button type="button" v-bind="triggerAttributes" class="flex w-60 items-center gap-2.5 rounded-xl p-2 text-left text-sm">
<KAvatar name="Avery Collins" image-url="/avatars/avery-collins.webp" size="lg" />
<span class="min-w-0 flex-1">
<span class="block truncate font-medium">Avery Collins</span>
<span class="block truncate text-muted">avery.collins@example.com</span>
</span>
<ChevronsUpDown class="size-4 shrink-0 text-muted" />
</button>
</template>
</KMenu>
</template>
The slot also gives open, to change the look of your element while the menu is open.
Items
An item has a label and can add an icon, a description and a shortcut. Give it an href to make it a link: links to another website open in a new tab. Set color to danger for destructive actions, and disabled to true to keep an item visible but unusable.
Write a shortcut like in useShortcuts, for example mod+e: the menu shows it the way the platform of the user writes it, ⌘E on a Mac and Ctrl+E elsewhere.
Between items, { separator: true } draws a line, and { label, description, readOnly: true } adds a heading that cannot be selected.
<script setup lang="ts">
import type { MenuEntry } from '@kuina-ui/vue'
import { ChevronDown, CircleUser, Copy, FileText, Flag, LogOut, Pencil, Share2, Trash2 } from 'lucide-vue-next'
const accountItems: MenuEntry[] = [
{ label: 'Avery Collins', description: 'avery.collins@example.com', readOnly: true },
{ separator: true },
{ label: 'Your profile', icon: CircleUser, href: '#' },
{ label: 'Terms & policies', icon: FileText, href: 'https://example.com/terms' },
{ label: 'Feature flags', icon: Flag, disabled: true },
{ separator: true },
{ label: 'Log out', icon: LogOut, color: 'danger' },
]
const transactionItems: MenuEntry[] = [
{ label: 'Edit', icon: Pencil, shortcut: 'mod+e' },
{ label: 'Duplicate', icon: Copy, shortcut: 'mod+d' },
{ label: 'Share', icon: Share2, description: 'Send a copy by email' },
{ separator: true },
{ label: 'Delete', icon: Trash2, shortcut: 'mod+backspace', color: 'danger' },
]
</script>
<template>
<KMenu label="Avery Collins" :items="accountItems" :trigger="{ variant: 'ghost', color: 'gray', trailingIcon: ChevronDown }" />
<KMenu label="Options" :items="transactionItems" :trigger="{ variant: 'outline', trailingIcon: ChevronDown }" />
</template>
Select an item
Give an item an onSelect function to run when it is chosen, with a click, Enter or Space. The menu then closes and gives the focus back to its button.
<script setup lang="ts">
import type { MenuEntry } from '@kuina-ui/vue'
const transactionItems: MenuEntry[] = [
{ label: 'Edit', onSelect: () => openTransactionEditor() },
{ label: 'Delete', color: 'danger', onSelect: () => deleteTransaction() },
]
</script>
Shortcuts
The shortcut of an item is only shown by default. To run the item when its shortcut is pressed, even while the menu is closed, pass the items to extractMenuShortcuts, then to useShortcuts.
<script setup lang="ts">
import { extractMenuShortcuts, type MenuEntry, useShortcuts } from '@kuina-ui/vue'
const transactionItems: MenuEntry[] = [
{ label: 'Edit', shortcut: 'mod+e', onSelect: () => openTransactionEditor() },
{ label: 'Duplicate', shortcut: 'mod+d', onSelect: () => duplicateTransaction() },
]
useShortcuts(extractMenuShortcuts(transactionItems))
</script>
Items without onSelect, and disabled items, are left out. Items in groups and submenus are included.
Item actions
Use actions to add small buttons at the end of an item, like renaming or deleting a saved report. Each action needs a label, read by screen readers, and an icon. They show when the item is hovered or focused, and → and ← move between the item and its actions.
<script setup lang="ts">
import type { MenuEntry } from '@kuina-ui/vue'
import { History, Pencil, Trash2 } from 'lucide-vue-next'
const savedReportItems: MenuEntry[] = [
{ label: 'Monthly spending', actions: [{ label: 'Delete report', icon: Trash2, onSelect: () => deleteReport('monthly') }] },
{
label: 'Subscriptions audit',
actions: [
{ label: 'Rename report', icon: Pencil, onSelect: () => renameReport('subscriptions') },
{ label: 'Delete report', icon: Trash2, onSelect: () => deleteReport('subscriptions') },
],
},
]
</script>
<template>
<KMenu label="Saved reports" :items="savedReportItems" :trigger="{ variant: 'outline', leadingIcon: History }" width="lg" />
</template>
Choosing an action runs its onSelect, not the one of the item, then closes the menu.
Submenus
Use submenu to open a second menu from an item. It opens on hover, or with →, Enter or Space, and ← or Esc closes it. A submenu takes the same entries as the menu, and can have its own submenus.
<script setup lang="ts">
import type { MenuEntry } from '@kuina-ui/vue'
import { Briefcase, ChevronDown, Copy, FolderInput, House, Pencil, Plus, Trash2 } from 'lucide-vue-next'
const transactionItems: MenuEntry[] = [
{ label: 'Edit', icon: Pencil },
{ label: 'Duplicate', icon: Copy },
{
label: 'Move to',
icon: FolderInput,
submenu: [
{ label: 'Household', icon: House },
{ label: 'Business', icon: Briefcase },
{ separator: true },
{ label: 'New category…', icon: Plus },
],
},
{ separator: true },
{ label: 'Delete', icon: Trash2, color: 'danger' },
]
</script>
<template>
<KMenu label="Options" :items="transactionItems" :trigger="{ variant: 'outline', trailingIcon: ChevronDown }" />
</template>
Checkbox items
Set type to checkbox and pass the state in checked. The menu stays open after a checkbox item is toggled, so several options can be changed in a row.
Use the indicator-position prop to put the check start or end (default), and the indicator-variant prop to show it solid (default) or ghost.
<script setup lang="ts">
import type { MenuEntry } from '@kuina-ui/vue'
import { ChevronDown } from 'lucide-vue-next'
import { computed, reactive } from 'vue'
const chartOptions = reactive({ gridLines: true, labels: false, shadows: true })
function toggleChartOption(chartOptionName: keyof typeof chartOptions) {
chartOptions[chartOptionName] = !chartOptions[chartOptionName]
}
const chartOptionItems = computed<MenuEntry[]>(() => [
{ label: 'Show grid lines', type: 'checkbox', checked: chartOptions.gridLines, onSelect: () => toggleChartOption('gridLines') },
{ label: 'Display labels', type: 'checkbox', checked: chartOptions.labels, onSelect: () => toggleChartOption('labels') },
{ label: 'Enable shadows', type: 'checkbox', checked: chartOptions.shadows, onSelect: () => toggleChartOption('shadows') },
{ label: 'Compare to last year', type: 'checkbox', disabled: true },
])
</script>
<template>
<KMenu label="Chart options" :items="chartOptionItems" :trigger="{ variant: 'soft', trailingIcon: ChevronDown }" />
<KMenu
label="Chart options"
:items="chartOptionItems"
:trigger="{ variant: 'soft', trailingIcon: ChevronDown }"
indicator-position="start"
indicator-variant="ghost"
/>
</template>
Radio items
Set type to radio for a choice between several options. Unlike a checkbox item, choosing one closes the menu.
<script setup lang="ts">
import type { MenuEntry, MenuItem } from '@kuina-ui/vue'
import { ChevronDown } from 'lucide-vue-next'
import { computed, ref } from 'vue'
const reportPeriods = ['Any time', 'Today', 'Last 7 days', 'Last 30 days', 'Last 3 months']
const selectedPeriod = ref('Any time')
const periodItems = computed<MenuEntry[]>(() =>
reportPeriods.map((reportPeriod): MenuItem => ({
label: reportPeriod,
type: 'radio',
checked: selectedPeriod.value === reportPeriod,
disabled: reportPeriod === 'Today',
onSelect: () => {
selectedPeriod.value = reportPeriod
},
})),
)
</script>
<template>
<KMenu :label="selectedPeriod" :items="periodItems" :trigger="{ variant: 'soft', trailingIcon: ChevronDown }" width="auto" />
</template>
Groups
Use { label, items } to group items under a heading, which screen readers announce with the group.
<script setup lang="ts">
import type { MenuEntry, MenuItem } from '@kuina-ui/vue'
import { SlidersHorizontal } from 'lucide-vue-next'
import { computed, reactive, ref } from 'vue'
const sortOrders = ['Newest first', 'Oldest first', 'Largest amount']
const selectedSortOrder = ref('Newest first')
const visibleTransactions = reactive({ pending: true, transfers: false, hidden: false })
function toggleVisibleTransactions(transactionKind: keyof typeof visibleTransactions) {
visibleTransactions[transactionKind] = !visibleTransactions[transactionKind]
}
const transactionViewItems = computed<MenuEntry[]>(() => [
{
label: 'Sort by',
items: sortOrders.map((sortOrder): MenuItem => ({
label: sortOrder,
type: 'radio',
checked: selectedSortOrder.value === sortOrder,
onSelect: () => {
selectedSortOrder.value = sortOrder
},
})),
},
{ separator: true },
{
label: 'Show',
items: [
{ label: 'Pending', type: 'checkbox', checked: visibleTransactions.pending, onSelect: () => toggleVisibleTransactions('pending') },
{ label: 'Transfers', type: 'checkbox', checked: visibleTransactions.transfers, onSelect: () => toggleVisibleTransactions('transfers') },
{ label: 'Hidden transactions', type: 'checkbox', checked: visibleTransactions.hidden, onSelect: () => toggleVisibleTransactions('hidden') },
],
},
])
</script>
<template>
<KMenu label="View" :items="transactionViewItems" :trigger="{ variant: 'outline', leadingIcon: SlidersHorizontal }" indicator-position="start" />
</template>
Side
Use the side prop to open the menu bottom (default), top, left or right of its button.
<KMenu label="Below" :trigger="{ variant: 'outline' }" :items="workspaces" />
<KMenu label="Above" side="top" :trigger="{ variant: 'outline' }" :items="workspaces" />
Align
Use the align prop to line the menu up with the start (default), the center or the end of its button.
<KMenu label="Start" width="sm" :trigger="{ variant: 'outline' }" :items="exportFormats" />
<KMenu label="Center" align="center" width="sm" :trigger="{ variant: 'outline' }" :items="exportFormats" />
<KMenu label="End" align="end" width="sm" :trigger="{ variant: 'outline' }" :items="exportFormats" />
Width
Use the width prop to set the width of the menu: sm, md (default), lg or xl. auto fits the items up to max-w-80, and trigger matches the width of the button.
<KMenu label="Auto" width="auto" :trigger="{ variant: 'outline' }" :items="exportFormats" />
<KMenu label="Large" width="lg" :trigger="{ variant: 'outline' }" :items="exportFormats" />
<KMenu label="Same width as this button" width="trigger" :trigger="{ variant: 'outline' }" :items="exportFormats" />
Flip
When there is not enough room on its side, the menu opens on the other side of its button: above instead of below near the bottom of the screen, and on the left instead of the right for a submenu. Set flip to false to always keep the side you chose.
<KMenu label="Options" :items="transactionItems" :flip="false" />
Fixed position
By default, the menu scrolls with the page. If its button sits in an area that does not scroll, like a sticky header or a fixed sidebar, set strategy to fixed so the menu stays next to it.
<KMenu label="Avery Collins" :items="profileItems" strategy="fixed" side="top" />
Open state
Use v-model:open to know whether the menu is open, or to open and close it from your code.
The menu is closed.
<script setup lang="ts">
import type { MenuEntry } from '@kuina-ui/vue'
import { ChevronDown } from 'lucide-vue-next'
import { ref } from 'vue'
const isMenuOpen = ref(false)
const exportFormats: MenuEntry[] = [{ label: 'CSV' }, { label: 'PDF' }, { label: 'Excel' }]
</script>
<template>
<KMenu
v-model:open="isMenuOpen"
label="Export"
:items="exportFormats"
:trigger="{ variant: 'outline', trailingIcon: ChevronDown }"
width="sm"
/>
<p>The menu is {{ isMenuOpen ? 'open' : 'closed' }}.</p>
</template>
Context menu
Use the anchor prop to open the menu at a point of the screen instead of under a button, like the pointer on a right click. Without a trigger slot, no button is shown: open the menu with v-model:open. The focus moves into the menu, and goes back to what had it when the menu closes.
<script setup lang="ts">
import type { FloatingPoint, MenuEntry } from '@kuina-ui/vue'
import { ref } from 'vue'
const isTransactionMenuOpen = ref(false)
const transactionMenuPoint = ref<FloatingPoint>({ x: 0, y: 0 })
function openTransactionMenu(contextMenuEvent: MouseEvent) {
transactionMenuPoint.value = { x: contextMenuEvent.clientX, y: contextMenuEvent.clientY }
isTransactionMenuOpen.value = true
}
</script>
<template>
<div @contextmenu.prevent="openTransactionMenu">Right-click anywhere in this area</div>
<KMenu
v-model:open="isTransactionMenuOpen"
label="Transaction actions"
:items="transactionMenuItems"
:anchor="transactionMenuPoint"
width="sm"
/>
</template>
The anchor prop also takes an element, or any object with a getBoundingClientRect() method, to open the menu against something else than its trigger.
Keyboard
| Key | Action |
|---|---|
| Enter, Space | On the button, opens the menu on its first item. On an item, selects it. |
| ↓, ↑ | On the button, opens the menu on its first or last item. In the menu, moves to the next or previous item. |
| Home, End | Moves to the first or last item. |
| → | Opens the submenu of the item on its first item, or moves to the next action of the item. |
| ← | Closes the submenu and goes back to its item, or moves to the previous action of the item. |
| A letter | Moves to the next item starting with that letter. Type several letters quickly to match the start of a label. |
| Esc | Closes the submenu, or the menu and focuses the button. |
| Tab | Closes the menu and moves on from the button. |
Customization
Use the ui prop to change the classes of each part of the menu, and class for the root element. Your classes always win: rounded-2xl replaces the default rounded-xl of the panel instead of being added to it.
<KMenu
label="Rounder"
:trigger="{ variant: 'outline' }"
:items="[{ label: 'Edit' }, { label: 'Duplicate' }]"
:ui="{ panel: 'rounded-2xl', item: 'rounded-lg' }"
/>
API
Props
| Prop | Type | Default |
|---|---|---|
label | string | |
items | MenuEntry[] | |
open | boolean | false |
trigger | { variant?, color?, size?, leadingIcon?, trailingIcon?, pill?, square?, disabled? } | |
anchor | HTMLElement | { x, y } | { getBoundingClientRect() } | the trigger |
side | 'bottom' | 'top' | 'left' | 'right' | 'bottom' |
align | 'start' | 'center' | 'end' | 'start' |
width | 'trigger' | 'auto' | 'sm' | 'md' | 'lg' | 'xl' | 'md' |
flip | boolean | true |
strategy | 'absolute' | 'fixed' | 'absolute' |
indicatorPosition | 'start' | 'end' | 'end' |
indicatorVariant | 'solid' | 'ghost' | 'solid' |
ui | { root?, panel?, separator?, groupLabel?, header?, headerLabel?, headerDescription?, item?, itemContent?, itemIcon?, itemText?, itemLabel?, itemDescription?, itemShortcut?, itemActions?, submenuIcon?, indicatorWrapper?, indicator?, indicatorCheckIcon?, radioDot?, newTabHint? } | |
class | string |
Items
MenuEntry is one of these four shapes.
| Entry | Fields |
|---|---|
| Item | label, description?, icon?, shortcut?, href?, color?: 'danger', type?: 'checkbox' | 'radio', checked?, disabled?, actions?: MenuItemAction[], submenu?: MenuEntry[], onSelect?(event) |
| Separator | separator: true |
| Group | label, items: MenuItem[] |
| Heading | label, description?, readOnly: true |
A MenuItemAction has a label, an icon and an onSelect?(event).
Slots
| Slot | Props | Description |
|---|---|---|
trigger | { open, triggerAttributes } | Replaces the button. Bind triggerAttributes on your element. |
Emits
| Event | Payload |
|---|---|
update:open | boolean |