Kobalte.v0.13.4

Popover

A popover positioned relative to an anchor element.

Import

ts
import { Popover } from "@kobalte/core/popover";
// or
import { Root, Trigger, ... } from "@kobalte/core/popover";
// or (deprecated)
import { Popover } from "@kobalte/core";
ts
import { Popover } from "@kobalte/core/popover";
// or
import { Root, Trigger, ... } from "@kobalte/core/popover";
// or (deprecated)
import { Popover } from "@kobalte/core";

Features

  • Follow the WAI ARIA Popover design pattern.
  • Supports modal and non-modal modes.
  • Provides screen reader announcements via rendered title and description.
  • Focus is fully managed and customizable.
  • Optionally render a pointing arrow.
  • Can be controlled or uncontrolled.

Anatomy

The popover consists of:

  • Popover: The root container for a popover.
  • Popover.Trigger: The button that opens the popover.
  • Popover.Anchor: An optional element to position the Popover.Content against.
  • Popover.Portal: Portals its children into the body when the popover is open.
  • Popover.Content: Contains the content to be rendered when the popover is open.
  • Popover.Arrow: An optional arrow element to render alongside the popover.
  • Popover.CloseButton: The button that closes the popover.
  • Popover.Title: An accessible title to be announced when the popover is open.
  • Popover.Description: An optional accessible description to be announced when the popover is open.
tsx
<Popover>
<Popover.Trigger />
<Popover.Anchor />
<Popover.Portal>
<Popover.Content>
<Popover.Arrow />
<Popover.CloseButton />
<Popover.Title />
<Popover.Description />
</Popover.Content>
</Popover.Portal>
</Popover>
tsx
<Popover>
<Popover.Trigger />
<Popover.Anchor />
<Popover.Portal>
<Popover.Content>
<Popover.Arrow />
<Popover.CloseButton />
<Popover.Title />
<Popover.Description />
</Popover.Content>
</Popover.Portal>
</Popover>

Example

Usage

Default open

An initial, uncontrolled open value can be provided using the defaultOpen prop.

tsx
<Popover defaultOpen>...</Popover>
tsx
<Popover defaultOpen>...</Popover>

Controlled open

The open prop can be used to make the open state controlled. The onOpenChange event is fired when the user presses the trigger, close button or interact outside, and receives the new value.

tsx
import { createSignal } from "solid-js";
function ControlledExample() {
const [open, setOpen] = createSignal(false);
return (
<Popover open={open()} onOpenChange={setOpen}>
<Popover.Trigger>{open() ? "Close" : "Open"}</Popover.Trigger>
<Popover.Portal>
<Popover.Content>...</Popover.Content>
</Popover.Portal>
</Popover>
);
}
tsx
import { createSignal } from "solid-js";
function ControlledExample() {
const [open, setOpen] = createSignal(false);
return (
<Popover open={open()} onOpenChange={setOpen}>
<Popover.Trigger>{open() ? "Close" : "Open"}</Popover.Trigger>
<Popover.Portal>
<Popover.Content>...</Popover.Content>
</Popover.Portal>
</Popover>
);
}

With custom anchor

Use the Popover.Anchor component to anchor the content to another element if you do not want to use the trigger as the anchor.

The popover opens when you click .

But it's anchored to the whole div.

tsx
<Popover>
<Popover.Anchor>
<p>
The popover opens when you click <Popover.Trigger>here</Popover.Trigger>.
</p>
<p>But it's anchored to the whole div.</p>
</Popover.Anchor>
<Popover.Portal>
<Popover.Content>...</Popover.Content>
</Popover.Portal>
</Popover>
tsx
<Popover>
<Popover.Anchor>
<p>
The popover opens when you click <Popover.Trigger>here</Popover.Trigger>.
</p>
<p>But it's anchored to the whole div.</p>
</Popover.Anchor>
<Popover.Portal>
<Popover.Content>...</Popover.Content>
</Popover.Portal>
</Popover>

Origin-aware animations

We expose a CSS custom property --kb-popover-content-transform-origin which can be used to animate the content from its computed origin.

css
/* style.css */
.popover__content {
transform-origin: var(--kb-popover-content-transform-origin);
animation: contentHide 250ms ease-in forwards;
}
.popover__content[data-expanded] {
animation: contentShow 250ms ease-out;
}
@keyframes contentShow {
from {
opacity: 0;
transform: scale(0.96);
}
to {
opacity: 1;
transform: scale(1);
}
}
@keyframes contentHide {
from {
opacity: 1;
transform: scale(1);
}
to {
opacity: 0;
transform: scale(0.96);
}
}
css
/* style.css */
.popover__content {
transform-origin: var(--kb-popover-content-transform-origin);
animation: contentHide 250ms ease-in forwards;
}
.popover__content[data-expanded] {
animation: contentShow 250ms ease-out;
}
@keyframes contentShow {
from {
opacity: 0;
transform: scale(0.96);
}
to {
opacity: 1;
transform: scale(1);
}
}
@keyframes contentHide {
from {
opacity: 1;
transform: scale(1);
}
to {
opacity: 0;
transform: scale(0.96);
}
}

API Reference

Popover

Popover is equivalent to the Root import from @kobalte/core/popover (and deprecated Popover.Root).

PropDescription
openboolean
The controlled open state of the popover.
defaultOpenboolean
The default open state when initially rendered. Useful when you do not need to control the open state.
onOpenChange(open: boolean) => void
Event handler called when the open state of the popover changes.
idstring
A unique identifier for the component. The id is used to generate id attributes for nested components. If no id prop is provided, a generated id will be used.
modalboolean
Whether the popover should be the only visible content for screen readers, when set to true:
- interaction with outside elements will be disabled.
- scroll will be locked.
- focus will be locked inside the popover content.
- elements outside the popover content will not be visible for screen readers.
preventScrollboolean
Whether the scroll should be locked even if the popover is not modal.
forceMountboolean
Used to force mounting the popover (portal and content) when more control is needed. Useful when controlling animation with SolidJS animation libraries.
translationsPopoverIntlTranslations
Localization strings.

Popover also accepts the following props to customize the placement of the Popover.Content.

PropDescription
getAnchorRect(anchor?: HTMLElement) => AnchorRect | undefined
Function that returns the anchor element's DOMRect.
anchorRefAccessor<HTMLElement | undefined>
A ref for the anchor element. Useful if you want to use an element outside Popover as the popover anchor.
placementPlacement
The placement of the popover.
gutternumber
The distance between the popover and the trigger/anchor element. By default, it's 0 plus half of the arrow offset, if it exists.
shiftnumber
The skidding of the popover along the anchor element.
flipboolean | string
Controls the behavior of the popover when it overflows the viewport:
- If a boolean, specifies whether the popover should flip to the opposite side when it overflows.
- If a string, indicates the preferred fallback placements when it overflows.
The placements must be spaced-delimited, e.g. "top left".
slideboolean
Whether the popover should slide when it overflows.
overlapboolean
Whether the popover can overlap the anchor element when it overflows.
sameWidthboolean
Whether the popover should have the same width as the anchor element. This will be exposed to CSS as --kb-popper-anchor-width.
fitViewportboolean
Whether the popover should fit the viewport. If this is set to true, the popover content will have maxWidth and maxHeight set to the viewport size. This will be exposed to CSS as --kb-popper-available-width and --kb-popper-available-height.
hideWhenDetachedboolean
Whether to hide the popover when the anchor element becomes occluded.
detachedPaddingnumber
The minimum padding in order to consider the anchor element occluded.
arrowPaddingnumber
The minimum padding between the arrow and the popover corner.
overflowPaddingnumber
The minimum padding between the popover and the viewport edge. This will be exposed to CSS as --kb-popper-overflow-padding.

Popover.Trigger

Popover.Trigger consists of Button.

Data attributeDescription
data-expandedPresent when the popover is open.
data-closedPresent when the popover is close.

Popover.Anchor , Popover.Content , Popover.Arrow, Popover.Title, Popover.Description and Popover.CloseButton share the same data-attributes.

Popover.Content

The popper positioner will copy the same z-index as the Popover.Content.

PropDescription
onOpenAutoFocus(event: Event) => void
Event handler called when focus moves into the component after opening. It can be prevented by calling event.preventDefault.
onCloseAutoFocus(event: Event) => void
Event handler called when focus moves to the trigger after closing. It can be prevented by calling event.preventDefault.
onEscapeKeyDown(event: KeyboardEvent) => void
Event handler called when the escape key is down. It can be prevented by calling event.preventDefault.
onPointerDownOutside(event: PointerDownOutsideEvent) => void
Event handler called when a pointer event occurs outside the bounds of the component. It can be prevented by calling event.preventDefault.
onFocusOutside(event: FocusOutsideEvent) => void
Event handler called when the focus moves outside the bounds of the component. It can be prevented by calling event.preventDefault.
onInteractOutside(event: InteractOutsideEvent) => void
Event handler called when an interaction (pointer or focus event) happens outside the bounds of the component. It can be prevented by calling event.preventDefault.

Popover.Arrow

PropDescription
sizenumber
The size of the arrow.

Rendered elements

ComponentDefault rendered element
Popovernone
Popover.Triggerbutton
Popover.Anchordiv
Popover.PortalPortal
Popover.Contentdiv
Popover.Arrowdiv
Popover.CloseButtonbutton
Popover.Titleh2
Popover.Descriptionp

Accessibility

Keyboard Interactions

KeyDescription
SpaceWhen focus is on the trigger, opens/closes the popover.
EnterWhen focus is on the trigger, opens/closes the popover.
TabMoves focus to the next focusable element.
Shift + TabMoves focus to the next previous element.
EscCloses the popover and moves focus to the trigger.