Skip to content

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

Components

Free

Popover for React Aria

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

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

import { Button } from "@/components/ui/button";
import { Field, FieldGroup, FieldLabel } from "@/components/ui/field";
import { Input } from "@/components/ui/input";
import {
  Popover, PopoverDescription, PopoverHeader, PopoverTitle,
  PopoverTrigger,
} from "@/components/ui/popover";
const PopoverDemo = () => (
  <PopoverTrigger>
    <Button variant="outline">Open popover</Button>
    <Popover className="w-64 max-w-full">
      <PopoverHeader>
        <PopoverTitle>Dimensions</PopoverTitle>
        <PopoverDescription>
          Set the dimensions for the layer.
        </PopoverDescription>
      </PopoverHeader>
      <FieldGroup className="min-w-0 gap-3">
        <Field className="min-w-0" orientation="horizontal">
          <FieldLabel className="w-1/2" htmlFor="popover-width">
            Width
          </FieldLabel>
          <Input className="min-w-0" defaultValue="100%" id="popover-width" />
        </Field>
        <Field className="min-w-0" orientation="horizontal">
          <FieldLabel className="w-1/2" htmlFor="popover-height">
            Height
          </FieldLabel>
          <Input className="min-w-0" defaultValue="25px" id="popover-height" />
        </Field>
      </FieldGroup>
    </Popover>
  </PopoverTrigger>
);

Accessibility

The trigger button gets aria-expanded, and the popover is a role="dialog" named by the h3 that PopoverTitle renders. Without a title, React Aria names it after the trigger, so an icon button's aria-label names the panel too. When it opens, focus goes to the panel itself and stays inside it, and the rest of the page is inert until it closes.

Space
When focus is on the trigger, opens the popover and moves focus into it.
Enter
When focus is on the trigger, opens the popover and moves focus into it.
Tab
Moves focus to the next focusable element in the popover, and from the last one back to the first.
Shift+Tab
Moves focus to the previous focusable element in the popover, and from the first one to the last.
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. placement on Popover sets it, written with a space: 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 sets that state to closed 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 first four set placement to left, top, bottom and right. The last two use start and end, which React Aria resolves from the provider's locale, so the start of the line opens on the right and the panel lays out right to left. A dir attribute alone changes neither.

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 React?

Keep a boolean in state, pass it as isOpen on PopoverTrigger and update it in onOpenChange, which receives the next state as a boolean. The props go on the trigger, not the Popover, because the trigger owns the state for both.

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.

A Cancel button sets the same state to closed. 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?

It does by default. React Aria lays an invisible underlay over the page, keeps focus inside the popover, stops the page from scrolling and makes the rest inert, and a press outside closes it. Add isNonModal to Popover to leave the page alone, but then the popover is no longer a dialog: focus stays where it was and a click outside doesn't close it.

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

Pass placement on Popover, written with a space: bottom start, bottom (the default here) or bottom end, and the same on the other sides. offset sets the gap from the trigger, 4 pixels in this file, and crossOffset nudges it along the edge.

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 Popover that is w-(--trigger-width).

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?

Pass a ref to that element as triggerRef on Popover. The trigger still opens and closes it, and the panel positions itself against the element you passed, which suits a button that edits a field or a row sitting elsewhere in the layout.

How do I give a Popover an accessible name in React Aria?

Put a PopoverTitle in it, and this file points the dialog's aria-labelledby at it. Without a title, React Aria names the dialog after the trigger, so a button that reads Dimensions gives a dialog called Dimensions. To name it after other text on the page, pass that element's id as aria-labelledby on Popover.

API Reference

Popover

PropType
defaultOpen?boolean

Whether the overlay is open by default (uncontrolled).

onOpenChange?(isOpen: boolean) → void

Handler that is called when the overlay's open state changes.

Parameters

isOpen – boolean

Returns
void
arrowBoundaryOffset?number

The minimum distance the arrow's edge should be from the edge of the overlay element.

Default
0
arrowRef?RefObject<Element | null>

A ref for the popover arrow element.

Type
RefObject<Element | null>
boundaryElement?Element

Element that that serves as the positioning boundary.

Default
document.body
children?ChildrenOrFunction<T>

The children of the component. A function may be provided to alter the children based on component state.

Type
ChildrenOrFunction<T>
containerPadding?number

The placement padding that should be applied between the element and its surrounding container.

Default
12
crossOffset?number

The additional offset applied along the cross axis between the element and its anchor element.

Default
0
getTargetRect?(target: Element) → DOMRect | null

Overrides the target element's bounding rectangle. Useful for positioning relative to a specific point such as the mouse cursor (e.g. context menus) or text selection.

Type
(target: Element) → DOMRect | null
Default
target.getBoundingClientRect()
Parameters

target – Element

Returns
DOMRect | null
isEntering?boolean

Whether the popover is currently performing an entry animation.

isExiting?boolean

Whether the popover is currently performing an exit animation.

isKeyboardDismissDisabled?boolean

Whether pressing the escape key to close the popover should be disabled. Most popovers should not use this option. When set to true, an alternative way to close the popover with a keyboard must be provided.

Default
false
isNonModal?boolean

Whether the popover is non-modal, i.e. elements outside the popover may be interacted with by assistive technologies. Most popovers should not use this option as it may negatively impact the screen reader experience. Only use with components such as combobox, which are designed to handle this situation carefully.

isOpen?boolean

Whether the overlay is open by default (controlled).

maxHeight?number

The maxHeight specified for the overlay element. By default, it will take all space up to the current viewport height.

offset?number

The additional offset applied along the main axis between the element and its anchor element. Ark default: 8.

Default
4
onBlurWithin?(e: FocusEvent) → void

Handler that is called when the target element and all descendants lose focus.

Parameters

e – FocusEvent

Returns
void
onFocusWithin?(e: FocusEvent) → void

Handler that is called when the target element or a descendant receives focus.

Parameters

e – FocusEvent

Returns
void
onFocusWithinChange?(isFocusWithin: boolean) → void

Handler that is called when the the focus within state changes.

Parameters

isFocusWithin – boolean

Returns
void
placement?"bottom" | "bottom left" | "bottom right" | "bottom start" | "bottom end" | "top" | "top left" | "top right" | "top start" | "top end" | "left" | "left top" | "left bottom" | "start" | "start top" | "start bottom" | "right" | "right top" | "right bottom" | "end" | "end top" | "end bottom"

The placement of the element with respect to its anchor element.

Type
"bottom" | "bottom left" | "bottom right" | "bottom start" | "bottom end" | "top" | "top left" | "top right" | "top start" | "top end" | "left" | "left top" | "left bottom" | "start" | "start top" | "start bottom" | "right" | "right top" | "right bottom" | "end" | "end top" | "end bottom"
Default
"bottom"
scrollRef?RefObject<Element | null>

A ref for the scrollable region within the overlay.

Type
RefObject<Element | null>
Default
overlayRef
shouldCloseOnInteractOutside?(element: Element) → boolean

When user interacts with the argument element outside of the popover ref, return true if onClose should be called. This gives you a chance to filter out interaction with elements that should not dismiss the popover. By default, onClose will always be called on interaction outside the popover ref.

Parameters

element – Element

Returns
boolean
shouldFlip?boolean

Whether the element should flip its orientation (e.g. top to bottom or left to right) when there is insufficient room for it to render completely.

Default
true
shouldSkipAnimation?boolean

Whether the popover should appear and disappear without an entry or exit animation. This is used by components such as PreviewTrigger to skip animations when quickly swapping between overlays.

shouldUpdatePosition?boolean

Whether the overlay should update its position automatically.

Default
true
slot?string | null

A slot name for the component. Slots allow the component to receive props from a parent component. An explicit null value indicates that the local props completely override all props received from a parent.

Type
string | null
style?StyleOrFunction<T>

The inline [style](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style) for the element. A function may be provided to compute the style based on component state.

Type
StyleOrFunction<T>
trigger?string

The name of the component that triggered the popover. This is reflected on the element as the data-trigger attribute, and can be used to provide specific styles for the popover depending on which element triggered it.

triggerRef?RefObject<Element | null>

The ref for the element which the popover positions itself with respect to. When used within a trigger component such as DialogTrigger, MenuTrigger, Select, etc., this is set automatically. It is only required when used standalone.

Type
RefObject<Element | null>
UNSTABLE_portalContainer?Element

The container element in which the overlay portal will be placed. This may have unknown behavior depending on where it is portalled to. Deprecated. - Use a parent UNSAFE_PortalProvider to set your portal container instead.

Default
document.body
render?DOMRenderFunction<E, T>

Overrides the default DOM element with a custom render function. This allows rendering existing components with built-in styles and behaviors such as router links, animation libraries, and pre-styled components. Requirements: - You must render the expected element type (e.g. if <button> is expected, you cannot render an <a>). - Only a single root DOM element can be rendered (no fragments). - You must pass through props and ref to the underlying DOM element, merging with your own prop as appropriate.

Type
DOMRenderFunction<E, T>
className?string

Merged with the part's classes.

CSS variableType
--trigger-anchor-pointstring

PopoverTrigger

data-slot="popover-trigger"

PropType
defaultOpen?boolean

Whether the overlay is open by default (uncontrolled).

onOpenChange?(isOpen: boolean) → void

Handler that is called when the overlay's open state changes.

Parameters

isOpen – boolean

Returns
void
isOpen?boolean

Whether the overlay is open by default (controlled).

PopoverHeader

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

PropType
className?string

Merged with the part's classes.

PopoverTitle

data-slot="popover-title"

PropType
level?number

The heading level.

Default
3
render?DOMRenderFunction<E, T>

Overrides the default DOM element with a custom render function. This allows rendering existing components with built-in styles and behaviors such as router links, animation libraries, and pre-styled components. Requirements: - You must render the expected element type (e.g. if <button> is expected, you cannot render an <a>). - Only a single root DOM element can be rendered (no fragments). - You must pass through props and ref to the underlying DOM element, merging with your own prop as appropriate.

Type
DOMRenderFunction<E, T>
className?string

Merged with the part's classes.

PopoverDescription

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

PropType
className?string

Merged with the part's classes.