Dropdown menu
Action menus for buttons, rows and avatars. Built on the browser's Popover API — they float above everything, close on a click outside or Esc, and place themselves so they always fit on screen.
<div class="dropdown">
<button class="btn dropdown-toggle">Options</button>
<div class="menu">
<div class="menu-header">Order #10421</div>
<button class="menu-item"><i class="mi mi-pencil"></i>Edit<span class="menu-shortcut">⌘E</span></button>
<a class="menu-item" href="/orders/10421"><i class="mi mi-eye"></i>View</a>
<hr class="menu-divider">
<button class="menu-item theme-red"><i class="mi mi-trash-2"></i>Delete</button>
</div>
</div>
That's all the markup. With moey.js:
- Opening — the button opens the menu next to it. In a split button, the
.dropdown-toggledoes. - Placement — below the button, flipping above or shifting sideways when there's no room. It follows the button while the page scrolls.
- Closing — choosing an item, a click outside, Esc, or opening another menu. Focus goes back to the button.
- Keyboard — ↓ / ↑ on the button opens it; ↑ ↓ Home End move; type a letter to jump to an item.
- Accessibility —
role="menu",menuitem,aria-haspopupandaria-expandedare added for you.
Submenus
Put a .dropdown inside a menu: its .menu-item gets a › and opens the inner menu beside it. Go as deep as you need.
<div class="menu">
<button class="menu-item">Edit</button>
<div class="dropdown">
<button class="menu-item">Share</button> <!-- gets the › -->
<div class="menu">
<button class="menu-item">Copy link</button>
…
</div>
</div>
</div>
- Mouse — opens after a short pause over the item (no flicker while passing over it); moving to another item closes it, with a little grace time so a diagonal move into the submenu doesn't.
- Click / tap opens it too. Keyboard — → opens it and focuses its first item, ← or Esc closes just that level, ↑ ↓ stay within a level.
- Placement — beside its item; near the screen edge it flips, and deeper levels keep cascading the same way.
- Choosing an item at any depth closes the whole menu. In the navbar's phone panel, submenus open inline instead.
Check & radio items
Give an item role="menuitemcheckbox" or role="menuitemradio" plus aria-checked. Clicking toggles it and keeps the menu open. Radios are grouped by the closest role="group".
<div role="group" aria-label="Sort by">
<div class="menu-header">Sort by</div>
<button class="menu-item" role="menuitemradio" aria-checked="true">Newest</button>
<button class="menu-item" role="menuitemradio" aria-checked="false">Oldest</button>
</div>
<button class="menu-item" role="menuitemcheckbox" aria-checked="true">Show archived</button>
Placement
Set data-moey-placement on the .menu. Whatever you pick, it flips to the other side or lines up with the other edge when it would run off the screen.
| Value | Opens |
|---|---|
bottom-start (default) · bottom-center · bottom-end | Below, lined up with the button's start, middle or end |
top-start · top-center · top-end | Above |
start · end | Beside the button, first item level with it (sidebars, toolbars). left / right pin the physical side. |
auto | Whichever of below / above has more room, growing toward the middle of the screen — a button on the right opens leftward, one near the bottom opens upward |
start/end follow the page direction, so they flip for right-to-left languages.
Colors
Menus are neutral by default. Color them with any theme — the theme tokens handle light and dark mode.
<div class="menu menu-purple"> <!-- neutral menu, accent highlight & checks -->
<div class="menu menu-subtle"> <!-- *-100 tint -->
<div class="menu menu-vivid menu-purple"> <!-- strong *-600 fill -->
<div class="menu menu-vivid menu-dark"> <!-- dark even in light mode -->
| Class | Looks like | Good for |
|---|---|---|
.menu-<color> | Neutral surface; hover, current item and checks in the color | Branded apps — the subtlest option |
.menu-subtle (+ .menu-<color>) | The theme's *-100 tint (a deep *-900 tint in dark mode) | Menus that belong to a colored area or status |
.menu-vivid · .menu-deep (+ .menu-<color>) | A full-color *-600 fill, or a dark *-800 one — white (or, for bright colors, dark) text | Toolbars, context menus; theme-dark for an inverse menu |
Inside subtle and solid menus, a colored item such as theme-red follows the menu's own colors (text, icon and hover) — no red on a tinted or filled menu; the icon and wording say "delete" well enough. In a neutral menu it stays red.
Right-click menus
<div data-moey-context="#file-menu">…</div>
<div class="menu" id="file-menu" popover>…</div>
A menu that lives somewhere else (not inside a .dropdown) gets the popover attribute and an id. Open it from any button with data-moey-open="#id", or on right-click with data-moey-context="#id".
Options
| Class | What it does |
|---|---|
data-moey-placement | Where it opens — see Placement |
.menu-match | At least as wide as the button |
.menu-sm · .menu-lg | Denser or roomier items |
.dropdown-toggle | Adds the chevron, which flips while open |
.menu-item + theme-red | Destructive (or any colored) item |
.menu-item.active / aria-current | The current page or choice |
data-moey-keep | On an item: choosing it keeps the menu open |
.menu-header · .menu-divider · .menu-text · .menu-body | Section label · line · free text · padded area for a small form |
.menu-shortcut · .menu-item-detail | Keyboard hint on the right · second line under an item (wrap both lines in .menu-item-content) |
From code & events
menu.addEventListener('moey:select', (e) => {
console.log(e.detail.value); // the item's data-value, or its text
});
Moey.openMenu(menu, button);
Moey.closeMenu(menu);
Events: moey:open (cancelable) · moey:select (cancelable — prevent it to keep the menu open) · moey:closed. A .menu without a .dropdown or popover is just a static menu panel.
Row actions
A ⋯ button at the end of each table row.
| Order | Customer | Total | Actions |
|---|---|---|---|
| #10421 | Ana Kim | $129.00 | |
| #10420 | Max Ruiz | $48.50 |
<td>
<div class="dropdown">
<button class="btn btn-gray btn-ghost btn-icon btn-sm" aria-label="Actions for #10421">
<i class="mi mi-ellipsis"></i>
</button>
<div class="menu" data-moey-placement="bottom-end">
<button class="menu-item"><i class="mi mi-eye"></i>View</button>
<div class="menu-divider"></div>
<button class="menu-item theme-red"><i class="mi mi-trash-2"></i>Delete</button>
</div>
</div>
</td>
Account menu
An avatar that opens the account menu, with a submenu for the color mode.
<div class="dropdown">
<button class="btn btn-gray btn-ghost gap-2"><span class="avatar avatar-sm">JD</span>Jane Doe<i class="mi mi-chevron-down"></i></button>
<div class="menu">
<div class="menu-header">jane@moey.test</div>
<a class="menu-item" href="#menu"><i class="mi mi-user"></i>Profile</a>
<a class="menu-item" href="#menu"><i class="mi mi-credit-card"></i>Billing</a>
<div class="dropdown">
<button class="menu-item"><i class="mi mi-moon"></i>Appearance</button>
<div class="menu">
<button class="menu-item" onclick="document.documentElement.dataset.moTheme='light'"><i class="mi mi-sun"></i>Light</button>
<button class="menu-item" onclick="document.documentElement.dataset.moTheme='dark'"><i class="mi mi-moon"></i>Dark</button>
<button class="menu-item" onclick="delete document.documentElement.dataset.moTheme"><i class="mi mi-monitor"></i>System</button>
</div>
</div>
<div class="menu-divider"></div>
<button class="menu-item theme-red"><i class="mi mi-log-out"></i>Sign out</button>
</div>
</div>