Skip to content

Dropdown

Popover with a trigger and arbitrary content. Closes on outside click and on Escape, and can be driven programmatically.

Import

typescript
import { Dropdown, DropdownMenu } from 'vuiii'

Basic Usage

With no trigger slot it renders a Button labelled by label. The content is whatever you put in the default slot — DropdownMenu is the usual choice.

vue
<Dropdown label="Options" color="primary">
  <template #default="{ close }">
    <DropdownMenu :items="menuItems" @item-click="({ item }) => { run(item); close() }" />
  </template>
</Dropdown>

Close it yourself

The default slot receives close, because the dropdown cannot know which of your clicks should dismiss it. Picking a menu item usually should.

Props

PropTypeDefaultDescription
labelstring-Label of the default button trigger
iconstring-Prefix icon of the default button trigger
colorButtonColor-Color of the default button trigger
blockbooleanfalseMakes the trigger full width
dropdownPlacement'left' | 'right' | 'center'-Alignment of the popover against the trigger
fullDropdownWidthbooleanfalseStretches the popover to the trigger's width

Slots

SlotDescription
defaultPopover content. Props: { close }
triggerReplaces the button trigger. Props: { open, close, toggle, isOpen }

Events

EventDescription
openWhen the popover opens
closeWhen the popover closes

Custom Trigger

The trigger slot hands you the controls, so any element can open the popover.

vue
<Dropdown>
  <template #trigger="{ toggle, isOpen }">
    <IconButton icon="pencil" title="Actions" @click="toggle()" />
  </template>

  <template #default="{ close }">
    <div class="custom-content">
      <button @click="doSomething(); close()">Action</button>
    </div>
  </template>
</Dropdown>

Placement

vue
<Dropdown label="Menu" dropdown-placement="right">
  <DropdownMenu :items="items" />
</Dropdown>

Programmatic Control

A template ref exposes open, close, toggle and isOpen. isOpen arrives already unwrapped — read it as a plain boolean, not .value.

vue
<script setup>
import { ref } from 'vue'
import type { DropdownRef } from 'vuiii'

const dropdownRef = ref<DropdownRef>()

dropdownRef.value?.open()
dropdownRef.value?.toggle()

if (dropdownRef.value?.isOpen) {
  // …
}
</script>

<template>
  <Dropdown ref="dropdownRef" label="Menu">
    <DropdownMenu :items="items" />
  </Dropdown>
</template>

Dismissal

Every open dropdown closes on an outside mousedown or on Escape, and the click still reaches the page underneath — so clicking straight into an input both closes the popover and focuses the field.

Storybook

For interactive examples with all variants, see Dropdown in Storybook.

Released under the MIT License.