GooseUI

Components

Popover

New

Shows rich content in a box next to its trigger. The trigger is a popovertarget button and the content is a native popover, so opening, closing on Escape or a click outside, the top layer and the Tab order all come from the browser and work before hydration. CSS anchor positioning places the box on the chosen side and flips it when there is no room. A tiny client script keeps aria-expanded in sync, moves focus into the box and positions it in browsers without anchor positioning.

Browser Support

Limited availability
4 features
Details
Popover

Opens and closes the content in the top layer, with light dismiss and Escape.

Your browser:
Anchor positioning

Places the content next to the trigger and flips it. Without it a small script computes the position; without JavaScript the content opens in the center of the screen.

Your browser:
@starting-style

Fades the content in when it opens. Without it the content appears at once.

Your browser:
transition-behavior

Fades the content out when it closes. Without it the content disappears at once.

Your browser:

Preview

Installation

npx shadcn@latest add @gooseui/popover
pnpm dlx shadcn@latest add @gooseui/popover
yarn dlx shadcn@latest add @gooseui/popover
bunx --bun shadcn@latest add @gooseui/popover

Usage

import { Button } from "@/components/ui/button"
import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from "@/components/ui/popover"

export default function Example() {
  return (
    <Popover>
      <PopoverTrigger asChild>
        <Button variant="outline">Open popover</Button>
      </PopoverTrigger>
      <PopoverContent>Place content for the popover here.</PopoverContent>
    </Popover>
  )
}

Examples

Side and alignment

side picks the preferred side, align the alignment along it, sideOffset and alignOffset the gaps in pixels. When the chosen side has no room, the content flips to the opposite one.

<PopoverContent side="top" align="start" sideOffset={8}>
  Placed on this side when there is room.
</PopoverContent>

Close button

PopoverClose is a native popovertargetaction="hide" button. With asChild it turns a Button into the close button, no JavaScript needed.

<PopoverContent>
  <p>Turn on notifications?</p>
  <PopoverClose asChild>
    <Button variant="ghost" size="sm">Not now</Button>
  </PopoverClose>
</PopoverContent>

Controlled

open and onOpenChange from a client component. The browser still toggles the popover on its own; onOpenChange reports every change and open opens or closes it from outside.

State: closed

"use client"

const [open, setOpen] = React.useState(false)

<Popover open={open} onOpenChange={setOpen}>
  <PopoverTrigger asChild>
    <Button variant="outline">Edit name</Button>
  </PopoverTrigger>
  <PopoverContent>…</PopoverContent>
</Popover>
<Button onClick={() => setOpen(true)}>Open from outside</Button>

Custom anchor

PopoverAnchor positions the content against another element, here the whole search field instead of the small button.

<Popover>
  <PopoverAnchor className="flex items-center gap-2 rounded-md border p-1">
    <Input placeholder="Search components" />
    <PopoverTrigger asChild>
      <Button size="sm" variant="secondary">Filters</Button>
    </PopoverTrigger>
  </PopoverAnchor>
  <PopoverContent align="start">…</PopoverContent>
</Popover>

Props

PropTypeDefaultDescription
openboolean-Controlled open state. Client components only
defaultOpenbooleanfalseOpen right after hydration
onOpenChange(open: boolean) => void-Called with the new state on every open and close. Client components only: functions cannot cross the server boundary
idstring-id of the content element. Generated when omitted
asChildbooleanfalseMerge the props into the only child, for example a Button (PopoverTrigger, PopoverClose, PopoverAnchor)
side"top" | "right" | "bottom" | "left""bottom"Preferred side of the anchor (PopoverContent)
align"start" | "center" | "end""center"Alignment along the side (PopoverContent)
sideOffsetnumber4Gap between the anchor and the content, px (PopoverContent)
alignOffsetnumber0Shift along the side for start and end, px (PopoverContent)
focusOnOpenbooleantrueMove focus to the first focusable element, or to the content itself, on open (PopoverContent)
classNamestring-Additional CSS classes. The content is w-72 with p-4 by default

Features

  • Opens, closes on Escape and on a click outside without any JavaScript: popover="auto" with a popovertarget trigger.
  • Opening another popover closes this one, and a popover opened from inside another stays open: the browser keeps the stack.
  • Content is rendered on the server, right after the trigger in the DOM, so Tab moves from the trigger into the content.
  • role="dialog" on the content, aria-haspopup, aria-controls and aria-expanded on the trigger.
  • Focus moves into the content on open and back to the trigger on close.
  • Parts are linked without React context, so Popover and its parts stay server components. A part rendered inside your own component is not reached: keep the parts in the JSX passed to Popover, or pass the same id to Popover.