Components
Popover
A small panel that opens next to the element that triggered it, for extra content or a short form.
Introduction
A popover shows a little more, right where the user is looking, and gets out of the way when they are done. It suits extra details, a preview, or a short form such as renaming a file, things that would be too much to always show on the page.
- It opens on the side of the trigger you choose, and flips to the other side when there is no room.
- Focus moves into it and returns to the trigger when it closes. Escape and a click outside close it. A screen reader announces it as a dialog, named by its title.
- Any content: text, a form, a list of controls. It has an optional arrow, four sizes and a fade-in.
- Open it from a button, or control it from your own state.
For a list of actions, use Navbar dropdowns or a menu. A popover is for content, not for commands.
Installation
centerUI has no CLI or registry yet: components are copied into your project. Popover uses Button for its close button, so it needs everything Button needs.
# bun
bun add react-aria-components tailwind-variants tailwind-merge clsx
# pnpm
pnpm add react-aria-components tailwind-variants tailwind-merge clsx
# npm
npm install react-aria-components tailwind-variants tailwind-merge clsxAdd Button first (see its Manual installation), then follow the steps below.
Manual installation
Copy these files into your project and keep the paths, or adjust the imports.
1. Add the styles
src/types/popover-styles.tsShow code
import { tv } from "tailwind-variants";
export const popoverStyles = tv({
slots: {
// The floating panel. `popover` is the class that gives it its small
// entrance animation (see globals.css).
popover:
"popover z-50 max-w-[calc(100vw-1rem)] rounded-xl border bg-card text-card-fg shadow-lg",
content: "flex flex-col gap-3 p-4 outline-none",
title: "m-0 text-sm leading-snug font-semibold",
description: "m-0 text-sm leading-snug text-muted-fg",
// The little triangle that points at the trigger. It is drawn pointing
// down (for a popover above its trigger) and turned for the other sides.
arrow: [
"block fill-card stroke-border",
"group-data-[placement=bottom]:rotate-180",
"group-data-[placement=left]:-rotate-90",
"group-data-[placement=right]:rotate-90",
],
},
variants: {
size: {
sm: { popover: "w-56" },
md: { popover: "w-72" },
lg: { popover: "w-96" },
auto: { popover: "w-auto" },
},
},
defaultVariants: { size: "md" },
});2. Add the Popover file
src/components/ui/popover/popover.tsxShow code
"use client";
import { type ReactNode, useContext, useEffect, useRef } from "react";
import {
Popover as AriaPopover,
type PopoverProps as AriaPopoverProps,
Dialog,
DialogTrigger,
type DialogTriggerProps,
Heading,
OverlayArrow,
OverlayTriggerStateContext,
} from "react-aria-components";
import { Button, type ButtonProps } from "@/components/ui/button/button";
import { cn } from "@/lib/cn";
import { popoverStyles } from "@/types/popover-styles";
export type PopoverProps = DialogTriggerProps;
/**
* A small panel that opens next to the element that triggered it, for extra
* content or a short form. Put the trigger (a `Button`) and a `PopoverContent`
* inside it; the trigger opens the content when pressed. Built on react-aria's
* DialogTrigger, so focus moves into the panel and comes back to the trigger,
* Escape and a click outside close it, and a screen reader announces it as a
* dialog.
*
* Use it for something the user asks for. For a list of actions use a menu, and
* for a message that needs an answer use a modal dialog.
*
* @example
* <Popover>
* <Button>Details</Button>
* <PopoverContent>
* <PopoverTitle>Shipping</PopoverTitle>
* <PopoverDescription>Arrives in 2 to 3 days.</PopoverDescription>
* </PopoverContent>
* </Popover>
*/
export function Popover(props: PopoverProps) {
return <DialogTrigger {...props} />;
}
export interface PopoverContentProps
extends Omit<
AriaPopoverProps,
"children" | "className" | "style" | "trigger"
> {
/** How wide the panel is. `auto` fits its content (up to the screen). */
size?: "sm" | "md" | "lg" | "auto";
/** Point a small arrow at the trigger. */
showArrow?: boolean;
/** Names the panel when it has no `PopoverTitle`. */
"aria-label"?: string;
/** Classes for the padded area inside the panel. */
className?: string;
/** The content, or a function that receives `close`. */
children: ReactNode | ((options: { close: () => void }) => ReactNode);
}
/**
* The panel of a `Popover`. It opens on the side of the trigger given by
* `placement`, and flips to the other side when there is not enough room.
*/
export function PopoverContent({
size,
showArrow = false,
placement = "bottom",
offset,
"aria-label": ariaLabel,
className,
children,
...props
}: PopoverContentProps) {
const styles = popoverStyles({ size });
const ref = useRef<HTMLElement>(null);
const state = useContext(OverlayTriggerStateContext);
// A non-modal popover is not closed by a click outside of it by react-aria,
// because the page behind it is meant to stay usable. A tip should still go
// away when the user goes on with the page, so close it here. The press
// still reaches whatever was clicked.
useEffect(() => {
if (!props.isNonModal || !state?.isOpen) return;
const closeOnOutsidePress = (event: PointerEvent) => {
const target = event.target as Element;
if (ref.current?.contains(target)) return;
// The trigger closes it by itself, and closing here would reopen it.
if (target.closest?.('[aria-expanded="true"]')) return;
state.close();
};
document.addEventListener("pointerdown", closeOnOutsidePress, true);
return () =>
document.removeEventListener("pointerdown", closeOnOutsidePress, true);
}, [props.isNonModal, state]);
return (
<AriaPopover
{...props}
ref={ref}
placement={placement}
// Leave room for the arrow.
offset={offset ?? (showArrow ? 12 : 8)}
className={styles.popover()}
>
{showArrow ? (
<OverlayArrow className="group">
<svg
aria-hidden="true"
width={12}
height={12}
viewBox="0 0 12 12"
className={styles.arrow()}
>
<path d="M0 0 L6 6 L12 0" />
</svg>
</OverlayArrow>
) : null}
<Dialog
aria-label={ariaLabel}
className={cn(styles.content(), className)}
>
{children}
</Dialog>
</AriaPopover>
);
}
/** The heading of a popover. It also names the popover for screen readers. */
export function PopoverTitle({
className,
children,
}: {
className?: string;
children?: ReactNode;
}) {
return (
<Heading slot="title" className={cn(popoverStyles().title(), className)}>
{children}
</Heading>
);
}
/** A line of supporting text under the title. */
export function PopoverDescription({
className,
children,
}: {
className?: string;
children?: ReactNode;
}) {
return (
<p className={cn(popoverStyles().description(), className)}>{children}</p>
);
}
/**
* A button that closes the popover it is in. It is a `Button`, so it takes the
* same variants and sizes.
*/
export function PopoverClose({ onPress, ...props }: ButtonProps) {
const state = useContext(OverlayTriggerStateContext);
return (
<Button
{...props}
onPress={(event) => {
onPress?.(event);
state?.close();
}}
/>
);
}3. Add the popover styles
In src/app/globals.css copy the popover-in keyframes and the .popover[data-entering] rule (with its prefers-reduced-motion override). Without them the popover still works, it just appears without the small fade.
4. Use it
import {
Popover,
PopoverClose,
PopoverContent,
PopoverDescription,
PopoverTitle,
} from "@/components/ui/popover/popover";Anatomy
A trigger and its content, held together by Popover.
<Popover>
<Button>Details</Button>
<PopoverContent placement="bottom" showArrow>
<PopoverTitle>Shipping</PopoverTitle>
<PopoverDescription>Arrives in 2 to 3 days.</PopoverDescription>
<PopoverClose size="sm">Got it</PopoverClose>
</PopoverContent>
</Popover>| Part | Rendered as | Notes |
|---|---|---|
| Popover | no element | Connects the trigger to the content and holds whether it is open. |
| Trigger | Button | Any Button. Pressing it opens the popover, and it reports whether the popover is open (aria-expanded) to screen readers. |
| PopoverContent | div (dialog) | The floating panel, with an entrance animation. It sits next to the trigger and flips when there is no room. |
| Arrow | svg | An optional triangle that points at the trigger. |
| PopoverTitle / PopoverDescription | h2 / p | A heading and a line of text. The title names the dialog for screen readers. |
| PopoverClose | Button | A button that closes the popover. |
The trigger must be a Button (or another react-aria component). Without a PopoverTitle, give the content an aria-label.
Examples
Live previews with code you can copy.
Usage
Placement
Which side of the trigger it opens on. If there is no room, it flips to the opposite side.
| placement | Where |
|---|---|
| top, bottom, left, right | Centered on that side of the trigger |
| top start, top end | Above, lined up with the start or the end of the trigger |
| bottom start, bottom end | Below, lined up with the start or the end of the trigger |
| left start, left end | To the left, lined up with the top or the bottom |
| right start, right end | To the right, lined up with the top or the bottom |
Alignment
Add start or end to line the popover up with an edge of the trigger instead of its middle.
Arrow
showArrow adds a small triangle that points at the trigger, on whichever side the popover opens.
Sizes
The width of the panel. auto fits its content, up to the width of the screen.
Icon trigger
A trigger with only an icon needs an aria-label.
Form
A short form in a popover. Give the content a function to receive close, and call it after saving. autoFocus on the field puts the cursor in it as soon as the popover opens.
File name: Untitled
Controlled
Keep whether it is open in your own state with isOpen and onOpenChange, so you can open or close it from somewhere else.
isOpen: false
Non-modal
By default a popover is modal: the rest of the page cannot be used while it is open, and a click outside only closes it. With isNonModal the page stays usable, so a click on another control both closes the popover and reaches that control. Use it for a tip that should not get in the way.
Component API
Popover and PopoverContent take the props below. PopoverContent also accepts the rest of what the react-aria Popover does.
Popover
| Prop | Type | Default |
|---|---|---|
| isOpen | boolean | - |
| defaultOpen | boolean | false |
| onOpenChange | (isOpen: boolean) => void | - |
PopoverContent
| Prop | Type | Default |
|---|---|---|
| placement | "top" | "bottom" | "left" | "right" | "top start" | "top end" | … | "bottom" |
| offset | number | 8 (12 with an arrow) |
| showArrow | boolean | false |
| size | "sm" | "md" | "lg" | "auto" | "md" |
| isNonModal | boolean | false |
| shouldFlip | boolean | true |
| aria-label | string | - (when there is no title) |
| children | ReactNode | ({ close }) => ReactNode | - |
PopoverTitle and PopoverDescription take only a className and their children. PopoverClose takes every Button prop.