Skip to content
npm ↗ GitHub ↗

Documentation

22/Actions and labels

Dropdown

Generic dropdown menu with keyboard navigation, shortcuts, dividers and destructive actions.

Right aligned

HTML
<div class="cb-dropdown ml-auto self-start" data-cb-dropdown>
  <button class="cb-btn cb-btn--outline cb-btn--icon" type="button" aria-label="Más opciones" data-cb-dropdown-trigger><svg viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><circle cx="5" cy="12" r="1.6"/><circle cx="12" cy="12" r="1.6"/><circle cx="19" cy="12" r="1.6"/></svg></button>
  <div class="cb-dropdown__panel cb-dropdown__panel--end" role="menu" aria-label="Opciones">
    <a class="cb-dropdown__item" href="#" role="menuitem">Perfil</a>
    <a class="cb-dropdown__item" href="#" role="menuitem">Configuración</a>
    <hr class="cb-dropdown__divider">
    <a class="cb-dropdown__item" href="#" role="menuitem">Cerrar sesión</a>
  </div>
</div>

Classes and modifiers

ClassDescription
.cb-dropdownRelative container.
.cb-dropdown__panelFloating panel. --end aligns it to the right, --up opens it upward, --wide widens it.
.cb-dropdown__itemItem. --rich for number + title + description; --danger for destructive actions.
.cb-dropdown__num, .cb-dropdown__title, .cb-dropdown__descParts of the rich item.
.cb-dropdown__shortcutRight-aligned shortcut.
.cb-dropdown__label, .cb-dropdown__dividerGroup title and divider.

data-cb-* attributes

AttributeDescription
data-cb-dropdownEnables the component.
data-cb-dropdown-triggerButton that opens it (by default, the first button).
data-cb-dropdown-panelPanel (by default, .cb-dropdown__panel).
data-cb-dropdown-hoverAlso opens on hover.
data-cb-dropdown-closeAny element inside the panel that should close it.

JavaScript

EventWhen it fires
cb:open / cb:closeOn open or close. cb:open can be canceled.
cb:selectWhen a menuitem is chosen (detail.item, detail.value).

Listen for the selection:

JS
document.querySelector('#acciones').addEventListener('cb:select', (event) => {
  console.log(event.detail.value);
});

Accessibility

  • The button gets aria-expanded, aria-controls and aria-haspopup="menu".
  • Esc closes it and returns focus to the button; clicking outside also closes it.
  • Use role="menu" only for actions. For navigation links, leave the panel without a role.