Popover

A floating panel anchored to the button that opens it.

The popover flips or shifts when there isn’t room on its side. Set side, align and sideOffset on PopoverContent to place it.

Preview
basic.tsx
import { Button } from "~/components/ui/button"
import {
  Popover,
  PopoverClose,
  PopoverContent,
  PopoverDescription,
  PopoverTitle,
  PopoverTrigger,
} from "~/components/ui/popover"
import { Stack } from "~/components/ui/stack"

export default function Example() {
  return (
    <Popover>
      <PopoverTrigger render={<Button variant="outline" />}>Share</PopoverTrigger>
      <PopoverContent>
        <Stack gap="2xs">
          <PopoverTitle>Share this project</PopoverTitle>
          <PopoverDescription>Anyone with the link can view it.</PopoverDescription>
        </Stack>
        <PopoverClose render={<Button size="sm" className="self-end" />}>Copy link</PopoverClose>
      </PopoverContent>
    </Popover>
  )
}

Dependencies

What it's built from and what's built from it in Base, read from the imports in its source.

UsesScroll AreaprimitiveTextprimitive
Used byNothing yet.
Packages @ui-sdk/core@0.1.0-beta.20@ui-sdk/react@0.1.0-beta.20

Installation

Set the project up for Base once first, as Getting started shows. Then add it with the CLI, or by hand.

With the CLI

terminal
npx shadcn@latest add https://design.zepez.dev/r/base/popover.json

Manually

Install its packages:

terminal
npm install @ui-sdk/core@0.1.0-beta.20 @ui-sdk/react@0.1.0-beta.20

Add the components it uses first, listed under Dependencies.

Then copy the source:

components/ui/popover.tsx
import {
  Popover,
  PopoverClose,
  PopoverContent as PopoverContentPrimitive,
  PopoverDescription as PopoverDescriptionPrimitive,
  PopoverTitle as PopoverTitlePrimitive,
  PopoverTrigger,
} from "@ui-sdk/react/popover"
import type * as React from "react"

import { ScrollArea } from "~/components/ui/scroll-area"
import { textVariants } from "~/components/ui/text"
import { cnState } from "~/lib/utils"

/** The floating panel. Placed below its trigger with a 4px gap unless you set `side` and `sideOffset`. */
function PopoverContent({
  className,
  children,
  sideOffset = 4,
  ...props
}: React.ComponentProps<typeof PopoverContentPrimitive>) {
  return (
    <PopoverContentPrimitive
      sideOffset={sideOffset}
      className={cnState(
        [
          textVariants({ size: "sm" }),
          "flex flex-col overflow-hidden max-h-(--available-height) w-72 max-w-[calc(100vw-1rem)] rounded-xl outline-none",
          "bg-overlay text-overlay-foreground shadow-lg ring-1 ring-foreground/5 dark:ring-foreground/10",
          "origin-(--transform-origin) transition-[opacity,scale] duration-100 ease-out data-starting-style:scale-95 data-starting-style:opacity-0 data-ending-style:scale-95 data-ending-style:opacity-0 motion-reduce:transition-none",
        ].join(" "),
        className,
      )}
      {...props}
    >
      {(state) => (
        <ScrollArea className="min-h-0">
          <div className="flex flex-col gap-lg p-lg">
            {typeof children === "function" ? children(state) : children}
          </div>
        </ScrollArea>
      )}
    </PopoverContentPrimitive>
  )
}

/** Names the popover. */
function PopoverTitle({ className, ...props }: React.ComponentProps<typeof PopoverTitlePrimitive>) {
  return (
    <PopoverTitlePrimitive
      className={cnState(textVariants({ size: "md", weight: "medium" }), className)}
      {...props}
    />
  )
}

/** Describes the popover. */
function PopoverDescription({
  className,
  ...props
}: React.ComponentProps<typeof PopoverDescriptionPrimitive>) {
  return (
    <PopoverDescriptionPrimitive
      className={cnState("text-muted-foreground", className)}
      {...props}
    />
  )
}

export { Popover, PopoverClose, PopoverContent, PopoverDescription, PopoverTitle, PopoverTrigger }

API reference

Generated from Base's types. Props marked ours are added here; the rest come from the ui-sdk part it wraps. Every part also accepts its element's native attributes.

Popover

Rich content in a floating panel, opened from a button. Put its trigger and content inside.

closeDelay

Milliseconds before the popover closes once the pointer leaves it and its trigger, with openOnHover.

Type number · Default 0

container

Renders the popup into this element instead of in place, like a container inside a shadow root or an iframe. On web components, a CSS selector. Nested popups stay inside their parent.

Type Element | DocumentFragment | null

controller

Opens, closes and inspects the popover from anywhere, and connects triggers outside it. Create one with createPopoverController().

Type PopupController<any, PopoverOpenChangeReason, {}, string, PopoverPayloadChangeReason>

debug

Logs every state change with its reason to the console, in development builds.

Type boolean

defaultOpen

Whether the popover starts open, when uncontrolled.

Type boolean · Default false

delay

Milliseconds the pointer rests on the trigger before the popover opens, with openOnHover.

Type number · Default 300

dismissible

Whether Escape, an outside press, focus or the window leaving, Android's back gesture and a sheet's swipe can close it. Triggers, Close parts and the controller always can. Pass false while it holds unsaved work, and ask in onDismissAttempt.

Type boolean · Default true

modal

false: the rest of the page stays usable, and focus leaving the popover closes it. true: also traps focus, locks scroll and makes the rest of the page inert, and an outside press only closes the popover. "trap-focus": traps focus but leaves the page scrollable and clickable.

Type boolean | "trap-focus" · Default false, or true while it presents as a sheet

onDismissAttempt

Runs when dismissible refused a dismissal made on purpose: Escape, an outside press, back or a swipe. Focus or the window leaving is refused without it. Ask "Discard changes?" here.

Type (details: { reason: "outside-press"; event?: MouseEvent; } | { reason: "escape-key"; event?: KeyboardEvent; } | { reason: "back"; event?: Event; } | { reason: "swipe"; event?: PointerEvent | TouchEvent; }) => void

onOpenChange

Runs when the popover wants to open or close. details.reason says why, and details.cancel() stops it.

Type (open: boolean, details: ChangeDetails<PopoverOpenChangeReason>) => void

onOpenChangeComplete

Runs once the popover has finished opening or closing, after any animation.

Type (open: boolean) => void

onPayloadChange

Runs when the payload the content shows changes: a trigger with another payload opens the popover, or takes the open popover over, which fires no onOpenChange, and so does a controller's open({ payload }) on the open popover. details.cancel() keeps the previous payload.

Type (payload: unknown, details: ChangeDetails<PopoverPayloadChangeReason>) => void

open

Whether the popover is open. Pass it to control the popover.

Type boolean

openOnHover

Whether resting the pointer on the trigger opens the popover. Pressing the trigger still opens it, so keyboard and touch users can too.

Type boolean · Default false

presentation

How the popover presents. "sheet" slides it up from the bottom of the screen, where it can be swiped away, and "auto" does that on small touch screens. Decided each time it opens; the server renders "popover" for "auto".

Type PresentationSetting<"popover"> · Default "popover"

PopoverContent

The floating panel. Placed below its trigger with a 4px gap unless you set side and sideOffset.

align

How the popup lines up with the anchor along that side. start and end follow the reading direction. How the popover lines up with the trigger along that side.

Type "center" | "start" | "end" · Default "center"

alignOffset

Shift along the anchor, in pixels. Shift along the trigger, in pixels.

Type number · Default 0

anchor

Positions the popup against this instead of its trigger: an element, a CSS selector, a point like { x, y }, or anything with getBoundingClientRect(). It's read on every check, so it can move. A controller's open({ anchor }) wins for that open.

Type string | Anchor | null

arrowPadding

Space to keep between the arrow and the popup's corners, in pixels.

Type number · Default 6

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: PopoverContentState<unknown>) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: PopoverContentState<unknown>) => string)

collisionPadding

Space to keep from the viewport edges, in pixels.

Type number · Default 8

container

Renders the popup into this element instead of in place, like a container inside a shadow root or an iframe. On web components, a CSS selector. Nested popups stay inside their parent.

Type Element | DocumentFragment | null

finalFocus

Where focus goes when the popover closes while focus is inside it.

Type PopupFocusTarget · Default "trigger"

initialFocus

Where focus goes when the popover opens. Opening on hover never moves focus, and opening with touch focuses the popover itself so no on-screen keyboard appears, unless this is set to something other than "auto".

Type PopoverInitialFocus · Default "auto"

inline

Renders the popover where it is in the page instead of above it, inside its parent's layout and stacking, and leaves placing it to your CSS: side, align and the offsets don't apply.

Type boolean · Default false

keepMounted

Whether the popover stays in the page, hidden, while closed. Its content keeps its state between opens.

Type boolean · Default false

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<PopoverContentState<unknown>>

side

Which side of the anchor the popup opens on. Flips to the other side when there isn't room. Which side of the trigger the popover opens on.

Type "start" | "end" | "top" | "bottom" | "left" | "right" · Default "bottom"

sideOffset

Gap between the anchor and the popup, in pixels. Gap between the trigger and the popover, in pixels.

Type number · Default 4

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: PopoverContentState<unknown>) => CSSProperties)

PopoverDescription

Describes the popover.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: {}) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: {}) => string)

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<{}>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: {}) => CSSProperties)

PopoverTitle

Names the popover.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: {}) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: {}) => string)

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<{}>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: {}) => CSSProperties)

PopoverTrigger

The button that opens the popover. A trigger outside the popover connects through controller.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: PopoverTriggerState) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: PopoverTriggerState) => string)

controller

Connects a trigger outside the popover, like one per table row, to the popover with this controller.

Type PopupController<any, PopoverOpenChangeReason, {}, string, PopoverPayloadChangeReason>

disabled

Stops presses. The button is natively disabled, so it leaves the tab order and its form doesn't submit through it, unless focusableWhenDisabled is set.

Type boolean · Default false

focusableWhenDisabled

Keeps a disabled trigger in the tab order with aria-disabled instead of the native attribute, so it can host a tooltip that says why. Presses still do nothing.

Type boolean · Default false

payload

Data for the content to render when this trigger opens the popover, like the row it belongs to.

Type unknown

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<PopoverTriggerState>

shortcut

A keyboard shortcut that presses the trigger, like "mod+i". mod is Command on Apple platforms and Control elsewhere. Sets aria-keyshortcuts.

Type string

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: PopoverTriggerState) => CSSProperties)