Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Popover for Svelte UI SDK

A panel that opens beside its button for a quick edit or a short note, and closes on Escape or a click outside. A Svelte popover on UI SDK.

On this page9 sections

A popover is a small panel that opens beside the button that pressed it, for a short note, a couple of settings or a quick edit such as a layer's width and height. Focus moves into it when it opens, and Escape or a click outside closes it.

When the task needs the whole screen and an explicit Cancel, use a Dialog. For a one-line hint that shows on hover or focus and holds nothing to click, a Tooltip is lighter.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Popover anatomy: trigger, title, positioner, arrow, description, content

Usage

<script lang="ts">
import { Button } from "@/components/ui/button"
import { Field, FieldGroup, FieldLabel } from "@/components/ui/field"
import { Input } from "@/components/ui/input"
import {
  Popover, PopoverContent, PopoverDescription, PopoverHeader,
  PopoverTitle, PopoverTrigger,
} from "@/components/ui/popover"
</script>
<Popover><PopoverTrigger>{#snippet render({ props })}<Button {...props} variant="outline">Open popover</Button>{/snippet}</PopoverTrigger><PopoverContent class="w-64 max-w-full"><PopoverHeader><PopoverTitle>Dimensions</PopoverTitle><PopoverDescription>Set the dimensions for the layer.</PopoverDescription></PopoverHeader><FieldGroup class="min-w-0 gap-3"><Field class="min-w-0" orientation="horizontal"><FieldLabel class="w-1/2">Width</FieldLabel><Input class="min-w-0" defaultValue="100%" /></Field><Field class="min-w-0" orientation="horizontal"><FieldLabel class="w-1/2">Height</FieldLabel><Input class="min-w-0" defaultValue="25px" /></Field></FieldGroup></PopoverContent></Popover>

Accessibility

The trigger is a button with aria-expanded and aria-haspopup="dialog", and the panel is a role="dialog" named by PopoverTitle and described by PopoverDescription. Focus moves to the first field when the popover opens and back to the trigger on Escape. It isn't trapped, so tabbing past the last field leaves the panel and closes it. Give every popover a PopoverTitle, or an aria-label on PopoverContent when it has none.

Space
When focus is on the trigger, opens or closes the popover.
Enter
When focus is on the trigger, opens or closes the popover.
Tab
Moves focus to the next focusable element in the popover. From the last one, focus moves to the element after the trigger and the popover closes.
Shift+Tab
Moves focus to the previous focusable element in the popover, and from the first one back to the trigger.
Escape
Closes the popover and moves focus to the trigger.

Demos

Basic

A title, Dimensions, and one line of description, with no fields under them. The panel lines up with the start of the button instead of its center, so it reads as a note pinned to that control. Pick this shape for a definition, a status or a short explanation that someone reads and then dismisses with Escape or a click elsewhere.

Preview width

Alignments

Three small buttons, Start, Center and End, each open a narrow panel lined up with that edge of the button. The edge comes from placement in the positioning prop on Popover: bottom-start, bottom or bottom-end. Near the edge of the screen, align toward the inside, and keep one alignment across a toolbar so its panels don't jump around.

Preview width

Form

The Dimensions panel as a small form, with Width and Height fields, Cancel and Save. The demo holds the open state, so Save, or Enter in a field, stores the size shown beside the trigger and closes the popover. Cancel is a PopoverClose, which closes it without saving. A longer form, or one that must be finished first, belongs in a Dialog.

Preview width

RTL

Arabic buttons inside a Direction provider set to right to left. The popover carries the direction of the place it is written to its portaled panel, so the panel reads right to left too. Four buttons set placement in the positioning prop to left, top, bottom and right. placement only takes physical sides, so the second row maps the start of the line to right and its end to left.

Preview width

FAQ

When should I use a Popover instead of a Dialog, a Tooltip or a Hover Card?

Use a Popover for a short form or a few controls that belong to the button that opened them, such as a column filter, a share link or the size of a layer. It opens on a click and closes when people are done with it.

A Dialog is for a task that takes over the screen until it's finished or canceled. A Tooltip shows a short label on hover or focus and can't hold anything clickable. A Hover Card previews a profile or a link while the pointer rests on it.

How do I open or close a Popover from code in Svelte?

Keep a boolean in state, pass it as open on Popover and update it in onOpenChange, which receives the next state as its first argument and the event details, with the reason it changed, as its second.

The trigger still has to be there, since the panel positions itself against it. Once the state is yours, a keyboard shortcut or a step in an onboarding tour can open the popover too.

How do I close a Popover after saving the form inside it?

Control the open state, as the answer above describes, and set it to closed in the form's submit handler once the save succeeds, as the Form demo does. If the save fails, leave it open so the error shows next to the fields.

For a Cancel button, use PopoverClose, which closes the popover without any state of yours. The fields reset each time the popover opens again, because a closed popover's content leaves the DOM.

Does a Popover block the rest of the page while it's open?

No. modal is false by default, so the page still scrolls and takes clicks, and a click outside or focus moving away closes the popover. Set modal on Popover to true to trap focus in the panel, lock the page's scroll and make the rest of the page inert, or to "trap-focus" to keep Tab inside while the page stays usable.

How do I align a Popover to the start or end of its trigger?

Set placement in the positioning prop on Popover: bottom-start, bottom (the default) or bottom-end, and the same suffixes on top, left and right. gutter in the same object sets the gap, 4 pixels unless you set it. PopoverContent also takes UI SDK's own align, side and sideOffset, which win over positioning.

The Alignments demo puts all three side by side. When there isn't room, the panel flips to the other side on its own.

How do I make a Popover as wide as its trigger?

Give the panel a width from the CSS variable that holds the trigger's width. On PopoverContent that is w-(--anchor-width), which UI SDK sets on the panel itself.

The class replaces the w-72 this file starts with, and the value follows the trigger when it resizes. Keep the max-w class too, so a wide trigger doesn't push the panel off a phone screen.

How do I position a Popover against a different element than its trigger?

Wrap that element in PopoverAnchor, and PopoverContent positions the panel against it while the trigger still opens and closes the popover. That suits a button that edits a field or a row sitting elsewhere in the layout. anchor on PopoverContent does the same without a wrapper, and also takes a CSS selector or a point such as a click's coordinates.

How do I give a Popover an accessible name in UI SDK?

Put a PopoverTitle in it. The panel points aria-labelledby at the title, and aria-describedby at a PopoverDescription, so a screen reader announces both when focus moves in. Without a title, UI SDK names the panel after its trigger, and an aria-label on PopoverContent replaces that name.

API Reference

Popover

PropType
open?boolean

Whether the popover is open. Pass it to control the popover. Two-way with bind:open.

defaultOpen?boolean

Whether the popover starts open, when uncontrolled.

Default
false
onOpenChange?(open: boolean, details: ChangeDetails<PopoverOpenChangeReason>) → void

Runs when the popover wants to open or close. details.reason says why, and details.cancel() stops it.

Type
(open: boolean, details: ChangeDetails<PopoverOpenChangeReason>) → void
Parameters

open – boolean

details – ChangeDetails<PopoverOpenChangeReason>

Returns
void
closeDelay?number

Milliseconds before the popover closes once the pointer leaves it and its trigger, with openOnHover.

Default
0
container?DocumentFragment | Element | null

Renders the popup into this element instead of in place, like a container inside a shadow root or an iframe. On web components, a CSS selector. Nested popups stay inside their parent.

Type
DocumentFragment | Element | null
controller?PopupController<any, PopoverOpenChangeReason, {}, string, PopoverPayloadChangeReason>

Opens, closes and inspects the popover from anywhere, and connects triggers outside it. Create one with createPopoverController().

Type
PopupController<any, PopoverOpenChangeReason, {}, string, PopoverPayloadChangeReason>
debug?boolean

Logs every state change with its reason to the console, in development builds.

delay?number

Milliseconds the pointer rests on the trigger before the popover opens, with openOnHover.

Default
300
dismissible?boolean

Whether Escape, an outside press, focus or the window leaving, Android's back gesture and a sheet's swipe can close it. Triggers, Close parts and the controller always can. Pass false while it holds unsaved work, and ask in onDismissAttempt.

Default
true
modal?"trap-focus" | boolean

false: the rest of the page stays usable, and focus leaving the popover closes it. true: also traps focus, locks scroll and makes the rest of the page inert, and an outside press only closes the popover. "trap-focus": traps focus but leaves the page scrollable and clickable.

Type
"trap-focus" | boolean
Default
false, or true while it presents as a sheet
onDismissAttempt?(details: DismissAttemptDetails<Extract<Reason, DismissAttemptReason>>) → void

Runs when dismissible refused a dismissal made on purpose: Escape, an outside press, back or a swipe. Focus or the window leaving is refused without it. Ask "Discard changes?" here.

Type
(details: DismissAttemptDetails<Extract<Reason, DismissAttemptReason>>) → void
Parameters

details – DismissAttemptDetails<Extract<Reason

DismissAttemptReason>>

Returns
void
onOpenChangeComplete?(open: boolean) → void

Runs once the popover has finished opening or closing, after any animation.

Parameters

open – boolean

Returns
void
onPayloadChange?(payload: unknown, details: ChangeDetails<PopoverPayloadChangeReason>) → void

Runs when the payload the content shows changes: a trigger with another payload opens the popover, or takes the open popover over, which fires no onOpenChange, and so does a controller's open({ payload }) on the open popover. details.cancel() keeps the previous payload.

Type
(payload: unknown, details: ChangeDetails<PopoverPayloadChangeReason>) → void
Parameters

payload – unknown

details – ChangeDetails<PopoverPayloadChangeReason>

Returns
void
openOnHover?boolean

Whether resting the pointer on the trigger opens the popover. Pressing the trigger still opens it, so keyboard and touch users can too.

Default
false
presentation?PresentationSetting<"popover">

How the popover presents. "sheet" slides it up from the bottom of the screen, where it can be swiped away, and "auto" does that on small touch screens. Decided each time it opens; the server renders "popover" for "auto".

Type
PresentationSetting<"popover">
Default
"popover"

PopoverTrigger

<button> data-slot="popover-trigger"

PropType
controller?PopupController<any, PopoverOpenChangeReason, {}, string, PopoverPayloadChangeReason>

Connects a trigger outside the popover, like one per table row, to the popover with this controller.

Type
PopupController<any, PopoverOpenChangeReason, {}, string, PopoverPayloadChangeReason>
disabled?boolean

Stops the trigger from opening the popover. A native button can't be focused or pressed, unless focusableWhenDisabled is set.

Default
false
focusableWhenDisabled?boolean

Keeps a disabled trigger in the tab order with aria-disabled instead of the native attribute, so it can host a tooltip that says why. Presses still do nothing.

Default
false
payload?unknown

Data for the content to render when this trigger opens the popover, like the row it belongs to.

shortcut?string

A keyboard shortcut that presses the trigger, like "mod+i". mod is Command on Apple platforms and Control elsewhere. Sets aria-keyshortcuts.

render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
AttributeType
data-closed?string

Present while the popover is closed, including while it animates out.

data-disabled?string

Present when the trigger is disabled.

data-open?string

Present while the popover is open. On a trigger, while it's open from that trigger.

PopoverContent

<div> data-slot="popover-content"

PropType
align?Align

How the popup lines up with the anchor along that side. start and end follow the reading direction. How the popover lines up with the trigger along that side.

Default
"center"
alignOffset?number

Shift along the anchor, in pixels. Shift along the trigger, in pixels.

Default
0
anchor?string | Anchor | null

Positions the popup against this instead of its trigger: an element, a CSS selector, a point like { x, y }, or anything with getBoundingClientRect(). It's read on every check, so it can move. A controller's open({ anchor }) wins for that open.

Type
string | Anchor | null
arrowPadding?number

Space to keep between the arrow and the popup's corners, in pixels.

Default
6
collisionPadding?number

Space to keep from the viewport edges, in pixels.

Default
8
container?DocumentFragment | Element | null

Renders the popup into this element instead of in place, like a container inside a shadow root or an iframe. On web components, a CSS selector. Nested popups stay inside their parent.

Type
DocumentFragment | Element | null
finalFocus?PopupFocusTarget

Where focus goes when the popover closes while focus is inside it.

Default
"trigger"
initialFocus?PopoverInitialFocus

Where focus goes when the popover opens. Opening on hover never moves focus, and opening with touch focuses the popover itself so no on-screen keyboard appears, unless this is set to something other than "auto".

Default
"auto"
inline?boolean

Renders the popover where it is in the page instead of above it, inside its parent's layout and stacking, and leaves placing it to your CSS: side, align and the offsets don't apply.

Default
false
keepMounted?boolean

Whether the popover stays in the page, hidden, while closed. Its content keeps its state between opens.

Default
false
side?PopoverSide

The side of the trigger it opens on. It flips when there's no room, unless avoidCollisions is off.

Default
"bottom"
sideOffset?number

Gap between the anchor and the popup, in pixels. Gap between the trigger and the popover, in pixels.

Default
4
render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

AttributeType
data-align"start" | "center" | "end"

How the popover lines up with its trigger.

Type
"start" | "center" | "end"
data-closed?string

Present while the popover is closed, including while it animates out.

data-ending-style?string

Present while the popover animates out. Style the state you animate to.

data-open?string

Present while the popover is open. On a trigger, while it's open from that trigger.

data-presentation"popover" | "sheet"

How the popover presents this time it's open.

Type
"popover" | "sheet"
data-side"top" | "right" | "bottom" | "left"

Where the popover ended up, after flipping away from edges. Set on the arrow too.

Type
"top" | "right" | "bottom" | "left"
data-starting-style?string

Present for the first frame. Style the state you animate from.

CSS variableType
--anchor-heightstring

Height of the anchor.

--anchor-widthstring

Width of the anchor, usually the trigger. Use it to match widths.

--arrow-offsetstring

Where the anchor's center falls along the popup's edge, kept arrowPadding from the corners. Position a ::before arrow with it.

--available-heightstring

Space between the anchor and the viewport edge. Use it for max-height. Not inherited, so a child that needs it copies it on the popup, like [--list-height:var(--available-height)], and reads max-h-(--list-height). The first popup registers both names for the whole page with CSS.registerProperty, so they don't inherit anywhere. A page that needs them to inherit calls it first, with inherits: true. An @property rule can't, since a registration in script wins.

--available-widthstring

Space beside the popup, up to the viewport edge. Not inherited either.

--drawer-dimstring

While it presents as a sheet: how dim the page behind is, from 0 to 1. The backdrop's opacity follows it.

--drawer-expandstring

While it presents as a sheet: 0 at the second-largest snap point and below, 1 at the largest. With one snap point, the whole travel.

--drawer-progressstring

While it presents as a sheet: how far open the sheet is, from 0 closed to 1 at its largest snap point. Never past 1.

--drawer-stackstring

While it presents as a sheet: how many sheets opened from inside this one are open above it, rising smoothly as each one opens. Push it back with it.

--sheet-backdropstring

Set it yourself: the color the page dims to while it presents as a sheet. Black at 0.2 by default.

--transform-originstring

Where the content scales from, on its trigger's side.

PopoverHeader

<div> data-slot="popover-header"

PropType
class?string

Merged with the part's classes.

PopoverTitle

<h2> data-slot="popover-title"

PropType
render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

PopoverDescription

<p> data-slot="popover-description"

PropType
render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.

PopoverClose

<button> data-slot="popover-close"

PropType
render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>