Skip to content

Dropdown Component

Learning Objectives

Through this chapter, you will master:

  • Basic dropdown menu and trigger configuration
  • Event handling and command passing for dropdown menus
  • Split button mode and different size usage
  • Style customization and best practices for dropdown menus
  • Dropdown menu applications in complex interaction scenarios

Estimated Learning Time: 75 minutes

Overview

The Dropdown component is used to collapse actions or menus into a dropdown menu, saving page space while providing rich operation options. It supports multiple trigger methods, flexible configuration options, and comprehensive event handling mechanisms.

Key Features

  • Multiple Trigger Methods: Supports both hover and click trigger modes
  • Flexible Menu Structure: Supports grouping, dividers, and disabled states
  • Split Button Mode: Provides combination of main action button and dropdown menu
  • Event Handling Mechanism: Complete command passing and event callbacks
  • Style Customization: Supports different sizes and theme styles
  • Accessibility: Good keyboard navigation and screen reader support

Use Cases

  • Extended menu for action buttons
  • Personal center menu for user avatars
  • More options for table row actions
  • Secondary menus in navigation bars
  • Feature grouping in toolbars

Basic Usage

Basic Dropdown Menu

Hover over the dropdown menu to expand more operations. Set the dropdown trigger element through the component slot and set the dropdown menu through the named slot dropdown. By default, you only need to hover over the element that triggers the menu, and the dropdown menu will be displayed without clicking.

vue
<template>
  <el-dropdown>
    <span class="el-dropdown-link">
      Dropdown Menu
      <el-icon class="el-icon--right">
        <arrow-down />
      </el-icon>
    </span>
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item>Action 1</el-dropdown-item>
        <el-dropdown-item>Action 2</el-dropdown-item>
        <el-dropdown-item>Action 3</el-dropdown-item>
        <el-dropdown-item disabled>Action 4</el-dropdown-item>
        <el-dropdown-item divided>Action 5</el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>
</template>

<script setup>
import { ArrowDown } from '@element-plus/icons-vue'
</script>

<style>
.el-dropdown-link {
  cursor: pointer;
  color: var(--el-color-primary);
  display: flex;
  align-items: center;
}
</style>

Trigger Methods

Use the trigger attribute to define how the dropdown is triggered.

vue
<template>
  <el-row class="block-col-2">
    <el-col :span="12">
      <span class="demonstration">Hover to trigger</span>
      <el-dropdown>
        <span class="el-dropdown-link">
          Dropdown Menu
          <el-icon class="el-icon--right">
            <arrow-down />
          </el-icon>
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item icon="Plus">Action 1</el-dropdown-item>
            <el-dropdown-item icon="CirclePlus">Action 2</el-dropdown-item>
            <el-dropdown-item icon="CirclePlusFilled">Action 3</el-dropdown-item>
            <el-dropdown-item icon="Check">Action 4</el-dropdown-item>
            <el-dropdown-item icon="CircleCheck">Action 5</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </el-col>
    <el-col :span="12">
      <span class="demonstration">Click to trigger</span>
      <el-dropdown trigger="click">
        <span class="el-dropdown-link">
          Dropdown Menu
          <el-icon class="el-icon--right">
            <arrow-down />
          </el-icon>
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item icon="Plus">Action 1</el-dropdown-item>
            <el-dropdown-item icon="CirclePlus">Action 2</el-dropdown-item>
            <el-dropdown-item icon="CirclePlusFilled">Action 3</el-dropdown-item>
            <el-dropdown-item icon="Check">Action 4</el-dropdown-item>
            <el-dropdown-item icon="CircleCheck">Action 5</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </el-col>
  </el-row>
</template>

Command Event

Clicking each dropdown item will trigger an event, and the parameter is the command assigned to the dropdown item.

vue
<template>
  <el-dropdown @command="handleCommand">
    <span class="el-dropdown-link">
      Dropdown Menu
      <el-icon class="el-icon--right">
        <arrow-down />
      </el-icon>
    </span>
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item command="a">Action 1</el-dropdown-item>
        <el-dropdown-item command="b">Action 2</el-dropdown-item>
        <el-dropdown-item command="c">Action 3</el-dropdown-item>
        <el-dropdown-item command="d" disabled>Action 4</el-dropdown-item>
        <el-dropdown-item command="e" divided>Action 5</el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>
</template>

<script setup>
import { ElMessage } from 'element-plus'
import { ArrowDown } from '@element-plus/icons-vue'

const handleCommand = (command) => {
  ElMessage(`click on item ${command}`)
}
</script>

Sizes

Besides default size, Dropdown component provides three additional sizes for you to choose among different scenarios.

vue
<template>
  <el-dropdown split-button type="primary" @click="handleClick">
    Default
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item>Action 1</el-dropdown-item>
        <el-dropdown-item>Action 2</el-dropdown-item>
        <el-dropdown-item>Action 3</el-dropdown-item>
        <el-dropdown-item>Action 4</el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>

  <el-dropdown size="large" split-button type="primary" @click="handleClick">
    Large
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item>Action 1</el-dropdown-item>
        <el-dropdown-item>Action 2</el-dropdown-item>
        <el-dropdown-item>Action 3</el-dropdown-item>
        <el-dropdown-item>Action 4</el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>

  <el-dropdown size="small" split-button type="primary" @click="handleClick">
    Small
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item>Action 1</el-dropdown-item>
        <el-dropdown-item>Action 2</el-dropdown-item>
        <el-dropdown-item>Action 3</el-dropdown-item>
        <el-dropdown-item>Action 4</el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>
</template>

<script setup>
const handleClick = () => {
  alert('button click')
}
</script>

Split Button

In split-button mode, the left button behaves like a normal button, and right side is the actual dropdown trigger.

vue
<template>
  <el-dropdown split-button type="primary" @click="handleClick" @command="handleCommand">
    Split Button
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item command="a">Action 1</el-dropdown-item>
        <el-dropdown-item command="b">Action 2</el-dropdown-item>
        <el-dropdown-item command="c">Action 3</el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>
</template>

<script setup>
import { ElMessage } from 'element-plus'

const handleClick = () => {
  ElMessage.info('click on left button!')
}

const handleCommand = (command) => {
  ElMessage(`click on item ${command}`)
}
</script>

API Reference

AttributeDescriptionTypeAccepted ValuesDefault
typemenu button type, refer to Button Component, only works when split-button is truestring
sizemenu size, also works on the split buttonstringlarge / default / smalldefault
max-heightthe max height of menustring / number
split-buttonwhether a button group is displayedbooleanfalse
disabledwhether to disablebooleanfalse
placementplacement of pop menustringtop/top-start/top-end/bottom/bottom-start/bottom-endbottom
triggerhow to triggerstringhover/clickhover
hide-on-clickwhether to hide menu after clicking menu-itembooleantrue
show-timeoutdelay time before show a dropdown (only works when trigger is hover)number250
hide-timeoutdelay time before hide a dropdown (only works when trigger is hover)number150
rolethe ARIA role attribute for the dropdown menustringmenu
tabindextabindex of Dropdownnumber0
popper-classcustom class name for Dropdown's dropdownstring
popper-optionspopper.js parametersobjectrefer to popper.js doc{}
teleportedwhether dropdown list is teleportedbooleantrue
NameDescriptionSubtags
content of Dropdown, notice that it's a slot of Dropdown, not Dropdown-Menu
dropdowncontent of the Dropdown MenuDropdown-Menu
Event NameDescriptionParameters
clickif split-button is true, triggers when left button is clicked
commandtriggers when a dropdown item is clickedthe command dispatched from the dropdown item
visible-changetriggers when the dropdown appears/disappearstrue when it appears, and false otherwise
NameDescriptionSubtags
content of Dropdown MenuDropdown-Item
AttributeDescriptionTypeAccepted ValuesDefault
commanda command to be dispatched to Dropdown's command callbackstring/number/object
disabledwhether the item is disabledbooleanfalse
dividedwhether a divider is displayedbooleanfalse
iconcustom iconstring / Component
NameDescription
content of Dropdown Item

Best Practices

  1. Choose appropriate trigger method: Use hover for quick access, click for more deliberate actions
  2. Provide clear visual feedback: Use icons and proper spacing to make menu items easily distinguishable
  3. Group related actions: Use dividers to separate different types of actions
  4. Handle disabled states: Clearly indicate when actions are not available
  5. Consider accessibility: Ensure keyboard navigation works properly
  6. Limit menu items: Keep the number of items manageable to avoid overwhelming users

Common Use Cases

User Profile Menu

vue
<template>
  <el-dropdown @command="handleCommand">
    <el-avatar :src="userAvatar" />
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item command="profile">Profile</el-dropdown-item>
        <el-dropdown-item command="settings">Settings</el-dropdown-item>
        <el-dropdown-item divided command="logout">Logout</el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>
</template>

Table Actions

vue
<template>
  <el-dropdown @command="handleRowAction">
    <el-button type="text">Actions</el-button>
    <template #dropdown>
      <el-dropdown-menu>
        <el-dropdown-item :command="{action: 'edit', row}">Edit</el-dropdown-item>
        <el-dropdown-item :command="{action: 'delete', row}">Delete</el-dropdown-item>
        <el-dropdown-item :command="{action: 'duplicate', row}">Duplicate</el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>
</template>

Element Plus Study Guide