Dropdown
stimeo--dropdown
A region toggled by a button. The open state, Esc, and outside clicks are handled for you.
The plainest way to toggle a region from a button. The content is yours: a set of links, a form, anything. Whether it is open is announced to screen readers. Esc closes it and returns focus to the button, and a click outside closes it too. If you need an application menu where the arrow keys walk the commands, use the menu instead.
Keyboard
| Key | Action |
|---|---|
| Enter / Space | Toggle the menu while the trigger is focused (native button). |
| Esc | Close the menu and return focus to the trigger. |
<%# Markup for the dropdown demo.
The single source used for both the live render and the copy-paste code.
Connect to stimeo--dropdown via data-controller / data-*-target / data-action.
The behavior is the library's; the look (classes) is the Playground's CSS. %>
<div class="dropdown" data-controller="stimeo--dropdown">
<button
type="button"
class="demo-trigger"
aria-expanded="false"
data-stimeo--dropdown-target="trigger"
data-action="click->stimeo--dropdown#toggle">
<%= t("components.dropdown.demo.trigger") %>
</button>
<%# The toggled menu. Starts hidden (closed).
Each placeholder link closes the menu and prevents navigation so the event
readout stays visible. Real destination links can omit :prevent.
(The links are inside the controller element, so an outside click won't close
it — wire it explicitly.) %>
<ul class="dropdown__menu" data-stimeo--dropdown-target="menu" hidden>
<li><a class="demo-menuitem" href="#profile"
data-action="click->stimeo--dropdown#close:prevent"><%= t(
"components.dropdown.demo.profile"
) %></a></li>
<li><a class="demo-menuitem" href="#settings"
data-action="click->stimeo--dropdown#close:prevent"><%= t(
"components.dropdown.demo.settings"
) %></a></li>
<li><a class="demo-menuitem" href="#sign-out"
data-action="click->stimeo--dropdown#close:prevent"><%= t(
"components.dropdown.demo.sign_out"
) %></a></li>
</ul>
</div>
<%# Readout: what the component just reported. Purely visual — the component
already announces what a screen reader needs, so this line is aria-hidden to
keep it from being read twice. The value is a code fragment (API identifiers
and values), so only the label is translated. %>
<p class="demo-readout" aria-hidden="true">
<span><%= t("catalog.readout.events") %></span>
<span class="demo-readout__value" data-readout>—</span>
</p>
/*
* Presentation-only styles for the dropdown demo.
* Stimeo UI itself ships behavior only (no CSS), so styling that makes the demo
* readable lives here. When adding a new component, create its
* demos/<name>/demo.css the same way.
*/
.dropdown {
position: relative;
display: inline-block;
}
.dropdown__menu {
position: absolute;
left: 0;
margin: 0.25rem 0 0;
padding: 0.25rem;
list-style: none;
min-width: 12rem;
background: var(--surface-card);
border: 1px solid var(--border-strong);
border-radius: 0.375rem;
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
}
.dropdown__menu li {
margin: 0;
}
// Every move of the open state is reported, with a `reason` saying what drove it
// — a trigger, Escape, a click outside, or a call from your own code. A real app
// subscribes to mirror the state elsewhere or to send an analytics event; this
// demo writes the event name and its reason into the readout so both are visible
// while you interact. `reason: "api"` is the one to watch for: a close you asked
// for comes back to your own listener.
// The page chrome uses some of these controllers too (the nav, the tab strip),
// so everything below is scoped to the demo itself.
const demoStage = document.querySelector(".demo-stage") ?? document;
const stateReadout = demoStage.querySelector("[data-readout]");
const stateRoot = demoStage.querySelector("[data-controller~='stimeo--dropdown']");
for (const name of ["open", "close"]) {
stateRoot?.addEventListener(`stimeo--dropdown:${name}`, (event) => {
const reason = event.detail?.reason;
if (stateReadout) {
stateReadout.textContent = reason
? `stimeo--dropdown:${name} · reason: ${reason}`
: `stimeo--dropdown:${name}`;
}
});
}
These demo styles use shared design tokens (light + dark). Copy the shared styles too, then toggle data-theme on your root element for dark mode.
The data-* attributes you add to your own HTML to wire this component. Put the data-controller below on a root element, then place its targets / values / actions inside that element.
On the root element
data-controller="stimeo--dropdown"
Targets
| Name | Description | Attribute |
|---|---|---|
trigger
required
|
The button that toggles the menu; its aria-expanded reflects open/closed state. |
data-stimeo--dropdown-target="trigger" |
menu
required
|
The disclosed region whose hidden attribute reflects visibility. | data-stimeo--dropdown-target="menu" |
Actions
| Name | Description | Action |
|---|---|---|
close
|
Hides the menu and sets the trigger's aria-expanded to false. |
stimeo--dropdown#close |
open
|
Reveals the menu and sets the trigger's aria-expanded to true. |
stimeo--dropdown#open |
toggle
|
Toggles the menu between open and closed. | stimeo--dropdown#toggle |
Events
| Name | Description | Event |
|---|---|---|
close
|
Fires right after the menu closes; detail.reason says what moved it. |
stimeo--dropdown:close |
open
|
Fires right after the menu opens; detail.reason says what moved it. |
stimeo--dropdown:open |
State hooks
The library only manages these ARIA/data attributes and custom properties. Your CSS reads them to render the look — selectors like [aria-selected], [aria-expanded], or var(--stimeo--…) hook into this state.
| Hook | Target | Meaning |
|---|---|---|
aria-expanded |
Trigger | "true" while the menu is open, "false" when closed. |
hidden |
Menu | Removed when open, present when closed. |