Popover
A panel anchored to a button, for a short form, a picker or a few details: a share link, an estimate, a date.
Usage
import {
Popover,
PopoverContent,
PopoverTitle,
PopoverTrigger,
} from 'nyctoui/popover'
<Popover>
<PopoverTrigger render={<Button />}>Share</PopoverTrigger>
<PopoverContent>
<PopoverTitle>Share issue</PopoverTitle>
…
</PopoverContent>
</Popover>The popover opens on click, moves focus inside, and closes on Escape or a click outside, returning focus to the trigger. The page behind stays usable. For a choice from a list, use a Dropdown or a Select; for a label on hover, a Tooltip.
The content stacks its children 12px apart inside 12px of padding and sizes to fit them, up to 360px. Set a width through sx.
Good for
- A Share popover with a link and a Copy button.
- A quick filter form above a table, such as a date range and a status.
- An info icon beside a metric in a dashboard, explaining how it’s worked out.
- A “Snooze until” picker in an email client.
Anatomy
<Popover>
<PopoverTrigger />
<PopoverContent>
<PopoverHeader>
<PopoverTitle />
<PopoverDescription />
<PopoverCloseButton />
</PopoverHeader>
<PopoverFooter>
<PopoverClose />
</PopoverFooter>
</PopoverContent>
</Popover>Examples
Form
Control open to close the popover once the form is saved. Escape and PopoverClose cancel.
Infotip
For an explanation behind an info icon, add openOnHover to the popover. Unlike a tooltip, it still opens on tap and its text reaches screen readers.
Best practices
- Keep a popover to one small task or one piece of information.
- Give it a clear way to close: a click outside, Escape, and a
PopoverClosebutton when it holds a form. - Use
openOnHoverfor an info icon, so it opens on hover and still opens on tap.
- Open a popover from inside another popover.
- Use a popover to pick from a list. That’s a select or a dropdown.
- Fill it with a long form. If it needs scrolling, use a dialog or a drawer.
API reference
Popover
Holds the open state. Renders no element.
| Prop | Type | Default |
|---|---|---|
defaultOpenStarts open. For uncontrolled use. | boolean | false |
openWhether the popover is open. For controlled use. | boolean | — |
onOpenChangeCalled when the popover opens or closes. | (open: boolean, details) => void | — |
onOpenChangeCompleteCalled once the open or close animation has finished. | (open: boolean) => void | — |
modalTraps focus, locks page scroll and blocks the page. trap-focus keeps only the focus trap. | boolean | 'trap-focus' | false |
openOnHoverAlso opens on hover, for an infotip. | boolean | false |
delayMilliseconds of hover before opening, with openOnHover. | number | — |
PopoverTrigger
The button that opens the popover. Renders a <button>.
| Prop | Type | Default |
|---|---|---|
renderReplaces the rendered element, e.g. render={<Button />}. | ReactElement | function | — |
PopoverContent
The panel. Renders a <div> in the top layer.
| Prop | Type | Default |
|---|---|---|
sideWhich side of the trigger to open on. Flips when it would clip. | 'top' | 'right' | 'bottom' | 'left' | 'bottom' |
alignHow to line up with the trigger along that side. | 'start' | 'center' | 'end' | 'start' |
sideOffsetGap between the trigger and the panel, in pixels. | number | 6 |
alignOffsetShift along the alignment axis, in pixels. | number | 0 |
anchorPositions against another element than the trigger. | Element | string | { x, y } | null | — |
initialFocusThe element to focus on open. Defaults to the first focusable one. | 'auto' | 'content' | 'none' | string | Element | function | — |
finalFocusThe element to focus on close. Defaults to the trigger. | 'none' | string | Element | function | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
PopoverHeader
Lays out the title, description and close button. Renders a <div>.
| Prop | Type | Default |
|---|---|---|
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
PopoverTitle
Labels the popover. Renders an <h2>.
| Prop | Type | Default |
|---|---|---|
renderReplaces the rendered element, e.g. render={<Button />}. | ReactElement | function | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
PopoverDescription
Describes the popover to assistive tech. Renders a <p>.
| Prop | Type | Default |
|---|---|---|
renderReplaces the rendered element, e.g. render={<Button />}. | ReactElement | function | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
PopoverFooter
Actions aligned to the end. Renders a <div>.
| Prop | Type | Default |
|---|---|---|
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
PopoverClose
Closes the popover. Renders a <button>.
| Prop | Type | Default |
|---|---|---|
renderReplaces the rendered element, e.g. render={<Button />}. | ReactElement | function | — |
PopoverCloseButton
A ghost icon Button with an X that closes the popover.
| Prop | Type | Default |
|---|---|---|
aria-labelThe accessible name of the button. | string | 'Close' |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |