Built on popover="auto", a <dialog> element, a popovertarget Trigger and an Invoker Command Close. The platform supplies the top layer, light dismiss, Escape, one-at-a-time stacking with nesting, placement next to the trigger, a declarative trigger that works before hydration, and focus return. The library adds the asynchronous half: opening a popover whose content loads is a transition, so the trigger shows data-pending and the popover opens once its content is ready; and the closes the user asks a part for run through the library too, so <ViewTransition> can animate the exit, in Firefox as well.
// No "use client": this file is a Server Component. The Popover.Root has no `open`, `defaultOpen`
// or `openChangeAction`, so it runs in JS-free mode: the Trigger is a native `popovertarget`
// invoker and Done is a `command="hide-popover"` button. The browser opens the popup in the top
// layer below the button, focuses the input (the first focusable element, through the dialog
// focusing steps), and closes it on Escape, a click outside or Done, before hydration and with
// JavaScript off. No `autoFocus`: React writes it only into
// server-rendered HTML, not after a client navigation; `initialFocus` covers both.
import { Popover } from "@funstack/ui/popover";
export function PopoverShare() {
return (
<Popover.Root>
<Popover.Trigger className="demo-button">Share</Popover.Trigger>
<Popover.Popup className="demo-popover" align="start">
<Popover.Title>Share this page</Popover.Title>
<input
className="demo-popover-input"
readOnly
value="https://ui.funstack.work/components/popover"
/>
<div className="demo-popover-actions">
<Popover.Close className="demo-button">Done</Popover.Close>
</div>
</Popover.Popup>
</Popover.Root>
);
}/* Popover: the Popup is the <dialog popover> itself. Open state is `:popover-open` on it (exact
in both modes); the Trigger carries `data-open` in both modes and `data-pending` in library
mode only. A <button> Trigger has no `aria-expanded` (the browser exposes its state). */
.demo-popover {
/* The popover UA stylesheet sets `inset: 0; margin: auto`: reset both for anchored placement. */
margin: 0;
inset: auto;
min-inline-size: 14rem;
padding: 16px;
border: 1px solid var(--color-border);
border-radius: 10px;
background: var(--color-bg);
color: var(--color-text);
box-shadow: 0 12px 40px rgb(0 0 0 / 0.25);
}
/* The gap. `data-side` is where the browser placed the popup (absent with `side={null}`); a
flip swaps the popup's margins, so the gap goes on both sides of the axis. */
.demo-popover[data-side="bottom"],
.demo-popover[data-side="top"] {
margin-block: 6px;
}
.demo-popover h2 {
margin: 0 0 8px;
padding: 0;
border: 0;
font-size: 1rem;
}
/* Enter and exit with CSS, both modes. Enter works everywhere; the exit animates in Chromium
(with `overlay`) and Safari and is instant in Firefox. On iOS, `position-try-fallbacks`
together with `@starting-style` can hang (WebKit bug 317926). */
.demo-popover {
opacity: 0;
translate: 0 -4px;
transition:
opacity 120ms,
translate 120ms,
display 120ms allow-discrete,
overlay 120ms allow-discrete;
}
.demo-popover:popover-open {
opacity: 1;
translate: 0 0;
}
@starting-style {
.demo-popover:popover-open {
opacity: 0;
translate: 0 -4px;
}
}
/* With `transition`, the view transition animates library-driven opens and closes (Firefox
included); dismissals keep the CSS exit above.
The library keeps the two apart; no extra rule is needed. */
::view-transition-new(.pop) {
animation: 160ms ease-out both demo-pop-in;
}
::view-transition-old(.pop) {
animation: 120ms ease-in both demo-pop-out;
}
@keyframes demo-pop-in {
from {
opacity: 0;
scale: 0.96;
}
}
@keyframes demo-pop-out {
to {
opacity: 0;
scale: 0.96;
}
}
@media (prefers-reduced-motion: reduce) {
.demo-popover {
transition: none;
}
::view-transition-group(*),
::view-transition-old(*),
::view-transition-new(*) {
animation: none !important;
}
}
/* Dim the popup while a Close action runs (the Close button carries data-pending). */
.demo-popover:popover-open:has([data-pending]) {
opacity: 0.8;
}
.demo-popover-actions {
display: flex;
justify-content: flex-end;
gap: 8px;
margin-top: 12px;
}
.demo-popover-input {
box-sizing: border-box;
inline-size: 100%;
padding: 0.35em 0.6em;
border: 1px solid var(--color-border);
border-radius: 6px;
background: var(--color-bg);
color: var(--color-text);
font: inherit;
}
/* The Trigger: `data-open` flips at the click in library mode and follows the shown popup in
JS-free mode. A page that never hydrates can use `.demo-button:has(+ :popover-open)`. */
.demo-button[data-open] {
border-color: var(--color-primary);
color: var(--color-primary);
}
.demo-button[data-pending] {
cursor: progress;
}
ul.demo-list {
margin: 0 0 12px;
padding: 0;
list-style: none;
}
ul.demo-list .demo-row {
justify-content: space-between;
max-inline-size: 20rem;
padding-block: 4px;
}
ul.demo-list .demo-row[data-archived] > span {
color: var(--color-text-secondary);
text-decoration: line-through;
}This share popover is a Server Component in JS-free mode. The Trigger and Done are native invokers, so it works before hydration and with JavaScript disabled; the platform's dialog focusing steps put focus on the input, the first focusable element.
import { Popover } from "@funstack/ui/popover";
<Popover.Root> {/* no DOM: ids, mode, state */}
<Popover.Trigger>Filters</Popover.Trigger> {/* button[popovertarget] */}
<Popover.Popup side="bottom" align="start"> {/* dialog[popover=auto][aria-modal=false] */}
<Popover.Title>Filters</Popover.Title> {/* h2, wired to aria-labelledby */}
<Popover.Description>…</Popover.Description> {/* p, wired to aria-describedby */}
<FilterForm />
<Popover.Close>Done</Popover.Close> {/* button[commandfor][command=hide-popover] */}
</Popover.Popup>
</Popover.Root>popovertarget. Render the Popup right after the Trigger where the layout allows: Safari 27.0 does not put a far-away popover next in the Tab order after its trigger. A <button popovertarget> or <button commandfor command> of your own that targets the popover is a foreign invoker (see Other ways to open and close).<dialog popover> itself. There is no Portal, Positioner, Arrow or Backdrop part: the top layer and CSS Anchor Positioning make them unnecessary, and a portalled popover would be inert inside a modal Dialog. A Popover nested inside a Popup is a nested popover; a Dialog opened from a Popup keeps it open.<p>. A <dialog> or <div> cannot sit in running text: the HTML parser closes the paragraph before it, so server-rendered HTML no longer matches React's tree and hydration fails (React also warns in development). A <span> Popup is not an option (see render below). For a Trigger inside a sentence, put the text in a <div> styled as your paragraph, or end the <p> and render the Popup right after it. The default <dialog> moves focus into itself on open, so it need not be adjacent; a <div> Popup away from its Trigger has the Safari 27.0 Tab order gap above.Form part: a popover-shown <dialog> ignores <form method="dialog">. "Submit, then close" is a <form action> plus a controlled open (recipe below), or a Popover.Close action.render: Popup renders a <dialog> (default) or a <div>; Trigger and Close render a <button> for the native invoker path, and another element works only through the library's click handler (library mode, or a Close with action). Wrong elements are development warnings.| JS-free mode | Library mode | |
|---|---|---|
| Selected when | Root has none of open, defaultOpen, openChangeAction, onOpenChange | Root has any of them |
| Open | the browser toggles it (the Trigger's popovertarget) | the click is intercepted and dispatched in the Root's transition; showPopover({ source: trigger }) runs in the commit that mounts the content. Any other open (a foreign invoker, a script) is cancelled at beforetoggle and routed the same way |
| Close by a part (Trigger, Close) | the browser | the library: openChangeAction(false) is awaited, then hidePopover() runs in the commit |
| Close by a dismissal (Escape, click outside, another popover) | the browser | the browser; a fact: onOpenChange(false) is called synchronously, nothing is pending, no view transition |
| Popup content | always mounted (in the server HTML) | rendered from committed open; unmounted while closed unless keepMounted |
Trigger data-open | kept current on the element from the popup's toggle event | optimistic, rendered |
Trigger data-pending | not emitted | emitted |
| Before hydration | everything works | the native open happens; the content arrives on hydration |
transition on Popup | no effect (development warning) | animates its own opens and library-driven closes (a string: this instance only) |
In library mode, opening and a part's close are the library's; a dismissal (Escape, a click outside, another popover opening) is the browser's: a fact, reported to onOpenChange (Requests and facts). The mode is decided on the first render; switching later is not supported (development warning).
<button commandfor command="show-popover">, a <button popovertarget>, a script showPopover()) is cancelled at beforetoggle and dispatched with that ToggleEvent. A foreign close happens natively, so it is a fact: onOpenChange(false) with "activate" and a hide-popover or toggle-popover command's CommandEvent, or a <button popovertarget>'s beforetoggle ToggleEvent, whose source is the button. Escape, a click outside and a script hidePopover() are "dismiss" facts.<button interestfor> of your own naming the Popup, one that does nothing else to it, opens it on interest (hover or keyboard focus) through the library, and in library mode the Popup hides again on lost interest: a "dismiss" fact, onOpenChange(false), whose details.event is the loseinterest event (its source is the button). Lost interest while the open is still pending cancels it (a request, openChangeAction(false, { reason: "dismiss" }), superseding the open). A Trigger-click open stays open on lost interest, as in the platform, and focus is not returned to the Trigger after a lost-interest hide. Do not add interestfor to the Trigger itself: HTML's author guidance rules out one element that shows the same rich content on interest and on activation, so "hover to preview, click to pin" is not a pattern the library recommends. For a hover preview in every engine, use HoverCard; for a name or description (role="tooltip" content), use Tooltip.A popup open inside another one (a Popover, Combobox, Menu or Dialog in a Popover's content) changes state only through its own request or its own dismissal. When the library closes the outer popup (its Close part or Trigger from the keyboard, a controlled open={false}, an item's activation, focus leaving a Menu or Combobox, lost interest), the inner one leaves the screen with it, still open in state: onOpenChange is not called, its Trigger keeps data-open, and it is shown again, on top, the next time the outer popup opens (if it is still mounted: content without keepMounted remounts from its defaultOpen). Dialogs, AlertDialogs and Drawers do the same for what is open inside them.
What the browser does itself is reported as before, for each popup it hides. Escape closes the innermost popup only. A press outside both closes both, the inner one first, each with onOpenChange(false, { reason: "dismiss" }). A press inside the outer popup but outside the inner one, the outer Close part pressed with the pointer included, closes the inner one (the press landed outside it) before the click closes the outer one. A submenu is the exception: it is a part of its menu, and closes with it (see Menu).
The same holds when what hides is not a popup: a keepMounted Tabs panel switched away, an Accordion or Collapsible panel that closes, a <Suspense> fallback shown again over content that was shown, an <Activity> of your own. A popup open inside leaves the screen with it, still open and with no onOpenChange, and comes back with it without moving focus (the commit that brings it back may be an arrow key on a tab, or data arriving while the user types elsewhere); a modal Dialog takes focus, as it must. A popup in JS-free mode inside any of them, or inside a popup the library closes, is closed with it (the DOM is its only state) and is not shown again. Content you hide with your own CSS is not covered: close the popup first.
Popover.Root| Prop | Type | Default | Description |
|---|---|---|---|
open | boolean | – | Controlled open state. undefined means uncontrolled. Selects library mode. Pass onOpenChange too: the browser closes the popover without asking (development warns). |
defaultOpen | boolean | – | Initial open state in uncontrolled library mode. A popover that starts open is shown after hydration (there is no attribute for an open popover in HTML); its content and the Trigger's data-open are already in the server HTML, while the Trigger is exposed collapsed until the popup is shown. defaultOpen={false} is the usual way to select library mode without an action. |
openChangeAction | ChangeAction<boolean, PopoverOpenChangeDetails> | – | Requests only. Awaited for true (the popover opens when it resolves and the content has rendered) and for a false the user asked a part for (Trigger, Close: the popover hides when it resolves). Never called for a dismissal (onOpenChange). Runs in the Root's transition; may be a Server Function; rejections reach the nearest Error Boundary above the Root. details.reason: "activate" (Trigger, Close, a foreign open), "dismiss" (lost interest while an interestfor open is still pending) or "platform" (found open on hydration; event is undefined). Selects library mode. |
onOpenChange | UrgentChangeHandler<boolean, PopoverOpenChangeDetails> | – | Facts only: the popover was closed without the library closing it. Always false. details.reason: "dismiss" (Escape, a click outside, another popover or modal dialog, a parent popover closing, script hidePopover(), lost interest on an interestfor button) or "activate" (a foreign invoker; a non-button Trigger pressed while open). Called synchronously in the browser's toggle event, outside any transition, and not awaited: nothing is pending and no view transition runs. In controlled mode set state directly here (onOpenChange={setOpen}), never in startTransition or after an await; a parent that keeps open gets the popover shown again at once. May return a promise (a Server Function); a rejection or a throw goes to reportError. Selects library mode. On a controlled Root with no openChangeAction, also a part's close (a Close part, the Trigger while open): the library performs it at once and calls this with "activate" (nothing waits). |
children | ReactNode | ((state: PopoverRootState) => ReactNode) | – | The parts. The function form receives the optimistic state and must not suspend. |
PopoverRootState is { open, pending }; open is optimistic, and both are false in JS-free mode.ref, className or style), and no disabled (the Trigger has it).Popover.TriggerA <button popovertarget>: see Triggers of a popup for what that gives it, and for a Trigger rendered as another element. One per Root: it is the popover's anchor and focus-return target, and a press on a second Trigger while open counts as outside.
| Prop | Type | Default | Description |
|---|---|---|---|
disabled | boolean | false | Native disabled: a disabled button is not an invoker and never dispatches. Owned, so a render element cannot re-enable it. In library mode with a link or component render, aria-disabled="true" instead (the element stays focusable). A host link is aria-disabled in every mode (it cannot be natively disabled), and its click is cancelled. Inside a `Toolbar.Root`, in every mode, a disabled Trigger is aria-disabled="true" instead: it stays focusable and in the arrow-key sequence, renders no invoker attribute, and its click is cancelled. A click runs no onClick, a render element's included, and is cancelled; a render element's disabled disables the part too (unavailable parts). |
className | string | ((state: PopoverTriggerState) => string | undefined) | – | A class name, or a function of the part's state. |
style | CSSProperties | ((state: PopoverTriggerState) => CSSProperties | undefined) | – | Inline style, or a function of the part's state. |
children | ReactNode | ((state: PopoverTriggerState) => ReactNode) | – | Rendered from optimistic state; must not suspend. |
render | ReactElement | ((props, state: PopoverTriggerState) => ReactNode) | – | Replace the default <button> (element form) or take over rendering (function form). |
PopoverTriggerState is { open, pending, disabled }; pending is true while the Root is pending: an open or a part's close, never a dismissal (a request still running when a dismissal superseded it keeps pending until that request settles). In JS-free mode state.open is false (React holds no value) while data-open follows the popup, written from its toggle event.type (only on a <button>), disabled, aria-disabled (a host link; in library mode also a component render; inside a Toolbar), popoverTarget (only on an invoker), aria-expanded (only on a Trigger that is no invoker), onClick (library mode), data-open, data-pending, data-disabled. Owned and never written: popoverTargetAction, commandfor, command.A click on a <button> Trigger while open is a part's close; a second click during a pending open cancels it (the latest intent wins). A Trigger rendered as another element (library mode only) is light-dismissed before its click; that close is a fact, onOpenChange(false) with "activate" and the click. aria-haspopup and aria-controls pass through.
Popover.Popup| Prop | Type | Default | Description |
|---|---|---|---|
side | "top" | "bottom" | "left" | "right" | "inline-start" | "inline-end" | null | "bottom" | Placement relative to the Trigger through inline position-area, with position-try-fallbacks: flip-block, flip-inline, flip-block flip-inline flipping it when it does not fit. inline-start / inline-end follow the popup's direction (inline-end is the right in LTR, the left in RTL); the other four are physical. null emits no inline placement and no data-side / data-align: your stylesheet owns placement. |
align | "start" | "center" | "end" | "center" | Alignment along the side. On a top / bottom side start / end are inline-relative (start lines up the inline-start edges: the left in LTR, the right in RTL); on the other sides the top / bottom edges. Ignored when side is null. |
keepMounted | boolean | false | Library mode. false: children are not rendered while closed, so content that suspends holds the open until it is ready. true: children stay in <Activity mode="hidden"> (state kept, effects stopped, pre-rendered at low priority; not in the server HTML). Unmounting waits for the popover's CSS exit animation, or happens in the close commit when transition is set. Ignored in JS-free mode (always mounted). An action of a part inside that rejects while it is hidden goes to reportError, not to an Error Boundary (see Action props). |
initialFocus | RefObject<HTMLElement | null> | string | – | The element to focus on open: a ref, or a selector resolved inside the popup. The library writes autofocus on it right before showPopover() and lets the platform focus it. Default: the platform's choice for a <dialog> (the first focusable element, else the popup); with render={<div />} focus stays on the Trigger. In JS-free mode the attribute is written after mount; prefer it to autoFocus, which React writes only into server-rendered HTML (not when the Popup renders on the client, for example after a client navigation to a Server Component page). |
transition | ViewTransitionClass | – | Library mode only. The class of the <ViewTransition> around the popup element; without it no boundary is rendered. A string animates this popover's own opens and closes that the library dispatched (the Trigger, a Close part) and its own mount and unmount (only as the top of what is inserted or removed), in all engines; nothing else (not another popover, not an unrelated transition, not content that changes inside). A controlled open you change in your own transition needs a map keyed on fsui-popover-open / fsui-popover-close plus addTransitionType (recipe). A type map is React's: it names every popover that has it and that a commit with its types reaches, and each plays the class; end it with default: "none" (Type maps). Dismissals animate with CSS only. Keep it static: changing it remounts the popup. Any value, "none" included, makes a mount of the Popup while it is open, or a second reveal of a mounted Popup by <Suspense> or <Activity>, inside a transition run the page cross-fade; a plain open does not, and neither does the mount of a closed Popup: it renders its element one commit after its mount, before paint, so its ref is still empty in the mount effects of the component that renders it (two-step mount). While a view transition runs, the popup's CSS transitions are cut for the library's own show or hide (a @keyframes animation is not, and a dismissal keeps its CSS exit). |
className | string | ((state: PopoverPopupState) => string | undefined) | – | A class name, or a function of the part's state. |
style | CSSProperties | ((state: PopoverPopupState) => CSSProperties | undefined) | – | Inline style, or a function of the part's state. |
children | ReactNode | ((state: PopoverPopupState) => ReactNode) | – | Content, or a function of the part's state. |
render | ReactElement | ((props, state: PopoverPopupState) => ReactNode) | – | Replace the default <dialog> (element form) or take over rendering (function form). |
PopoverPopupState is { open, side, align }; open is committed (false in JS-free mode; :popover-open is the DOM truth); side / align are where the browser placed the popup while it is shown (a physical side, after any fallback flip, in both modes), your props otherwise.id, popover ("auto"), open (never rendered), aria-modal ("false": Chromium exposes a <dialog popover> as modal otherwise), data-side / data-align (unless side is null); aria-labelledby and aria-describedby name the Title and the Description only while each is mounted (none while the content is not rendered), merged with yours.role="dialog" is a default you can override ("region" or "group" for a non-interactive card). onToggle and onBeforeToggle are not accepted; use a ref for the platform events. Your style wins over the inline placement.
Popover.Close| Prop | Type | Default | Description |
|---|---|---|---|
action | Action<[details: PopoverCloseActionDetails & Rejectable]> | – | Runs in this button's own transition (data-pending on it), then closes the popover: in library mode through the Root (openChangeAction(false) awaited, view transition if set), in JS-free mode with hidePopover(). A rejection reaches the nearest Error Boundary above the button and leaves the popover open. A second click while it is pending is ignored, and the button is aria-disabled="true" meanwhile, unless whilePending is "run". With action the button renders no commandfor / command, so a click before hydration does nothing (rather than hiding the popover without running the action). Without action in JS-free mode the button is a native command="hide-popover" invoker; in library mode it asks the Root and is pending until that close has committed. |
whilePending | "run" | "ignore" | "ignore" | What a click, Enter or Space does while this part's action is pending. "ignore": nothing (the click is cancelled, aria-disabled="true" meanwhile). "run": the action runs again (data-pending until every run has settled, no aria-disabled); the popover closes once the last run has settled, and only if the latest run resolved: a rejection leaves it open in both modes. No effect without action: the part then shows data-pending while the Root's close waits, never aria-disabled. See A second click while pending. |
disabled | boolean | false | Native disabled; owned, so a render element cannot re-enable it. With a link or component render where the Close handles the click (library mode, or with action), aria-disabled="true" instead (the element stays focusable). A host link is aria-disabled in every mode (it cannot be natively disabled), and its click is cancelled. A click runs no onClick, a render element's included, and is cancelled; a render element's disabled disables the part too (unavailable parts). |
className | string | ((state: PopoverCloseState) => string | undefined) | – | A class name, or a function of the part's state. |
style | CSSProperties | ((state: PopoverCloseState) => CSSProperties | undefined) | – | Inline style, or a function of the part's state. |
children | ReactNode | ((state: PopoverCloseState) => ReactNode) | – | Content, or a function of the part's state. |
render | ReactElement | ((props, state: PopoverCloseState) => ReactNode) | – | Replace the default <button> (element form) or take over rendering (function form). |
PopoverCloseState is { pending, disabled }; pending runs from the click until the close this part asked for has committed, with or without action (library mode).type (on a <button> only), disabled, aria-disabled, commandfor, command (not with action, and not on a link or other non-button host element), onClick (library mode, or with action), data-pending, data-disabled; a render element cannot re-enable a disabled Close. Owned and never written: popoverTarget, popoverTargetAction.In library mode the click is always intercepted, so every Close goes through the Root. By default (whilePending="ignore") a second click while the action is pending is ignored (and cancelled), and the button is aria-disabled="true" for as long as it shows data-pending (the action and the hide); never natively disabled, so it keeps focus. To hide at once and finish the work in the background, do not return the promise: action={() => { void apply(); }}. If the user dismisses the popover and opens it again while the action runs, the action's close is dropped: it belonged to the earlier open. Popovers have no return value.
Popover.Title, Popover.Description| Prop | Type | Default | Description |
|---|---|---|---|
className | string | ((state: PopoverTitleState) => string | undefined) | – | A class name, or a function of the part's state. |
style | CSSProperties | ((state: PopoverTitleState) => CSSProperties | undefined) | – | Inline style, or a function of the part's state. |
children | ReactNode | ((state: PopoverTitleState) => ReactNode) | – | Content, or a function of the part's state. |
render | ReactElement | ((props, state: PopoverTitleState) => ReactNode) | – | Replace the default <h2> (Title) or <p> (Description) (element form), or take over rendering (function form). render={<h3 />} changes the heading level. |
PopoverTitleState and PopoverDescriptionState are PopoverPopupState).id.A dialog-role Popup with neither a Title nor an aria-label is a development warning once shown.
| Attribute | On | Meaning |
|---|---|---|
:popover-open | Popup | The popover is shown (DOM truth, both modes). |
data-side, data-align | Popup | While shown: where the browser placed the popup (top / bottom / left / right, after any flip; read from the computed position-area). Otherwise the requested placement. Absent with side={null}. |
aria-expanded | Trigger, only when it is no invoker: a link or other non-button element, or a disabled component render | Whether the popup is shown (library mode). A <button> Trigger has none: the browser exposes the state. |
data-open | Trigger | Library mode: optimistic open state. JS-free mode: whether the popup is shown, written from its toggle event. |
data-pending | Trigger | Library mode: an open or a part's close is in flight. A dismissal never pends, but a request still running when a dismissal superseded it keeps it until that request settles. |
data-pending | Close | From the click until the close it asked for has committed: its action, then the Root's close (library mode, also without action). |
aria-disabled | Close | "true" with action, while data-pending with whilePending="ignore" (the default): a second click is ignored meanwhile. |
data-disabled | Trigger, Close | The disabled prop (both modes). |
| Key | Where | Behaviour | Who |
|---|---|---|---|
| Enter / Space | Trigger | Toggles. Opening moves focus into the popup, as a <dialog> does: to the initialFocus target, else the first focusable element, else the popup itself (see Initial focus below). | the browser (JS-free) or the Trigger |
| Tab / Shift+Tab | Popup | Moves through the popup; the popup follows the Trigger in focus order. Tab past the end leaves, and the popover stays open. | the browser |
| Escape | anywhere | Closes the innermost open popover; focus returns to its Trigger; onOpenChange(false, { reason: "dismiss" }). | the browser (and the library for a nested popover) |
| Escape | a modal Dialog opened from the Popup | Closes the Dialog only; a second Escape closes the popover. | the browser |
<dialog>, opening runs the dialog focusing steps: the initialFocus target, else the first focusable element, else the popup itself (the APG non-modal dialog), in every engine. For a disclosure-style card that keeps focus on the Trigger, render a <div>: render={<div />} with role="region" and an aria-label.role="dialog" and aria-modal="false" explicitly (WebKit implements no minimum role for popovers; Chromium would expose a <dialog popover> as modal). The browser exposes the Trigger's expanded state (Triggers of a popup). No aria-haspopup (the APG disclosure shape); add aria-haspopup="dialog" or aria-controls yourself if you want them.lightDismiss or modal prop: light dismiss and Escape are what popover="auto" is. A panel that must survive stray clicks is a Dialog.Concept: Transitions & ViewTransition
Library mode: fsui-popover plus fsui-popover-open or fsui-popover-close; a controlled change carries none unless you add them, and a dismissal starts no transition at all. JS-free mode starts no transitions. These types are public API: key a transition map on them, and add them with addTransitionType for a controlled change (Transitions). A string transition is keyed on an internal per-instance type instead, which this component's own dispatch adds.
With transition, the Popup renders <ViewTransition> around the popover element itself. A library-driven open shows the popover in the layout effect of its commit and a library-driven close hides it in the layout effect of its commit, so the view transition captures the popup's box as ::view-transition-new on open and ::view-transition-old (with its content) on close. That is the only way a popover's exit can animate in Firefox.
<Popover.Popup transition={{ "fsui-popover-open": "pop-in", "fsui-popover-close": "pop-out", default: "none" }}>A map like this one names every Popup that has it and that the commit reaches whenever any popover opens or closes, and each plays its class. On a page with several, give each transition="pop" instead (it animates that popover alone) and tell the open from the close in CSS with :active-view-transition-type(fsui-popover-open).
::view-transition-new(.pop-in) { animation: 160ms ease-out both pop-in; }
::view-transition-old(.pop-out) { animation: 120ms ease-in both pop-out; }
@media (prefers-reduced-motion: reduce) {
::view-transition-group(*), ::view-transition-old(*), ::view-transition-new(*) { animation: none !important; }
}fsui-popover plus fsui-popover-open or fsui-popover-close. A controlled close carries none.transition: the library turns the popup's CSS transitions off for the moment of its own show and hide while a view transition runs, so you need no extra rule (A popup with a CSS exit and a view transition).transition that mounts inside a transition (a Root inserted by one, a nested Popup mounting with the outer content) starts none either: it renders its element one commit after its mount, before paint (two-step mount). One that is open when it mounts, or a mounted one that <Suspense> or <Activity> reveals again, runs the page cross-fade (the transition prop, issue #35). The page receives no pointer events while a view transition runs: keep them short.Concept: Pending & optimistic state
Library mode: the Trigger shows the optimistic open state (data-open) and data-pending while an open or a part's close is in flight. A dismissal is a fact: the popup, data-open, the committed open and the expanded state assistive technology hears all change at once, and nothing is pending. The Popup's content renders from the committed open. A Close with an action is a pure trigger. Every Close shows data-pending from the click until the close it asked for has committed; without action it is never aria-disabled.
"use client";
import { startTransition, use, useState } from "react";
import { Popover } from "@funstack/ui/popover";
import type { PopoverOpenChangeDetails } from "@funstack/ui/popover";
import { sleep } from "./fake-api.js";
// Cache the promises so `use()` sees the same one on every render.
const cache = new Map<string, Promise<readonly string[]>>();
function loadPermissions(id: string): Promise<readonly string[]> {
let promise = cache.get(id);
if (promise === undefined) {
promise = sleep(1000).then(() => (id === "notes" ? ["Rename"] : ["Rename", "Archive"]));
cache.set(id, promise);
}
return promise;
}
function RowActions({ id, archive }: { id: string; archive: () => Promise<void> }) {
const permissions = use(loadPermissions(id));
return (
<div className="demo-popover-actions">
<Popover.Close className="demo-button" action={() => sleep(400)}>
{({ pending }) => (pending ? "Renaming…" : "Rename")}
</Popover.Close>
{permissions.includes("Archive") && (
<Popover.Close className="demo-button demo-danger" action={archive}>
{({ pending }) => (pending ? "Archiving…" : "Archive")}
</Popover.Close>
)}
</div>
);
}
// Library mode: `defaultOpen={false}` (or any action prop) hands the open to the library. The
// Popup's children render from committed state, so <RowActions> mounts inside the opening
// transition and suspends: the popup stays hidden and the Trigger carries data-pending until the
// permissions are loaded, then it opens once, complete. A Close with an `action` runs it with its
// own pending state, then closes through the Root: the commit hides the popup, and
// `transition="pop"` animates that exit with a view transition, Firefox included. Escape and
// clicks outside are the browser's, facts reported to `onOpenChange` (reason "dismiss"), and use
// the CSS fade.
export function PopoverActions() {
const [archived, setArchived] = useState<readonly string[]>([]);
const [last, setLast] = useState("(none yet)");
const record = (open: boolean, { reason }: PopoverOpenChangeDetails) => {
// The synchronous part of an action: this setState is a transition update.
setLast(`openChangeAction(${open}, "${reason}")`);
};
const recordFact = (open: boolean, { reason }: PopoverOpenChangeDetails) => {
// A fact handler: this setState is urgent.
setLast(`onOpenChange(${open}, "${reason}")`);
};
const rows = [
{ id: "report", name: "Quarterly report" },
{ id: "notes", name: "Offsite notes" },
];
return (
<>
<ul className="demo-list">
{rows.map(({ id, name }) => (
<li key={id} className="demo-row" data-archived={archived.includes(id) ? "" : undefined}>
<span>{name}</span>
<Popover.Root defaultOpen={false} openChangeAction={record} onOpenChange={recordFact}>
<Popover.Trigger className="demo-button" aria-label={`Actions for ${name}`}>
{({ pending }) => (pending ? <span className="demo-spinner" aria-hidden /> : "⋯")}
</Popover.Trigger>
<Popover.Popup className="demo-popover" align="end" transition="pop">
<Popover.Title>{name}</Popover.Title>
<RowActions
id={id}
archive={async () => {
await sleep(800);
// After an await: wrap the update in startTransition (React limitation).
startTransition(() => setArchived((ids) => [...ids, id]));
}}
/>
</Popover.Popup>
</Popover.Root>
</li>
))}
</ul>
<div className="demo-row">
<button type="button" className="demo-button" onClick={() => cache.clear()}>
Clear the cache
</button>
</div>
<output className="demo-readout">
last call: <code>{last}</code>
</output>
</>
);
}/* Popover: the Popup is the <dialog popover> itself. Open state is `:popover-open` on it (exact
in both modes); the Trigger carries `data-open` in both modes and `data-pending` in library
mode only. A <button> Trigger has no `aria-expanded` (the browser exposes its state). */
.demo-popover {
/* The popover UA stylesheet sets `inset: 0; margin: auto`: reset both for anchored placement. */
margin: 0;
inset: auto;
min-inline-size: 14rem;
padding: 16px;
border: 1px solid var(--color-border);
border-radius: 10px;
background: var(--color-bg);
color: var(--color-text);
box-shadow: 0 12px 40px rgb(0 0 0 / 0.25);
}
/* The gap. `data-side` is where the browser placed the popup (absent with `side={null}`); a
flip swaps the popup's margins, so the gap goes on both sides of the axis. */
.demo-popover[data-side="bottom"],
.demo-popover[data-side="top"] {
margin-block: 6px;
}
.demo-popover h2 {
margin: 0 0 8px;
padding: 0;
border: 0;
font-size: 1rem;
}
/* Enter and exit with CSS, both modes. Enter works everywhere; the exit animates in Chromium
(with `overlay`) and Safari and is instant in Firefox. On iOS, `position-try-fallbacks`
together with `@starting-style` can hang (WebKit bug 317926). */
.demo-popover {
opacity: 0;
translate: 0 -4px;
transition:
opacity 120ms,
translate 120ms,
display 120ms allow-discrete,
overlay 120ms allow-discrete;
}
.demo-popover:popover-open {
opacity: 1;
translate: 0 0;
}
@starting-style {
.demo-popover:popover-open {
opacity: 0;
translate: 0 -4px;
}
}
/* With `transition`, the view transition animates library-driven opens and closes (Firefox
included); dismissals keep the CSS exit above.
The library keeps the two apart; no extra rule is needed. */
::view-transition-new(.pop) {
animation: 160ms ease-out both demo-pop-in;
}
::view-transition-old(.pop) {
animation: 120ms ease-in both demo-pop-out;
}
@keyframes demo-pop-in {
from {
opacity: 0;
scale: 0.96;
}
}
@keyframes demo-pop-out {
to {
opacity: 0;
scale: 0.96;
}
}
@media (prefers-reduced-motion: reduce) {
.demo-popover {
transition: none;
}
::view-transition-group(*),
::view-transition-old(*),
::view-transition-new(*) {
animation: none !important;
}
}
/* Dim the popup while a Close action runs (the Close button carries data-pending). */
.demo-popover:popover-open:has([data-pending]) {
opacity: 0.8;
}
.demo-popover-actions {
display: flex;
justify-content: flex-end;
gap: 8px;
margin-top: 12px;
}
.demo-popover-input {
box-sizing: border-box;
inline-size: 100%;
padding: 0.35em 0.6em;
border: 1px solid var(--color-border);
border-radius: 6px;
background: var(--color-bg);
color: var(--color-text);
font: inherit;
}
/* The Trigger: `data-open` flips at the click in library mode and follows the shown popup in
JS-free mode. A page that never hydrates can use `.demo-button:has(+ :popover-open)`. */
.demo-button[data-open] {
border-color: var(--color-primary);
color: var(--color-primary);
}
.demo-button[data-pending] {
cursor: progress;
}
ul.demo-list {
margin: 0 0 12px;
padding: 0;
list-style: none;
}
ul.demo-list .demo-row {
justify-content: space-between;
max-inline-size: 20rem;
padding-block: 4px;
}
ul.demo-list .demo-row[data-archived] > span {
color: var(--color-text-secondary);
text-decoration: line-through;
}data-open and data-pending (it is exposed collapsed until the popup is shown), and openChangeAction(true, { reason: "activate" }) runs.open renders the Popup's children; RowActions suspends, so React holds the commit. The popup stays hidden; the Trigger keeps spinning.showPopover({ source: trigger }) runs in the same commit, so the popup is anchored below the Trigger and the platform focuses fully rendered content. With transition="pop" it animates in.openChangeAction(false, { reason: "activate" }) and the commit that ends it hides the popup inside a view transition: the exit animates in every engine.onOpenChange(false, { reason: "dismiss" }) in the same event. Nothing is pending, and openChangeAction is not called.openChangeActionHere every openChangeAction call takes 2 seconds, like a server call, and onOpenChange logs the dismissals. The Trigger is exposed as expanded exactly while the popup is shown:
Last: (none yet)
"use client";
import { startTransition, useState } from "react";
import { Popover } from "@funstack/ui/popover";
import type { PopoverOpenChangeDetails } from "@funstack/ui/popover";
import { sleep } from "./fake-api.js";
// A slow `openChangeAction`: a pretend server call of 2 seconds for every request (an open, or a
// close with the Trigger or the Close). Opening: the Trigger gets data-open and data-pending at
// once, the popup is shown when the action settles. Closing with the Trigger or the Close: the
// popup stays shown until the action settles. Escape and a click outside are the browser's, a
// fact: the popup hides at once, nothing is pending and the action is not called; `onOpenChange`
// reports it, synchronously. The browser exposes the Trigger as expanded exactly while the popup
// is shown. The readout is plain text, not a live region.
export function PopoverSlowOpen() {
const [log, setLog] = useState("(none yet)");
const slow = async (open: boolean, { reason }: PopoverOpenChangeDetails) => {
const call = `openChangeAction(${open}, "${reason}")`;
setLog(`${call}: running…`);
await sleep(2000);
// After an await: wrap the update in startTransition (React limitation).
startTransition(() => setLog(`${call}: settled`));
};
// A fact: called in the browser's event, not awaited. A plain setState here is urgent.
const dismissed = (open: boolean, { reason }: PopoverOpenChangeDetails) => {
setLog(`onOpenChange(${open}, "${reason}")`);
};
return (
<div className="demo-stack">
<Popover.Root openChangeAction={slow} onOpenChange={dismissed}>
<Popover.Trigger className="demo-button">
{({ open, pending }) => (
<>
Filters{pending && <span className="demo-spinner" aria-hidden />}
<span aria-hidden>{open ? " ▴" : " ▾"}</span>
</>
)}
</Popover.Trigger>
<Popover.Popup className="demo-popover">
<Popover.Title>Filters</Popover.Title>
<p>Saved filters for this list.</p>
<Popover.Close className="demo-button">Close</Popover.Close>
</Popover.Popup>
</Popover.Root>
<p className="demo-readout">
Last: <code>{log}</code>
</p>
</div>
);
}/* Popover: the Popup is the <dialog popover> itself. Open state is `:popover-open` on it (exact
in both modes); the Trigger carries `data-open` in both modes and `data-pending` in library
mode only. A <button> Trigger has no `aria-expanded` (the browser exposes its state). */
.demo-popover {
/* The popover UA stylesheet sets `inset: 0; margin: auto`: reset both for anchored placement. */
margin: 0;
inset: auto;
min-inline-size: 14rem;
padding: 16px;
border: 1px solid var(--color-border);
border-radius: 10px;
background: var(--color-bg);
color: var(--color-text);
box-shadow: 0 12px 40px rgb(0 0 0 / 0.25);
}
/* The gap. `data-side` is where the browser placed the popup (absent with `side={null}`); a
flip swaps the popup's margins, so the gap goes on both sides of the axis. */
.demo-popover[data-side="bottom"],
.demo-popover[data-side="top"] {
margin-block: 6px;
}
.demo-popover h2 {
margin: 0 0 8px;
padding: 0;
border: 0;
font-size: 1rem;
}
/* Enter and exit with CSS, both modes. Enter works everywhere; the exit animates in Chromium
(with `overlay`) and Safari and is instant in Firefox. On iOS, `position-try-fallbacks`
together with `@starting-style` can hang (WebKit bug 317926). */
.demo-popover {
opacity: 0;
translate: 0 -4px;
transition:
opacity 120ms,
translate 120ms,
display 120ms allow-discrete,
overlay 120ms allow-discrete;
}
.demo-popover:popover-open {
opacity: 1;
translate: 0 0;
}
@starting-style {
.demo-popover:popover-open {
opacity: 0;
translate: 0 -4px;
}
}
/* With `transition`, the view transition animates library-driven opens and closes (Firefox
included); dismissals keep the CSS exit above.
The library keeps the two apart; no extra rule is needed. */
::view-transition-new(.pop) {
animation: 160ms ease-out both demo-pop-in;
}
::view-transition-old(.pop) {
animation: 120ms ease-in both demo-pop-out;
}
@keyframes demo-pop-in {
from {
opacity: 0;
scale: 0.96;
}
}
@keyframes demo-pop-out {
to {
opacity: 0;
scale: 0.96;
}
}
@media (prefers-reduced-motion: reduce) {
.demo-popover {
transition: none;
}
::view-transition-group(*),
::view-transition-old(*),
::view-transition-new(*) {
animation: none !important;
}
}
/* Dim the popup while a Close action runs (the Close button carries data-pending). */
.demo-popover:popover-open:has([data-pending]) {
opacity: 0.8;
}
.demo-popover-actions {
display: flex;
justify-content: flex-end;
gap: 8px;
margin-top: 12px;
}
.demo-popover-input {
box-sizing: border-box;
inline-size: 100%;
padding: 0.35em 0.6em;
border: 1px solid var(--color-border);
border-radius: 6px;
background: var(--color-bg);
color: var(--color-text);
font: inherit;
}
/* The Trigger: `data-open` flips at the click in library mode and follows the shown popup in
JS-free mode. A page that never hydrates can use `.demo-button:has(+ :popover-open)`. */
.demo-button[data-open] {
border-color: var(--color-primary);
color: var(--color-primary);
}
.demo-button[data-pending] {
cursor: progress;
}
ul.demo-list {
margin: 0 0 12px;
padding: 0;
list-style: none;
}
ul.demo-list .demo-row {
justify-content: space-between;
max-inline-size: 20rem;
padding-block: 4px;
}
ul.demo-list .demo-row[data-archived] > span {
color: var(--color-text-secondary);
text-decoration: line-through;
}data-open and data-pending at once (here a spinner and ▴) and stays collapsed; the popup is shown, and the Trigger expanded, when the action settles.data-open goes at once, the popup stays shown and the Trigger expanded until the action settles.data-open goes and the Trigger is collapsed at once; nothing is pending and the slow action is not called; onOpenChange is.The readout under the example is plain text, not a live region, so it does not interrupt a screen reader.
Concept: Suspense
Content that suspends holds the open: the popup stays hidden with the Trigger pending, then shows once, complete. A <Suspense> inside the Popup opens at once with a skeleton instead.
Concept: Browser-owned state
JS-free mode: the browser owns everything through the Trigger's popovertarget and the Close's Invoker Command. Library mode: opening and a close asked for by a part are requests (openChangeAction); a dismissal (Escape, a click outside, another popover opening) is the browser's, a fact: the popover closes at once and onOpenChange(false) is called with reason: "dismiss". Dismissals animate with CSS only.
Concept: Server Components
JS-free mode: everything, including autoFocus in server-rendered markup, except a Popover.Close with action, which does nothing until hydration. Library mode: the Trigger opens the popover natively, but the content is not in the HTML yet; on hydration the Popup finds it open, dispatches openChangeAction(true) with reason: "platform" and the content mounts.
autoFocus in server-rendered markup. After hydration, and for content React renders on the client, use initialFocus: React does not write autofocus on client-rendered elements.popovertarget, so a click opens the popover natively, but the content is not in the HTML yet (with defaultOpen it is, and the Trigger already has data-open; only the showing waits for hydration). On hydration the Popup finds it open and dispatches openChangeAction(true, { reason: "platform" }) with no event; the content mounts into the open popover. If the action rejects or a controlled parent refuses, the popover closes again.The Trigger's styling hook is data-open in both modes (optimistic in library mode, written from the popup's toggle event in JS-free mode); a <button> Trigger has no aria-expanded to select on (Triggers of a popup). On a page that never hydrates, style the Trigger from the popup that follows it:
.trigger[data-open] { color: var(--accent); }
/* Without JavaScript: the Popup right after the Trigger. */
.trigger:has(+ :popover-open) { color: var(--accent); }The Popup emits no data-open: :popover-open is exact. Enter and exit animate in CSS, in both modes:
.popover {
margin: 0;
inset: auto; /* the popover UA stylesheet sets inset: 0 */
opacity: 0;
translate: 0 -4px;
transition: opacity 120ms, translate 120ms, display 120ms allow-discrete, overlay 120ms allow-discrete;
}
.popover:popover-open { opacity: 1; translate: 0 0; }
@starting-style { .popover:popover-open { opacity: 0; translate: 0 -4px; } }
/* The gap, static on the requested edge (side="bottom"): a flip swaps the popup's margins, so
it moves to the other edge by itself. */
.popover { margin-block-start: 6px; }
/* A class that serves any side: both sides of the axis, keyed on data-side, the closed logical
values included so the first layout has the gap. A one-sided margin keyed on the resolved
data-side is the wrong form: after a flip it lands on the far edge. */
.popover-any[data-side="top"], .popover-any[data-side="bottom"] { margin-block: 6px; }
.popover-any[data-side="left"], .popover-any[data-side="right"],
.popover-any[data-side="inline-start"], .popover-any[data-side="inline-end"] { margin-inline: 6px; }
@media (prefers-reduced-motion: reduce) { .popover { transition: none; } }data-side is where the popup is, read from the computed position-area while it is shown; the Tooltip page explains the mechanism, its render cost and the margin and animation caveats, which apply here as they are. Popover has no Arrow part; an arrow drawn with a pseudo-element on [data-side] follows a flip (see the Tooltip arrow).
const [open, setOpen] = useState(false);
<Popover.Root open={open} openChangeAction={setOpen} onOpenChange={setOpen}>
<Popover.Trigger>Rename</Popover.Trigger>
<Popover.Popup initialFocus="[name=title]">
<Popover.Title>Rename</Popover.Title>
<form
action={async (formData) => {
await rename(formData);
startTransition(() => setOpen(false)); // a library-driven close: view transition if set
}}
>
<input name="title" defaultValue={title} />
<SubmitButton>Save</SubmitButton>
<Popover.Close>Cancel</Popover.Close>
</form>
</Popover.Popup>
</Popover.Root>// The handler alone: Popover.Close, Escape and a click outside all close at once.
<Popover.Root open={open} onOpenChange={setOpen}>
<Popover.Popup side={null} className="popover centered">…</Popover.Popup>
</Popover.Root>Without a Trigger there is no anchor: the popover is centred by the UA stylesheet unless you position it, so use side={null}. Open it with a plain setOpen(true) from your own event handler, not from another part's action, which would wait for every action pending in the app (Open your own controlled overlay urgently).
With no openChangeAction there is nobody to ask, so Popover.Close closes the popover in the click and tells onOpenChange(false, { reason: "activate" }), also while a save is pending elsewhere. What this shape costs: no veto (a parent that keeps open at true sees it close and open again), the close animates with your CSS exit rather than the Popup's transition, and a Popover.Trigger does nothing there (a development warning says so). With openChangeAction={setOpen} added, Popover.Close is a request: it runs in the Root's transition and waits for a save pending elsewhere (about 1.4 s with a 1.5 s save), while Escape and a click outside still close at once, and so does an urgent setOpen(false) of your own, even while that request is held.
<Popover.Root defaultOpen={false}>
<Popover.Trigger>Share</Popover.Trigger>
<Popover.Popup>
<Popover.Title>Share</Popover.Title>
<Popover.Root defaultOpen={false}> {/* nested auto popover */}
<Popover.Trigger>Permissions</Popover.Trigger>
<Popover.Popup side="right" align="start">…</Popover.Popup>
</Popover.Root>
</Popover.Popup>
</Popover.Root>Opening the inner keeps the outer open; Escape closes the inner and focus returns to "Permissions"; a click outside closes both. Outer parts must sit outside the inner Root. An inner Popup with transition that is closed when the outer content mounts costs the outer open nothing (two-step mount); one that is open then pays the page cross-fade. In Safari, a nested popover with position-try-fallbacks can hang (WebKit bug 302008): give the inner Popup side={null} and place it in CSS if you hit it.
side="inline-start" / "inline-end" and align on a top or bottom side use the self-* keywords, which follow the popup's inherited direction: an RTL page and an RTL container both mirror them, with no JavaScript. Firefox misplaces a popover inside a direction island (#89; see Tooltip).position-try-fallbacks together with @starting-style can hang (WebKit bug 317926).