← Components

Containment · Material 3: Tooltips (rich)

Popover

A small panel anchored to a button, pulled out of it with a goo bridge. With openOnHover it works as a hover card.

Examples and variants

Click

Details next to a control.

Example
function PopoverBasic() {
  return (
    <Popover>
      <PopoverTrigger render={<Button variant="secondary" />}>Details</PopoverTrigger>
      <PopoverContent>
        <PopoverTitle>Northwind</PopoverTitle>
        <PopoverDescription>A short panel pulled out of the button with a goo bridge.</PopoverDescription>
      </PopoverContent>
    </Popover>
  )
}

Hover card

openOnHover on the trigger.

Example
function PopoverHover() {
  return (
    <Popover>
      <PopoverTrigger openOnHover render={<Button variant="link" />}>
        @piper
      </PopoverTrigger>
      <PopoverContent>
        <PopoverTitle>Piper Phan</PopoverTitle>
        <PopoverDescription>Opens on hover, like a hover card.</PopoverDescription>
      </PopoverContent>
    </Popover>
  )
}

Spec drawing

Measured from the live component at default density. Each code maps to the token that sets it; change the token and the drawing follows.

Install

With the CLI

The shadcn CLI only delivers files: it copies our code from the Isotopi registry into your project and installs the packages it uses. Add the foundation once, then the component.

Terminal
npx shadcn@latest add https://isotopi.com/r/minimal.json
npx shadcn@latest add https://isotopi.com/r/popover.json

By hand

No CLI needed. Add the foundation once (see Getting started), then follow these steps. They include every component this one builds on.

  1. Install the packages it uses.

    Terminal
    npm install @base-ui/react cn
  2. Copy this file to the same paths in your project. Imports use the @/ alias for your project root.

    • components/ui/popover.tsxpopover
      components/ui/popover.tsx
      "use client"
      
      import * as React from "react"
      import { Popover as PopoverPrimitive } from "@base-ui/react/popover"
      import { cn } from "cn"
      import { GooLayer } from "@/components/motion/goo-layer"
      import { tokenMs } from "@/lib/motion/token-ms"
      import { activeTrigger, motionLevel, usePortalMotion } from "@/lib/overlay"
      
      const BridgeContext = React.createContext(false)
      
      function Popover({ onOpenChange, ...props }: PopoverPrimitive.Root.Props) {
        const [bridge, setBridge] = React.useState(false)
        const timer = React.useRef(0)
      
        return (
          <BridgeContext.Provider value={bridge}>
            <PopoverPrimitive.Root
              {...props}
              onOpenChange={(open, details) => {
                onOpenChange?.(open, details)
                window.clearTimeout(timer.current)
                const level = motionLevel()
                if (level === "calm" || level === "still") {
                  setBridge(false)
                  return
                }
                setBridge(true)
                timer.current = window.setTimeout(
                  () => setBridge(false),
                  tokenMs(open ? "--duration-fast" : "--duration-quick")
                )
              }}
            />
          </BridgeContext.Provider>
        )
      }
      
      function PopoverTrigger(props: PopoverPrimitive.Trigger.Props) {
        return <PopoverPrimitive.Trigger data-slot="popover-trigger" {...props} />
      }
      
      function PopoverTitle({ className, ...props }: PopoverPrimitive.Title.Props) {
        return <PopoverPrimitive.Title className={cn("text-sm font-medium", className)} {...props} />
      }
      
      function PopoverDescription({ className, ...props }: PopoverPrimitive.Description.Props) {
        return (
          <PopoverPrimitive.Description
            className={cn("text-sm text-muted-foreground", className)}
            {...props}
          />
        )
      }
      
      function PopoverContent({ className, children, ...props }: PopoverPrimitive.Popup.Props) {
        const ref = React.useRef<HTMLDivElement>(null)
        const bridge = React.useContext(BridgeContext)
        usePortalMotion(ref)
      
        return (
          <PopoverPrimitive.Portal>
            <PopoverPrimitive.Positioner className="z-overlay" sideOffset={8}>
              <PopoverPrimitive.Popup
                ref={ref}
                data-slot="popover-content"
                className={cn(
                  "overlay-surface overlay-grow w-(--popover-width) rounded-(--popover-corner) p-(--popover-padding) text-foreground",
                  className
                )}
                {...props}
              >
                {bridge ? <PopoverBridge popupRef={ref} /> : null}
                {children}
              </PopoverPrimitive.Popup>
            </PopoverPrimitive.Positioner>
          </PopoverPrimitive.Portal>
        )
      }
      
      function PopoverBridge({ popupRef }: { popupRef: React.RefObject<HTMLElement | null> }) {
        const [geom, setGeom] = React.useState({
          left: 0,
          top: 0,
          width: 48,
          height: 48,
          neckX: 16,
          neckY: 16,
          edgeX: 32,
          edgeY: 32,
          ready: false,
        })
      
        React.useLayoutEffect(() => {
          let frame = 0
          let tries = 0
          const measure = () => {
            const popup = popupRef.current
            const trigger = activeTrigger("popover-trigger")
            if (!popup || !trigger || popup.getBoundingClientRect().width === 0) {
              if (tries++ < 20) frame = requestAnimationFrame(measure)
              return
            }
            const panel = popup.getBoundingClientRect()
            const button = trigger.getBoundingClientRect()
            const buttonX = button.left + button.width / 2
            const buttonY = button.top + button.height / 2
            const panelX = panel.left + panel.width / 2
            const panelY = panel.top + panel.height / 2
            const horizontal = Math.abs(panelX - buttonX) > Math.abs(panelY - buttonY)
            const neckX = (horizontal ? buttonX + Math.sign(panelX - buttonX) * (button.width / 2) : buttonX) - panel.left
            const neckY = (horizontal ? buttonY : buttonY + Math.sign(panelY - buttonY) * (button.height / 2)) - panel.top
            const edgeX = (horizontal ? panelX - Math.sign(panelX - buttonX) * (panel.width / 2) : panelX) - panel.left
            const edgeY = (horizontal ? panelY : panelY - Math.sign(panelY - buttonY) * (panel.height / 2)) - panel.top
            const pad = 28
            const left = Math.min(neckX, edgeX) - pad
            const top = Math.min(neckY, edgeY) - pad
            setGeom({
              left,
              top,
              width: Math.max(neckX, edgeX) + pad - left,
              height: Math.max(neckY, edgeY) + pad - top,
              neckX: neckX - left,
              neckY: neckY - top,
              edgeX: edgeX - left,
              edgeY: edgeY - top,
              ready: true,
            })
          }
          measure()
          return () => cancelAnimationFrame(frame)
        }, [popupRef])
      
        return (
          <span
            className="popover-bridge pointer-events-none absolute"
            style={{ left: geom.left, top: geom.top, width: geom.width, height: geom.height }}
          >
            <GooLayer width="100%" height="100%" viewBox={`0 0 ${geom.width} ${geom.height}`} className="overflow-visible">
              <circle cx={geom.neckX} cy={geom.neckY} r="11" fill="var(--color-surface-raised)" />
              <circle cx={geom.edgeX} cy={geom.edgeY} r="16" fill="var(--color-surface-raised)" />
            </GooLayer>
          </span>
        )
      }
      
      export { Popover, PopoverTrigger, PopoverContent, PopoverTitle, PopoverDescription }
      

Imports

What the examples on this page import:

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

Anatomy

  1. 1Container · --popover-container, --popover-corner, --popover-padding, --popover-width.
  2. 2Title and description · Optional.
  3. 3Bridge · The goo neck that connects it to the trigger while opening.

Props

PropTypeDefaultDescription
PopoverTrigger openOnHoverboolean—Opens on hover (hover card).
open / onOpenChangeboolean—Controlled state.

Tokens

Every visual spec is a component token in styles/tokens/components.css, pointing at a semantic token. Change a token to restyle the component; themes change the semantic layer.

TokenPoints toLightDark
--popover-containervar(--surface-container-raised)oklch(1 0 0)oklch(0.22 0 0)
--popover-cornervar(--shape-lg)22pxsame
--popover-paddingvar(--space-3)12pxsame
--popover-widthvar(--layout-popover-max)320pxsame

Motion

TriggerWhat happensTokens
OpenGrows from the trigger with a short goo bridge that snaps.--duration-fast, --goo-duration

Calm keeps the motion without overshoot. Still (and reduced motion) keeps a designed frame with no movement.

Accessibility

  • Escape closes and focus returns to the trigger.
  • The trigger is always a Button (render={<Button />}).
  • Focus moves in when it holds controls.
  • Hover-only content must be reachable another way.

Usage

Do

  • Use for small forms and details.

Don’t

  • Don't use for decisions that block the task (use Dialog).

Code

components/ui/popover.tsx
"use client"

import * as React from "react"
import { Popover as PopoverPrimitive } from "@base-ui/react/popover"
import { cn } from "cn"
import { GooLayer } from "@/components/motion/goo-layer"
import { tokenMs } from "@/lib/motion/token-ms"
import { activeTrigger, motionLevel, usePortalMotion } from "@/lib/overlay"

const BridgeContext = React.createContext(false)

function Popover({ onOpenChange, ...props }: PopoverPrimitive.Root.Props) {
  const [bridge, setBridge] = React.useState(false)
  const timer = React.useRef(0)

  return (
    <BridgeContext.Provider value={bridge}>
      <PopoverPrimitive.Root
        {...props}
        onOpenChange={(open, details) => {
          onOpenChange?.(open, details)
          window.clearTimeout(timer.current)
          const level = motionLevel()
          if (level === "calm" || level === "still") {
            setBridge(false)
            return
          }
          setBridge(true)
          timer.current = window.setTimeout(
            () => setBridge(false),
            tokenMs(open ? "--duration-fast" : "--duration-quick")
          )
        }}
      />
    </BridgeContext.Provider>
  )
}

function PopoverTrigger(props: PopoverPrimitive.Trigger.Props) {
  return <PopoverPrimitive.Trigger data-slot="popover-trigger" {...props} />
}

function PopoverTitle({ className, ...props }: PopoverPrimitive.Title.Props) {
  return <PopoverPrimitive.Title className={cn("text-sm font-medium", className)} {...props} />
}

function PopoverDescription({ className, ...props }: PopoverPrimitive.Description.Props) {
  return (
    <PopoverPrimitive.Description
      className={cn("text-sm text-muted-foreground", className)}
      {...props}
    />
  )
}

function PopoverContent({ className, children, ...props }: PopoverPrimitive.Popup.Props) {
  const ref = React.useRef<HTMLDivElement>(null)
  const bridge = React.useContext(BridgeContext)
  usePortalMotion(ref)

  return (
    <PopoverPrimitive.Portal>
      <PopoverPrimitive.Positioner className="z-overlay" sideOffset={8}>
        <PopoverPrimitive.Popup
          ref={ref}
          data-slot="popover-content"
          className={cn(
            "overlay-surface overlay-grow w-(--popover-width) rounded-(--popover-corner) p-(--popover-padding) text-foreground",
            className
          )}
          {...props}
        >
          {bridge ? <PopoverBridge popupRef={ref} /> : null}
          {children}
        </PopoverPrimitive.Popup>
      </PopoverPrimitive.Positioner>
    </PopoverPrimitive.Portal>
  )
}

function PopoverBridge({ popupRef }: { popupRef: React.RefObject<HTMLElement | null> }) {
  const [geom, setGeom] = React.useState({
    left: 0,
    top: 0,
    width: 48,
    height: 48,
    neckX: 16,
    neckY: 16,
    edgeX: 32,
    edgeY: 32,
    ready: false,
  })

  React.useLayoutEffect(() => {
    let frame = 0
    let tries = 0
    const measure = () => {
      const popup = popupRef.current
      const trigger = activeTrigger("popover-trigger")
      if (!popup || !trigger || popup.getBoundingClientRect().width === 0) {
        if (tries++ < 20) frame = requestAnimationFrame(measure)
        return
      }
      const panel = popup.getBoundingClientRect()
      const button = trigger.getBoundingClientRect()
      const buttonX = button.left + button.width / 2
      const buttonY = button.top + button.height / 2
      const panelX = panel.left + panel.width / 2
      const panelY = panel.top + panel.height / 2
      const horizontal = Math.abs(panelX - buttonX) > Math.abs(panelY - buttonY)
      const neckX = (horizontal ? buttonX + Math.sign(panelX - buttonX) * (button.width / 2) : buttonX) - panel.left
      const neckY = (horizontal ? buttonY : buttonY + Math.sign(panelY - buttonY) * (button.height / 2)) - panel.top
      const edgeX = (horizontal ? panelX - Math.sign(panelX - buttonX) * (panel.width / 2) : panelX) - panel.left
      const edgeY = (horizontal ? panelY : panelY - Math.sign(panelY - buttonY) * (panel.height / 2)) - panel.top
      const pad = 28
      const left = Math.min(neckX, edgeX) - pad
      const top = Math.min(neckY, edgeY) - pad
      setGeom({
        left,
        top,
        width: Math.max(neckX, edgeX) + pad - left,
        height: Math.max(neckY, edgeY) + pad - top,
        neckX: neckX - left,
        neckY: neckY - top,
        edgeX: edgeX - left,
        edgeY: edgeY - top,
        ready: true,
      })
    }
    measure()
    return () => cancelAnimationFrame(frame)
  }, [popupRef])

  return (
    <span
      className="popover-bridge pointer-events-none absolute"
      style={{ left: geom.left, top: geom.top, width: geom.width, height: geom.height }}
    >
      <GooLayer width="100%" height="100%" viewBox={`0 0 ${geom.width} ${geom.height}`} className="overflow-visible">
        <circle cx={geom.neckX} cy={geom.neckY} r="11" fill="var(--color-surface-raised)" />
        <circle cx={geom.edgeX} cy={geom.edgeY} r="16" fill="var(--color-surface-raised)" />
      </GooLayer>
    </span>
  )
}

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