Skip to the content
Konstruktor UI

Navigation

Menu

A list of actions that opens from a button. <kui-menu> follows the WAI-ARIA menu button pattern: items, checkbox and radio items, groups, separators, shortcuts and a danger item.

<kui-menu> needs kui.js; the markup works without it as described below

The menu opens in the browser's top layer (the popover attribute), so a card with overflow: hidden does not cut it. Choosing an item fires kui-select on the element with the item and its data-value.

Examples

Actions with icons, shortcuts and a danger item

Press the button, or focus it and press Enter or Arrow Down. Arrows move, a letter jumps to the next item that starts with it, Escape closes and returns focus to the button.

HTML
<kui-menu>
  <button class="kui-button" type="button"><svg class="kui-icon" aria-hidden="true"><use href="kit/icons.svg#ellipsis"></use></svg><span>Document actions</span></button>
  <div class="kui-menu" role="menu" aria-label="Document actions" hidden>
    <button role="menuitem" aria-keyshortcuts="Control+R"><svg class="kui-icon" aria-hidden="true"><use href="kit/icons.svg#pencil"></use></svg><span class="kui-menu__label">Rename</span><span class="kui-menu__shortcut" aria-hidden="true">Ctrl R</span></button>
    <button role="menuitem" aria-keyshortcuts="Control+D"><svg class="kui-icon" aria-hidden="true"><use href="kit/icons.svg#copy"></use></svg><span class="kui-menu__label">Duplicate</span><span class="kui-menu__shortcut" aria-hidden="true">Ctrl D</span></button>
    <button role="menuitem"><svg class="kui-icon" aria-hidden="true"><use href="kit/icons.svg#download"></use></svg><span class="kui-menu__label">Download as PDF</span></button>
    <button role="menuitem" aria-disabled="true"><svg class="kui-icon" aria-hidden="true"><use href="kit/icons.svg#share-2"></use></svg><span class="kui-menu__label">Share (needs a team plan)</span></button>
    <div role="separator"></div>
    <button role="menuitem" class="kui-menu__item--danger"><svg class="kui-icon" aria-hidden="true"><use href="kit/icons.svg#trash-2"></use></svg><span class="kui-menu__label">Delete</span></button>
  </div>
</kui-menu>

Checkbox and radio items

A checkbox item turns on and off; a radio item picks one of its group. The menu closes after each choice, as the pattern asks, and the tick shows the next time it opens.

HTML
<kui-menu placement="bottom-end">
  <button class="kui-button" type="button"><svg class="kui-icon" aria-hidden="true"><use href="kit/icons.svg#sliders-horizontal"></use></svg><span>View</span></button>
  <div class="kui-menu" role="menu" aria-label="View" hidden>
    <button role="menuitemcheckbox" aria-checked="true">Show closed cases</button>
    <button role="menuitemcheckbox" aria-checked="false">Show amounts in EUR</button>
    <div role="separator"></div>
    <div role="group" aria-labelledby="menu-sort-label">
      <div class="kui-menu__group-label" id="menu-sort-label">Sort by</div>
      <button role="menuitemradio" aria-checked="true">Date of the next hearing</button>
      <button role="menuitemradio" aria-checked="false">Client’s name</button>
      <button role="menuitemradio" aria-checked="false">Iznos potraživanja</button>
    </div>
  </div>
</kui-menu>

An icon-only trigger with link items

Items may be links; the menu closes and the browser follows the link.

HTML
<kui-menu placement="bottom-end">
  <button class="kui-button kui-button--icon kui-button--ghost" type="button" aria-label="Account"><svg class="kui-icon" aria-hidden="true"><use href="kit/icons.svg#user"></use></svg></button>
  <div class="kui-menu" role="menu" aria-label="Account" hidden>
    <div class="kui-menu__group-label">Signed in as Nikola Đurić</div>
    <a role="menuitem" href="#menu-profile"><svg class="kui-icon" aria-hidden="true"><use href="kit/icons.svg#user"></use></svg><span class="kui-menu__label">Profile</span></a>
    <a role="menuitem" href="#menu-billing"><svg class="kui-icon" aria-hidden="true"><use href="kit/icons.svg#credit-card"></use></svg><span class="kui-menu__label">Plan and billing</span></a>
    <div role="separator"></div>
    <a role="menuitem" href="#menu-out"><svg class="kui-icon" aria-hidden="true"><use href="kit/icons.svg#log-out"></use></svg><span class="kui-menu__label">Sign out</span></a>
  </div>
</kui-menu>

States

Every state

Hover and the focused item share a soft fill; keyboard focus adds the ring. A disabled item (aria-disabled="true") is faded, skipped by the arrows and does nothing when pressed; say why in its name. A danger item is red and its word says what it destroys. A checked item shows a tick at the end. The open trigger has aria-expanded="true". A long menu scrolls inside itself, at most 70 percent of the window tall.

HTML
<div class="kui-menu" role="menu" aria-label="Every state, shown open">
  <button type="button" role="menuitem" tabindex="-1"><svg class="kui-icon" aria-hidden="true"><use href="kit/icons.svg#file"></use></svg><span class="kui-menu__label">Default</span></button>
  <button type="button" role="menuitem" tabindex="-1" aria-disabled="true"><svg class="kui-icon" aria-hidden="true"><use href="kit/icons.svg#lock"></use></svg><span class="kui-menu__label">Disabled, locked by Ana</span></button>
  <button type="button" role="menuitemcheckbox" tabindex="-1" aria-checked="true">Checked</button>
  <button type="button" role="menuitem" tabindex="0"><svg class="kui-icon" aria-hidden="true"><use href="kit/icons.svg#file-text"></use></svg><span class="kui-menu__label">Zahtev za izdavanje uverenja o državljanstvu</span><span class="kui-menu__shortcut" aria-hidden="true">Ctrl ⇧ U</span></button>
  <div role="separator"></div>
  <button type="button" role="menuitem" tabindex="-1" class="kui-menu__item--danger"><svg class="kui-icon" aria-hidden="true"><use href="kit/icons.svg#trash-2"></use></svg><span class="kui-menu__label">Delete 14 files</span></button>
</div>

Classes and attributes

kui-menu
The element. Its first button is the trigger; its [role="menu"] is the menu.
placement
bottom-start (default), bottom-end, top-start or top-end.
.kui-menu[role="menu"]
The menu. Write hidden on it, so it does not show before kui.js runs.
[role="menuitem"]
An item: a button, or a link for navigation. An icon, a .kui-menu__label and an optional .kui-menu__shortcut.
[role="menuitemcheckbox"], [role="menuitemradio"]
An item that is on or off (aria-checked). Radio items in one [role="group"] exclude each other.
[role="separator"]
A line between groups.
.kui-menu__group-label
A small heading of a group; tie it to the group with aria-labelledby.
.kui-menu__item--danger
A destructive item, in red.
aria-disabled="true"
An item that cannot be chosen now.
aria-keyshortcuts
The shortcut, for screen readers; the visible .kui-menu__shortcut is aria-hidden.
kui-select
Event when an item is chosen; detail.item and detail.value (data-value or the text).

Keyboard

Enter or Space or Arrow Down
On the button, opens the menu on the first item.
Arrow Up
On the button, opens the menu on the last item.
Arrow Down or Arrow Up
Moves to the next or previous item; wraps at the ends.
Home or End
Moves to the first or the last item.
A letter
Moves to the next item that starts with it.
Enter or Space
Chooses the focused item and closes the menu.
Escape
Closes the menu and returns focus to the button.
Tab
Closes the menu and moves on.

Accessibility

  • Follows the WAI-ARIA menu button pattern. The element sets aria-haspopup, aria-expanded and aria-controls on the trigger.
  • Items are not in the tab order; the arrows move between them, and focus returns to the trigger on Escape.
  • Give the menu a name: aria-label, or the element ties it to the trigger’s text.
  • A press outside the menu closes it without moving focus.
  • Use a menu for actions. To pick a value in a form, use a select or a combobox; to go to pages, use links in a nav.

Do and do not

Do

  • Keep a menu under about ten items; group them with separators.
  • Put the destructive item last, after a separator.
  • Ask for confirmation after a destructive item, in a dialog.

Do not

  • Do not put a form field or a second menu inside a menu.
  • Do not hide the only way to do a common action in a menu.
  • Do not use a menu as a website’s navigation.