Dropdown
Display actions beside a trigger. HuiDropdownMenu holds menu items, HuiDropdownItem selects a command, and HuiDropdownSubMenu groups nested actions.
Import
import { HuiDropdown, HuiDropdownMenu, HuiDropdownItem, HuiDropdownSubMenu } from '@nscode/handy-ui'
Triggers
Hover is the default. Click and context menu triggers use the same menu content. Selected commands appear in a message.
<script setup lang="ts">
import {
HuiButton,
HuiDropdown,
HuiDropdownMenu,
HuiDropdownItem,
HuiSpace,
HuiMessage
} from '@nscode/handy-ui'
const triggers = ['hover', 'click', 'contextmenu'] as const
const selected = (command: string | number | undefined) => HuiMessage.info(`Selected: ${command}`)
</script>
<template>
<HuiSpace wrap>
<HuiDropdown v-for="trigger in triggers" :key="trigger" :trigger="trigger" @command="selected">
<HuiButton>{{ trigger }}</HuiButton>
<template #dropdown>
<HuiDropdownMenu>
<HuiDropdownItem command="edit" icon="edit">Edit</HuiDropdownItem>
<HuiDropdownItem command="copy" icon="copy">Copy</HuiDropdownItem>
<HuiDropdownItem command="unavailable" disabled>Unavailable</HuiDropdownItem>
<HuiDropdownItem command="preview" divided>Preview</HuiDropdownItem>
</HuiDropdownMenu>
</template>
</HuiDropdown>
</HuiSpace>
</template>
Sizes
Submenus inherit the root size. The trigger button has its own size.
<script setup lang="ts">
import {
HuiButton,
HuiDropdown,
HuiDropdownMenu,
HuiDropdownItem,
HuiDropdownSubMenu,
HuiSpace,
HuiMessage
} from '@nscode/handy-ui'
const sizes = ['large', 'default', 'small'] as const
const selected = (command: string | number | undefined) =>
HuiMessage.success(`Selected: ${command}`)
</script>
<template>
<HuiSpace wrap>
<HuiDropdown v-for="size in sizes" :key="size" :size="size" trigger="click" @command="selected">
<HuiButton :size="size">{{ size }}</HuiButton>
<template #dropdown>
<HuiDropdownMenu>
<HuiDropdownSubMenu title="Export" icon="copy">
<HuiDropdownItem command="excel">Excel</HuiDropdownItem>
<HuiDropdownItem command="pdf">PDF</HuiDropdownItem>
</HuiDropdownSubMenu>
</HuiDropdownMenu>
</template>
</HuiDropdown>
</HuiSpace>
</template>
Controlled Visibility And Split Buttons
Passing visible makes visibility controlled, including explicit undefined (closed). An update listener alone keeps internal state. Both modes emit an update request before an actual visibility change. Parents may reject the request; removing the prop restores previous internal state.
The main split button emits buttonClick. Its arrow opens the menu. Set visible with v-model:visible to control the menu.
<script setup lang="ts">
import { ref } from 'vue'
import {
HuiDropdown,
HuiDropdownMenu,
HuiDropdownItem,
HuiSwitch,
HuiSpace,
HuiMessage
} from '@nscode/handy-ui'
const visible = ref(false)
const disabled = ref(false)
const saved = () => HuiMessage.success('Saved')
const selected = (command: string | number | undefined) => HuiMessage.info(`Selected: ${command}`)
</script>
<template>
<HuiSpace direction="vertical">
<HuiSpace>
<HuiSwitch v-model="visible" active-text="Menu open" inactive-text="Menu closed" />
<HuiSwitch v-model="disabled" active-text="Disabled" inactive-text="Enabled" />
</HuiSpace>
<HuiDropdown
v-model:visible="visible"
split-button
trigger="click"
type="primary"
:disabled="disabled"
arrow-label="More save actions"
@button-click="saved"
@command="selected"
>
Save
<template #dropdown>
<HuiDropdownMenu>
<HuiDropdownItem command="save-new">Save and create</HuiDropdownItem>
<HuiDropdownItem command="save-draft">Save draft</HuiDropdownItem>
</HuiDropdownMenu>
</template>
</HuiDropdown>
</HuiSpace>
</template>
Nested Submenus
Only one submenu opens at each level. Every item sends its command through the root dropdown. Hovering a title for 150ms or clicking it opens its submenu.
<script setup lang="ts">
import {
HuiButton,
HuiDropdown,
HuiDropdownMenu,
HuiDropdownItem,
HuiDropdownSubMenu,
HuiMessage
} from '@nscode/handy-ui'
const selected = (command: string | number | undefined) =>
HuiMessage.success(`Selected: ${command}`)
</script>
<template>
<HuiDropdown trigger="click" @command="selected">
<HuiButton>File actions</HuiButton>
<template #dropdown>
<HuiDropdownMenu>
<HuiDropdownSubMenu title="Export" icon="copy">
<HuiDropdownItem command="excel">Excel</HuiDropdownItem>
<HuiDropdownItem command="pdf">PDF</HuiDropdownItem>
</HuiDropdownSubMenu>
<HuiDropdownSubMenu title="Move to">
<HuiDropdownItem command="group-a">Group A</HuiDropdownItem>
<HuiDropdownSubMenu title="Group B">
<HuiDropdownItem command="group-b-1">Subgroup 1</HuiDropdownItem>
<HuiDropdownItem command="group-b-2">Subgroup 2</HuiDropdownItem>
</HuiDropdownSubMenu>
</HuiDropdownSubMenu>
</HuiDropdownMenu>
</template>
</HuiDropdown>
</template>
Left Placement
placement sets the preferred direction. A submenu flips when the preferred side has insufficient room.
<script setup lang="ts">
import {
HuiButton,
HuiDropdown,
HuiDropdownMenu,
HuiDropdownItem,
HuiDropdownSubMenu,
HuiMessage
} from '@nscode/handy-ui'
const selected = (command: string | number | undefined) => HuiMessage.info(`Selected: ${command}`)
</script>
<template>
<div style="display: flex; justify-content: flex-end">
<HuiDropdown trigger="click" placement="bottom-end" @command="selected">
<HuiButton>Left submenu</HuiButton>
<template #dropdown>
<HuiDropdownMenu>
<HuiDropdownSubMenu title="Export" placement="left-start">
<HuiDropdownItem command="excel">Excel</HuiDropdownItem>
<HuiDropdownItem command="pdf">PDF</HuiDropdownItem>
</HuiDropdownSubMenu>
</HuiDropdownMenu>
</template>
</HuiDropdown>
</div>
</template>
Disabled Submenus And Repeated Selection
Disabled titles cannot open and are skipped by arrow navigation. The root hide-on-click setting applies to all nested items.
<script setup lang="ts">
import { ref } from 'vue'
import {
HuiButton,
HuiDropdown,
HuiDropdownMenu,
HuiDropdownItem,
HuiDropdownSubMenu,
HuiSwitch,
HuiSpace,
HuiMessage
} from '@nscode/handy-ui'
const disabled = ref(true)
const selected = (command: string | number | undefined) => HuiMessage.info(`Selected: ${command}`)
</script>
<template>
<HuiSpace direction="vertical">
<HuiSwitch v-model="disabled" active-text="Submenu disabled" inactive-text="Submenu enabled" />
<HuiDropdown trigger="click" :hide-on-click="false" @command="selected">
<HuiButton>Repeated selection</HuiButton>
<template #dropdown>
<HuiDropdownMenu>
<HuiDropdownSubMenu title="Export" :disabled="disabled">
<HuiDropdownItem command="excel">Excel</HuiDropdownItem>
<HuiDropdownItem command="pdf" disabled>PDF (unavailable)</HuiDropdownItem>
</HuiDropdownSubMenu>
<HuiDropdownItem command="preview">Preview</HuiDropdownItem>
</HuiDropdownMenu>
</template>
</HuiDropdown>
</HuiSpace>
</template>
Title Slot And Popup Class
The title slot overrides the title prop. popper-class is applied to the popup under body, so its custom CSS must use a global selector.
<script setup lang="ts">
import {
HuiButton,
HuiDropdown,
HuiDropdownMenu,
HuiDropdownItem,
HuiDropdownSubMenu,
HuiTag,
HuiSpace,
HuiMessage
} from '@nscode/handy-ui'
const selected = (command: string | number | undefined) =>
HuiMessage.success(`Selected: ${command}`)
</script>
<template>
<HuiDropdown trigger="click" @command="selected">
<HuiButton>Export records</HuiButton>
<template #dropdown>
<HuiDropdownMenu>
<HuiDropdownSubMenu title="Export" popper-class="demo-export-submenu-en">
<template #title>
<HuiSpace><span>Export</span><HuiTag size="small">2</HuiTag></HuiSpace>
</template>
<HuiDropdownItem command="current">Current page</HuiDropdownItem>
<HuiDropdownItem command="all">All records</HuiDropdownItem>
</HuiDropdownSubMenu>
</HuiDropdownMenu>
</template>
</HuiDropdown>
</template>
<style>
.demo-export-submenu-en {
border-style: dashed;
}
</style>
Popup host and fullscreen containers
The default host is body. Set teleport=false to keep the root menu and every submenu inside their container, including fullscreen. A string selects an existing CSS target; this example mounts that dropdown after its target exists.
<script setup lang="ts">
import { onBeforeUnmount, onMounted, ref, useId } from 'vue'
import {
HuiButton,
HuiDropdown,
HuiDropdownItem,
HuiDropdownMenu,
HuiDropdownSubMenu,
HuiSpace
} from '@nscode/handy-ui'
import type { DropdownCommand } from '@nscode/handy-ui'
const stage = ref<HTMLElement>()
const mounted = ref(false)
const fullscreen = ref(false)
const status = ref('Waiting for a menu command')
const hostId = `dropdown-host-${useId().replace(/[^\w-]/g, '-')}`
const onCommand = (command: DropdownCommand) => {
status.value = `Selected: ${command}`
}
const syncFullscreen = () => {
fullscreen.value = document.fullscreenElement === stage.value
}
const toggleFullscreen = async () => {
const element = stage.value
if (!element) return
try {
if (document.fullscreenElement === element) await document.exitFullscreen()
else if (typeof element.requestFullscreen === 'function') await element.requestFullscreen()
else status.value = 'Fullscreen is unavailable; the inline menu still works'
} catch {
status.value = 'Fullscreen was not allowed; the inline menu still works'
}
}
onMounted(() => {
mounted.value = true
document.addEventListener('fullscreenchange', syncFullscreen)
})
onBeforeUnmount(() => {
document.removeEventListener('fullscreenchange', syncFullscreen)
if (document.fullscreenElement === stage.value)
void document.exitFullscreen().catch(() => undefined)
})
</script>
<template>
<HuiSpace direction="vertical" fill>
<div ref="stage" class="dropdown-teleport-stage">
<p>The inline menu and every submenu stay in this container, including fullscreen.</p>
<HuiSpace>
<HuiButton @click="toggleFullscreen">{{
fullscreen ? 'Exit fullscreen' : 'Enter fullscreen'
}}</HuiButton>
<HuiDropdown :teleport="false" trigger="click" @command="onCommand">
<HuiButton>Container settings</HuiButton>
<template #dropdown>
<HuiDropdownMenu>
<HuiDropdownItem command="Normal speed">Normal speed</HuiDropdownItem>
<HuiDropdownSubMenu title="Subtitles">
<HuiDropdownItem command="Subtitles off">Subtitles off</HuiDropdownItem>
<HuiDropdownItem command="Chinese subtitles">Chinese subtitles</HuiDropdownItem>
</HuiDropdownSubMenu>
</HuiDropdownMenu>
</template>
</HuiDropdown>
</HuiSpace>
<span role="status">{{ status }}</span>
</div>
<div :id="hostId" class="dropdown-teleport-host">Popup host selected by CSS</div>
<HuiSpace>
<HuiDropdown trigger="click" @command="onCommand">
<HuiButton>Default body menu</HuiButton>
<template #dropdown
><HuiDropdownMenu
><HuiDropdownItem command="body">Body command</HuiDropdownItem></HuiDropdownMenu
></template
>
</HuiDropdown>
<HuiDropdown v-if="mounted" :teleport="`#${hostId}`" trigger="click" @command="onCommand">
<HuiButton>Custom host menu</HuiButton>
<template #dropdown
><HuiDropdownMenu
><HuiDropdownItem command="Custom host">Host command</HuiDropdownItem></HuiDropdownMenu
></template
>
</HuiDropdown>
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.dropdown-teleport-stage {
display: flex;
flex-direction: column;
gap: var(--hui-spacing-3);
padding: var(--hui-spacing-4);
border: var(--hui-border-width) var(--hui-border-style) var(--hui-border-color);
border-radius: var(--hui-border-radius);
background: var(--hui-bg-color);
color: var(--hui-text-color);
}
.dropdown-teleport-stage:fullscreen {
width: 100vw;
min-height: 100vh;
box-sizing: border-box;
}
.dropdown-teleport-host {
padding: var(--hui-spacing-2);
border: var(--hui-border-width) dashed var(--hui-border-color);
}
</style>
Props
HuiDropdown
| Prop | Type | Default | Description |
|---|---|---|---|
| trigger | 'hover' | 'click' | 'contextmenu' | 'hover' | How the root opens. Split buttons treat context menu as click. |
| placement | DropdownPlacement | 'bottom-start' | Preferred placement with automatic flipping. |
| teleport | string | boolean | 'body' | Host for the root menu and every submenu. False keeps them inline; a CSS target must already exist. |
| offset | number | 8 | Distance from the trigger or context menu point. |
| disabled | boolean | false | Disable the trigger and close its menu. |
| hideOnClick | boolean | true | Close the entire menu after selecting an item at any level. |
| tabindex | number | 0 | Trigger tab order. |
| visible | boolean | - | Controlled visibility, with v-model:visible. |
| size | 'large' | 'default' | 'small' | - | Inherits ConfigProvider, then uses default. Applies to submenus. |
| splitButton | boolean | false | Render a main action and a separate arrow button. |
| type | ButtonType | - | Split button type. Inherits ConfigProvider button settings. |
| arrowLabel | string | Locale text | Accessible name for the split arrow button. |
HuiDropdownItem
| Prop | Type | Default | Description |
|---|---|---|---|
| command | string | number | - | Command sent to the root. An omitted command emits undefined. |
| disabled | boolean | false | Prevent selection. |
| divided | boolean | false | Show a divider above the item. |
| icon | string | - | Leading icon. |
HuiDropdownSubMenu
| Prop | Type | Default | Description |
|---|---|---|---|
| title | string | - | Submenu title. The title slot takes precedence. |
| icon | string | - | Leading title icon. |
| disabled | boolean | false | Prevent opening. The root disabled state also applies. |
| placement | 'right-start' | 'left-start' | 'right-start' | Preferred direction with automatic flipping. |
| popperClass | string | - | Extra class on the submenu popup. |
HuiDropdownMenu has no props.
Events
All commands, including nested ones, are emitted by HuiDropdown. Submenus have no independent events.
| Event | Arguments | Description |
|---|---|---|
| command | (command: DropdownCommand) | Select an enabled item. |
| visibleChange | (visible: boolean) | Root visibility changes, including controlled changes. |
| update:visible | (value: boolean) | Request a visibility change in either state mode. |
| buttonClick | (event: MouseEvent) | Select the main split button. |
Slots
| Component | Slot | Description |
|---|---|---|
| HuiDropdown | default | Trigger content, or main split button content. |
| HuiDropdown | dropdown | Root menu. |
| HuiDropdownMenu | default | Items and submenus. |
| HuiDropdownItem | default | Item content. |
| HuiDropdownSubMenu | default | Items and nested submenus. |
| HuiDropdownSubMenu | title | Custom title, overriding the title prop. |
Methods
Call these methods on a root dropdown ref. Submenus have no public methods.
| Method | Return | Description |
|---|---|---|
| open() | void | Request opening; changes emit update:visible(true) in either mode. Controlled mode waits for the parent value. |
| close() | void | Request closing; changes emit update:visible(false) in either mode. Controlled mode waits for the parent value. |
The root ref also exposes its current read-only visible value.
Types
import type {
DropdownCommand,
DropdownProps,
DropdownEmits,
DropdownItemProps,
DropdownSubMenuProps,
DropdownSubMenuPlacement,
DropdownSize,
DropdownTrigger,
DropdownPlacement
} from '@nscode/handy-ui'
type DropdownCommand = string | number | undefined
type DropdownSubMenuPlacement = 'right-start' | 'left-start'
type DropdownSize = 'large' | 'default' | 'small'
type DropdownTrigger = 'hover' | 'click' | 'contextmenu'
type DropdownPlacement =
| 'top'
| 'top-start'
| 'top-end'
| 'bottom'
| 'bottom-start'
| 'bottom-end'
| 'left'
| 'left-start'
| 'left-end'
| 'right'
| 'right-start'
| 'right-end'
interface DropdownSubMenuProps {
title?: string
icon?: string
disabled?: boolean
placement?: DropdownSubMenuPlacement
popperClass?: string
}
Keyboard
Each menu level has one Tab stop. Arrow keys, Home/End and text-prefix navigation skip disabled items. Repeating one character cycles matches; characters typed within 500ms form a prefix. IME composition, keyCode 229 and Ctrl/Meta/Alt combinations do not navigate or execute commands. Submenu opening and closing follow the actual popup direction.
| Key | Behavior |
|---|---|
| Enter, Space, Down on the trigger | Open and focus the first item. |
| Up, Down, Home, End inside a menu | Move within the current level, skipping disabled entries. |
| Right, Enter, Space on a submenu title | Open and focus the first child. |
| Left inside a submenu | Close that level and focus its title. |
| Escape | Close only the innermost submenu. Close the root when no submenu remains. |
| Tab, Shift+Tab | Close the whole menu and continue from the root trigger. |
For a submenu actually placed on the left, Left opens it and Right closes it.