Dropdown
<wa-dropdown>
Dropdowns display a list of options triggered by a button or other element. They support keyboard navigation, submenus, and checkable items for building menus and context actions.
Dropdowns consist of a trigger and a panel. By default, activating the trigger will expose the panel and interacting outside of the panel will close it.
Dropdowns are designed to work well with dropdown items to provide a list of options the user can select from. However, dropdowns can also be used in lower-level applications. The API gives you complete control over showing, hiding, and positioning the panel.
Examples
Getting the Selected Item
When an item is selected, the wa-select event will be emitted by the dropdown. You can inspect event.detail.item to get a reference to the selected item. If you've provided a value for each dropdown item, it will be available in event.detail.item.value.
To keep the dropdown open after selection, call event.preventDefault() in the wa-select event's callback.
Showing Icons
Use the icon slot to add icons to dropdown items. This works best with icon elements.
Showing Labels & Dividers
Use any heading, e.g. <h1>–<h6> to add labels and the <wa-divider> element for separators.
Type
Showing Details
Use the details slot to display details, such as keyboard shortcuts, inside dropdown items.
Checkable Items
You can turn a dropdown item into a checkable option by setting type="checkbox". Add the checked attribute to make it checked initially. When clicked, the item's checked state will toggle and the dropdown will close. You can cancel the wa-select event if you want to keep it open instead.
When a checkable option exists anywhere in the dropdown, all items will receive additional padding so they align properly.
Destructive Items
Add variant="danger" to any dropdown item to highlight that it's a dangerous action.
Danger zone
Placement
The preferred placement of the dropdown can be set with the placement attribute. Note that the actual position may vary to ensure the panel remains in the viewport.
Distance
The distance from the panel to the trigger can be customized using the distance attribute. This value is specified in pixels.
Offset
The offset of the panel along the trigger can be customized using the skidding attribute. This value is specified in pixels.
Submenus
To create submenus, nest dropdown items inside of a dropdown item and assign slot="submenu" to each one. You can also add dividers as needed.
Dropdown items that have a submenu will not dispatch the wa-select event. However, items inside the submenu will, unless they also have a submenu.
As a UX best practice, avoid using more than one level of submenu when possible.
Disabling Items
Add the disabled attribute to any dropdown item to disable it.
Importing
If you're using the autoloader or a hosted project, components load on demand — no manual import needed. To cherry-pick a component manually, use one of the following snippets.
Import this component directly from the CDN:
import 'https://ka-f.webawesome.com/[email protected]/components/dropdown/dropdown.js';
After installing Web Awesome via npm, import this component:
import '@awesome.me/webawesome/dist/components/dropdown/dropdown.js';
If you're self-hosting Web Awesome, import this component from your server:
import './webawesome/dist/components/dropdown/dropdown.js';
To import this component for React 18 or below, use the following code:
import WaDropdown from '@awesome.me/webawesome/dist/react/dropdown/index.js';
Slots
Learn more about using slots.
| Name | Description |
|---|---|
| (default) | The dropdown's items, typically <wa-dropdown-item> elements. |
trigger
|
The element that triggers the dropdown, such as a <wa-button> or <button>. |
Attributes & Properties
Learn more about attributes and properties.
| Name | Description | Reflects | |
|---|---|---|---|
css |
One or more CSSResultGroup to include in the component's shadow root. Host styles are automatically prepended.
Type
CSSResultGroup | undefinedDefault
[sizeStyles, styles] |
||
distancedistance |
The distance of the dropdown menu from its trigger.
Type
numberDefault
0 |
||
openopen |
Opens or closes the dropdown.
Type
booleanDefault
false |
|
|
placementplacement |
The placement of the dropdown menu in reference to the trigger. The menu will shift to a more optimal location if
the preferred placement doesn't have enough room.
Type
'top'
| 'top-start'
| 'top-end'
| 'bottom'
| 'bottom-start'
| 'bottom-end'
| 'right'
| 'right-start'
| 'right-end'
| 'left'
| 'left-start'
| 'left-end'Default
'bottom-start' |
|
|
sizesize |
The dropdown's size.
Type
'xs' | 's' | 'm' | 'l' | 'xl' | 'small' | 'medium' | 'large'Default
'm' |
|
|
skiddingskidding |
The offset of the dropdown menu along its trigger.
Type
numberDefault
0 |
Events
Learn more about events.
| Name | Description |
|---|---|
wa-after-hide |
Emitted after the dropdown has been hidden. |
wa-after-show |
Emitted after the dropdown has been shown. |
wa-hide |
Emitted when the dropdown is about to hide. |
wa-select |
Emitted when an item in the dropdown is selected. |
wa-show |
Emitted when the dropdown is about to show. |
CSS custom properties
Learn more about CSS custom properties.
| Name | Description |
|---|---|
--hide-duration |
The duration of the hide animation.
|
--show-duration |
The duration of the show animation.
|
CSS parts
Learn more about CSS parts.
| Name | Description | CSS selector |
|---|---|---|
base |
The component's host element. |
::part(base)
|
menu |
The dropdown menu container. |
::part(menu)
|
Dependencies
This component automatically imports the following elements. Sub-dependencies, if any exist, will also be included in this list.