Popover
61 lines · 1 file · MIT
A panel that opens from a control and holds a short form or a paragraph, in the menu’s surface.
import { Popover as BasePopover } from "@base-ui/react/popover";
import type { ReactElement, ReactNode } from "react";
import { cn } from "./cn";
/**
* A panel that opens from a control and holds more than a menu can: a short
* form, a few settings, a paragraph with a link. Same surface as `Menu`
* (12px radius, ring shadow, opens at once, fades out from its trigger), but
* it takes focus and stays open until Esc or a click away.
*
* `trigger` is the element that opens it (a Button); it keeps its own look.
*/
export function Popover({
trigger,
children,
align = "start",
side,
className,
open,
onOpenChange,
}: {
trigger: ReactElement;
children: ReactNode;
align?: "start" | "center" | "end";
side?: "top" | "right" | "bottom" | "left";
/** The panel's own sizing; it is `w-72` by default. */
className?: string;
open?: boolean;
onOpenChange?: (open: boolean) => void;
}) {
return (
<BasePopover.Root open={open} onOpenChange={onOpenChange}>
<BasePopover.Trigger render={trigger} />
<BasePopover.Portal>
<BasePopover.Positioner sideOffset={6} align={align} side={side} className="z-50">
<BasePopover.Popup
className={cn(
"w-72 max-w-[calc(100vw-2rem)] origin-[var(--transform-origin)] rounded-xl bg-surface p-3 text-sm text-text smooth-shadow-ring-xl outline-none transition-[opacity,scale,filter] duration-150 ease-out data-[ending-style]:scale-[0.98] data-[ending-style]:opacity-0 data-[ending-style]:blur-[4px] data-[starting-style]:transition-none motion-reduce:transition-none",
className,
)}
>
{children}
</BasePopover.Popup>
</BasePopover.Positioner>
</BasePopover.Portal>
</BasePopover.Root>
);
}
export function PopoverTitle({ children }: { children: ReactNode }) {
return <BasePopover.Title className="text-sm font-semibold">{children}</BasePopover.Title>;
}
export function PopoverDescription({ children }: { children: ReactNode }) {
return (
<BasePopover.Description className="mt-1 text-xs text-pretty text-muted">
{children}
</BasePopover.Description>
);
}
Install
One command. The files land in your repo, with everything they import.
$ bunx shadcn@latest add @blocco/popover- Category
- Overlays
- Files
- popover.tsx
- Brings in
- @base-ui/react