Dropdown

Dropdown

A dropdown is a menu in the top layer, opened with the native popover attribute. Outside click and Esc close it with no JavaScript.

Class names

Class nameTypeDescription
dropdownComponentPut on an element with `popover`; anchored under its invoker
dropdown-menuPartList of `<a>` or `<button>` items inside the dropdown
popovertargetStateOn the trigger button: the id of the popover to toggle
commandfor / commandStateAlternative trigger: `command="toggle-popover"`

Invoker command

commandfor and command="toggle-popover" do the same as popovertarget.

<button class="btn btn-primary" commandfor="dropdown-ex2-menu" command="toggle-popover">Account</button>
<div id="dropdown-ex2-menu" class="dropdown" popover>
  <ul class="dropdown-menu">
    <li><a href="#">Profile</a></li>
    <li><a href="#">Billing</a></li>
    <li><button>Sign out</button></li>
  </ul>
</div>

With icons

<button class="btn btn-outline" popovertarget="dropdown-ex3-menu">Actions</button>
<div id="dropdown-ex3-menu" class="dropdown" popover>
  <ul class="dropdown-menu">
    <li>
      <button>
        <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true"><path d="M12 5v14M5 12h14"/></svg>
        New file
      </button>
    </li>
    <li>
      <button>
        <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true"><path d="M5 12h14"/></svg>
        Remove
      </button>
    </li>
  </ul>
</div>

Icon button trigger

An icon-only trigger needs an accessible name.

<button class="btn btn-ghost" popovertarget="dropdown-ex4-menu" aria-label="More actions">
  <svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><circle cx="5" cy="12" r="2"/><circle cx="12" cy="12" r="2"/><circle cx="19" cy="12" r="2"/></svg>
</button>
<div id="dropdown-ex4-menu" class="dropdown" popover>
  <ul class="dropdown-menu">
    <li><button>Rename</button></li>
    <li><button>Archive</button></li>
    <li><button>Delete</button></li>
  </ul>
</div>

Custom content

The dropdown can hold any content, not only a menu.

<button class="btn" popovertarget="dropdown-ex5-panel">Notifications</button>
<div id="dropdown-ex5-panel" class="dropdown" popover>
  <p><strong>Notifications</strong></p>
  <p>You are all caught up.</p>
</div>