---
title: "Popover"
description: "Popover component - floating content panels using native popover API."
slug: "popover"
---

import { DocPage, PageHeader, SkillPanel, Example, ExampleLabel, ExampleHint, ExampleCode, SourceFiles, SourceNote, StatesSection, CodeBlock, Demo, DemoCode, ChangelogEntries, ArchBody, StatsClaim, StatsCards, PageOverlay, DocText, DocLink, H2, SkillField, SkillText, SkillRow, SkillGrid, Variant, ApiPill, Dim, CodeCard, TableCard, ApiSection } from "../../lib/components/index";

<DocPage meta={meta}>

<PageHeader slug="popover" title="Popover" component="popover">
  Rich content in a floating panel, triggered by a button. Uses the native Popover API.
</PageHeader>

<SkillPanel component="popover">
  <SkillField label="Native basis">
<SkillText><code>popover</code> attribute - native Popover API with CSS anchor positioning for placement.</SkillText>
  </SkillField>

  <SkillField label="Web Platform APIs">
<SkillRow>
    <ApiPill href="https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/popover" title="native popover API"><code>popover attribute</code></ApiPill>
    <ApiPill href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#popovertarget" title="declarative trigger wiring"><code>popovertarget</code></ApiPill>
    <ApiPill href="https://developer.mozilla.org/en-US/docs/Web/CSS/position-area" title="grid-based anchor positioning"><code>position-area</code></ApiPill>
    <ApiPill href="https://developer.mozilla.org/en-US/docs/Web/CSS/position-try-fallbacks" title="automatic flip on overflow"><code>position-try-fallbacks</code></ApiPill>
    <ApiPill href="https://developer.mozilla.org/en-US/docs/Web/CSS/@starting-style" title="entry animation"><code>@starting-style</code></ApiPill>
  </SkillRow>
  </SkillField>

  <SkillField label="Classes">
<SkillRow>
    <code>.popover</code>
    <code>.popover-header</code>
    <code>.popover-title</code>
    <code>.popover-description</code>
    <code>.popover-content</code>
  </SkillRow>
  </SkillField>

  <SkillField label="Attributes">
<div style="display:flex;flex-wrap:wrap;gap:0.375rem;margin-bottom:0;">
    <code>data-side="top|right|bottom|left"</code>
    <code>data-align="start|center|end"</code>
  </div>
  </SkillField>
</SkillPanel>

<p class="text-sm font-medium mb-2 mt-6">Default</p>

<p class="text-muted-foreground text-sm mb-4">Click the trigger to open a popover below (default side). Uses native Popover API - no JS for open/close.</p>

```html example height="22"
<button class="btn" data-variant="outline" popovertarget="demo-popover">Open Popover</button>
<div class="popover" id="demo-popover" popover>
  <div class="popover-header">
    <p class="popover-title">Dimensions</p>
    <p class="popover-description">Set the dimensions for the layer.</p>
  </div>
  <div class="popover-content">
    <p style="margin: 0; font-size: 0.8125rem; color: var(--muted-foreground)">Width: 100% • Height: auto</p>
  </div>
</div>
```

<p class="text-sm font-medium mb-2 mt-8">With Form</p>

<p class="text-muted-foreground text-sm mb-4">Popover containing form fields - the canonical shadcn popover pattern.</p>

```html example height="22"
<button class="btn" data-variant="outline" popovertarget="demo-popover-form">Open Popover</button>
<div class="popover" id="demo-popover-form" popover style="width: 20rem">
  <div class="popover-header">
    <p class="popover-title">Dimensions</p>
    <p class="popover-description">Set the dimensions for the layer.</p>
  </div>
  <div class="popover-content">
    <div style="display: grid; gap: 0.5rem">
      <div style="display: grid; grid-template-columns: 1fr 2fr; align-items: center; gap: 1rem">
        <label class="label" for="pop-width" style="text-align: right">Width</label>
        <input class="input" id="pop-width" type="text" value="100%" style="height: 2rem; font-size: 0.8125rem" />
      </div>
      <div style="display: grid; grid-template-columns: 1fr 2fr; align-items: center; gap: 1rem">
        <label class="label" for="pop-max-width" style="text-align: right">Max. width</label>
        <input class="input" id="pop-max-width" type="text" value="300px" style="height: 2rem; font-size: 0.8125rem" />
      </div>
      <div style="display: grid; grid-template-columns: 1fr 2fr; align-items: center; gap: 1rem">
        <label class="label" for="pop-height" style="text-align: right">Height</label>
        <input class="input" id="pop-height" type="text" value="25px" style="height: 2rem; font-size: 0.8125rem" />
      </div>
      <div style="display: grid; grid-template-columns: 1fr 2fr; align-items: center; gap: 1rem">
        <label class="label" for="pop-max-height" style="text-align: right">Max. height</label>
        <input class="input" id="pop-max-height" type="text" value="none" style="height: 2rem; font-size: 0.8125rem" />
      </div>
    </div>
  </div>
</div>
```

<p class="text-sm font-medium mb-2 mt-8">Align</p>

<p class="text-muted-foreground text-sm mb-4">Use <code>data-align</code> on the popover to control horizontal alignment: <code>start</code>, <code>center</code> (default), or <code>end</code>.</p>

```html example height="22"
<button class="btn" data-variant="outline" popovertarget="demo-pop-start">Start</button>
<div class="popover" id="demo-pop-start" popover data-align="start">
  <div class="popover-header">
    <p class="popover-title">Start aligned</p>
    <p class="popover-description">Popover aligned to the start edge.</p>
  </div>
</div>
<button class="btn" data-variant="outline" popovertarget="demo-pop-center">Center</button>
<div class="popover" id="demo-pop-center" popover>
  <div class="popover-header">
    <p class="popover-title">Center aligned</p>
    <p class="popover-description">Default center alignment.</p>
  </div>
</div>
<button class="btn" data-variant="outline" popovertarget="demo-pop-end">End</button>
<div class="popover" id="demo-pop-end" popover data-align="end">
  <div class="popover-header">
    <p class="popover-title">End aligned</p>
    <p class="popover-description">Popover aligned to the end edge.</p>
  </div>
</div>
```

<p class="text-sm font-medium mb-2 mt-8">Side</p>

<p class="text-muted-foreground text-sm mb-4">Use <code>data-side</code> to position the popover on a specific side of the trigger: <code>top</code>, <code>right</code>, <code>bottom</code> (default), or <code>left</code>.</p>

```html example height="22"
<button class="btn" data-variant="outline" popovertarget="demo-pop-top">Top</button>
<div class="popover" id="demo-pop-top" popover data-side="top">
  <div class="popover-header">
    <p class="popover-title">Top side</p>
    <p class="popover-description">Positioned above the trigger.</p>
  </div>
</div>
<button class="btn" data-variant="outline" popovertarget="demo-pop-right">Right</button>
<div class="popover" id="demo-pop-right" popover data-side="right">
  <div class="popover-header">
    <p class="popover-title">Right side</p>
    <p class="popover-description">Positioned to the right.</p>
  </div>
</div>
<button class="btn" data-variant="outline" popovertarget="demo-pop-bottom">Bottom</button>
<div class="popover" id="demo-pop-bottom" popover>
  <div class="popover-header">
    <p class="popover-title">Bottom side</p>
    <p class="popover-description">Default bottom positioning.</p>
  </div>
</div>
<button class="btn" data-variant="outline" popovertarget="demo-pop-left">Left</button>
<div class="popover" id="demo-pop-left" popover data-side="left">
  <div class="popover-header">
    <p class="popover-title">Left side</p>
    <p class="popover-description">Positioned to the left.</p>
  </div>
</div>
```

```html example label="Density" hint="Set data-density on the component root to scale its content padding. A whitespace policy, not a zoom: only padding scales (ratio 0.75 / 1 / 1.25), typography stays identical. comfortable matches the unsized default." height="22"
<div class="flex flex-wrap gap-2">
  <button class="btn" data-variant="outline" popovertarget="density-popover-compact">Compact</button>
  <div class="popover" id="density-popover-compact" popover data-density="compact">
    <div class="popover-header"
      ><p class="popover-title">Compact density</p><p class="popover-description">Content padding 0.75rem.</p></div
    >
  </div>
  <button class="btn" data-variant="outline" popovertarget="density-popover-comfortable">Comfortable</button>
  <div class="popover" id="density-popover-comfortable" popover data-density="comfortable">
    <div class="popover-header"
      ><p class="popover-title">Comfortable density</p
      ><p class="popover-description">Identical to the unsized default - 1rem.</p></div
    >
  </div>
  <button class="btn" data-variant="outline" popovertarget="density-popover-spacious">Spacious</button>
  <div class="popover" id="density-popover-spacious" popover data-density="spacious">
    <div class="popover-header"
      ><p class="popover-title">Spacious density</p><p class="popover-description">Content padding 1.25rem.</p></div
    >
  </div>
</div>
```

<StatesSection>
  <p class="text-sm text-muted-foreground m-0 mb-3">Named states via the shared State API, driven per popover through the bound <code>api</code>:</p>
  <ul class="text-sm text-muted-foreground m-0 mb-3 ps-5">
    <li><code>default</code> - hidden (the authored state; toggled by <code>popovertarget</code>)</li>
    <li><code>open</code> - shown via the native <code>showPopover()</code></li>
  </ul>
  <p class="text-sm text-muted-foreground">The first demo carries <code>data-state-demo</code>; <code>bun run screenshots</code> drives it via <code>el.api.setState(name)</code> and captures <code>screenshots/\{mode\}/popover-\{state\}.png</code>.</p>

Machine contract - verified against <a class="link" href="../schemas/popover.schema.json"><code>popover.schema.json</code></a> by <code>bun run verify</code>:

```states
| State | Type | Values | Default | Description |
| --- | --- | --- | --- | --- |
| `open` | `boolean` | `true`, `false` | `false` | Shown - driven through the component's own State API. |
```

</StatesSection>

<ApiSection component="popover" />

<SourceFiles component="popover" section="css">
  <SourceNote for="css">Uses <code>position-area</code> for anchor positioning, <code>@starting-style</code> for enter animations, and <code>position-try-fallbacks</code> for automatic viewport-overflow flipping. Supports <code>data-side</code> and <code>data-align</code> attributes.</SourceNote>
</SourceFiles>

<SourceFiles component="popover" section="js">
  <SourceNote for="js">Assigns unique CSS anchor names per trigger–popover pair. Opening, closing, and light-dismiss are handled entirely by the native <code>popovertarget</code> attribute.</SourceNote>
</SourceFiles>

</DocPage>
