Popover

Slant / Overlays

A panel anchored to its trigger that flips to stay in view.

Demo

Usage

@tuttn/slant
import { Popover } from '@tuttn/slant'

// The root renders no element: it holds the state and ties the parts together.
<Popover placement="bottom-start">
  <Popover.Trigger>Filters</Popover.Trigger>
  <Popover.Content>
    <FilterFields />
    <Popover.Close variant="primary">Done</Popover.Close>
  </Popover.Content>
</Popover>

// It keeps its own open state, starting from defaultOpen; pass open and
// onOpenChange to control it.
<Popover open={open} onOpenChange={setOpen}>
  <Popover.Trigger asChild><a href="/share">Share</a></Popover.Trigger>
  <Popover.Content label="Share this task" initialFocus={linkField}>…</Popover.Content>
</Popover>

Props

PropTypeDefaultDescription
openboolean—Whether the panel is open. Pass it with onOpenChange to control the popover; leave it out and it keeps its own state.
defaultOpenbooleanfalseWhether the panel starts open, when uncontrolled.
onOpenChange(open: boolean) => void—Called with the new state whenever the popover opens or closes.
placementPlacement'bottom-start'The side of the trigger the panel sits on, and the edge it lines up with. Start and end follow the text direction. The panel flips to the other side when this one lacks room, and slides along it to stay in view.
offsetnumber6The gap between the trigger and the panel, in pixels.
childrenReactNode—Popover.Trigger and Popover.Content, at any depth. The popover renders no element of its own.
Popover.TriggerButtonProps—The Slant Button that opens and closes the panel; every Button prop works, asChild too. It carries aria-haspopup="dialog", aria-expanded and, while open, aria-controls. Your onClick runs first and can call preventDefault to stop the toggle. It is the anchor and where focus returns.
Popover.Content{ label?: string; initialFocus?: RefObject<HTMLElement | null> }—The panel (role="dialog"), rendered in a portal only while open. Named by label, else by the trigger’s text. initialFocus is where focus lands on opening; by default the first tabbable element, else the panel.
Popover.CloseButtonProps—A Slant Button that closes the panel and returns focus to the trigger. Your onClick runs first and can call preventDefault to keep it open.
…restHTMLAttributes<HTMLDivElement>—Popover itself renders nothing, so it takes no attributes. Each part forwards its ref, className, style and other attributes to its own element: the trigger’s button, the panel’s div, the close button.

Opening moves focus into the panel: to initialFocus, else the first tabbable element, else the panel itself. Escape closes it and returns focus to the trigger, as does Popover.Close. A press outside closes it and leaves focus where the press put it; focus moving anywhere outside closes it too. Tab moves through the panel as usual, and past its last control closes it and goes on to the control after the trigger (Shift+Tab before the first, to the control before it), so the portal costs nothing in the tab order.

The panel is not modal: the page stays live around it, nothing traps focus, and the scroll is not locked. For a decision the reader must make before going on, use a Dialog. Escape closes only the topmost layer, so a tooltip or menu open inside the panel closes first.

The panel renders in a portal at the end of the body, carrying its root’s theme, direction and density, so an overflow: hidden box or a stacking context around the trigger cannot clip it. It hangs from the side placement names, offset pixels away, flips to the other side when that one lacks room, slides along it to stay in view, and follows the trigger when anything scrolls. Start and end follow the text direction. Without open the popover keeps its own state, starting from defaultOpen.