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
| Class | Description |
|---|---|
.cb-dropdown | Relative container. |
.cb-dropdown__panel | Floating panel. --end aligns it to the right, --up opens it upward, --wide widens it. |
.cb-dropdown__item | Item. --rich for number + title + description; --danger for destructive actions. |
.cb-dropdown__num, .cb-dropdown__title, .cb-dropdown__desc | Parts of the rich item. |
.cb-dropdown__shortcut | Right-aligned shortcut. |
.cb-dropdown__label, .cb-dropdown__divider | Group title and divider. |
data-cb-* attributes
| Attribute | Description |
|---|---|
data-cb-dropdown | Enables the component. |
data-cb-dropdown-trigger | Button that opens it (by default, the first button). |
data-cb-dropdown-panel | Panel (by default, .cb-dropdown__panel). |
data-cb-dropdown-hover | Also opens on hover. |
data-cb-dropdown-close | Any element inside the panel that should close it. |
JavaScript
| Event | When it fires |
|---|---|
cb:open / cb:close | On open or close. cb:open can be canceled. |
cb:select | When 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-controlsandaria-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.