# Popover

> Animated popover for React and shadcn that grows out of its trigger, moves focus inside and closes on Esc, built on Base UI.

A panel that opens from its trigger's transform-origin with a 5px directional offset, closes at half the distance in 150ms, and reverses mid-flight. Its trigger presses by colour, never by scale.

- Tier: Free (MIT)
- Interaction: Press
- Page: https://eased.dev/components/popover
- Installs: @base-ui/react
- Registry: https://eased.dev/r/popover.json

## Install

```bash
npx shadcn add https://eased.dev/r/popover.json
```

A non-modal panel anchored to a button, on Base UI's Popover and the Eased floating layer: it grows out of its trigger, moves focus inside, and closes on Escape or an outside click.

## When to use

- A small task next to the thing it acts on: sharing a document, renaming a file, setting a reminder.
- A short form or a few controls that should not take people off the page: invite by email, pick a due date.
- Detail that people open on purpose and that can hold buttons and links, which a tooltip cannot.

## When not to use

- A one-line label for an icon button: use a [Tooltip](/components/tooltip).
- A list of actions or options to choose from: use a [Dropdown Menu](/components/dropdown-menu) or a [Select](/components/select).
- A decision that must be made before people carry on, or a long form: use a [Dialog](/components/dialog).

## Usage

```tsx share-popover.tsx
import { Button } from "@/components/eased/button/button";
import {
  Popover,
  PopoverClose,
  PopoverContent,
  PopoverDescription,
  PopoverFooter,
  PopoverTitle,
  PopoverTrigger,
} from "@/components/eased/popover/popover";

export function SharePopover() {
  return (
    <Popover>
      <PopoverTrigger>Share</PopoverTrigger>
      <PopoverContent align="end">
        <PopoverTitle>Share “Q3 launch plan”</PopoverTitle>
        <PopoverDescription>People you invite can view and comment.</PopoverDescription>
        {/* an email field and the people with access */}
        <PopoverFooter>
          <span className="text-ui-sm text-text-muted">Anyone with the link can view</span>
          <PopoverClose render={<Button variant="primary" size="sm" className="radius-nested" />}>Done</PopoverClose>
        </PopoverFooter>
      </PopoverContent>
    </Popover>
  );
}
```

## Examples

### Icon trigger, opening to the side

Use `size="icon-sm"` for a square trigger and give it an `aria-label`. The panel flips to the other side when there is no room, and enters from the side it lands on.

```tsx
<Popover>
  <PopoverTrigger size="icon-sm" variant="ghost" aria-label="Set reminder">
    <BellIcon />
  </PopoverTrigger>
  <PopoverContent side="right" align="start" className="w-64">
    <PopoverTitle>Remind me</PopoverTitle>
    …
  </PopoverContent>
</Popover>
```

### Controlled

Control the open state to close the panel after an action completes.

```tsx
const [open, setOpen] = useState(false);

<Popover open={open} onOpenChange={setOpen}>
  <PopoverTrigger>Rename</PopoverTrigger>
  <PopoverContent>
    <form onSubmit={(e) => { e.preventDefault(); rename(); setOpen(false); }}>…</form>
  </PopoverContent>
</Popover>
```

## API reference

### Popover

Base UI's `Popover.Root`, unchanged: `open`, `defaultOpen`, `onOpenChange`, `modal` (`false` by default; `true` or `"trap-focus"` to keep focus inside), `actionsRef`.

### PopoverTrigger

An Eased [Button](/components/button) that opens the popover, stays pressed (`aria-expanded`) while it is open, and never scales on press.

| Prop | Type | Default | Description |
|---|---|---|---|
| `variant` | `ButtonVariant` | `"secondary"` | The Button variant. |
| `size` | `ButtonSize` | `"sm"` | The Button size; `"icon-sm"` for a square icon trigger. |
| `render` | `ReactElement` | | Bring your own trigger; style it with `floatingTrigger` from the floating primitive. |
| `openOnHover` | `boolean` | `false` | Also open on hover. |
| `delay` / `closeDelay` | `number` | `300` / `0` | Hover delays in ms, with `openOnHover`. |
| `className` | `string` | `""` | Passed to the Button. |

Other Base UI `Popover.Trigger` props pass through.

### PopoverContent

| Prop | Type | Default | Description |
|---|---|---|---|
| `side` | `"top" \| "bottom" \| "left" \| "right" \| "inline-start" \| "inline-end"` | `"bottom"` | Preferred side; flips on collision and the enter direction follows. |
| `align` | `"start" \| "center" \| "end"` | `"center"` | |
| `sideOffset` | `number` | `6` | Gap to the trigger, px. |
| `alignOffset` | `number` | `0` | |
| `motion` | `Partial<FloatingMotion>` | | Override the enter distance, scale, fade or exit. |
| `initialFocus` | Base UI `initialFocus` | first tabbable element | Where focus goes on open. |
| `finalFocus` | Base UI `finalFocus` | the trigger | Where focus returns on close. |
| `container` | `HTMLElement \| null` | `document.body` | Portal target. |
| `zIndex` | `number` | `50` | Raise it inside a higher layer, such as a dialog. |
| `className` | `string` | `""` | Appended to the panel's classes. Set a width here; the default is `w-80`, never wider than the viewport. |

The panel keeps 8px from the viewport edge, and has 16px padding and corners.

### PopoverTitle, PopoverDescription, PopoverFooter and PopoverClose

`PopoverTitle` and `PopoverDescription` label and describe the panel. `PopoverFooter` is a full-bleed sunken action bar at the bottom; it keeps the body's padding as its inset, so buttons using `radius-nested` get concentric corners. `PopoverClose` is Base UI's `Popover.Close`; pass `render` to make any button close the panel.

## Keyboard

| Key | Action |
|---|---|
| `Enter` / `Space` | On the trigger, opens or closes the popover. |
| `Esc` | Closes the popover and returns focus to the trigger. |
| `Tab` | Moves through the controls inside the panel. |

## Accessibility

- Base UI gives the panel `role="dialog"`, labelled by `PopoverTitle` and described by `PopoverDescription`, and sets `aria-expanded` on the trigger.
- On open, focus moves to the first tabbable element inside, or to the panel itself when opened by touch so the on-screen keyboard does not pop up. On close it returns to the trigger.
- The popover is non-modal: the rest of the page stays usable and focus is not trapped. Set `modal` on `Popover` when it should be.
- An outside click or `Esc` dismisses it.

## Motion

The panel grows out of its trigger: it scales from 0.97 at Base UI's transform origin and travels 5px toward its resting place from the side of the trigger, on the snappy spring, while its opacity fades in over 200ms. It closes in 150ms, travelling half as far and scaling half as much, with no overshoot. Both are CSS transitions, so closing during the open turns around mid-flight. The trigger presses by colour, never by scale, so the origin stays put. Reduced motion keeps only a 100ms fade.

## Notes for AI

Use Popover for a small interactive panel tied to a button: give it a `PopoverTitle`, put actions in `PopoverFooter`, and set a width with `className` on `PopoverContent` when 320px is wrong. Raise `zIndex` when it opens inside a dialog.

Do not use it for plain hover labels (that is Tooltip) or for menus of actions (that is Dropdown Menu). Do not add your own open animation, focus management or outside-click handling, and do not scale the trigger on press: Base UI and the floating layer already handle them.

## Related

- [Tooltip](/components/tooltip): a short, non-interactive label on hover and focus.
- [Dropdown Menu](/components/dropdown-menu): a list of actions from a button.
- [Dialog](/components/dialog): a modal decision or form that blocks the page.

## Example

`components/eased/popover/popover-demo.tsx`

```tsx
"use client";

import { useEffect, useId, useRef, useState, type FormEvent } from "react";
import { Avatar } from "../avatar/avatar";
import { Button } from "../button/button";
import { Input } from "../input/input";
import { DemoFrame } from "../lib/demo-frame";
import { rowGrid, rowGridRow } from "../lib/layout";
import { TextSwap } from "../text-swap/text-swap";
import { Popover, PopoverClose, PopoverContent, PopoverDescription, PopoverFooter, PopoverTitle, PopoverTrigger } from "./popover";

type Person = { name: string; email: string; role: "Owner" | "Can edit" | "Can view" };

const PEOPLE: Person[] = [
  { name: "Mara Lindqvist", email: "mara@northwind.io", role: "Owner" },
  { name: "Devon Okafor", email: "devon@northwind.io", role: "Can edit" },
];

export function PopoverDemo() {
  const [people, setPeople] = useState(PEOPLE);

  return (
    <DemoFrame
      title="Q3 launch plan"
      meta={`${people.length} people`}
      actions={
        <Popover>
          <PopoverTrigger>
            <ShareIcon />
            Share
          </PopoverTrigger>
          {/* --col is the Invite button's width and the role column's, so the roles start under it. */}
          <PopoverContent align="end" className="[--col:5.5rem]">
            <SharePanel people={people} onInvite={(p) => setPeople((list) => [...list, p])} />
          </PopoverContent>
        </Popover>
      }
    >
      <div className="flex min-h-0 flex-1 flex-col gap-4 px-4 py-5">
        <div className="flex flex-col gap-2">
          <p className="text-ui-md font-medium text-text">Goals</p>
          <p className="text-ui text-pretty text-text-secondary">
            Ship offline sync to every workspace, cut first-load time under 1.2s and open the API to the waitlist.
          </p>
        </div>
        <div className="flex flex-col gap-2.5">
          {[
            ["Offline sync GA", "Aug 14"],
            ["Perf budget in CI", "Aug 28"],
            ["Public API beta", "Sep 18"],
          ].map(([label, date]) => (
            <div key={label} className="flex items-center justify-between gap-3 text-ui-sm">
              <span className="flex min-w-0 items-center gap-2 text-text">
                <span aria-hidden className="size-1.5 shrink-0 rounded-pill bg-text-muted" />
                <span className="truncate">{label}</span>
              </span>
              <span className="shrink-0 text-text-muted tabular-nums">{date}</span>
            </div>
          ))}
        </div>
      </div>
    </DemoFrame>
  );
}

function SharePanel({ people, onInvite }: { people: Person[]; onInvite: (p: Person) => void }) {
  const id = useId();
  const [email, setEmail] = useState("");
  const [copied, setCopied] = useState(false);
  const timer = useRef(0);
  useEffect(() => () => window.clearTimeout(timer.current), []);

  const valid = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email.trim());

  const invite = (e: FormEvent) => {
    e.preventDefault();
    if (!valid) return;
    const address = email.trim().toLowerCase();
    const name = address.split("@")[0].replace(/[._-]+/g, " ").replace(/\b\w/g, (c) => c.toUpperCase());
    onInvite({ name, email: address, role: "Can view" });
    setEmail("");
  };

  const copy = () => {
    void navigator.clipboard?.writeText("https://northwind.io/d/q3-launch").catch(() => {});
    setCopied(true);
    window.clearTimeout(timer.current);
    timer.current = window.setTimeout(() => setCopied(false), 1600);
  };

  return (
    <>
      <PopoverTitle>Share “Q3 launch plan”</PopoverTitle>
      <PopoverDescription>People you invite can view and comment.</PopoverDescription>

      <form onSubmit={invite} className="mt-4 flex gap-2">
        <label htmlFor={id} className="sr-only">
          Email address
        </label>
        <Input
          id={id}
          type="email"
          size="sm"
          value={email}
          onValueChange={setEmail}
          placeholder="name@company.com"
          autoComplete="off"
          className="flex-1"
        />
        <Button type="submit" size="sm" disabled={!valid} className="w-(--col)">
          Invite
        </Button>
      </form>

      <ul className={`${rowGrid} mt-3 max-h-36 gap-x-2.5 overflow-y-auto`} aria-label="People with access">
        {people.map((p) => (
          <li key={p.email} className={`${rowGridRow} h-11`}>
            <Avatar name={p.name} size="md" />
            <span className="flex min-w-0 flex-col">
              <span className="truncate text-ui-sm text-text">{p.name}</span>
              <span className="truncate text-ui-xs text-text-muted">{p.email}</span>
            </span>
            <span className="truncate text-ui-xs text-text-muted">{p.role}</span>
          </li>
        ))}
      </ul>

      <PopoverFooter>
        <Button variant="ghost" size="sm" edge="start" onClick={copy}>
          <LinkIcon />
          <TextSwap>{copied ? "Copied" : "Copy link"}</TextSwap>
          <span className="sr-only" aria-live="polite">
            {copied ? "Link copied" : ""}
          </span>
        </Button>
        <PopoverClose render={<Button variant="primary" size="sm" />}>Done</PopoverClose>
      </PopoverFooter>
    </>
  );
}

function ShareIcon() {
  return (
    <svg viewBox="0 0 16 16" className="size-4 shrink-0" fill="none" stroke="currentColor" strokeWidth={1.5} strokeLinecap="round" strokeLinejoin="round" aria-hidden>
      <path d="M8 10V2.5M5 5.25 8 2.25l3 3M3.5 8.5v3.75c0 .7.55 1.25 1.25 1.25h6.5c.7 0 1.25-.55 1.25-1.25V8.5" />
    </svg>
  );
}

function LinkIcon() {
  return (
    <svg viewBox="0 0 16 16" className="size-4 shrink-0" fill="none" stroke="currentColor" strokeWidth={1.5} strokeLinecap="round" strokeLinejoin="round" aria-hidden>
      <path d="M7 9a2.5 2.5 0 0 0 3.5 0l2-2A2.5 2.5 0 0 0 9 3.5l-.5.5M9 7a2.5 2.5 0 0 0-3.5 0l-2 2A2.5 2.5 0 0 0 7 12.5l.5-.5" />
    </svg>
  );
}
```
