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 name | Type | Description |
|---|---|---|
dropdown | Component | Put on an element with `popover`; anchored under its invoker |
dropdown-menu | Part | List of `<a>` or `<button>` items inside the dropdown |
popovertarget | State | On the trigger button: the id of the popover to toggle |
commandfor / command | State | Alternative trigger: `command="toggle-popover"` |
Dropdown
Without CSS anchor positioning, the menu opens as a sheet at the bottom of the viewport.
<button class="btn" popovertarget="dropdown-ex1-menu">Options</button>
<div id="dropdown-ex1-menu" class="dropdown" popover>
<ul class="dropdown-menu">
<li><button>Edit</button></li>
<li><button>Duplicate</button></li>
<li><a href="#">Share</a></li>
</ul>
</div>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.
Notifications
You are all caught up.
<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>