# Popover

A small panel anchored to its trigger, with a title, content and a close button.

A small raised surface anchored to its trigger: title, description, body and a close button. Click opens it, Escape or a click outside closes it, and focus moves in and returns to the trigger. Behaviour comes from the Zag popover machine, through Ark UI in React, Vue and Svelte.

Kind: component · Category: Overlays · Status: beta · Targets: React, Vue, Svelte · Behaviour: Zag `popover` machine, through Ark UI

## Add

```bash
npx fndn add popover
```

It writes the folder `popover/` into the project's ui folder (`index.ts`, `popover.tsx`, `Popover.vue`, `Popover.svelte`, `popover.recipe.ts`), and what it is built from: [Button](/components/button.md), [Tooltip](/components/tooltip.md). First time: [Installation](/installation.md).

## Usage

```tsx
import { Popover } from "@/components/ui/popover";
```

```vue
import { Popover } from "@/components/ui/popover";
```

```svelte
import { Popover } from "$lib/components/ui/popover";
```

## Parts

In the DOM the root carries `data-slot="popover"` and each part `data-slot="popover-<part>"`; in the recipe the root is the `base` slot and each part a slot named after it in camelCase.

| Part | Element | data-slot | Flags | Notes |
| --- | --- | --- | --- | --- |
| `root` | `div` | `popover` |  | Layout-neutral hook around the trigger (display: contents) |
| `trigger` | `button` | `popover-trigger` |  | aria-haspopup=dialog, aria-expanded, data-state=open\|closed. The default trigger is a Button. Marked data-popover="trigger". |
| `positioner` | `div` | `popover-positioner` |  | Placed by the engine; rendered in a portal. Exposes --transform-origin and --available-height |
| `content` | `div` | `popover-content` |  | role=dialog surface composing the shared popup surface (`lib/popup.ts`: surface, motion, keyboard-instant; stacking from base.css); data-state=open\|closed. Keeps its own width, 12px padding and a 480px limit (--popup-max-height). |
| `header` | `div` | `popover-header` | optional |  |
| `title` | `h2` | `popover-title` | optional |  |
| `close` | `button` | `popover-close` | optional | A Button: ghost, size xs, square, shape rounded, muted, aria-label=Close, with a tooltip of the same text. Only its placement is the popover's. Marked data-popover="close". |
| `description` | `div` | `popover-description` | optional |  |
| `body` | `div` | `popover-body` | optional |  |

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `open` | `boolean` | `false` | Bindable (React: `defaultOpen` and `onOpenChange`; Vue: `v-model:open`; Svelte: `bind:open`). data-state=open\|closed on trigger and content. |
| `title` | `string` |  |  |
| `description` | `string` |  |  |
| `closable` | `boolean` | `true` | Show the close button. |
| `placement` | `"top" \| "top-start" \| "top-end" \| "bottom" \| "bottom-start" \| "bottom-end" \| "left" \| "right"` | `"bottom-start"` | Preferred side; flips when there is no room. |
| `triggerLabel` | `string` |  | Renders a default Button trigger with this label. Otherwise pass your own trigger. |

Other attributes and `class` go to the root; `class` is merged through the recipe.

## Events

| Event | Payload |
| --- | --- |
| `openChange` | (open: boolean) |

## Slots

| Slot | Content |
| --- | --- |
| `default` | Body content |
| `trigger` | Your own trigger element (React `trigger` prop, Vue `trigger` slot, Svelte `trigger` snippet that spreads the props it receives) |
| `title` | Vue: rich title |
| `description` | Vue: rich description |

## States

`open`, `hover`, `press`, `focus-visible`

## Keyboard and accessibility

Role: dialog (non-modal).

Keyboard:

- Enter / Space on the trigger open and close
- Focus moves into the surface (first focusable, else the surface itself)
- Tab moves through the surface and on into the page
- Escape closes and returns focus to the trigger
- A click or focus outside closes

Notes:

- Labelled by its title and described by its description when present.
- Opening and closing from the keyboard are instant.

## Motion

- surface: the shared popup recipe (`popup`): opacity + scale from motion.popup-scale out of the trigger, duration.popup, easing.out-strong; closes at duration.fast; instant from the keyboard
- close button: the button's own hover and press (ghost Button)
- mounted open (defaultOpen, or open at mount): the surface is drawn at once, with no entrance (data-mounted-open until it first closes); its close, and every later open, animate as above

## Tokens

`color.ink`, `color.ink-2`, `text.xs`, `text.sm`

## Dependencies

- [Button](/components/button.md)
- [Tooltip](/components/tooltip.md)

## Examples

### Share a preview link

React · `share-link.tsx`

```tsx
import { ShareNetwork } from "@phosphor-icons/react";
import { Button } from "@/components/ui/button";
import { Clipboard } from "@/components/ui/clipboard";
import { Popover } from "@/components/ui/popover";

const previewUrl = "https://web-app-git-feat-usage-alerts.northwind.example";

export default function ShareLink() {
  return (
    <div className="flex w-80 items-center justify-between gap-3">
      <div className="flex min-w-0 flex-col">
        <span className="text-sm font-medium text-ink">feat/usage-alerts</span>
        <span className="text-xs text-ink-2">Preview deployed 4 min ago</span>
      </div>
      <Popover
        placement="bottom-end"
        title="Share preview"
        description="Anyone in Northwind with the link can open this preview."
        trigger={<Button size="sm" icon={<ShareNetwork data-slot="icon" weight="bold" />}>Share</Button>}
      >
        <Clipboard variant="field" value={previewUrl} aria-label="Preview link" />
      </Popover>
    </div>
  );
}
```

Vue · `share-link.vue`

```vue
<script setup lang="ts">
import { PhShareNetwork } from "@phosphor-icons/vue";
import { Button } from "@/components/ui/button";
import { Clipboard } from "@/components/ui/clipboard";
import { Popover } from "@/components/ui/popover";

const previewUrl = "https://web-app-git-feat-usage-alerts.northwind.example";
</script>

<template>
  <div class="flex w-80 items-center justify-between gap-3">
    <div class="flex min-w-0 flex-col">
      <span class="text-sm font-medium text-ink">feat/usage-alerts</span>
      <span class="text-xs text-ink-2">Preview deployed 4 min ago</span>
    </div>
    <Popover
      placement="bottom-end"
      title="Share preview"
      description="Anyone in Northwind with the link can open this preview."
    >
      <template #trigger>
        <Button size="sm">
          <template #icon><PhShareNetwork data-slot="icon" weight="bold" /></template>
          Share
        </Button>
      </template>
      <Clipboard variant="field" :value="previewUrl" aria-label="Preview link" />
    </Popover>
  </div>
</template>
```

Svelte · `share-link.svelte`

```svelte
<script lang="ts">
  import ShareNetwork from "phosphor-svelte/lib/ShareNetworkIcon";
  import { Button } from "$lib/components/ui/button";
  import { Clipboard } from "$lib/components/ui/clipboard";
  import { Popover } from "$lib/components/ui/popover";

  const previewUrl = "https://web-app-git-feat-usage-alerts.northwind.example";
</script>

<div class="flex w-80 items-center justify-between gap-3">
  <div class="flex min-w-0 flex-col">
    <span class="text-sm font-medium text-ink">feat/usage-alerts</span>
    <span class="text-xs text-ink-2">Preview deployed 4 min ago</span>
  </div>
  <Popover
    placement="bottom-end"
    title="Share preview"
    description="Anyone in Northwind with the link can open this preview."
  >
    {#snippet trigger(props)}
      <Button {...props()} size="sm">
        {#snippet icon()}<ShareNetwork data-slot="icon" weight="bold" />{/snippet}
        Share
      </Button>
    {/snippet}
    <Clipboard variant="field" value={previewUrl} aria-label="Preview link" />
  </Popover>
</div>
```

### Edit the spend limit in place

React · `spend-limit.tsx`

```tsx
import { useState } from "react";
import { Button } from "@/components/ui/button";
import { Field } from "@/components/ui/field";
import { Input } from "@/components/ui/input";
import { Popover } from "@/components/ui/popover";

/* edit one value where it is shown; Save closes the popover (open is controlled) */
export default function SpendLimit() {
  const [open, setOpen] = useState(false);
  const [limit, setLimit] = useState("500");
  const [draft, setDraft] = useState(limit);

  return (
    <div className="flex w-80 items-center justify-between gap-3">
      <div className="flex flex-col">
        <span className="text-sm font-medium text-ink">Monthly spend limit</span>
        <span className="text-xs text-ink-2">$312.40 used this month</span>
      </div>
      <Popover
        open={open}
        onOpenChange={(next) => { setOpen(next); setDraft(limit); }}
        placement="bottom-end"
        title="Monthly spend limit"
        description="Deploys pause when usage reaches the limit. Owners get an email at 80%."
        trigger={<Button size="sm">${Number(limit).toLocaleString("en-US")}</Button>}
      >
        <form className="flex items-end gap-2" onSubmit={(event) => { event.preventDefault(); setLimit(draft); setOpen(false); }}>
          <Field label="Limit in USD" className="flex-1">
            <Input inputMode="numeric" value={draft} onChange={(event) => setDraft(event.target.value)} />
          </Field>
          <Button type="submit" variant="primary">Save</Button>
        </form>
      </Popover>
    </div>
  );
}
```

Vue · `spend-limit.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { Button } from "@/components/ui/button";
import { Field } from "@/components/ui/field";
import { Input } from "@/components/ui/input";
import { Popover } from "@/components/ui/popover";

/* edit one value where it is shown; Save closes the popover (open is controlled) */
const open = ref(false);
const limit = ref("500");
const draft = ref(limit.value);

function save() {
  limit.value = draft.value;
  open.value = false;
}
</script>

<template>
  <div class="flex w-80 items-center justify-between gap-3">
    <div class="flex flex-col">
      <span class="text-sm font-medium text-ink">Monthly spend limit</span>
      <span class="text-xs text-ink-2">$312.40 used this month</span>
    </div>
    <Popover
      v-model:open="open"
      placement="bottom-end"
      title="Monthly spend limit"
      description="Deploys pause when usage reaches the limit. Owners get an email at 80%."
      @open-change="draft = limit"
    >
      <template #trigger>
        <Button size="sm">${{ Number(limit).toLocaleString("en-US") }}</Button>
      </template>
      <form class="flex items-end gap-2" @submit.prevent="save">
        <Field label="Limit in USD" class="flex-1">
          <Input v-model="draft" inputmode="numeric" />
        </Field>
        <Button type="submit" variant="primary">Save</Button>
      </form>
    </Popover>
  </div>
</template>
```

Svelte · `spend-limit.svelte`

```svelte
<script lang="ts">
  import { Button } from "$lib/components/ui/button";
  import { Field } from "$lib/components/ui/field";
  import { Input } from "$lib/components/ui/input";
  import { Popover } from "$lib/components/ui/popover";

  /* edit one value where it is shown; Save closes the popover (open is controlled) */
  let open = $state(false);
  let limit = $state("500");
  let draft = $state("500");

  function save(event: SubmitEvent) {
    event.preventDefault();
    limit = draft;
    open = false;
  }
</script>

<div class="flex w-80 items-center justify-between gap-3">
  <div class="flex flex-col">
    <span class="text-sm font-medium text-ink">Monthly spend limit</span>
    <span class="text-xs text-ink-2">$312.40 used this month</span>
  </div>
  <Popover
    bind:open
    onopenchange={() => (draft = limit)}
    placement="bottom-end"
    title="Monthly spend limit"
    description="Deploys pause when usage reaches the limit. Owners get an email at 80%."
  >
    {#snippet trigger(props)}
      <Button {...props()} size="sm">${Number(limit).toLocaleString("en-US")}</Button>
    {/snippet}
    <form class="flex items-end gap-2" onsubmit={save}>
      <Field label="Limit in USD" class="flex-1">
        <Input bind:value={draft} inputmode="numeric" />
      </Field>
      <Button type="submit" variant="primary">Save</Button>
    </form>
  </Popover>
</div>
```

### Explain a number

React · `uptime.tsx`

```tsx
import { Info } from "@phosphor-icons/react";
import { Button } from "@/components/ui/button";
import { Popover } from "@/components/ui/popover";

/* a short explanation, no title or close button: Escape or a click outside closes it */
export default function Uptime() {
  return (
    <div className="flex items-center gap-1">
      <span className="text-sm text-ink-2">Uptime, last 30 days</span>
      <span className="text-sm font-medium text-ink">98.2%</span>
      <Popover
        closable={false}
        placement="top"
        trigger={<Button variant="ghost" size="xs" square muted aria-label="How uptime is measured" icon={<Info data-slot="icon" weight="bold" />} />}
      >
        Checked every minute from Frankfurt, Virginia and Singapore. Planned maintenance on Nov 9 is not counted.
      </Popover>
    </div>
  );
}
```

Vue · `uptime.vue`

```vue
<script setup lang="ts">
import { PhInfo } from "@phosphor-icons/vue";
import { Button } from "@/components/ui/button";
import { Popover } from "@/components/ui/popover";
</script>

<!-- a short explanation, no title or close button: Escape or a click outside closes it -->
<template>
  <div class="flex items-center gap-1">
    <span class="text-sm text-ink-2">Uptime, last 30 days</span>
    <span class="text-sm font-medium text-ink">98.2%</span>
    <Popover :closable="false" placement="top">
      <template #trigger>
        <Button variant="ghost" size="xs" square muted aria-label="How uptime is measured">
          <template #icon><PhInfo data-slot="icon" weight="bold" /></template>
        </Button>
      </template>
      Checked every minute from Frankfurt, Virginia and Singapore. Planned maintenance on Nov 9 is not counted.
    </Popover>
  </div>
</template>
```

Svelte · `uptime.svelte`

```svelte
<script lang="ts">
  import Info from "phosphor-svelte/lib/InfoIcon";
  import { Button } from "$lib/components/ui/button";
  import { Popover } from "$lib/components/ui/popover";
</script>

<!-- a short explanation, no title or close button: Escape or a click outside closes it -->
<div class="flex items-center gap-1">
  <span class="text-sm text-ink-2">Uptime, last 30 days</span>
  <span class="text-sm font-medium text-ink">98.2%</span>
  <Popover closable={false} placement="top">
    {#snippet trigger(props)}
      <Button {...props()} variant="ghost" size="xs" square muted aria-label="How uptime is measured">
        {#snippet icon()}<Info data-slot="icon" weight="bold" />{/snippet}
      </Button>
    {/snippet}
    Checked every minute from Frankfurt, Virginia and Singapore. Planned maintenance on Nov 9 is not counted.
  </Popover>
</div>
```

## Recipe

`popover.recipe.ts`:

```ts
import { tv, type VariantProps } from "../lib/tv";
import { popup } from "../lib/popup";

/* popover — a small raised surface anchored to its trigger: title, description, body, close.
 * Behaviour comes from the Zag popover machine (data-state on trigger and content).
 * The root is only a hook for the trigger; the surface lives in a portal.
 *
 * The surface and its motion are the shared popup recipe (lib/popup.ts), composed into the content;
 * its stacking comes from base.css; a popover keeps its own width, its roomier padding and a
 * taller limit. The close button and the default trigger are real Buttons
 * (the trigger: outline, sm, with this recipe's trigger classes as its className), marked
 * data-popover="trigger" and data-popover="close". */

export const popover = tv({
  slots: {
    base: "contents",
    /* a trigger in a toolbar keeps its size (passed to the Button trigger as its className) */
    trigger: "shrink-0",
    positioner: "max-w-[calc(100vw-16px)]",
    content: [
      popup(), "[--popup-max-height:480px]",
      "flex flex-col gap-1 w-max min-w-[200px] max-w-[min(320px,calc(100vw-16px))] p-3 text-sm leading-normal",
    ],
    header: "flex items-start justify-between gap-2",
    title: "min-w-0 m-0 text-sm font-medium leading-[1.4] text-ink wrap-anywhere",
    description: "text-xs leading-normal text-ink-2 wrap-anywhere",
    body: "mt-1 min-w-0 wrap-anywhere",
    /* the close button (ghost, xs, square, rounded, muted) reaches into the corner */
    close: "shrink-0 -my-[3px] ms-0 -me-1",
  },
});

export type PopoverRecipeProps = VariantProps<typeof popover>;
```
