Popover
A small panel that opens from a button, for a few fields or a closer look.
Installation
pnpm dlx @human-kit/design add popovernpx @human-kit/design add popoveryarn dlx @human-kit/design add popoverbunx @human-kit/design add popoverThe 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.
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, aTooltip.Root. For a task with several steps, or one that must be finished, aDialog.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
nonModalfor one that is read beside the page, so the page still scrolls and takes clicks. - Leave
placementatbottom-startunless the trigger sits at an edge: the panel flips on its own when there is no room.
Accessibility
- The trigger is a
<button>witharia-haspopup="dialog"andaria-expanded; it stays pressed while the panel is open. - The panel is a
role="dialog", named by itsPopover.Titleand described by itsPopover.Description, unless you givearia-labeloraria-labelledby. - Modal, the focus moves into the panel and stays there until it closes; then it goes back to the trigger.
Escapeand a press outside close it. Each can be turned off, withshouldCloseOnEscapeandshouldCloseOnInteractOutside.- A trigger with an icon and no text needs an
aria-label.
API reference
Popover.Root
| Prop | Type | Default |
|---|---|---|
open | boolean Whether the panel is open. Bindable. | — |
defaultOpen | boolean The open state at the start, when you give no | false |
onOpenChange | (open: boolean, details: PopoverOpenChangeDetails) => void Called when it opens or closes, with the reason. | — |
controlledOpen | boolean Stops writing back to | false |
triggerRef | HTMLElement | null The element the panel opens from, when it is not a | — |
The root holds the state and draws nothing of its own.
Popover.Trigger
| Prop | Type | Default |
|---|---|---|
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 | 'default' |
element | HTMLButtonElement | null The rendered element. Bindable. | — |
Plus every attribute of a native <button>.
Popover.Content
| Prop | Type | Default |
|---|---|---|
placement | ExtendedPlacement Where the panel sits against the trigger: | 'bottom-start' |
offset | number The distance from the trigger, in pixels. | 4 |
shouldFlip | boolean Moves the panel to the other side when there is no room. | true |
boundaryElement | Element | null The element whose edges the panel keeps within. | — |
nonModal | boolean Lets the page be used while the panel is open: the focus is not held, and the page scrolls. | false |
shouldCloseOnEscape | boolean Closes the panel on | true |
shouldCloseOnInteractOutside | boolean Closes the panel on a press outside it. | true |
shouldCloseOnBlur | boolean Closes the panel when the focus leaves it. On by default when | — |
initialFocus | HTMLElement | 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
| Prop | Type | Default |
|---|---|---|
children | Snippet The title and the description, kept close together over what the panel holds. | — |
class | string Classes for the element. | — |
Plus every attribute of a <div>.
Popover.Title
| Prop | Type | Default |
|---|---|---|
children | Snippet The name of the panel. The content is labelled by it. | — |
class | string Classes for the element. | — |
Plus every attribute of an <h2>.
Popover.Description
| Prop | Type | Default |
|---|---|---|
children | Snippet A line read after the name. The content is described by it. | — |
class | string Classes for the element. | — |
Plus every attribute of a <p>.