Popover
A small panel that opens next to its trigger and holds a few fields, details or actions, while the page around it stays usable. For a question that must be answered first use a dialog; for a plain text hint use a tooltip; for a list of commands use a menu.
Install
npx shadcn@latest add https://thrumui.dev/r/popover.jsonAdds components/ui/popover.tsx and installs @base-ui/react@1.9.0. Nothing else in your project changes.
import { Popover } from "@/components/ui/popover";Props
| Prop | Type | Default | Description |
|---|---|---|---|
Popover: open | boolean | – | Whether it is open, when the parent holds the state. Use it with onOpenChange. |
Popover: defaultOpen | boolean | Default false | Whether it starts open, when the popover holds its own state. |
Popover: onOpenChange | (open: boolean) => void | – | Called when the panel opens or closes, whatever caused it. |
Popover: modal | boolean | "trap-focus" | Default false | true locks the page behind the panel and keeps the focus inside it. trap-focus only keeps the focus inside. |
Popover: sound | boolean | Default true | Play a soft sound on opening and on closing, if the visitor has sound on. |
PopoverTrigger | button props | – | The button that opens the panel. Style it with buttonClass(). |
PopoverTrigger: openOnHover | boolean | Default false | Also open when the pointer rests on the trigger for 300 ms. A press still opens it. |
PopoverContent: side | "top" | "right" | "bottom" | "left" | Default "bottom" | The side of the trigger the panel opens on. It flips to the opposite side when there is no room. |
PopoverContent: align | "start" | "center" | "end" | Default "center" | How the panel lines up with the trigger along that side. |
PopoverContent: sideOffset | number | Default 4 | The gap between the trigger and the panel, in pixels. With an arrow the default is 10. |
PopoverContent: alignOffset | number | Default 0 | Moves the panel along the trigger's side, in pixels. |
PopoverContent: collisionPadding | number | Default 8 | The space kept between the panel and the edge of the screen, in pixels. |
PopoverContent: anchor | Element | RefObject<Element> | – | Place the panel against this element instead of the trigger. |
PopoverContent: arrow | boolean | Default false | Draw a small arrow that points at the trigger. |
PopoverContent: className | string | – | Extra classes for the panel. w-96 makes it wider; the default is 288 pixels. |
PopoverHeader | ComponentProps<"div"> | – | Holds the title and the description close together. |
PopoverTitle | heading props | – | The panel's name, announced when it opens. |
PopoverDescription | paragraph props | – | The explanation under the title, read after the name. |
PopoverClose | button props | – | A button that closes the panel. Its own onClick runs first. |
Motion and sound
Motion
- The panel fades in and grows from 96% to full size over 150 ms, from the edge that touches the trigger.
- Closing plays the same change backwards, and a second press during it reverses it.
- Reduced motion: the panel appears and leaves at once.
Sound
- A soft rising tone when the panel opens.
- A soft falling tone when it closes, whichever way it was closed.
Sound is off until the visitor turns it on. Pass sound={false} to keep this component silent. How sound works
Accessibility
- It follows the WAI-ARIA dialog pattern in its non-modal form: role dialog, named by PopoverTitle and described by PopoverDescription.
- A panel with no PopoverTitle needs an aria-label on PopoverContent.
- The trigger carries aria-expanded and aria-haspopup, so a screen reader says that it opens something and whether it is open.
- Focus moves into the panel when it opens and goes back to the trigger when it closes. A press outside closes it.
- The panel never leaves the screen: it flips to the other side, shifts along the trigger, and narrows to the space there is.
- Under a glass material the panel is translucent. Its tint keeps the text at 4.5 to 1 over any page, and the arrow takes the same tint.
| Keys | Action |
|---|---|
| Enter / Space | On the trigger: open the panel, or close it when it is open. Focus moves to the first control inside. |
| Tab / Shift Tab | Move through the controls inside the panel. Past the last one, focus leaves and the panel closes. |
| Esc | Close the panel. Focus returns to the trigger. |
Source
"use client";
import type { ComponentProps } from "react";
import { Popover as PopoverPrimitive } from "@base-ui/react/popover";
import { cn } from "@/lib/utils";
import { play } from "@/lib/thrum-sound";
export type PopoverSide = "top" | "right" | "bottom" | "left";
export type PopoverAlign = "start" | "center" | "end";
export type PopoverProps = PopoverPrimitive.Root.Props & {
/** Play a soft sound when the panel opens and when it closes, if the visitor has sound on. */
sound?: boolean;
};
/**
* A small panel that opens next to its trigger and holds anything: a few fields, details, a short list of actions.
* The page stays usable around it; a press outside or Escape closes it. `Popover` holds the state,
* `PopoverTrigger` opens it, `PopoverContent` is the panel.
*/
export function Popover({ sound = true, onOpenChange, ...props }: PopoverProps) {
return (
<PopoverPrimitive.Root
onOpenChange={(open, details) => {
if (sound) play(open ? "open" : "close");
onOpenChange?.(open, details);
}}
{...props}
/>
);
}
/** The button that opens the panel. Style it with `buttonClass()` or pass a `Button` through `render`. */
export function PopoverTrigger(props: PopoverPrimitive.Trigger.Props) {
return <PopoverPrimitive.Trigger data-slot="popover-trigger" {...props} />;
}
/** A button that closes the panel. */
export function PopoverClose(props: PopoverPrimitive.Close.Props) {
return <PopoverPrimitive.Close data-slot="popover-close" {...props} />;
}
export type PopoverContentProps = PopoverPrimitive.Popup.Props &
Pick<PopoverPrimitive.Positioner.Props, "alignOffset" | "collisionPadding" | "anchor"> & {
/** The side of the trigger the panel opens on. It flips to the opposite side when there is no room. */
side?: PopoverSide;
/** How the panel lines up with the trigger along that side. */
align?: PopoverAlign;
/** The gap between the trigger and the panel, in pixels. The default is 4, or 10 with an arrow. */
sideOffset?: number;
/** Draw a small arrow that points at the trigger. */
arrow?: boolean;
};
/**
* The panel. It is placed against the trigger, kept inside the screen, and is 288 pixels wide unless `className` says otherwise.
* It is a floating surface, so it turns to glass when the page's material asks for it.
*/
export function PopoverContent({ side = "bottom", align = "center", sideOffset, alignOffset, collisionPadding = 8, anchor, arrow = false, className, children, ...props }: PopoverContentProps) {
return (
<PopoverPrimitive.Portal>
<PopoverPrimitive.Positioner
side={side}
align={align}
sideOffset={sideOffset ?? (arrow ? 10 : 4)}
alignOffset={alignOffset}
collisionPadding={collisionPadding}
anchor={anchor}
className="z-50 outline-none"
>
<PopoverPrimitive.Popup
data-slot="popover"
data-surface="floating"
className={cn(
"grid w-72 max-w-(--available-width) origin-(--transform-origin) gap-3 rounded-lg border bg-popover p-4 text-sm text-popover-foreground shadow-md outline-none",
"transition-[opacity,scale] duration-150 ease-out data-ending-style:scale-[0.96] data-ending-style:opacity-0 data-starting-style:scale-[0.96] data-starting-style:opacity-0 motion-reduce:transition-none",
className,
)}
{...props}
>
{arrow && (
// A square turned on its corner and cut along the panel's edge, so only the half outside the panel shows.
// It takes the panel's own colour, glass included.
<PopoverPrimitive.Arrow
data-slot="popover-arrow"
className={cn(
"size-2.5 border bg-inherit [clip-path:polygon(0_0,100%_0,0_100%)]",
"data-[side=bottom]:-top-1.5 data-[side=bottom]:rotate-45 data-[side=top]:-bottom-1.5 data-[side=top]:rotate-225",
"data-[side=right]:-left-1.5 data-[side=right]:-rotate-45 data-[side=left]:-right-1.5 data-[side=left]:rotate-135",
)}
/>
)}
{children}
</PopoverPrimitive.Popup>
</PopoverPrimitive.Positioner>
</PopoverPrimitive.Portal>
);
}
/** Title and description, set close together. */
export function PopoverHeader({ className, ...props }: ComponentProps<"div">) {
return <div data-slot="popover-header" className={cn("grid gap-1", className)} {...props} />;
}
/** The panel's name. Screen readers announce it when the panel opens. */
export function PopoverTitle({ className, ...props }: PopoverPrimitive.Title.Props) {
return <PopoverPrimitive.Title data-slot="popover-title" className={cn("text-sm leading-5 font-semibold", className)} {...props} />;
}
export function PopoverDescription({ className, ...props }: PopoverPrimitive.Description.Props) {
return <PopoverPrimitive.Description data-slot="popover-description" className={cn("text-sm text-muted-foreground", className)} {...props} />;
}Files and dependencies
- Adds
components/ui/popover.tsx- Packages
@base-ui/react@1.9.0- Brings along
- Sound engine
Changelog
- 1.0.0 · 2026-12-03First release.