Popover Primitive
Use Popover to show a small panel next to a trigger, such as filters. It positions the panel and handles dismissal when someone presses Escape or clicks outside.
Dimensions
Set the width for this layout.
Anatomy
Use the Astro primitive adapter to render Popover anatomy with the Runtime wiring included.
---import { Popover } from "@starwind-ui/astro/popover";---
<Popover.Root> <Popover.Trigger>Open popover</Popover.Trigger> <Popover.Backdrop /> <Popover.Positioner> <Popover.Popup> <Popover.Title>Popover title</Popover.Title> <Popover.Description>Popover description</Popover.Description> <Popover.Close>Close</Popover.Close> <Popover.Arrow /> </Popover.Popup> </Popover.Positioner></Popover.Root>Use the React primitive adapter when Popover state participates in React rendering.
import { Popover } from "@starwind-ui/react/popover";
export function Example() { return ( <Popover.Root> <Popover.Trigger>Open popover</Popover.Trigger> <Popover.Backdrop /> <Popover.Positioner> <Popover.Popup> <Popover.Title>Popover title</Popover.Title> <Popover.Description>Popover description</Popover.Description> <Popover.Close>Close</Popover.Close> <Popover.Arrow /> </Popover.Popup> </Popover.Positioner> </Popover.Root> );}Use the Vue components to build Popover.
<script setup lang="ts">import Popover from "@starwind-ui/vue/popover";</script>
<template> <Popover.Root> <Popover.Trigger>Open popover</Popover.Trigger> <Popover.Backdrop /> <Popover.Positioner> <Popover.Popup> <Popover.Title>Popover title</Popover.Title> <Popover.Description>Popover description</Popover.Description> <Popover.Close>Close</Popover.Close> <Popover.Arrow /> </Popover.Popup> </Popover.Positioner> </Popover.Root></template>Use the Svelte components to build Popover.
<script lang="ts"> import { Popover } from "@starwind-ui/svelte/popover";</script>
<Popover.Root> <Popover.Trigger>Open popover</Popover.Trigger> <Popover.Backdrop /> <Popover.Positioner> <Popover.Popup> <Popover.Title>Popover title</Popover.Title> <Popover.Description>Popover description</Popover.Description> <Popover.Close>Close</Popover.Close> <Popover.Arrow /> </Popover.Popup> </Popover.Positioner></Popover.Root>Render the Popover data-sw-* contract yourself, then initialize createPopover.
<div data-sw-popover> <button data-sw-popover-trigger type="button" aria-haspopup="dialog">Open popover</button> <div data-sw-popover-backdrop hidden></div> <div data-sw-popover-positioner> <div data-sw-popover-popup role="dialog" tabindex="-1" hidden> <h2 data-sw-popover-title>Popover title</h2> <p data-sw-popover-description>Popover description</p> <button data-sw-popover-close type="button">Close</button> <div data-sw-popover-arrow></div> </div> </div></div>
<script type="module"> import { createPopover } from "@starwind-ui/runtime/popover";
const root = document.querySelector("[data-sw-popover]"); if (root) { createPopover(root); }</script>Astro, React, Vue, and Svelte components use the same underlying interactions. Each framework has its own way to read and update state. See the examples and your installed packageโs types for the available props and events. Plain HTML uses DOM events and Runtime methods.
Floating Behavior
| Fact | Value |
|---|---|
| Anchor part | trigger |
| Positioner part | positioner |
| Popup part | popup |
| Portal part | portal |
| Option props | side, align, sideOffset, avoidCollisions, collisionStrategy |
API Reference
Root
The main element that owns the Popover Runtime instance.
- Default element
- div
- Discovery hook
- data-sw-popover
- Role
- -
Props
open boolean -
- Description
- Controls whether Popover is open.
- Kind
- control
- Targets
- root
- Full type
- boolean
- React
Use open for controlled state and defaultOpen for default state, and onOpenChange for change proposals.
opendefaultOpenonOpenChange
- Astro
Use open or defaultOpen for initial state, listen for starwind:open-change, and call setOpen for later updates.
opendefaultOpenstarwind:open-changesetOpen
- Runtime / HTML
Use data-default-open for initial state, listen for starwind:open-change, and call setOpen for later updates.
data-default-openstarwind:open-changesetOpen
defaultOpen boolean false
- Description
- Sets whether Popover starts open.
- Kind
- control
- Targets
- root
- Full type
- boolean
- React
Use open for controlled state and defaultOpen for default state, and onOpenChange for change proposals.
opendefaultOpenonOpenChange
- Astro
Use open or defaultOpen for initial state, listen for starwind:open-change, and call setOpen for later updates.
opendefaultOpenstarwind:open-changesetOpen
- Runtime / HTML
Use data-default-open for initial state, listen for starwind:open-change, and call setOpen for later updates.
data-default-openstarwind:open-changesetOpen
closeOnEscape boolean true
- Description
- Closes Popover when Escape is pressed.
- Kind
- option
- Targets
- -
- Full type
- boolean
closeOnOutsideInteract boolean true
- Description
- Closes Popover when the user interacts outside it.
- Kind
- option
- Targets
- -
- Full type
- boolean
modal boolean false
- Description
- Makes Popover behave as a modal overlay.
- Kind
- option
- Targets
- -
- Full type
- boolean
openOnHover boolean false
- Description
- Configures the open on hover option for the Root part.
- Kind
- option
- Targets
- -
- Full type
- boolean
closeDelay number 200
- Description
- Sets how long Popover waits before closing.
- Kind
- option
- Targets
- root
- Full type
- number
onCloseComplete (open: boolean, details: PopoverCloseCompleteDetails) => void -
- Description
- Runs after Popover has finished closing.
- Kind
- callback
- Targets
- -
- Full type
- (open: boolean, details: PopoverCloseCompleteDetails) => void
onOpenChange (open: boolean, details: PopoverOpenChangeDetails) => void -
- Description
- Runs when Popover opens or closes.
- Kind
- callback
- Targets
- -
- Full type
- (open: boolean, details: PopoverOpenChangeDetails) => void
- React
Use open for controlled state and defaultOpen for default state, and onOpenChange for change proposals.
opendefaultOpenonOpenChange
- Astro
Use open or defaultOpen for initial state, listen for starwind:open-change, and call setOpen for later updates.
opendefaultOpenstarwind:open-changesetOpen
- Runtime / HTML
Use data-default-open for initial state, listen for starwind:open-change, and call setOpen for later updates.
data-default-openstarwind:open-changesetOpen
Events
openChange onOpenChange open: boolean
- Description
- Fires when Popover opens or closes.
- DOM event
- starwind:open-change
- Details type
- PopoverOpenChangeDetails
- Timing
- before-state-commit
- Cancelable
- Yes
- Cancellation sequence
- Check internal eligibility and intent.
- Create one details object for the proposal.
- Call the Runtime callback with the details object when the controller exposes one.
- Dispatch the cancelable DOM event with the same details object, including when the callback canceled it.
- Read details.isCanceled, including cancellation caused by preventDefault().
- Apply the accepted state.
- Notify Runtime subscribers and other accepted-only observers.
closeComplete onCloseComplete open: boolean
- Description
- Fires after Popover has finished closing.
- DOM event
- starwind:close-complete
- Details type
- PopoverCloseCompleteDetails
- Timing
- after-state-commit
- Cancelable
- No
State
open boolean open / defaultOpen
- Description
- Tracks whether Popover is open.
- Initial attribute
- data-default-open
- Runtime getter
- getOpen
- Runtime setter
- setOpen
- React
Use open for controlled state and defaultOpen for default state, and onOpenChange for change proposals.
opendefaultOpenonOpenChange
- Astro
Use open or defaultOpen for initial state, listen for starwind:open-change, and call setOpen for later updates.
opendefaultOpenstarwind:open-changesetOpen
- Runtime / HTML
Use data-default-open for initial state, listen for starwind:open-change, and call setOpen for later updates.
data-default-openstarwind:open-changesetOpen
Data Attributes
Runtime hooks
State
Metadata
Trigger
The control that opens, closes, or targets the Popover content.
- Default element
- button
- Discovery hook
- data-sw-popover-trigger
- Role
- -
Props
asChild boolean -
- Description
- Merges behavior onto your child element instead of rendering the default Trigger element.
- Kind
- rendering
- Targets
- trigger
- Full type
- boolean
Data Attributes
Runtime hooks
State
Metadata
Portal
Moves Popover overlay content to the document body when needed.
- Default element
- div
- Discovery hook
- data-sw-popover-portal
- Role
- -
Data Attributes
Runtime hooks
Metadata
Positioner
Positions the Popover content relative to its trigger.
- Default element
- div
- Discovery hook
- data-sw-popover-positioner
- Role
- -
Props
side "top" | "right" | "bottom" | "left" "bottom"
- Description
- Sets the preferred side for Popover content.
- Kind
- option
- Targets
- positioner, popup
- Full type
- "top" | "right" | "bottom" | "left"
align "start" | "center" | "end" "center"
- Description
- Sets how Popover content aligns to its trigger.
- Kind
- option
- Targets
- positioner, popup
- Full type
- "start" | "center" | "end"
sideOffset number 4
- Description
- Sets the distance between Popover content and its trigger.
- Kind
- option
- Targets
- positioner, popup
- Full type
- number
avoidCollisions boolean true
- Description
- Allows Popover content to shift or flip to stay visible.
- Kind
- option
- Targets
- positioner, popup
- Full type
- boolean
collisionStrategy "initial-placement" | "best-fit" "initial-placement"
- Description
- Configures the collision strategy option for the Positioner part.
- Kind
- option
- Targets
- positioner, popup
- Full type
- "initial-placement" | "best-fit"
Data Attributes
Runtime hooks
State
Metadata
Popup
The floating content container for Popover.
- Default element
- div
- Discovery hook
- data-sw-popover-popup
- Role
- dialog
Props
side "top" | "right" | "bottom" | "left" "bottom"
- Description
- Sets the preferred side for Popover content.
- Kind
- option
- Targets
- positioner, popup
- Full type
- "top" | "right" | "bottom" | "left"
align "start" | "center" | "end" "center"
- Description
- Sets how Popover content aligns to its trigger.
- Kind
- option
- Targets
- positioner, popup
- Full type
- "start" | "center" | "end"
sideOffset number 4
- Description
- Sets the distance between Popover content and its trigger.
- Kind
- option
- Targets
- positioner, popup
- Full type
- number
avoidCollisions boolean true
- Description
- Allows Popover content to shift or flip to stay visible.
- Kind
- option
- Targets
- positioner, popup
- Full type
- boolean
collisionStrategy "initial-placement" | "best-fit" "initial-placement"
- Description
- Configures the collision strategy option for the Popup part.
- Kind
- option
- Targets
- positioner, popup
- Full type
- "initial-placement" | "best-fit"
Data Attributes
Runtime hooks
State
Metadata
Arrow
The arrow element that visually points to the trigger.
- Default element
- div
- Discovery hook
- data-sw-popover-arrow
- Role
- -
Data Attributes
Runtime hooks
Backdrop
The backdrop shown behind the Popover overlay.
- Default element
- div
- Discovery hook
- data-sw-popover-backdrop
- Role
- -
Data Attributes
Runtime hooks
State
Title
The accessible title for Popover.
- Default element
- h2
- Discovery hook
- data-sw-popover-title
- Role
- -
Data Attributes
Runtime hooks
Description
Supporting description text for Popover.
- Default element
- p
- Discovery hook
- data-sw-popover-description
- Role
- -
Data Attributes
Runtime hooks
Close
A control that closes Popover.
- Default element
- button
- Discovery hook
- data-sw-popover-close
- Role
- -
Data Attributes
Runtime hooks
Viewport
The visible viewport for Popover content.
- Default element
- div
- Discovery hook
- data-sw-popover-viewport
- Role
- -
Data Attributes
Runtime hooks
Runtime API
- Factory
createPopover- Import
@starwind-ui/runtime/popover- Root hook
- root
data-sw-popover - Option props
- closeOnEscape, closeOnOutsideInteract, defaultOpen, modal, onCloseComplete, onOpenChange, open, openOnHover
Option Lifecycles
| Option | Lifecycle |
|---|---|
| closeOnEscape | constructor-only |
| closeOnOutsideInteract | constructor-only |
| defaultOpen | constructor-only |
| modal | constructor-only |
| onCloseComplete | constructor-only |
| onOpenChange | constructor-only |
| open | setter-backed |
| openOnHover | constructor-only |
Runtime Setters
Related Styled Components
Changelog
View version history v1.3.0 10 releases
v1.3.0
- Popover content opens with the direction of its trigger and keeps it while open.
- Popover now returns focus without scrolling the page. Before, in Safari, closing a popover after scrolling could make the page jump back to the trigger.
- A click during the Popover close animation now goes to the page below the popup. Before, the closing popup caught the click, so a quick click on a control under it did nothing. An optional Popover backdrop still covers the page until the popup is hidden.
- When you close a Popover with focus inside it, focus now goes back to the trigger as the close starts. The closing popup takes no keyboard input until it hides.
- Fixed Popover in React and Vue when the Popup mounts after the Root. Before, the trigger did not open it. A Popup that you hide and show again, or replace with a new `key`, also opens again.
- In React and Vue, an open Popup that unmounts now leaves the Root closed and removes the scroll lock. When it mounts again, it opens without a change event.
- In React, a standalone `defaultOpen` Popover with no Portal no longer throws when it mounts.
- In Vue, this Primitive now uses the same `asChild` child handling as the other Vue Primitives. Its public API is unchanged. An invalid `asChild` child now gives one shorter error message.
- Escape in a Select or Combobox inside a Popover now closes only that inner list. Before, when focus was on the inner trigger or input, the same Escape also closed the Popover.
- In Svelte, the `data-default-*` attributes of Popover now come only from its `default*` props, and `false` flags such as `data-close-on-click` are left out, as in the other frameworks.
- In Vue, this component now needs Vue 3.5.21 or newer.
- Internal code cleanup, such as unused code and import order. This cleanup does not change behavior.
v1.2.0
- In Astro, an attribute that you set on a Popover part now wins over the part's own default for the same attribute, such as `role` or `aria-label`.
- In Svelte, a defined `open` that you pass when Popover mounts now stays under your control, as in React and Vue. To change it, use `bind:` or update your state in the change callback. A form reset keeps it. If you pass `undefined` at mount, Popover keeps its own state.
- In Svelte, the `children` snippet on the Popover root no longer receives `open`. Use `bind:open`.
- Popover no longer moves focus back to its trigger when you move to another control while it closes. A field that you press outside the popup keeps focus.
- Fixed a Popover trigger on a link or span that stayed disabled when a framework wrote `disabled="false"`. On a native form control, any `disabled` attribute still disables it.
- In Svelte, the Popover trigger now accepts a link in `child`. Before, it warned in development, type checks rejected the link, and `ref` did not run. Use the new `TriggerChildPayload` type for your snippet.
v1.1.2
- Vue Popover keeps its current open state when a change is canceled and responds to later open or close requests from your app.
- Fixed Color Picker popups inside native dialogs so their controls remain visible and receive input.
- Fixed popups extending beyond the viewport near an edge. They now shift to fit while keeping space from the trigger.
- Reduced background work when a React page uses multiple menus, tooltips, or other portals.
- Fixed nested Svelte overlays inside a dialog appearing behind their parent or failing to receive clicks.
- Reduced background work when a Vue page uses multiple menus, tooltips, or other portals.
- Fixed nested overlays appearing behind their parent in Vue and Svelte. Overlays can now render under the document body; if custom CSS or DOM queries require them to stay inside a parent, set a portal container or add `data-floating-root` to that parent.
- Fixed React 18 retaining removed popup elements when the parent component stays mounted.
- Reduced repeated JavaScript when Astro Popover is used with other Starwind components.
v1.1.1
- Removed old comments from the Vue Primitive files that said the code was internal and not for release. The CLI also removes the blank line that these comments left in the Vue Primitive `index.ts` files that it copies into your project.
v1.1.0
- Added `container` and `disabled` to the Portal part. `container` sets the element that holds the popup, and `disabled` keeps the popup in place. Portals now place nested popups in the same way every time, and they show their placement in data attributes.
v1.0.0
- This Primitive is now stable at version `1.0.0`. Its API and behavior stay the same as in the previous release.
v0.1.3
- The React Primitive files that the CLI copies into your project are now marked with `"use client"`, so they stay client components in React server frameworks.
- In React, you can cancel a change from its change callback or DOM event before it applies. React then shows only the changes that apply.
v0.1.2
- Astro and React Popover now accept `collisionStrategy`. Set it to `"best-fit"` to place the popup on the side with the most space when the screen is small.
v0.1.1
- When `modal` is set, the page behind the Popover no longer scrolls while it is open.
v0.1.0
- First release. Popover handles the open state, placement next to the trigger, focus, closing, open and close animations, and popovers inside popovers.