Skip to content

Popover

Shows interactive content next to the control that opened it, without leaving the page.

Installation

pnpm dlx shadcn@latest add @silkware/popover

To copy the component by hand, install its dependencies:

pnpm add react-aria-components cn

Then copy the source into components/ui/popover.tsx:

components/ui/popover.tsx
"use client"
import * as React from "react"import { cn } from "cn"import {  DialogTrigger,  Heading,  Popover as PopoverPrimitive,  type DialogTriggerProps,  type PopoverProps as PopoverPrimitiveProps,} from "@animate-ui/react/primitives/react-aria"
function PopoverTrigger({ children, ...props }: DialogTriggerProps) {  return (    <DialogTrigger data-slot="popover-trigger" {...props}>      {children}    </DialogTrigger>  )}
function Popover({  className,  placement = "bottom",  offset = 4,  crossOffset = 0,  ...props}: Omit<PopoverPrimitiveProps, "className"> & {  className?: string}) {  return (    <PopoverPrimitive      data-slot="popover-content"      placement={placement}      offset={offset}      crossOffset={crossOffset}      className={cn(        "z-50 flex w-72 origin-(--trigger-anchor-point) flex-col gap-4 rounded-md bg-popover p-4 text-sm text-popover-foreground shadow-md ring-1 ring-foreground/10 outline-hidden",        className      )}      {...props}    />  )}
function PopoverHeader({ className, ...props }: React.ComponentProps<"div">) {  return (    <div      data-slot="popover-header"      className={cn("flex flex-col gap-1 text-sm", className)}      {...props}    />  )}
function PopoverTitle({  className,  ...props}: React.ComponentProps<typeof Heading>) {  return (    <Heading      data-slot="popover-title"      className={cn("font-medium", className)}      {...props}    />  )}
function PopoverDescription({  className,  ...props}: React.ComponentProps<"div">) {  return (    <div      data-slot="popover-description"      className={cn("text-muted-foreground", className)}      {...props}    />  )}
export {  Popover,  PopoverDescription,  PopoverHeader,  PopoverTitle,  PopoverTrigger,}

Usage

import { Button } from "@/components/ui/button"import {  Popover,  PopoverDescription,  PopoverHeader,  PopoverTitle,  PopoverTrigger,} from "@/components/ui/popover"
<PopoverTrigger>  <Button variant="outline">Reorder point</Button>  <Popover placement="bottom start">    <PopoverHeader>      <PopoverTitle>Reorder point</PopoverTitle>      <PopoverDescription>        Purchasing is alerted when stock falls below this level.      </PopoverDescription>    </PopoverHeader>  </Popover></PopoverTrigger>

PopoverTrigger takes the pressable trigger first and the Popover second. The popover is w-72 wide, and a column of its children is spaced by a gap. Set another width with className.

Examples

Placement

placement chooses the side: top, right, bottom or left. If there is no room, the popover flips to the opposite side. The default is bottom.

Alignment

Add start or end to a vertical placement to line the popover up with an edge of the trigger: bottom start, bottom and bottom end. Use bottom start for a trigger at the left of a row, and bottom end for one at the right.

Controlled

Pass isOpen and onOpenChange to PopoverTrigger to close the popover after the user applies a change, and to read the result.

Window: 7:00 to 15:00

Accessibility

KeyAction
SpaceOpens the popover from the trigger.
EnterOpens the popover from the trigger.
TabMoves focus to the next control inside the popover.
ShiftTabMoves focus to the previous control inside the popover.
EscCloses the popover and returns focus to the trigger.

React Aria provides:

  • A trigger that reports its expanded state to assistive technology, and a popover that is tied to the trigger.
  • Focus that moves into the popover when it opens and returns to the trigger when it closes.
  • Closing on Escape and on an interaction outside the popover. isKeyboardDismissDisabled and shouldCloseOnInteractOutside change this.
  • Positioning that keeps the popover on screen: it flips to the other side and shifts along the edge when there is no room.

You provide:

  • A visible name on the trigger, or an aria-label when it holds only an icon.
  • An aria-label on the popover, or an aria-labelledby that points at the id of its PopoverTitle, when it holds more than one control. The popover has the dialog role, and PopoverTitle does not name it on its own.
  • A label on every field inside the popover.

Guidelines

When to use

  • For a few controls tied to one element: a number, a date window, a short list of choices.
  • For a small piece of extra information that the user asked for with a press and may want to interact with.
  • For a filter or a setting that applies without a page change.

When not to use

  • For a hint that explains a control. Use a Tooltip.
  • For a preview that appears when the user hovers over a link. Use a Hover Card.
  • For a list of commands. Use a Dropdown Menu.
  • For a task that needs the user's full attention. Use a Dialog.
  • For a long form or content to compare with the page. Use a Sheet.

Content

  • Keep it small: one title, a sentence, and one to three controls.
  • Title the popover after the thing it changes, in sentence case.
  • Apply the change with a button that names the result ("Apply window"), or apply it as the user edits and say so.

Focus and dismissal

A popover is not modal: it closes when the user presses elsewhere, so never put the only copy of unsaved work in it. When the user must finish a form before leaving, use a dialog.

Interactive or just text

Do

Use a popover for a short choice or a few fields that the user can act on.

Don’t

Don’t use a popover for a sentence of help. It needs a press to open and a press to close. A tooltip or a line of text under the field does the job.

API reference

PopoverTrigger

Wraps the trigger and the popover through the React Aria DialogTrigger. It accepts every DialogTrigger prop.

PropTypeDefaultDescription
childrenReactNodeNoneA pressable trigger, then the Popover.
isOpen?booleanNoneWhether the popover is open. Makes the popover controlled.
defaultOpen?booleanNoneWhether the popover is open on first render, when uncontrolled.
onOpenChange?(isOpen: boolean) => voidNoneCalled when the popover opens or closes.

Popover

Renders the React Aria Popover. It accepts every Popover prop and the props below.

PropTypeDefaultDescription
placement?Placement"bottom"Where the popover opens relative to the trigger, for example top, bottom start or right end.
offset?number4The distance in pixels from the trigger along the main axis.
crossOffset?number0The shift in pixels along the cross axis.
shouldFlip?booleantrueFlips the popover to the opposite side when there is no room.
isKeyboardDismissDisabled?booleanfalseStops Escape from closing the popover. Most popovers should not set it.
className?stringNoneClasses merged onto the popover. Use it to set the width.
children?ReactNodeNoneThe popover content.

PopoverHeader

A div that stacks the title and the description. It accepts every div prop.

PopoverTitle

Renders a heading through the React Aria Heading. It does not label the popover by itself: give it an id and pass that to aria-labelledby on Popover. It accepts every Heading prop.

PopoverDescription

A div with muted text. It accepts every div prop.