A popover is an overlay element positioned relative to a trigger.
Custom trigger
DialogTrigger works with any pressable React Aria component (e.g. Button, Link, etc.). Use the <Pressable> component or usePress hook to wrap a custom trigger element such as a third party component or DOM element.
import {Pressable, DialogTrigger} from 'react-aria-components/Popover';
import {Popover} from './Popover';
<DialogTrigger>
<Pressable>
<span role="button">Custom trigger</span>
</Pressable>
<Popover style={{padding: 16}}>
This popover was triggered by a custom button.
</Popover>
</DialogTrigger>
Accessibility
<Pressable> child must have an interactive ARIA role or use an appropriate semantic HTML element so that screen readers can announce the trigger. Trigger components must forward their ref and spread all props to a DOM element.const CustomTrigger = React.forwardRef((props, ref) => (
<button {...props} ref={ref} />
));
Custom anchor
To position a popover relative to a different element than its trigger, use the triggerRef and isOpen props instead of <DialogTrigger>. onOpenChange will be called when the user closes the popover.
import {useState, useRef} from 'react';
import {Popover} from './Popover';
import {Button} from './Button';
function Example() {
let [isOpen, setOpen] = useState(false);
let triggerRef = useRef(null);
return (
<div>
<Button onPress={() => setOpen(true)}>Trigger</Button>
<span ref={triggerRef} style={{paddingLeft: 12}}>Popover will be positioned relative to me</span>
<Popover
style={{padding: 16}}
triggerRef={triggerRef}
isOpen={isOpen}
onOpenChange={setOpen}>
I'm over here!
</Popover>
</div>
);
}
Restoring focus
By default, a popover restores focus to the trigger element when it closes. Use the restoreFocus prop to restore focus to a specific ref instead, or pass a function to choose a target dynamically. See FocusScope for more details.
import {useRef} from 'react';
import {DialogTrigger} from 'react-aria-components/Popover';
import {Popover} from './Popover';
import {TextField} from './TextField';
import {Button} from './Button';
import {Switch} from './Switch';
function Example() {
let inputRef = useRef<HTMLInputElement>(null);
return (
<div style={{display: 'flex', gap: 8, alignItems: 'end'}}>
<TextField label="Search" placeholder="Search products" inputRef={inputRef} />
<DialogTrigger>
<Button>Filters</Button>
<Popover restoreFocus={inputRef} style={{padding: 16}}>
<div style={{display: 'flex', flexDirection: 'column', gap: 12}}>
<Switch defaultSelected>In stock</Switch>
<Switch>On sale</Switch>
</div>
</Popover>
</DialogTrigger>
</div>
);
}
Examples
API
<DialogTrigger>
<Button />
<Popover>
<OverlayArrow />
</Popover>
</DialogTrigger>
DialogTrigger
A DialogTrigger opens a dialog when a trigger element is pressed.
| Name | Type | |
|---|---|---|
children | ReactNode | |
isOpen | boolean | |
Whether the overlay is open by default (controlled). | ||
defaultOpen | boolean | |
Whether the overlay is open by default (uncontrolled). | ||
onOpenChange | | |
Handler that is called when the overlay's open state changes. | ||
Popover
A popover is an overlay element positioned relative to a trigger.
| Name | Type | Default |
|---|---|---|
trigger | string | Default: — |
The name of the component that triggered the popover. This is reflected on the element
as the | ||
triggerRef | RefObject | Default: — |
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. | ||
isEntering | boolean | Default: — |
Whether the popover is currently performing an entry animation. | ||
isExiting | boolean | Default: — |
Whether the popover is currently performing an exit animation. | ||
shouldSkipAnimation | boolean | Default: — |
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. | ||
restoreFocus | RefObject | Default: — |
Where focus goes when the popover closes. By default, focus returns to the element that was
focused when the popover opened. Pass a ref or a function to choose another element, for
example when that element was removed while the popover was open. A function receives the
current interaction modality and the element focus is about to return to by default, or
| ||
placement | Placement | Default: 'bottom'
|
The placement of the element with respect to its anchor element. | ||
arrowRef | RefObject | Default: — |
A ref for the popover arrow element. | ||
isNonModal | boolean | Default: — |
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. | ||
isKeyboardDismissDisabled | boolean | Default: false
|
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. | ||
shouldCloseOnInteractOutside | | Default: — |
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. | ||
boundaryElement | Element | Default: document.body
|
Element that that serves as the positioning boundary. | ||
scrollRef | RefObject | Default: overlayRef
|
A ref for the scrollable region within the overlay. | ||
shouldUpdatePosition | boolean | Default: true
|
Whether the overlay should update its position automatically. | ||
maxHeight | number | Default: — |
The maxHeight specified for the overlay element. By default, it will take all space up to the current viewport height. | ||
arrowBoundaryOffset | number | Default: 0
|
The minimum distance the arrow's edge should be from the edge of the overlay element. | ||
getTargetRect | | Default: target.getBoundingClientRect()
|
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. | ||
children | ChildrenOrFunction | Default: — |
The children of the component. A function may be provided to alter the children based on component state. | ||
isOpen | boolean | Default: — |
Whether the overlay is open by default (controlled). | ||
defaultOpen | boolean | Default: — |
Whether the overlay is open by default (uncontrolled). | ||
onOpenChange | | Default: — |
Handler that is called when the overlay's open state changes. | ||
shouldFlip | boolean | Default: true
|
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. | ||
offset | number | Default: 8
|
The additional offset applied along the main axis between the element and its anchor element. | ||
crossOffset | number | Default: 0
|
The additional offset applied along the cross axis between the element and its anchor element. | ||
containerPadding | number | Default: 12
|
The placement padding that should be applied between the element and its surrounding container. | ||
onFocusWithin | | Default: — |
Handler that is called when the target element or a descendant receives focus. | ||
onBlurWithin | | Default: — |
Handler that is called when the target element and all descendants lose focus. | ||
onFocusWithinChange | | Default: — |
Handler that is called when the the focus within state changes. | ||
style | StyleOrFunction | Default: — |
The inline style for the element. A function may be provided to compute the style based on component state. | ||
className | ClassNameOrFunction | Default: 'react-aria-Popover'
|
The CSS className for the element. A function may be provided to compute the class based on component state. | ||
aria-label | string | Default: — |
Defines a string value that labels the current element. | ||
aria-labelledby | string | Default: — |
Identifies the element (or elements) that labels the current element. | ||
aria-describedby | string | Default: — |
Identifies the element (or elements) that describes the object. | ||
aria-details | string | Default: — |
Identifies the element (or elements) that provide a detailed, extended description for the object. | ||
slot | string | null | Default: — |
A slot name for the component. Slots allow the component to receive props from a parent
component. An explicit | ||
render | DOMRenderFunction | Default: — |
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:
| ||
Default className: react-aria-Popover
| Render Prop | CSS Selector |
|---|---|
trigger | CSS Selector: [data-trigger="..."]
|
| The name of the component that triggered the popover, e.g. "DialogTrigger" or "ComboBox". | |
placement | CSS Selector: [data-placement="left | right | top | bottom"]
|
| The placement of the popover relative to the trigger. | |
isEntering | CSS Selector: [data-entering]
|
| Whether the popover is currently entering. Use this to apply animations. | |
isExiting | CSS Selector: [data-exiting]
|
| Whether the popover is currently exiting. Use this to apply animations. | |
| CSS Variable |
|---|
--trigger-width |
| The width of the popover trigger element. |
--trigger-anchor-point |
The position of the trigger relative to the popover. Use with transform-origin when applying scale transitions. |
OverlayArrow
An OverlayArrow renders a custom arrow element relative to an overlay element such as a popover or tooltip such that it aligns with a trigger element.
| Name | Type | |
|---|---|---|
children | ChildrenOrFunction | |
The children of the component. A function may be provided to alter the children based on component state. | ||
style | StyleOrFunction | |
The inline style for the element. A function may be provided to compute the style based on component state. | ||
className | ClassNameOrFunction | |
The CSS className for the element. A function may be provided to compute the class based on component state. | ||
id | string | |
The element's unique identifier. See MDN. | ||
render | DOMRenderFunction | |
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:
| ||
Default className: react-aria-OverlayArrow
| Render Prop | CSS Selector |
|---|---|
placement | CSS Selector: [data-placement="left | right | top | bottom"]
|
| The placement of the overlay relative to the trigger. | |