A popover is an overlay element positioned relative to a trigger.
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 {Popover} from '@react-spectrum/s2/Popover';
import {Button} from '@react-spectrum/s2/Button';
import {style} from '@react-spectrum/s2/style' with {type: 'macro'};
import {useState, useRef} from 'react';
function Example() {
let [isOpen, setOpen] = useState(false);
let triggerRef = useRef(null);
return (
<div className={style({display: 'flex', flexDirection: 'column', gap: 64, alignItems: 'center'})}>
<Button onPress={() => setOpen(!isOpen)}>
Open popover
</Button>
<div
ref={triggerRef}
className={style({
padding: 8,
backgroundColor: 'gray-100',
borderRadius: 'default',
font: 'ui'
})}>
Popover appears here
</div>
<Popover
triggerRef={triggerRef}
isOpen={isOpen}
onOpenChange={setOpen}>
<div className={style({font: 'ui'})}>
Popover with custom trigger reference
</div>
</Popover>
</div>
);
}
<Example />
API
<DialogTrigger>
<Button />
<Popover>
{/* ... */}
</Popover>
</DialogTrigger>
DialogTrigger
| 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
| Name | Type | Default |
|---|---|---|
children | ReactNode | Default: — |
The children of the popover. | ||
padding | 'default' | 'none' | Default: 'default'
|
The amount of padding around the contents of the dialog. | ||
styles | PopoverStylesProp | Default: — |
Spectrum-defined styles, returned by the | ||
hideArrow | boolean | Default: false
|
Whether a popover's arrow should be hidden. | ||
size | 'S'
| 'M'
| 'L' | Default: — |
The size of the Popover. If not specified, the popover fits its contents. | ||
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. | ||
placement | Placement | Default: 'bottom'
|
The placement of the element with respect to its anchor 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. | ||
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. | ||
isEntering | boolean | Default: — |
Whether the dialog is currently performing an entry animation. Focus is not moved into the dialog until the animation completes, since moving the screen reader cursor may cause scrolling that interrupts the animation. | ||
isOpen | boolean | Default: — |
Whether the overlay is open by default (controlled). | ||
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. | ||
role | 'dialog' | 'alertdialog' | Default: 'dialog'
|
The accessibility role for the dialog. | ||
id | string | Default: — |
The element's unique identifier. See MDN. | ||
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. | ||
UNSAFE_className | UnsafeClassName | Default: — |
Sets the CSS className
for the element. Only use as a last resort. Use the | ||
UNSAFE_style | CSSProperties | Default: — |
Sets inline style for the
element. Only use as a last resort. Use the | ||
slot | string | null | Default: — |
A slot name for the component. Slots allow the component to receive props from a parent
component. An explicit | ||