v0.0.2
Demos (demo panel, opens in a new tab)

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

Basic usage: view
BLADE

Put the trigger in the trigger slot and the items in the default slot as zb:dropdown.item.

blade
<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>
PropValues
triggerSlot: the element that opens the menu

Alignment

Alignment: view
BLADE

The menu aligns to the right edge of the trigger (end); for buttons on the left, use align="start".

blade
<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>
PropValues
alignstart | end

Opening upward

Opening upward: view
BLADE

For triggers at the bottom of the page, open the menu upward with position="top" so it does not overflow the screen.

blade
<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>
PropValues
positionbottom | top

Items

Items: view
BLADE

An item with href is a link, one without it a button; set external on links to another site.

blade
<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>
PropValues
hrefA link when given, otherwise a button
externaltrue | false (with href: new tab, arrow icon, screen reader note)

Composable parts

Composable parts: view
BLADE

Write the menu with zb:dropdown.trigger and zb:dropdown.content; use zb:dropdown.heading for group headings and zb:dropdown.separator for separators.

blade
<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>
PropValues
zb:dropdown.triggerThe element that opens the menu
zb:dropdown.contentMenu box
zb:dropdown.itemhref, external, icon, danger
heading · separatorGroup heading and separator

Props

The values the component takes. What are props and slots?

PropTypeDefaultDescription
align—endstart · end
position—bottombottom · top
full-widthbooleanfalseTrigger and menu as wide as the container
dropdown.item: href—nullAn a when given, otherwise a button (type="button")
dropdown.item: externalbooleanfalseTogether 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 · falseIcon 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

1 example

Full width

Full width: view
BLADE

full-width stretches the trigger and the menu to the container width; in narrow panels it works like a select list.

blade
<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>
PropValues
full-widthtrue | false

Developer notes

Events, hooks and details
  • Add wire:click or x-on:click to items that are buttons. Long lists scroll after a height of 16rem.
  • A class given to zb: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) and aria-controls (the panel id). The menu does not use role="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
disabled on zb:dropdown.item goes 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.heading is a visual label only and does not group the items for screen readers.
Zubazar UI · ZubazarDesigned for people.