Popover
An anchored popover on Base UI Popover.
For a little more detail or a small form tied to one control. It does not block the page: a click outside closes it, and so does moving focus away.
Default
import { Button } from "@/components/ui/button"
import {
Popover,
PopoverContent,
PopoverDescription,
PopoverHeader,
PopoverTitle,
PopoverTrigger,
} from "@/components/ui/popover"
export default function PopoverDefault() {
return (
<Popover>
<PopoverTrigger render={<Button variant="outline" />}>Billing period</PopoverTrigger>
<PopoverContent>
<PopoverHeader>
<PopoverTitle>1 to 30 November</PopoverTitle>
<PopoverDescription>
Invoices are issued on the last day of the period and are due 30 days later.
</PopoverDescription>
</PopoverHeader>
</PopoverContent>
</Popover>
)
}
With a form
"use client"
import { useState } from "react"
import { Button } from "@/components/ui/button"
import { Field, FieldLabel } from "@/components/ui/field"
import { Input } from "@/components/ui/input"
import {
Popover,
PopoverContent,
PopoverDescription,
PopoverHeader,
PopoverTitle,
PopoverTrigger,
} from "@/components/ui/popover"
export default function PopoverWithAForm() {
const [open, setOpen] = useState(false)
const [estimate, setEstimate] = useState("6")
return (
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger render={<Button variant="outline" />}>Estimate: {estimate} hours</PopoverTrigger>
<PopoverContent align="start">
<PopoverHeader>
<PopoverTitle>Estimate</PopoverTitle>
<PopoverDescription>How long the task should take, in hours.</PopoverDescription>
</PopoverHeader>
<form
className="flex flex-col gap-3"
onSubmit={(event) => {
event.preventDefault()
const next = new FormData(event.currentTarget).get("hours")
if (typeof next === "string" && next) setEstimate(next)
setOpen(false)
}}
>
<Field>
<FieldLabel htmlFor="estimate-hours">Hours</FieldLabel>
<Input id="estimate-hours" name="hours" type="number" min={1} max={80} defaultValue={estimate} required />
</Field>
<Button type="submit">Save estimate</Button>
</form>
</PopoverContent>
</Popover>
)
}
Placement
import { Button } from "@/components/ui/button"
import {
Popover,
PopoverContent,
PopoverDescription,
PopoverHeader,
PopoverTitle,
PopoverTrigger,
} from "@/components/ui/popover"
const placements = [
{ side: "top", align: "start", label: "Top, start" },
{ side: "right", align: "center", label: "Right, centre" },
{ side: "bottom", align: "end", label: "Bottom, end" },
{ side: "left", align: "center", label: "Left, centre" },
] as const
export default function PopoverPlacement() {
return (
<div className="flex flex-wrap items-center justify-center gap-2">
{placements.map(({ side, align, label }) => (
<Popover key={side}>
<PopoverTrigger render={<Button variant="outline" />}>{label}</PopoverTrigger>
<PopoverContent side={side} align={align} className="w-56">
<PopoverHeader>
<PopoverTitle>{label}</PopoverTitle>
<PopoverDescription>
Opens on the {side} side of its trigger. It moves to another side when this one has no room.
</PopoverDescription>
</PopoverHeader>
</PopoverContent>
</Popover>
))}
</div>
)
}
Installation
npx shadcn@latest add @khaeli/popoverbunx --bun shadcn@latest add @khaeli/popoverInstall @khaeli/foundation first: this item's styling lives in its tokens and [data-slot] rules.
Installs with
- Registry items
- npm packages
- @base-ui/react ^1.7.0
Keyboard
EnterSpace- On the trigger, opens the popover. Focus moves to the first field or button inside it, or to the popover itself when it has neither.
Tab- Moves through the controls inside. After the last one focus goes on to the next control on the page and the popover closes.
Escape- Closes the popover and returns focus to the trigger.
Accessible name
- The popover has the role
dialog. It is named byPopoverTitleand described byPopoverDescription; without a title givePopoverContentanaria-label. - The trigger takes your button through
renderand says that it opens a dialog and whether it is open (aria-haspopup,aria-expanded). Its name is the button's text. sideandalignare where the popover would like to open. It moves to another side when that one has no room.
Registry JSON
The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/popover.json