Dropdown
A list that opens from a button and groups options such as export formats or row actions.
zb:dropdown zubazar/ui · free (MIT)
Basic usage
Put the trigger in the trigger slot and the items in the default slot as zb:dropdown.item.
<zb:dropdown align="start">
<zb:slot:trigger>
<zb:button variant="white">Export</zb:button>
</zb:slot:trigger>
<zb:dropdown.item>Excel (.xlsx)</zb:dropdown.item>
<zb:dropdown.item>PDF</zb:dropdown.item>
<zb:dropdown.item>CSV</zb:dropdown.item>
</zb:dropdown>
| Prop | Values |
|---|---|
trigger | Slot: the element that opens the menu |
Alignment
The menu aligns to the right edge of the trigger (end); for buttons on the left, use align="start".
<div class="flex justify-between">
<zb:dropdown align="start">
<zb:slot:trigger><zb:button variant="white">Left-aligned</zb:button></zb:slot:trigger>
<zb:dropdown.item>By customer</zb:dropdown.item>
<zb:dropdown.item>By date</zb:dropdown.item>
</zb:dropdown>
<zb:dropdown>
<zb:slot:trigger><zb:button variant="white">Right-aligned</zb:button></zb:slot:trigger>
<zb:dropdown.item>By customer</zb:dropdown.item>
<zb:dropdown.item>By date</zb:dropdown.item>
</zb:dropdown>
</div>
| Prop | Values |
|---|---|
align | start | end |
Opening upward
For triggers at the bottom of the page, open the menu upward with position="top" so it does not overflow the screen.
<zb:dropdown position="top" align="start">
<zb:slot:trigger><zb:button variant="white">Bulk action</zb:button></zb:slot:trigger>
<zb:dropdown.item>Activate</zb:dropdown.item>
<zb:dropdown.item>Deactivate</zb:dropdown.item>
<zb:dropdown.item>Archive</zb:dropdown.item>
</zb:dropdown>
| Prop | Values |
|---|---|
position | bottom | top |
Items
An item with href is a link, one without it a button; set external on links to another site.
<zb:dropdown align="start">
<zb:slot:trigger>
<zb:button variant="ghost" size="icon" aria-label="Row actions"><zb:icon name="ellipsis-vertical" /></zb:button>
</zb:slot:trigger>
<zb:dropdown.item href="#"><zb:icon name="eye" size="4" />View</zb:dropdown.item>
<zb:dropdown.item><zb:icon name="pencil-square" size="4" />Edit</zb:dropdown.item>
<zb:dropdown.item x-on:click="$dispatch('toast', { message: 'Product copied.' })"><zb:icon name="document-duplicate" size="4" />Copy</zb:dropdown.item>
<div class="my-1 border-t border-line"></div>
<zb:dropdown.item><zb:icon name="archive-box" size="4" />Archive</zb:dropdown.item>
<div class="my-1 border-t border-line"></div>
<zb:dropdown.item href="#" external><zb:icon name="question-mark-circle" size="4" />Help center</zb:dropdown.item>
</zb:dropdown>
| Prop | Values |
|---|---|
href | A link when given, otherwise a button |
external | true | false (with href: new tab, arrow icon, screen reader note) |
Composable parts
Write the menu with zb:dropdown.trigger and zb:dropdown.content; use zb:dropdown.heading for group headings and zb:dropdown.separator for separators.
<zb:dropdown align="end">
<zb:dropdown.trigger>
<zb:button variant="white">Order actions <zb:icon name="chevron-down" size="4" /></zb:button>
</zb:dropdown.trigger>
<zb:dropdown.content class="w-60">
<zb:dropdown.heading>Order #10482</zb:dropdown.heading>
<zb:dropdown.item icon="pencil-square">Edit</zb:dropdown.item>
<zb:dropdown.item icon="user-plus">Assign an agent</zb:dropdown.item>
<zb:dropdown.item icon="arrow-down-tray" href="#">Download the invoice</zb:dropdown.item>
<zb:dropdown.separator />
<zb:dropdown.item icon="trash" danger>Delete the order</zb:dropdown.item>
</zb:dropdown.content>
</zb:dropdown>
| Prop | Values |
|---|---|
zb:dropdown.trigger | The element that opens the menu |
zb:dropdown.content | Menu box |
zb:dropdown.item | href, external, icon, danger |
heading · separator | Group heading and separator |
Props
The values the component takes. What are props and slots?
| Prop | Type | Default | Description |
|---|---|---|---|
align | — | end | start · end |
position | — | bottom | bottom · top |
full-width | boolean | false | Trigger and menu as wide as the container |
dropdown.item: href | — | null | An a when given, otherwise a button (type="button") |
dropdown.item: external | boolean | false | Together with href: target="_blank" rel="noopener", an arrow icon on the right and the screen reader note "(opens in a new tab)"; no effect without href |
dropdown.item: icon · danger | — | null · false | Icon on the left of the item; danger marks a red (destructive, such as delete) action |
dropdown.trigger · dropdown.content | — | — | Composable usage: trigger and menu box |
dropdown.heading · dropdown.separator | — | — | Group heading and separator line |
Slots: trigger: the element that opens the menu · default: zb:dropdown.item items · Composable: zb:dropdown.trigger and zb:dropdown.content.
Advanced
Full width
full-width stretches the trigger and the menu to the container width; in narrow panels it works like a select list.
<zb:dropdown full-width>
<zb:slot:trigger><zb:button variant="white" class="w-full justify-between">Kadıköy store <zb:icon name="chevron-down" size="4" /></zb:button></zb:slot:trigger>
<zb:dropdown.item>Beşiktaş store</zb:dropdown.item>
<zb:dropdown.item>Kadıköy store</zb:dropdown.item>
<zb:dropdown.item>Üsküdar store</zb:dropdown.item>
</zb:dropdown>
| Prop | Values |
|---|---|
full-width | true | false |
Developer notes
- Add
wire:clickorx-on:clickto items that are buttons. Long lists scroll after a height of 16rem. - A
classgiven tozb:dropdown.content(e.g.w-60) is added to the menu box.
Accessibility
- ARIA and roles
- The first button or link inside the trigger gets
aria-expanded(whether the menu is open) andaria-controls(the panel id). The menu does not userole="menu": items are plain buttons and links, also reachable with Tab. - Keyboard
- On the trigger, Down arrow opens the menu on its first item and Up arrow on its last. In the menu, Up and Down arrows cycle through the items; Home and End jump to the first and last. Arrow keys typed in a search box inside the menu stay in the box.
- Closing and focus
- Escape closes the menu and returns focus to the trigger; inside a drawer or modal only the menu closes. Clicking an item, clicking outside, or moving focus out of the menu also closes it.
- Disabled items
disabledonzb:dropdown.itemgoes to the button, and the arrow keys skip disabled buttons.- What you provide
- Give an icon-only trigger (a "more" button, for example) an
aria-label.zb:dropdown.headingis a visual label only and does not group the items for screen readers.