Dropdown

A menu of actions

The children of .dropdown are <button> and <a>. Give each button popovertarget + popovertargetaction="hide" so a click closes the menu; listen for the button's own click to act. <hr> separates groups.

Alignment: start, center and end of the button

data-align on the menu, default start. The same attribute as the popover.

Side: below, above, left and right of the button

data-side, default bottom. A menu that does not fit flips to the other side.

From an icon button at the end of a row, aligned to its end

data-align="end" keeps a menu from an end-of-row button inside the page. An icon button needs an aria-label.

Order #1042

$84.50 · Paid

Headings, shortcuts and a disabled item

<h3> is a gray heading, <kbd> sits at the end, and disabled makes an item unclickable and skipped by Tab. A link cannot be disabled: use aria-disabled="true" and remove its href. The shortcut is only a label: wire the key yourself.

Checkbox items: choose which columns to show

Use <input type="checkbox"> inside a <label>. Native checkboxes: Space ticks the focused one, the menu stays open, and the page listens for change and reads checked.

Radio items: choose one

Use <input type="radio"> with the same name. The arrow keys move the choice within the group, as in any radio group.

API

Methods (native)

Member Type Description
showPopover() method Opens the menu.
hidePopover() method Closes the menu.
togglePopover(force?) method Opens it when closed, closes it when open.

Events (native)

Event Detail Description
toggle newState: "open" / "closed" On the menu, after it opens or closes.
click none On an item: do its action.
change none On a checkbox or radio item: read checked.

Keyboard

Key Description
Enter / Space On the button: opens or closes the menu. On an item: chooses it.
Tab / Shift Tab Moves through the items; the menu comes right after its button in the focus order.
↑ ↓ In a radio group: moves the choice.
Esc Closes the menu and returns focus to the button.

No script and no role="menu": items are plain buttons and links, so Tab moves between them instead of the arrow keys.

Attributes

Attribute / element Values Description
class="dropdown" on the menu The component: a bordered list at least 10rem wide.
popover + id auto (default) Closes on an outside click, Escape, or another menu.
popovertarget the menu's id On the button; the menu is placed against it.
popovertargetaction hide on an item, toggle (default) on the button On an item button: closes the menu on click.
data-side none or bottom (default), top, left, right The side of the button the menu opens on. Flips when it does not fit.
data-align none or start (default), center, end How the menu lines up with the button.
> button, > a items Menu items. An icon before the text is gray.
> label > input type="checkbox" or type="radio" A checkbox or radio item; the menu stays open.
> h2, > h3, > h4 text A small gray group heading.
> hr Separates groups.
kbd inside an item A shortcut label at the end of the item.
disabled, aria-disabled disabled on a button or input, aria-disabled="true" on a link Grayed out; a disabled button is skipped by Tab.
data-variant danger A destructive item, in red.