Skip to the content
human-kit design
34 — Overlays

Popover

A small panel that opens from a button, for a few fields or a closer look.

Installation

pnpm dlx @human-kit/design add popover
npx @human-kit/design add popover
yarn dlx @human-kit/design add popover
bunx @human-kit/design add popover

The command copies the files to src/lib/components/ui/popover. It also installs the packages that the files import.

Usage

<script lang="ts">
	import * as Popover from '$lib/components/ui/popover';
</script>

<Popover.Root>
	<Popover.Trigger>Adjust dose</Popover.Trigger>
	<Popover.Content>
		<Popover.Header>
			<Popover.Title>Daily dose</Popover.Title>
		</Popover.Header>
	</Popover.Content>
</Popover.Root>

Examples

A small form

Edit one or two values in place, without leaving the page. Bind open to close the panel from its own buttons once the change is saved.

From an icon

A trigger takes the sizes of a button: size="icon-sm" and variant="ghost" make a quiet one beside a value. Give it an aria-label, since it has no text.

AMH 1.8 ng/mL

Guidelines

  • Use it for a few controls or a short explanation tied to one element: a filter, a quick edit, the meaning of a value.
  • For a list of actions, use a Menu.Root. For a hint of a few words on hover, a Tooltip.Root. For a task with several steps, or one that must be finished, a Dialog.Root.
  • Give the panel a Popover.Title, even a short one: it names the panel for a screen reader.
  • Keep it modal, the default, when the panel holds a form. Use nonModal for one that is read beside the page, so the page still scrolls and takes clicks.
  • Leave placement at bottom-start unless the trigger sits at an edge: the panel flips on its own when there is no room.

Accessibility

  • The trigger is a <button> with aria-haspopup="dialog" and aria-expanded; it stays pressed while the panel is open.
  • The panel is a role="dialog", named by its Popover.Title and described by its Popover.Description, unless you give aria-label or aria-labelledby.
  • Modal, the focus moves into the panel and stays there until it closes; then it goes back to the trigger.
  • Escape and a press outside close it. Each can be turned off, with shouldCloseOnEscape and shouldCloseOnInteractOutside.
  • A trigger with an icon and no text needs an aria-label.

API reference

Popover.Root

PropTypeDefault
openboolean

Whether the panel is open. Bindable.

—
defaultOpenboolean

The open state at the start, when you give no open.

false
onOpenChange(open: boolean, details: PopoverOpenChangeDetails) => void

Called when it opens or closes, with the reason. details.cancel() refuses the change.

—
controlledOpenboolean

Stops writing back to open: it reports through onOpenChange only, so the parent decides.

false
triggerRefHTMLElement | null

The element the panel opens from, when it is not a Popover.Trigger. Bindable.

—

The root holds the state and draws nothing of its own.

Popover.Trigger

PropTypeDefault
variant'default' | 'outline' | 'filled' | 'ghost' | 'link'

The look of the button.

'outline'
size'sm' | 'default' | 'lg' | 'icon' | 'icon-lg' | 'icon-sm' | 'icon-xs' | 'icon-2xs'

The size of the button. The icon sizes make a square for an icon alone.

'default'
elementHTMLButtonElement | null

The rendered element. Bindable.

—

Plus every attribute of a native <button>.

Popover.Content

PropTypeDefault
placementExtendedPlacement

Where the panel sits against the trigger: bottom, top-end, right, and the rest.

'bottom-start'
offsetnumber

The distance from the trigger, in pixels.

4
shouldFlipboolean

Moves the panel to the other side when there is no room.

true
boundaryElementElement | null

The element whose edges the panel keeps within.

—
nonModalboolean

Lets the page be used while the panel is open: the focus is not held, and the page scrolls.

false
shouldCloseOnEscapeboolean

Closes the panel on Escape.

true
shouldCloseOnInteractOutsideboolean

Closes the panel on a press outside it.

true
shouldCloseOnBlurboolean

Closes the panel when the focus leaves it. On by default when nonModal.

—
initialFocusHTMLElement | string | (() => HTMLElement | null | undefined)

The element, or its selector, that takes the focus first when modal.

—

Plus every attribute of a <div>, on the panel.

Popover.Header

PropTypeDefault
childrenSnippet

The title and the description, kept close together over what the panel holds.

—
classstring

Classes for the element.

—

Plus every attribute of a <div>.

Popover.Title

PropTypeDefault
childrenSnippet

The name of the panel. The content is labelled by it.

—
classstring

Classes for the element.

—

Plus every attribute of an <h2>.

Popover.Description

PropTypeDefault
childrenSnippet

A line read after the name. The content is described by it.

—
classstring

Classes for the element.

—

Plus every attribute of a <p>.