Native HTML Overlays in Production: <dialog>, Popover API, and CSS Anchor Positioning
How Haze UI builds top-layer overlays in plain HTML and CSS with Invoker Commands, anchor positioning fallbacks, and clean exit animations.
For nearly a decade, building a production modal or dropdown menu meant accepting hundreds of lines of JavaScript plumbing: z-index stacking contexts, React portals appended to document.body, inert background toggles, Escape key listeners, focus traps, and Popper/Floating UI coordinate loops.
In 2026, browsers handle almost all of that natively in the top layer. Yet using <dialog>, popover, and CSS Anchor Positioning in production still comes with real browser-boundary edges. If you simply copy bare MDN examples, exit animations snap abruptly, Safari/Firefox version gaps leave menus stranded in the wrong corner, and alert confirmations accidentally allow backdrop clicks.
Here is the exact battle-tested pattern Haze UI uses across Dialog, Drawer, Popover, Dropdown Menu, and Alert Dialog.
Choosing between <dialog> and [popover]
Every overlay belongs in one of two categories depending on whether the rest of the page should remain interactive while it is open:
| Requirement | <dialog> (showModal()) |
popover (popover="auto") |
|---|---|---|
| Blocks background interaction | Yes (marks rest of page inert natively) | No (unless backdrop is styled explicitly) |
| Focus trapping | Built-in while modal is open | Focus stays natural; Tab exits popover cleanly |
| Dismissal behavior | Escape (closedby="any" adds backdrop click) |
Native light dismiss (Escape, outside click, opening another popover) |
| Anchor positioning | Typically centered viewport or edge Drawer | Anchored to trigger via --anchor-name |
| Best Haze UI components | Dialog, Alert Dialog, Drawer | Popover, Dropdown Menu |
Never open a confirmation prompt with popover. If deleting a workspace requires explicit user acknowledgement, use <dialog role="alertdialog" closedby="closerequest"> so accidental outside clicks cannot dismiss a destructive decision.
Native modals with command, commandfor, and closedby
Invoker Commands (command="show-modal" and commandfor="<id>") let buttons open and close <dialog> elements purely declaratively—and <form method="dialog"> closes the modal cleanly on submit with zero JavaScript.
Here is the core structure behind Dialog:
<button type="button" command="show-modal" commandfor="invite-modal">
Invite teammate
</button>
<dialog
id="invite-modal"
aria-labelledby="invite-modal-title"
aria-describedby="invite-modal-desc"
closedby="any"
class="m-auto rounded-2xl border border-frame bg-popover p-0 text-popover-foreground shadow-float
translate-y-2 scale-[0.96] opacity-0
transition-[opacity,scale,translate,overlay,display] transition-discrete duration-200 ease-snappy
open:translate-y-0 open:scale-100 open:opacity-100
starting:open:translate-y-2 starting:open:scale-[0.96] starting:open:opacity-0
backdrop:bg-background/60 backdrop:opacity-0
backdrop:transition-[opacity,overlay,display] backdrop:transition-discrete backdrop:duration-200
open:backdrop:opacity-100 starting:open:backdrop:opacity-0"
>
<header class="border-b border-border p-5 pr-14">
<h2 id="invite-modal-title">Invite teammate</h2>
<p id="invite-modal-desc">They will receive an email link valid for 48 hours.</p>
</header>
<form method="dialog" class="absolute top-5 right-5">
<button type="submit" aria-label="Close">✕</button>
</form>
</dialog>
Look closely at three details that prevent subtle bugs:
closedby="any"vsclosedby="closerequest": Standard dialogs allow clicking the::backdropto close ("any"), whereas Alert Dialog sets"closerequest"so only Escape or explicit action buttons close it.- Backdrop clicks in older browsers: A tiny progressive enhancement checks
if (!("closedBy" in HTMLDialogElement.prototype))and closes onevent.target === dialog. Wait—notice whyp-0is placed on<dialog>and padding lives on an inner<div>! If<dialog>itself has padding, clicking inside that padding hits<dialog>directly instead of a child and triggers a false backdrop close. Keeping padding inside the inner wrapper ensuresevent.target === dialogonly fires on actual backdrop clicks. transition-discrete+starting:open:: Withouttransition-discrete(CSStransition-behavior: allow-discrete),display: nonetakes effect immediately when.close()runs, killing the exit animation. Withoverlayanddisplayincluded in the transition list, both the modal and its::backdropfade and slide smoothly in both directions.
Anchoring popovers and dropdown menus without JavaScript
The Popover API handles opening (popovertarget), light-dismiss closing, and Escape focus return automatically with zero script. Positioning the panel next to its trigger button uses CSS Anchor Positioning, paired with a @supports guard so older browsers degrade cleanly.
Inside Popover, the trigger declares style="anchor-name: --my-id" and the popover panel declares style="position-anchor: --my-id":
/* Simplified Tailwind v4 utility equivalents used in popover.astro */
.popover-panel {
/* Fallback (no CSS Anchor Positioning): centered sheet with backdrop */
margin: auto;
}
@supports (position-area: block-end) {
.popover-panel {
margin: 0;
margin-block: 4px;
position-area: block-end span-inline-end;
position-try-fallbacks: flip-block, flip-inline, flip-block flip-inline;
}
.popover-panel::backdrop {
background: transparent;
}
}
Why does this fallback design matter in production? If you write bare position-area without wrapping it inside @supports (position-area: block-end), a browser that supports popover but lacks anchor positioning renders the panel stuck at top: 0; left: 0 or misaligned off-screen. By making the un-anchored state an intentional centered sheet with backdrop:bg-background/60, every browser gets a readable, well-aligned surface—and browsers with Anchor Positioning automatically upgrade to edge-snapped dropdown placement with collision flipping (position-try-fallbacks).
Naming anchors dynamically so instances never collide
One subtle footgun in CSS Anchor Positioning catches developers the moment they place two dropdown menus or tooltips on the same page: anchor names are document-scoped unless scoped via anchor-scope or unique IDs.
If a reusable component hard-codes anchor-name: --menu-anchor, clicking the second menu on the page anchors its panel to the first menu’s trigger! That is why every anchored component in Haze UI requires a caller-provided id prop and derives:
id={id}on the popover elementpopovertarget={id}on the triggeranchor-name: --${id}andposition-anchor: --${id}
Following that rule across Dropdown Menu, Popover, and Tooltip guarantees zero collisions even when twelve interactive cards appear together on the Showcase.
Testing top-layer overlays in Playwright and reduced motion
When writing end-to-end checks for overlays, test two things visually and programmatically:
- Top-layer hit testing: Don’t just assert
toBeVisible(). Checkdocument.elementFromPoint(cx, cy)inside an open menu to prove sibling grid cards, sticky headers, oroverflow-hiddenancestors never clip or cover menu items. - Reduced motion compliance: Under
prefers-reduced-motion: reduce, our Motion system collapsestransition-durationandanimation-durationto0.01msacross*,::backdrop, and::details-contentso overlays appear and close instantaneously without disorienting users.
Inspect Dialog, Drawer, and Dropdown Menu on their interactive stages to test keyboard Escape, backdrop clicks, and viewport resizing firsthand, or read Accessible Accordions, Tabs, and Menus That Work Before JavaScript Loads for our companion disclosure patterns.
Frequently asked questions
What happens to a Haze UI Popover or Dropdown Menu in browsers without CSS Anchor Positioning?
Haze UI guards anchor rules inside supports-[position-area:block-end]. In browsers without anchor positioning support, the popover opens cleanly as a centered sheet over a dimmed backdrop—still keyboard-accessible with native Escape and outside-click dismissal.
Why does dialog.astro still include a small script alongside command and commandfor?
While current Chrome, Firefox, and Safari releases support Invoker Commands natively, older browser versions do not open <dialog> declaratively from button[commandfor]. A 20-line progressive fallback checks 'command' in HTMLButtonElement.prototype and handles clicks only where missing.
How do you animate <dialog> and [popover] when they close instead of just popping away?
Use transition-discrete with open: and starting:open: classes in Tailwind CSS v4 so display and overlay transitions wait until opacity, scale, and translate finish.
When should I use <dialog> versus [popover]?
Use <dialog> (opened via showModal()) for modal tasks that require user focus, inert background pages, or explicit confirmation—such as Alert Dialogs and Drawers. Use [popover] for non-modal anchored surfaces like Dropdown Menus, Tooltips, and quick filters that close automatically on light dismiss.
Components & blocks mentioned
Browse all entries- DialogUI
Native modal with a title, description, body, and footer actions.
npx shadcn@latest add @haze/dialog - DrawerUI
Modal panel that slides in from the right, left or bottom edge, with a header, scrolling body and footer, and swipe to dismiss.
npx shadcn@latest add @haze/drawer - PopoverUI
Button that opens a floating panel next to it, built on the native popover attribute with no script.
npx shadcn@latest add @haze/popover Button that opens a short list of links, with group headings, leading icons, trailing hints, a destructive style and one level of submenus.
npx shadcn@latest add @haze/dropdown-menu- TooltipUI
Small text bubble that appears above or below a control on hover and on keyboard focus.
npx shadcn@latest add @haze/tooltip A native confirmation dialog that starts focus on Cancel and ignores outside clicks.
npx shadcn@latest add @haze/alert-dialog
Continue reading
8 min read
8 min read
9 min read