usePopover @astryx-svelte/core v0.3.0 · Popover

Usage

Headless hook for click-triggered popovers with focus trapping. Combines useLayer with useFocusTrap, auto-focus, light dismiss, Escape handling, and an optional hidden close button for accessible dialog-like popover behavior. Use for custom interactive floating content that needs keyboard navigation.
ts
import { Popover } from '@astryx-svelte/core';

Best practices

GuidancePractices
DoUse for interactive content such as menus, pickers, forms, and command panels that need focus management.
DoPrefer the Popover component for standard trigger-content pairs; use the hook for custom trigger patterns.
Don'tUse for non-interactive hover previews: use useHoverCard or useTooltip instead.

Parameters

ParameterTypeDefaultDescription
onShow () => voidCallback fired when the popover becomes visible.
onHide () => voidCallback fired when the popover is hidden. Use this to return focus to the trigger when needed.
xstyle StyleArgStyleX styles applied to the popover content wrapper, after the default surface styles.
hasLightDismiss booleantrueWhether clicking outside dismisses the popover.
hasEscapeDismiss booleantrueWhether pressing Escape dismisses the popover. Only takes full effect together with hasLightDismiss: false, since native light dismiss also closes on Escape.
hasAutoFocus booleantrueWhether to automatically focus the first focusable element when opened.
hasCloseButton booleantrueWhether to include a hidden close button that appears for keyboard users.
closeButtonLabel string'Close popover'Accessible label for the hidden close button.
dialogLabel stringAccessible label for the popover dialog (only applies when role is "dialog"). Provide one when there is no visible title.
role 'none' | 'dialog''dialog'ARIA role on the content wrapper. Use "dialog" for genuine dialog content; use "none" for listbox/menu popups whose own content role should be exposed and whose trigger keeps DOM focus.
isModal booleantrueWhether a dialog-role popover is modal (aria-modal). Only applies when role is "dialog".
hasSurface booleantrueWhether to apply the default popover surface background, radius, and shadow.

Returns

FieldTypeDefaultDescription
triggerRef (el: HTMLElement | null) => voidRef callback to attach to the trigger element for CSS anchor positioning.

Element references are attachments here. The port returns an Attachment<HTMLElement> named attach…; spread it onto the element instead of assigning a ref.

contentRef HTMLDivElement | nullRef for the popover content container used by focus trapping.

Element references are attachments here. The port returns an Attachment<HTMLElement> named attach…; spread it onto the element instead of assigning a ref.Upstream declares RefObject<HTMLDivElement | null>.

anchorId stringCSS anchor name for advanced positioning cases.
show (options?: { skipAutoFocus?: boolean; }) => voidImperatively show the popover. skipAutoFocus preserves current focus for input-triggered popovers.
hide () => voidImperatively hide the popover.
toggle () => voidToggle the popover open or closed.
isOpen booleanWhether the popover is currently open.
id stringUnique ID for aria-describedby or aria-controls.
render (children: string | Snippet, props?: ContextRenderProps) => string | SnippetRender function for anchor-positioned popover content. Pass placement and alignment here. Logical: start/end resolve against the popover's own inherited direction (RTL mirrors in pure CSS).

A Svelte hook cannot return markup. This hook returns state only; render its companion component and pass the hook’s value to it.Upstream declares (children: ReactNode, props?: ContextRenderProps) => ReactNode.

triggerProps { 'aria-haspopup': 'dialog' | 'true'; 'aria-expanded': boolean; 'aria-controls': string; }ARIA attributes to spread onto the trigger element. aria-haspopup reflects the popover role.

Examples

Common configurations, variations, and states.
usePopover — Quick Actions
Custom quick-actions popover using usePopover for trigger refs, ARIA attributes, and focus trapping.