Getting started

Actions

Forms

Layout

Navigation

Overlays

Feedback

Data display

Controllers

Helpers

Recipes

PopoverController

Portal mount, floating position, dismissal and teardown of a popup.

Reach for it for a popup, hover card or menu panel beside an anchor.

Import

import { PopoverController, createPopoverController } from '@ecopages/radiant-ui/controllers';

Try it

The button toggles an open flag and calls sync(), which shows the panel beside the button. A press outside, or focus leaving, calls onDismiss. The panel is portaled into the example frame with getPortalContainer; without it the panel goes to the nearest modal surface that holds the button, else document.body, and portal: false keeps it where it is.

<div data-example="popover">
	<button type="button">Toggle</button>
	<div data-box hidden>Popover content</div>
</div>
import { PopoverController } from '@ecopages/radiant-ui/controllers';

export function mount(root: HTMLElement): () => void {
	const trigger = root.querySelector('button')!;
	const panel = root.querySelector<HTMLElement>('[data-box]')!;

	let open = false;
	const popover = new PopoverController({
		getAnchor: () => trigger,
		getFloating: () => panel,
		getOpen: () => open,
		getPlacement: () => 'bottom-start',
		getPortalContainer: () => root,
		onDismiss: () => {
			open = false;
			popover.sync();
		},
	});
	popover.attach();

	trigger.addEventListener('click', () => {
		open = !open;
		popover.sync();
	});
	return () => popover.destroy();
}

mount runs once for each example element on the page, and the function it returns undoes it; see how the examples run.

Config

OptionDescription
getAnchorThe element the popup sits beside.
getFloatingThe popup surface where it lives in the owner. While it is portaled out, getFloatingElement() falls back to the portal's mounted node.
getOpenWhether the popup is open.
getPlacementA RuiPlacement, such as bottom-start. Read when the popup is positioned.
gapPixels between anchor and popup. Default 8.
portalMount the popup in its container while open: getPortalContainer, else the nearest modal surface holding the anchor, else document.body. Default true; false leaves it in place.
matchAnchorWidthGive the popup the anchor width.
getPortalContainerWhere a portaled popup goes instead of document.body, read each time the popup opens. When it is omitted or returns null: the nearest [aria-modal="true"] or open modal <dialog> ancestor of the anchor, else document.body.
onDismissCalled when the user dismisses the open popup. Dismissal listens only when this is set. Read on each attach(); updateConfig does not change it.
dismissOnWhich dismissals onDismiss hears about. Defaults to all. Read once, when the controller is created.

Lifecycle

attach() enables sync() and, when onDismiss is set, dismissal. sync() does nothing before attach() or after destroy(); call it whenever open changes. destroy() removes the floating listeners, ends dismissal, returns a portaled popup to its place. teardown() closes the popup the same way but stays attached, so the next sync() mounts it again. Also updateConfig(patch), contains(node), isOutside(target) and getFloatingElement(), which returns getFloating(), else the portal's mounted node (so through the exit transition), else null. Dismissal counts a portaled popup that came from inside the surface (a portaled select or calendar inside the popover) as inside, so working in it does not close the popover.

With a host, createPopoverController(host, config) does the same on connect and disconnect; see bindController.

Accessibility: what the controller does and what you must provide

The controller positions and dismisses; it sets no aria-expanded, aria-controls or aria-haspopup on the anchor and manages no focus. Set them yourself, and give the popup a role (dialog, menu, listbox) with a name. For a popup that holds interactive content, move focus into it on open and back to the anchor on close.

With the default portal: true the popup sits at the end of document.body, or inside the nearest [aria-modal="true"] or modal <dialog> surface when the anchor is inside one (a body-level popup would stack below a modal), so Tab from the anchor skips it. Use portal: false or getPortalContainer to place it next to the anchor in DOM order when keyboard users must reach it, or move focus into it when it opens. Dismissal follows DismissController: Escape closes only the top-most layer, and a window blur does not close the popup.

Protected hooks

Extend the class and override these to change how it decides. A component never extends a controller; a subclass is registered with bindController.

HookDescription
position(anchor, floating)Decides where the open popup sits. Runs from sync() each time the popover is open. Return a cleanup function; it runs before the next position(), when the popover closes and on destroy(). The default is attachFloating with the configured placement, gap and matchAnchorWidth.