Native Popover API Menu
A zero-dependency showcase of the native HTML Popover API — a dropdown menu, hint tooltip, and modal-style dialog built purely from the popover and popovertarget attributes. Light-dismiss, the top layer, Escape handling, and backdrop styling come for free from the browser, with an optional CSS Anchor Positioning variant gated behind @supports so tethered popovers snap to their trigger without a single line of positioning JavaScript.
MCP
Code
:root {
--bg: #0c0d10;
--surface: #15161b;
--surface-2: #1d1f27;
--ink: #e9eaf0;
--muted: #9a9cab;
--accent: #8b5cf6;
--accent-2: #22d3ee;
--ok: #34d399;
--warn: #fbbf24;
--danger: #f87171;
--line: rgba(255, 255, 255, 0.10);
--line-2: rgba(255, 255, 255, 0.18);
--r-sm: 8px;
--r-md: 12px;
--r-lg: 16px;
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
margin: 0;
min-height: 100vh;
background:
radial-gradient(1100px 560px at 12% -10%, rgba(139, 92, 246, 0.16), transparent 60%),
radial-gradient(900px 520px at 100% 0%, rgba(34, 211, 238, 0.10), transparent 55%),
var(--bg);
color: var(--ink);
font-family: "Inter", system-ui, -apple-system, sans-serif;
line-height: 1.5;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
code, kbd {
font-family: "JetBrains Mono", ui-monospace, SFMono-Regular, monospace;
}
.wrap {
max-width: 940px;
margin: 0 auto;
padding: 48px 24px 64px;
}
/* ---------- Masthead ---------- */
.masthead__tag {
display: inline-flex;
align-items: center;
gap: 8px;
font-size: 12px;
font-weight: 600;
letter-spacing: 0.04em;
text-transform: uppercase;
color: var(--accent-2);
padding: 6px 12px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(34, 211, 238, 0.06);
}
.masthead h1 {
margin: 18px 0 10px;
font-size: clamp(28px, 5vw, 40px);
font-weight: 700;
letter-spacing: -0.02em;
}
.lede {
margin: 0;
max-width: 68ch;
color: var(--muted);
font-size: 15.5px;
}
.lede code {
color: var(--ink);
background: var(--surface-2);
padding: 1px 6px;
border-radius: 6px;
font-size: 0.88em;
}
.support {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin-top: 20px;
}
.support__chip {
display: inline-flex;
align-items: center;
gap: 7px;
font-size: 12.5px;
font-weight: 500;
padding: 6px 11px;
border-radius: 999px;
border: 1px solid var(--line);
background: var(--surface);
color: var(--muted);
}
.support__chip::before {
content: "";
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--muted);
}
.support__chip.is-ok { color: var(--ink); border-color: rgba(52, 211, 153, 0.4); }
.support__chip.is-ok::before { background: var(--ok); box-shadow: 0 0 8px rgba(52, 211, 153, 0.6); }
.support__chip.is-no { border-color: rgba(248, 113, 113, 0.35); }
.support__chip.is-no::before { background: var(--danger); }
/* ---------- Grid / panels ---------- */
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 18px;
margin-top: 32px;
}
.panel {
background: linear-gradient(180deg, var(--surface) 0%, rgba(21, 22, 27, 0.75) 100%);
border: 1px solid var(--line);
border-radius: var(--r-lg);
padding: 22px;
position: relative;
overflow: visible;
}
.panel--wide { grid-column: 1 / -1; }
.panel__head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 8px;
}
.panel__head h2 {
margin: 0;
font-size: 16px;
font-weight: 600;
}
.panel__note {
margin: 0 0 18px;
font-size: 13.5px;
color: var(--muted);
}
.panel__note code {
color: var(--accent-2);
background: rgba(34, 211, 238, 0.08);
padding: 1px 6px;
border-radius: 6px;
font-size: 0.86em;
}
.pill {
font-family: "JetBrains Mono", monospace;
font-size: 11.5px;
color: var(--accent);
background: rgba(139, 92, 246, 0.12);
border: 1px solid rgba(139, 92, 246, 0.3);
padding: 3px 9px;
border-radius: 999px;
white-space: nowrap;
}
/* ---------- Buttons ---------- */
.btn {
font: inherit;
font-weight: 600;
font-size: 14px;
color: var(--ink);
background: var(--surface-2);
border: 1px solid var(--line-2);
border-radius: var(--r-md);
padding: 10px 16px;
display: inline-flex;
align-items: center;
gap: 9px;
cursor: pointer;
transition: transform 0.12s ease, background 0.15s ease, border-color 0.15s ease, box-shadow 0.15s ease;
}
.btn:hover { border-color: var(--line-2); background: #23252f; }
.btn:active { transform: translateY(1px); }
.btn:focus-visible {
outline: 2px solid var(--accent-2);
outline-offset: 2px;
}
.btn--primary {
background: var(--surface-2);
border-color: var(--line-2);
}
.btn--primary[aria-expanded="true"] {
border-color: var(--accent);
box-shadow: 0 0 0 3px rgba(139, 92, 246, 0.18);
}
.btn--danger {
color: #fff;
background: linear-gradient(180deg, #ef5f5f, #e04848);
border-color: rgba(248, 113, 113, 0.5);
}
.btn--danger:hover { background: linear-gradient(180deg, #f16d6d, #e24f4f); }
.btn--ghost {
background: transparent;
border-color: var(--line-2);
}
.btn--ghost:hover { background: var(--surface-2); }
.btn--soft {
font-family: "JetBrains Mono", monospace;
font-size: 12.5px;
padding: 9px 13px;
background: var(--surface);
border-color: var(--line);
color: var(--accent-2);
}
.btn--soft:hover { background: var(--surface-2); border-color: var(--accent-2); }
.chev { width: 15px; height: 15px; color: var(--muted); transition: transform 0.2s ease; }
.btn--primary[aria-expanded="true"] .chev { transform: rotate(180deg); }
.avatar {
width: 26px;
height: 26px;
border-radius: 50%;
display: inline-grid;
place-items: center;
font-size: 11px;
font-weight: 700;
color: #fff;
background: linear-gradient(135deg, var(--accent), var(--accent-2));
flex: none;
}
.avatar--lg { width: 40px; height: 40px; font-size: 14px; }
/* ---------- Dropdown menu popover ---------- */
.anchor-scope { position: relative; }
.menu {
margin: 0;
padding: 8px;
width: 236px;
background: var(--surface-2);
border: 1px solid var(--line-2);
border-radius: var(--r-md);
box-shadow: 0 20px 48px -12px rgba(0, 0, 0, 0.7), 0 0 0 1px rgba(0, 0, 0, 0.4);
color: var(--ink);
}
/* fallback positioning when anchor positioning is unavailable */
.menu:popover-open {
position: absolute;
inset: auto;
top: calc(anchor(bottom, 100%));
}
.menu__id {
display: flex;
align-items: center;
gap: 11px;
padding: 8px 8px 10px;
}
.menu__id strong { font-size: 13.5px; display: block; }
.menu__id small { color: var(--muted); font-size: 12px; }
.menu__sep {
border: none;
border-top: 1px solid var(--line);
margin: 6px 4px;
}
.menu__item {
width: 100%;
display: flex;
align-items: center;
gap: 11px;
padding: 9px 10px;
border: none;
background: transparent;
color: var(--ink);
font: inherit;
font-size: 13.5px;
font-weight: 500;
border-radius: var(--r-sm);
cursor: pointer;
text-align: left;
transition: background 0.12s ease, color 0.12s ease;
}
.menu__item svg { width: 16px; height: 16px; color: var(--muted); flex: none; }
.menu__item:hover,
.menu__item:focus-visible {
background: rgba(139, 92, 246, 0.16);
outline: none;
}
.menu__item:focus-visible { box-shadow: inset 0 0 0 1px var(--accent); }
.menu__item:hover svg { color: var(--accent-2); }
.menu__item--danger { color: var(--danger); }
.menu__item--danger svg { color: var(--danger); }
.menu__item--danger:hover { background: rgba(248, 113, 113, 0.14); }
.menu__kbd {
margin-left: auto;
font-size: 11px;
color: var(--muted);
background: var(--surface);
border: 1px solid var(--line);
border-radius: 5px;
padding: 1px 6px;
min-width: 18px;
text-align: center;
}
/* ---------- Tooltip popover ---------- */
.field-label {
display: flex;
align-items: center;
gap: 8px;
font-size: 13px;
font-weight: 600;
color: var(--muted);
margin-bottom: 8px;
}
.hint-trigger {
width: 18px;
height: 18px;
border-radius: 50%;
border: 1px solid var(--line-2);
background: var(--surface-2);
color: var(--muted);
font: 600 11px/1 "Inter", sans-serif;
cursor: help;
display: inline-grid;
place-items: center;
}
.hint-trigger:hover,
.hint-trigger:focus-visible {
color: var(--accent-2);
border-color: var(--accent-2);
outline: none;
}
.input {
width: 100%;
font: inherit;
font-family: "JetBrains Mono", monospace;
font-size: 13px;
color: var(--ink);
background: var(--bg);
border: 1px solid var(--line);
border-radius: var(--r-md);
padding: 11px 13px;
letter-spacing: 0.02em;
}
.input:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.tooltip {
margin: 0;
max-width: 260px;
padding: 11px 13px;
background: #0a0b0e;
color: var(--ink);
border: 1px solid var(--line-2);
border-radius: var(--r-sm);
font-size: 12.5px;
line-height: 1.45;
box-shadow: 0 14px 36px -10px rgba(0, 0, 0, 0.75);
}
/* ---------- Dialog (manual) popover ---------- */
.dialog {
margin: auto;
width: min(420px, calc(100vw - 40px));
padding: 26px;
text-align: center;
background: var(--surface);
border: 1px solid var(--line-2);
border-radius: var(--r-lg);
color: var(--ink);
box-shadow: 0 32px 70px -18px rgba(0, 0, 0, 0.8);
}
.dialog::backdrop {
background: rgba(6, 7, 9, 0.62);
backdrop-filter: blur(3px);
}
.dialog__icon {
width: 48px;
height: 48px;
margin: 0 auto 14px;
display: grid;
place-items: center;
border-radius: 50%;
color: var(--danger);
background: rgba(248, 113, 113, 0.12);
border: 1px solid rgba(248, 113, 113, 0.3);
}
.dialog__icon svg { width: 24px; height: 24px; }
.dialog h3 { margin: 0 0 8px; font-size: 18px; font-weight: 600; }
.dialog p { margin: 0 0 20px; color: var(--muted); font-size: 14px; }
.dialog__actions {
display: flex;
gap: 10px;
justify-content: center;
}
.dialog__actions .btn { flex: 1; justify-content: center; }
/* ---------- Toolbar + log ---------- */
.toolbar {
display: flex;
flex-wrap: wrap;
gap: 10px;
margin-bottom: 16px;
}
.log {
background: #0a0b0e;
border: 1px solid var(--line);
border-radius: var(--r-md);
padding: 12px 14px;
font-family: "JetBrains Mono", monospace;
font-size: 12px;
max-height: 150px;
overflow-y: auto;
display: flex;
flex-direction: column;
gap: 5px;
}
.log__row { color: var(--ink); }
.log__row--muted { color: var(--muted); }
.log__row .t { color: var(--accent-2); }
.log__row .e { color: var(--accent); font-weight: 500; }
.log__row .on { color: var(--ok); }
.log__row .off { color: var(--warn); }
/* ---------- Popover open animation ---------- */
[popover]:popover-open {
animation: pop-in 0.16s cubic-bezier(0.2, 0.9, 0.3, 1.2);
}
@keyframes pop-in {
from { opacity: 0; transform: translateY(-6px) scale(0.97); }
to { opacity: 1; transform: translateY(0) scale(1); }
}
.dialog:popover-open { animation: dlg-in 0.2s ease; }
@keyframes dlg-in {
from { opacity: 0; transform: translateY(8px) scale(0.96); }
to { opacity: 1; transform: none; }
}
/* ---------- Footer + toast ---------- */
.foot {
margin-top: 34px;
display: flex;
align-items: center;
gap: 10px;
color: var(--muted);
font-size: 13px;
}
.foot .dot {
width: 9px; height: 9px; border-radius: 50%;
background: var(--ok);
box-shadow: 0 0 10px rgba(52, 211, 153, 0.7);
}
.toast {
position: fixed;
left: 50%;
bottom: 24px;
transform: translateX(-50%) translateY(24px);
background: var(--surface-2);
color: var(--ink);
border: 1px solid var(--line-2);
border-radius: var(--r-md);
padding: 11px 18px;
font-size: 13.5px;
font-weight: 500;
box-shadow: 0 16px 40px -12px rgba(0, 0, 0, 0.7);
opacity: 0;
pointer-events: none;
transition: opacity 0.22s ease, transform 0.22s ease;
z-index: 50;
}
.toast.is-show {
opacity: 1;
transform: translateX(-50%) translateY(0);
}
/* ---------- CSS Anchor Positioning progressive enhancement ---------- */
@supports (anchor-name: --x) {
.menu-trigger { anchor-name: --account-anchor; }
.menu:popover-open {
position: fixed;
top: anchor(--account-anchor bottom);
left: anchor(--account-anchor left);
margin-top: 8px;
position-try-fallbacks: flip-block, flip-inline;
}
}
/* ---------- Responsive ---------- */
@media (max-width: 640px) {
.grid { grid-template-columns: 1fr; }
}
@media (max-width: 520px) {
.wrap { padding: 32px 16px 48px; }
.toolbar .btn { flex: 1; justify-content: center; }
.dialog__actions { flex-direction: column; }
}// Native Popover API Menu — zero positioning libraries.
// Everything visible is a real [popover]; JS only wires ARIA state,
// an event log, and progressive-enhancement feature detection.
(function () {
"use strict";
const $ = (sel, ctx = document) => ctx.querySelector(sel);
const accountBtn = $("#account-btn");
const accountMenu = $("#account-menu");
const hintBtn = $("#hint-btn");
const hintTip = $("#key-tip");
const logEl = $("#log");
const toastEl = $("#toast");
const supportEl = $("#support");
/* ---------------------------------------------------------------
* 1. Feature detection — surface what the browser supports.
* ------------------------------------------------------------- */
const hasPopover =
typeof HTMLElement !== "undefined" &&
HTMLElement.prototype.hasOwnProperty("popover");
const hasAnchor =
typeof CSS !== "undefined" &&
CSS.supports &&
CSS.supports("anchor-name: --x");
const hasTopLayerAnim = CSS && CSS.supports && CSS.supports("overlay: auto");
function chip(label, ok) {
const el = document.createElement("span");
el.className = "support__chip " + (ok ? "is-ok" : "is-no");
el.textContent = (ok ? "" : "no ") + label;
return el;
}
supportEl.append(
chip("Popover API", hasPopover),
chip("CSS Anchor Positioning", hasAnchor),
chip("overlay transitions", hasTopLayerAnim)
);
// Graceful fallback: if the Popover API is missing, the [popover]
// elements would stay visible in normal flow. Hide them and route
// triggers through a lightweight toggle so the demo still functions.
if (!hasPopover) {
document.querySelectorAll("[popover]").forEach((el) => {
el.setAttribute("data-fallback", "");
el.style.display = "none";
el.style.position = "absolute";
});
wireFallback();
}
/* ---------------------------------------------------------------
* 2. Event log helper.
* ------------------------------------------------------------- */
function now() {
return new Date().toLocaleTimeString([], {
hour12: false,
minute: "2-digit",
second: "2-digit",
});
}
let firstLog = true;
function log(name, state) {
if (firstLog) {
logEl.innerHTML = "";
firstLog = false;
}
const row = document.createElement("div");
row.className = "log__row";
const cls = state === "open" ? "on" : "off";
row.innerHTML =
'<span class="t">' + now() + "</span> " +
'<span class="e">' + name + "</span> → " +
'<span class="' + cls + '">' + state + "</span>";
logEl.prepend(row);
while (logEl.children.length > 30) logEl.lastChild.remove();
}
/* ---------------------------------------------------------------
* 3. Dropdown menu — keep aria-expanded in sync with toggle event.
* ------------------------------------------------------------- */
if (hasPopover) {
accountMenu.addEventListener("toggle", (e) => {
const open = e.newState === "open";
accountBtn.setAttribute("aria-expanded", String(open));
log("menu", e.newState);
if (open) {
// move focus to first item for keyboard users
const first = accountMenu.querySelector(".menu__item");
if (first) requestAnimationFrame(() => first.focus());
}
});
// Arrow-key roving focus inside the menu.
accountMenu.addEventListener("keydown", (e) => {
const items = [...accountMenu.querySelectorAll(".menu__item")];
const idx = items.indexOf(document.activeElement);
if (e.key === "ArrowDown") {
e.preventDefault();
items[(idx + 1) % items.length].focus();
} else if (e.key === "ArrowUp") {
e.preventDefault();
items[(idx - 1 + items.length) % items.length].focus();
} else if (e.key === "Home") {
e.preventDefault();
items[0].focus();
} else if (e.key === "End") {
e.preventDefault();
items[items.length - 1].focus();
}
});
accountMenu.querySelectorAll(".menu__item").forEach((item) => {
item.addEventListener("click", () => {
const label = item.textContent.trim().split("\n")[0].trim();
accountMenu.hidePopover();
toast(item.id === "signout" ? "Signed out" : label + " opened");
});
});
/* -------------------------------------------------------------
* 4. Tooltip — imperative show/hide on hover + focus.
* ----------------------------------------------------------- */
hintTip.addEventListener("toggle", (e) => log("tooltip", e.newState));
const showTip = () => {
try { hintTip.showPopover(); } catch (_) {}
};
const hideTip = () => {
try { hintTip.hidePopover(); } catch (_) {}
};
hintBtn.addEventListener("mouseenter", showTip);
hintBtn.addEventListener("mouseleave", hideTip);
hintBtn.addEventListener("focus", showTip);
hintBtn.addEventListener("blur", hideTip);
/* -------------------------------------------------------------
* 5. Dialog logging + confirm toast.
* ----------------------------------------------------------- */
const dialog = $("#confirm-dialog");
dialog.addEventListener("toggle", (e) => log("dialog", e.newState));
$("#confirm-delete").addEventListener("click", () =>
toast("Workspace deleted")
);
/* -------------------------------------------------------------
* 6. Imperative control toolbar.
* ----------------------------------------------------------- */
document.querySelectorAll("[data-act]").forEach((btn) => {
btn.addEventListener("click", () => {
const act = btn.dataset.act;
try {
if (act === "show") accountMenu.showPopover();
else if (act === "hide") accountMenu.hidePopover();
else accountMenu.togglePopover();
} catch (err) {
// showPopover throws if already shown — harmless here.
}
});
});
}
/* ---------------------------------------------------------------
* 7. Toast notification.
* ------------------------------------------------------------- */
let toastTimer;
function toast(msg) {
toastEl.textContent = msg;
toastEl.classList.add("is-show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => toastEl.classList.remove("is-show"), 2200);
}
/* ---------------------------------------------------------------
* 8. Minimal fallback for browsers without the Popover API.
* ------------------------------------------------------------- */
function wireFallback() {
document.querySelectorAll("[popovertarget]").forEach((trigger) => {
trigger.addEventListener("click", () => {
const target = document.getElementById(
trigger.getAttribute("popovertarget")
);
if (!target) return;
const action = trigger.getAttribute("popovertargetaction");
const showing = target.style.display === "block";
const next =
action === "show" ? true : action === "hide" ? false : !showing;
target.style.display = next ? "block" : "none";
});
});
// Escape closes auto-style fallbacks.
document.addEventListener("keydown", (e) => {
if (e.key === "Escape") {
document
.querySelectorAll('[popover="auto"][data-fallback]')
.forEach((el) => (el.style.display = "none"));
}
});
}
})();<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Native Popover API Menu</title>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=JetBrains+Mono:wght@400;500&display=swap"
rel="stylesheet"
/>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<main class="wrap">
<header class="masthead">
<div class="masthead__tag">Web Platform · No JS positioning</div>
<h1>Native Popover API Menu</h1>
<p class="lede">
Three overlay patterns — a dropdown menu, a tooltip, and a modal-style dialog — built with
nothing but the <code>popover</code> attribute and <code>popovertarget</code>. The browser
owns the top layer, light-dismiss and Escape. An <code>@supports</code> block adds CSS
Anchor Positioning where available.
</p>
<div class="support" id="support" aria-live="polite"></div>
</header>
<section class="grid">
<!-- ============ 1. Dropdown menu (auto + anchor) ============ -->
<article class="panel">
<div class="panel__head">
<h2>Dropdown menu</h2>
<span class="pill">popover="auto"</span>
</div>
<p class="panel__note">
Declarative trigger via <code>popovertarget</code>. Click outside or press
<kbd>Esc</kbd> to light-dismiss. Anchored to the button with CSS anchor positioning
when supported.
</p>
<div class="anchor-scope">
<button
class="btn btn--primary menu-trigger"
popovertarget="account-menu"
aria-haspopup="menu"
aria-expanded="false"
id="account-btn"
>
<span class="avatar" aria-hidden="true">RM</span>
Rosa Márquez
<svg class="chev" viewBox="0 0 16 16" aria-hidden="true">
<path d="M4 6l4 4 4-4" fill="none" stroke="currentColor" stroke-width="1.6"
stroke-linecap="round" stroke-linejoin="round" />
</svg>
</button>
<div id="account-menu" popover="auto" class="menu" role="menu" aria-label="Account">
<div class="menu__id">
<span class="avatar avatar--lg" aria-hidden="true">RM</span>
<div>
<strong>Rosa Márquez</strong>
<small>[email protected]</small>
</div>
</div>
<hr class="menu__sep" />
<button class="menu__item" role="menuitem" type="button">
<svg viewBox="0 0 16 16" aria-hidden="true"><circle cx="8" cy="5.5" r="2.6" fill="none" stroke="currentColor" stroke-width="1.4"/><path d="M3 13c0-2.5 2.2-4 5-4s5 1.5 5 4" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linecap="round"/></svg>
Profile
<kbd class="menu__kbd">P</kbd>
</button>
<button class="menu__item" role="menuitem" type="button">
<svg viewBox="0 0 16 16" aria-hidden="true"><path d="M8 2v3M8 11v3M2 8h3M11 8h3M4 4l2 2M10 10l2 2M12 4l-2 2M6 10l-2 2" stroke="currentColor" stroke-width="1.3" stroke-linecap="round"/><circle cx="8" cy="8" r="2.2" fill="none" stroke="currentColor" stroke-width="1.3"/></svg>
Settings
<kbd class="menu__kbd">,</kbd>
</button>
<button class="menu__item" role="menuitem" type="button">
<svg viewBox="0 0 16 16" aria-hidden="true"><path d="M6 3H3v10h3M6 8h7M13 8l-2.5-2.5M13 8l-2.5 2.5" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linecap="round" stroke-linejoin="round"/></svg>
Billing
</button>
<hr class="menu__sep" />
<button class="menu__item menu__item--danger" role="menuitem" type="button" id="signout">
<svg viewBox="0 0 16 16" aria-hidden="true"><path d="M10 3H3v10h7M9 8h5M14 8l-2.5-2.5M14 8l-2.5 2.5" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linecap="round" stroke-linejoin="round"/></svg>
Sign out
</button>
</div>
</div>
</article>
<!-- ============ 2. Tooltip (auto, imperative hover) ============ -->
<article class="panel">
<div class="panel__head">
<h2>Hint tooltip</h2>
<span class="pill">showPopover()</span>
</div>
<p class="panel__note">
Same API, driven imperatively. Hover or focus the field label to reveal help text in the
top layer — no <code>z-index</code>, no clipping by overflow.
</p>
<label class="field-label" for="api-key">
API secret key
<button
class="hint-trigger"
id="hint-btn"
aria-describedby="key-tip"
aria-label="What is this field?"
type="button"
>?</button>
</label>
<input class="input" id="api-key" type="text" value="sk_live_9f2b•••••••••••" readonly />
<div id="key-tip" popover="auto" class="tooltip" role="tooltip">
Your secret key authenticates server-side requests. Never expose it in client code —
rotate it if it leaks.
</div>
</article>
<!-- ============ 3. Modal-style dialog (manual) ============ -->
<article class="panel panel--wide">
<div class="panel__head">
<h2>Confirmation dialog</h2>
<span class="pill">popover="manual"</span>
</div>
<p class="panel__note">
A <code>manual</code> popover ignores light-dismiss, so it behaves like a modal. It still
rides the top layer and gets a styled <code>::backdrop</code>. Close only via its buttons.
</p>
<button class="btn btn--danger" popovertarget="confirm-dialog" popovertargetaction="show">
Delete workspace…
</button>
<div id="confirm-dialog" popover="manual" class="dialog" role="dialog" aria-modal="true" aria-labelledby="dlg-title">
<div class="dialog__icon" aria-hidden="true">
<svg viewBox="0 0 24 24"><path d="M12 8v5M12 16.5v.01M12 3l9 16H3z" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/></svg>
</div>
<h3 id="dlg-title">Delete “Aurora” workspace?</h3>
<p>
This permanently removes 42 resources and 3 members. This action cannot be undone.
</p>
<div class="dialog__actions">
<button class="btn btn--ghost" popovertarget="confirm-dialog" popovertargetaction="hide">Cancel</button>
<button class="btn btn--danger" popovertarget="confirm-dialog" popovertargetaction="hide" id="confirm-delete">Yes, delete it</button>
</div>
</div>
</article>
<!-- ============ 4. Imperative controls ============ -->
<article class="panel panel--wide">
<div class="panel__head">
<h2>Imperative control</h2>
<span class="pill">.togglePopover()</span>
</div>
<p class="panel__note">
The JS API mirrors the attributes. These buttons script the dropdown directly and every
state change is logged from the <code>toggle</code> event.
</p>
<div class="toolbar">
<button class="btn btn--soft" data-act="show">showPopover()</button>
<button class="btn btn--soft" data-act="hide">hidePopover()</button>
<button class="btn btn--soft" data-act="toggle">togglePopover()</button>
</div>
<div class="log" id="log" aria-live="polite" aria-label="Event log">
<div class="log__row log__row--muted">Interact with any popover to see events…</div>
</div>
</article>
</section>
<footer class="foot">
<span class="dot"></span>
Every overlay here is a native popover. View source — there is no positioning library.
</footer>
</main>
<div class="toast" id="toast" role="status" aria-live="polite"></div>
<script src="script.js"></script>
</body>
</html>Native Popover API Menu
This demo leans entirely on the HTML Popover API — the popover attribute plus popovertarget on a button — to open three distinct overlay patterns without any positioning library. The account dropdown menu and the info tooltip use popover="auto", so the browser handles the top layer, light-dismiss (click outside or press Escape), and only-one-open-at-a-time semantics. The confirmation dialog uses popover="manual" to stay put until the user makes a choice, giving you a modal-ish flow with a styled ::backdrop.
Every open element is promoted to the browser’s top layer, so it always paints above surrounding content with no z-index juggling. You can drive popovers declaratively (a plain button with popovertarget) or imperatively with showPopover(), hidePopover(), and togglePopover() — the toolbar buttons below show both. The beforetoggle and toggle events keep the trigger’s aria-expanded state and a live status log in sync.
For tethered placement the menu upgrades progressively: a @supports (anchor-name: --x) block wires the dropdown to its button with CSS Anchor Positioning (anchor-name / position-anchor / anchor()), so it follows the trigger natively. Browsers without anchor support fall back to a sensible absolute layout, and a runtime feature check surfaces exactly which capabilities your browser has.