Components
Popover
An accessible popup anchored to a button.
Anatomy#
Import the component and assemble its parts:
import { Popover } from '@egoist/solid-base/popover';
<Popover.Root>
<Popover.Trigger />
<Popover.Portal>
<Popover.Backdrop />
<Popover.Positioner>
<Popover.Popup>
<Popover.Arrow />
<Popover.Viewport>
<Popover.Title />
<Popover.Description />
<Popover.Close />
</Popover.Viewport>
</Popover.Popup>
</Popover.Positioner>
</Popover.Portal>
</Popover.Root>;Examples#
Opening on hover#
To open the popover on hover, set the openOnHover prop on the trigger.
Use the delay prop to specify how long to wait (in milliseconds) before the popover opens on
hover, and closeDelay for how long to wait before it closes.
Detached triggers#
A popover can be controlled by a trigger located either inside or outside the <Popover.Root>
component. For simple, one-off interactions, place the <Popover.Trigger> inside
<Popover.Root>, as shown in the example at the top of this page.
However, if defining the popover's content next to its trigger is not practical, you can use a
detached trigger. This involves placing the <Popover.Trigger> outside of <Popover.Root> and
linking them with a handle created by the Popover.createHandle() function.
The imperative methods on the handle, such as open() and close(), require a <Popover.Root>
using the same handle to be mounted. Calls made while no root is attached to the handle — before
one mounts, or after it unmounts — are ignored. Each time a root mounts, it starts from fresh
state: a call made while no root was attached is not replayed, and no open state carries over
from a previous mount.
const demoPopover = Popover.createHandle();
<Popover.Trigger handle={demoPopover}>Trigger</Popover.Trigger>
<Popover.Root handle={demoPopover}>
...
</Popover.Root>Multiple triggers#
A single popover can be opened by multiple trigger elements. You can achieve this by using the
same handle for several detached triggers, or by placing multiple <Popover.Trigger>
components inside a single <Popover.Root>.
<Popover.Root>
<Popover.Trigger>Trigger 1</Popover.Trigger>
<Popover.Trigger>Trigger 2</Popover.Trigger>
...
</Popover.Root>const demoPopover = Popover.createHandle();
<Popover.Trigger handle={demoPopover}>Trigger 1</Popover.Trigger>
<Popover.Trigger handle={demoPopover}>Trigger 2</Popover.Trigger>
<Popover.Root handle={demoPopover}>
...
</Popover.Root>The popover can render different content depending on which trigger opened it. This is achieved
by passing a payload to the <Popover.Trigger> and using a function as the child of
<Popover.Root>.
The payload can be strongly typed by providing a type argument to the createHandle() function:
const demoPopover = Popover.createHandle<{ text: string }>();
<Popover.Trigger handle={demoPopover} payload={{ text: 'Trigger 1' }}>
Trigger 1
</Popover.Trigger>
<Popover.Trigger handle={demoPopover} payload={{ text: 'Trigger 2' }}>
Trigger 2
</Popover.Trigger>
<Popover.Root handle={demoPopover}>
{(state) => (
<Popover.Portal>
<Popover.Positioner sideOffset={8}>
<Popover.Popup class={styles.Popup}>
<Popover.Arrow class={styles.Arrow} />
<Popover.Title class={styles.Title}>Popover</Popover.Title>
<Show when={state.payload}>
{(payload) => (
<Popover.Description class={styles.Description}>
This has been opened by {payload().text}
</Popover.Description>
)}
</Show>
<Popover.Close aria-label="Close" class={styles.Close}>
<XIcon />
</Popover.Close>
</Popover.Popup>
</Popover.Positioner>
</Popover.Portal>
)}
</Popover.Root>Controlled mode with multiple triggers#
You can control the popover's open state externally using the open and onOpenChange props on
<Popover.Root>. This allows you to manage the popover's visibility based on your application's
state. When using multiple triggers, you have to manage which trigger is active with the
triggerId prop on <Popover.Root> and the id prop on each <Popover.Trigger>.
Note that there is no separate onTriggerIdChange prop. Instead, the onOpenChange callback
receives an additional argument, eventDetails, which contains the trigger element that initiated
the state change.
Animating the Popover#
You can animate a popover as it moves between different trigger elements. This includes animating its position, size, and content.
Position and size
To animate the popover's position, apply CSS transitions to the left, right, top, and
bottom properties of the Positioner part. To animate its size, transition the width and
height of the Popup part.
Content
The popover also supports content transitions. This is useful when different triggers display different content within the same popover.
To enable content animations, wrap the content in the <Popover.Viewport> part. This part
provides features to create direction-aware animations. It renders a div with a
data-activation-direction attribute that indicates the new trigger's position relative to the
previous one. The value is a space-separated set of up to two tokens (one per axis) — left or
right for the horizontal axis and up or down for the vertical axis (for example,
right down). Match a single token with the ~= attribute selector, such as
[data-activation-direction~='right'].
Inside the <Popover.Viewport>, the content is further wrapped in divs with data attributes to
help with styling:
data-current: The currently visible content when no transitions are present or the incoming content.data-previous: The outgoing content during a transition.
You can use these attributes to style the enter and exit animations.
API reference#
<Popover.Root>
Groups all parts of the popover. Doesn't render its own HTML element.
defaultOpenbooleanfalseWhether the popover is initially open.
To render a controlled popover, use the open prop instead.
openboolean—Whether the popover is currently open.
onOpenChange| ((open: boolean, eventDetails: PopoverRoot.ChangeEventDetails) => void)—Event handler called when the popover is opened or closed.
onOpenChangeComplete((open: boolean) => void)—Event handler called after any animations complete when the popover is opened or closed.
actionsRefActionsRef<PopoverRoot.Actions>—A ref to imperative actions: a ref object ({ current: null }) or a callback receiving them (and null on unmount). - unmount: Ends the closing phase of the popover after an externally controlled closing animation finishes. Call preventUnmountOnClose() in onOpenChange first, otherwise the popover completes closing on its own. Whether it leaves the DOM is decided by keepMounted on the portal. - close: Closes the popover imperatively when called.
modalboolean | 'trap-focus'falseDetermines if the popover enters a modal state when open. - true: user interaction is limited to the popover: document page scroll is locked, and pointer interactions on outside elements are disabled. - false: user interaction with the rest of the document is allowed. - 'trap-focus': focus is trapped inside the popover, but document page scroll is not locked and pointer interactions outside of it remain enabled.
On touch devices, a true modal blocks outside taps but leaves the page scrollable unless the popup spans nearly the full viewport width, matching native iOS behavior.
When modal is true, focus trapping is enabled only if <Popover.Close> is rendered inside <Popover.Popup>. It can be visually hidden with your own CSS if needed, such as Tailwind's sr-only utility.
When modal is 'trap-focus', render <Popover.Close> inside <Popover.Popup> so touch screen readers can escape the popup.
triggerIdstring | null—ID of the trigger that the popover is associated with. This is useful in conjunction with the open prop to create a controlled popover. There's no need to specify this prop when the popover is uncontrolled (that is, when the open prop is not set).
defaultTriggerIdstring | null—ID of the trigger that the popover is associated with. This is useful in conjunction with the defaultOpen prop to create an initially open popover.
handlePopoverHandle<Payload>—A handle to associate the popover with a trigger. If specified, allows external triggers to control the popover's open state.
<Popover.Trigger>
A button that opens the popover. Renders a <button> element.
disabledbooleanfalseWhether the component ignores user interaction.
handlePopoverHandle<Payload>—A handle to associate the trigger with a popover.
payloadPayload—A payload to pass to the popover when it is opened.
idstring—ID of the trigger. In addition to being forwarded to the rendered element, it is also used to specify the active trigger for the popover in controlled mode (with the PopoverRoot triggerId prop).
openOnHoverbooleanfalseWhether the popover should also open when the trigger is hovered.
delaynumber300How long to wait before the popover may be opened on hover. Specified in milliseconds.
Requires the openOnHover prop.
closeDelaynumber0How long to wait before closing the popover that was opened on hover. Specified in milliseconds.
Requires the openOnHover prop.
nativeButtonbooleantrueWhether the component renders a native <button> element when replacing it via the render prop. Set to false if the rendered element is not a button (for example, <div>).
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
data-disabledPresent when the trigger is disabled.
<Popover.Portal>
A portal element that moves the popup to a different part of the DOM. By default, the portal element is appended to <body>. Renders a <div> element.
keepMountedbooleanfalseWhether to keep the portal mounted in the DOM while the popup is hidden.
containerPortalContainer—A parent element to render the portal element into.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<Popover.Positioner>
Positions the popover against the trigger. Renders a <div> element.
anchor| Element | null | VirtualElement | RefObject<Element | null> | (() => Element | VirtualElement | null)—An element to position the popup against. By default, the popup will be positioned against the trigger.
Accepts an element, a virtual element, a ref object, or a function (such as a signal accessor) that returns an element or a virtual element.
positionMethod'absolute' | 'fixed''absolute'Determines which CSS position property to use.
sideSide'bottom'Which side of the anchor element to align the popup against. May automatically change to avoid collisions.
sideOffsetnumber | OffsetFunction0Distance between the anchor and the popup in pixels. Also accepts a function that returns the distance to read the dimensions of the anchor and positioner elements, along with its side and alignment.
The function takes a data object parameter with the following properties: - data.anchor: the dimensions of the anchor element with properties width and height. - data.positioner: the dimensions of the positioner element with properties width and height. - data.side: which side of the anchor element the positioner is aligned against. - data.align: how the positioner is aligned relative to the specified side.
alignAlign'center'How to align the popup relative to the specified side.
alignOffsetnumber | OffsetFunction0Additional offset along the alignment axis in pixels. Also accepts a function that returns the offset to read the dimensions of the anchor and positioner elements, along with its side and alignment.
The function takes a data object parameter with the following properties: - data.anchor: the dimensions of the anchor element with properties width and height. - data.positioner: the dimensions of the positioner element with properties width and height. - data.side: which side of the anchor element the positioner is aligned against. - data.align: how the positioner is aligned relative to the specified side.
collisionBoundaryBoundary'clipping-ancestors'An element or a rectangle that delimits the area that the popup is confined to.
collisionPaddingPadding5Additional space to maintain from the edge of the collision boundary.
stickybooleanfalseWhether to maintain the popup in the viewport after the anchor element was scrolled out of view.
arrowPaddingnumber5Minimum distance to maintain between the arrow and the edges of the popup.
Use it to prevent the arrow element from hanging out of the rounded corners of a popup.
disableAnchorTrackingbooleanfalseWhether to disable the popup from tracking any layout shift of its positioning anchor.
collisionAvoidanceCollisionAvoidance—Determines how to handle collisions when positioning the popup.
side controls overflow on the preferred placement axis (top/bottom or left/right): - 'flip': keep the requested side when it fits; otherwise try the opposite side (top and bottom, or left and right). - 'shift': never change side; keep the requested side and move the popup within the clipping boundary so it stays visible. - 'none': do not correct side-axis overflow.
align controls overflow on the alignment axis (start/center/end): - 'flip': keep side, but swap start and end when the requested alignment overflows. - 'shift': keep side and requested alignment, then nudge the popup along the alignment axis to fit. - 'none': do not correct alignment-axis overflow.
fallbackAxisSide controls fallback behavior on the perpendicular axis when the preferred axis cannot fit: - 'start': allow perpendicular fallback and try the logical start side first (top before bottom, or left before right in LTR). - 'end': allow perpendicular fallback and try the logical end side first (bottom before top, or right before left in LTR). - 'none': do not fallback to the perpendicular axis.
When side is 'shift', explicitly setting align only supports 'shift' or 'none'. If align is omitted, it defaults to 'flip'.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
--available-widthThe available width between the trigger and the edge of the viewport.
--available-heightThe available height between the trigger and the edge of the viewport.
--anchor-widthThe anchor's width.
--anchor-heightThe anchor's height.
--transform-originThe coordinates that this element is anchored to. Used for animations and transitions.
--positioner-widthThe width of the popover's positioner. It is important to set width to this value when using CSS to animate size changes.
--positioner-heightThe height of the popover's positioner. It is important to set height to this value when using CSS to animate size changes.
<Popover.Popup>
A container for the popover contents. Renders a <div> element.
initialFocusFocusTargetProp—Determines the element to focus when the popover is opened. By default, focus moves to the first tabbable element inside the popup, except when the popover is opened by touch — then the popup itself is focused to avoid opening the virtual keyboard.
- false: Do not move focus. - true: Move focus based on the default behavior (first tabbable element or popup). - An element or a ref object ({ current }): Move focus to that element. - function: Called with the interaction type (mouse, touch, pen, or keyboard). Return an element to focus, true to use the default behavior, null to fall back to the default behavior, or false/undefined to do nothing.
finalFocusFocusTargetProp—Determines the element to focus when the popover is closed.
- false: Do not move focus. - true: Move focus based on the default behavior (trigger or previously focused element). - An element or a ref object ({ current }): Move focus to that element. - function: Called with the interaction type (mouse, touch, pen, or keyboard). Return an element to focus, true to use the default behavior, null to fall back to the default behavior, or false/undefined to do nothing.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
data-instantPresent if animations should be instant.
--popup-widthThe width of the popup.
--popup-heightThe height of the popup.
<Popover.Arrow>
Displays an element positioned against the popover anchor. Renders a <div> element.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
data-uncenteredPresent when the popover arrow is uncentered.
<Popover.Backdrop>
An overlay displayed beneath the popover. Renders a <div> element.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<Popover.Title>
A heading that labels the popover. Renders an <h2> element.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<Popover.Description>
A paragraph with additional information about the popover. Renders a <p> element.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<Popover.Close>
A button that closes the popover. Renders a <button> element.
disabledbooleanfalseWhether the component ignores user interaction.
nativeButtonbooleantrueWhether the component renders a native <button> element when replacing it via the render prop. Set to false if the rendered element is not a button (for example, <div>).
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<Popover.Viewport>
A viewport for displaying content transitions. This component is only required if one popup can be opened by multiple triggers, its content changes based on the trigger, and switching between them is animated. Renders a <div> element.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
data-currentApplied to the direct child of the viewport when no transitions are present or the new content when it's entering.
data-previousApplied to the direct child of the viewport that contains the exiting content when transitions are present.
data-activation-directionIndicates the direction from which the popup was activated. This can be used to create directional animations based on how the popup was triggered. Contains space-separated values for both horizontal and vertical axes.
data-transitioningIndicates that the viewport is currently transitioning between old and new content.
data-instantPresent if animations should be instant.
--popup-widthThe width of the parent popup. This variable is placed on the 'previous' container and stores the width of the popup when the previous content was rendered. It can be used to freeze the dimensions of the popup when animating between different content.
--popup-heightThe height of the parent popup. This variable is placed on the 'previous' container and stores the height of the popup when the previous content was rendered. It can be used to freeze the dimensions of the popup when animating between different content.
The Viewport is optional — reach for it only when a single popup is opened by multiple triggers,
its content differs per trigger, and the switch between them is animated. When used, set
width: var(--positioner-width) and height: var(--positioner-height) on the Positioner so its
box is frozen to the measured size during the transition; otherwise content-driven resizing can
make the popup thrash or flip to another side.
createHandle#
Popover.createHandle<Payload>() creates a Popover.Handle that connects detached triggers to a
root. The handle exposes open(triggerId), close() and a reactive isOpen getter.