Popover
Content that floats over the page, anchored to the button that opens it.
Renders a native popover, so opening, closing, Escape and clicking away all
come from the platform — there is no JavaScript in this component.
import { Popover } from "antlitz";
Any button opens it by naming its id in popoverTarget, a native attribute
that Button passes through.
<Button popoverTarget="filters">Filters</Button>
<Popover id="filters">…</Popover>
API
| Prop | Type | Default |
|---|---|---|
| align | "start" | "center" | "end" | start |
| id | string | — |
| popover | "auto" | "manual" | "hint" | auto |
| radius | "none" | "sm" | "md" | "lg" | "full" | none |
| side | "top" | "bottom" | "left" | "right" | bottom |
| size | "sm" | "md" | "lg" | md |
| variant | "outline" | "soft" | "solid" | outline |
id is required because the trigger points at it. popover is the native
mode: auto closes when the reader clicks elsewhere, presses Escape or opens
another popover; manual stays open until something closes it; hint
behaves like auto, except that opening one leaves an open auto popover
alone — the mode meant for tooltips.
Usage
export function Basic() {
return (
<>
<Button variant="outline" popoverTarget="popover-basic">
Open
</Button>
<Popover id="popover-basic">
Anchored to the button that opened it.
</Popover>
</>
);
}The trigger is announced as expanded or collapsed without an aria-expanded
written by hand, and Tab moves from the trigger into the open popover, so it
can be reached from the keyboard however far apart the two sit in the
markup.
Placement
export function Sides() {
return SIDES.map((side) => (
<div key={side}>
<Button variant="outline" popoverTarget={`popover-side-${side}`}>
{side}
</Button>
<Popover id={`popover-side-${side}`} side={side}>
Opens on the {side}.
</Popover>
</div>
));
}side is physical — top, right, bottom, left.
export function Alignment() {
return ALIGNS.map((align) => (
<div key={align}>
<Button variant="outline" popoverTarget={`popover-align-${align}`}>
{align}
</Button>
<Popover id={`popover-align-${align}`} align={align} className="w-56">
Aligned to the {align} of its trigger.
</Popover>
</div>
));
}align follows the writing direction: start lines the popover up with the
trigger's leading edge, which is its right edge in a right-to-left page. When
there is no room on the chosen side, the popover flips to the opposite one.
Placement uses CSS anchor positioning. A browser without it opens the popover centred in the viewport, as a native popover does on its own — still usable, only not beside its trigger.
To open one from script, pass the trigger as the source — without it the
browser has nothing to anchor the popover to.
popover.showPopover({ source: button });
Variants
export function Variants() {
return VARIANTS.map((variant) => (
<div key={variant}>
<Button variant="outline" popoverTarget={`popover-variant-${variant}`}>
{variant}
</Button>
<Popover id={`popover-variant-${variant}`} variant={variant}>
A {variant} popover.
</Popover>
</div>
));
}Sizes
export function Sizes() {
return SIZES.map((size) => (
<div key={size}>
<Button variant="outline" popoverTarget={`popover-size-${size}`}>
{size}
</Button>
<Popover id={`popover-size-${size}`} size={size}>
Padding {size}.
</Popover>
</div>
));
}Size sets padding. The width follows the content; set one with className
when the content should wrap at a fixed measure.
Radius
export function Radius() {
return RADII.map((radius) => (
<div key={radius}>
<Button
variant="outline"
radius={radius}
popoverTarget={`popover-radius-${radius}`}
>
{radius}
</Button>
<Popover id={`popover-radius-${radius}`} radius={radius}>
Radius {radius}.
</Popover>
</div>
));
}Composition
export function Composition() {
return (
<>
<Button variant="outline" popoverTarget="popover-filters">
Filters
</Button>
<Popover id="popover-filters" className="w-64">
<Fieldset legend="Status">
<Field orientation="horizontal" label="Open">
<Checkbox name="status" value="open" defaultChecked />
</Field>
<Field orientation="horizontal" label="Closed">
<Checkbox name="status" value="closed" />
</Field>
</Fieldset>
<div className="mt-4 flex justify-end">
<Button
size="sm"
popoverTarget="popover-filters"
popoverTargetAction="hide"
>
Done
</Button>
</div>
</Popover>
</>
);
}A button inside the popover with popoverTargetAction="hide" closes it, so a
panel can carry its own Done or Cancel without any state.
Popover is not a menu. role="menu" promises arrow-key navigation between
items, which takes script; a popover of plain buttons and links is reached
with Tab and needs no role.
Manual
export function Manual() {
return (
<>
<Button
variant="outline"
popoverTarget="popover-manual"
popoverTargetAction="show"
>
Show
</Button>
<Button
variant="outline"
popoverTarget="popover-manual"
popoverTargetAction="hide"
>
Hide
</Button>
<Popover id="popover-manual" popover="manual" className="w-64">
Stays open until hidden. Clicking elsewhere or pressing Escape leaves it
alone.
</Popover>
</>
);
}popoverTargetAction also takes show, so separate buttons can open and
close a manual popover.