Popover

A panel of any content that opens beside the button that names it, flipping to stay on the screen and closing on Escape or a click outside, with no script.

Example

View Code
<button class="button" type="button" popovertarget="share">Share</button>

<div class="popover" id="share" popover>
    <h3>Share this page</h3>
    <p>Anyone with the link can read it.</p>
    <button class="button" type="button">Copy link</button>
</div>

When to use it

A small panel of anything beside the button that opens it: a share box with a link to copy, a short explanation, a filter or two, a quick form. The page stays usable while it is open.

It is not a menu. A list of links and actions belongs in a dropdown, which turns each one into a menu row. It is not a hint either: a few words that explain a control, with nothing to click, are a tooltip. And a question the reader must answer before going on is a dialog.

How it works

The panel is a native popover. The button carries popovertarget with the panel's id, and the panel carries popover and the class. The browser opens it, puts it in the top layer, closes it on Escape or a click outside, and keeps the button's expanded state current. No script is needed.

The button that opens the panel is its anchor, so there is no wrapper and no anchor name. The panel can sit anywhere in the page, and a page can hold many. In Chromium and Firefox the browser also puts the panel straight after its button in the tab order, wherever it sits in the markup. WebKit was not measured, because its Tab key skips links and buttons by default.

A panel opened by script has no button to sit beside unless you pass one as source: panel.showPopover({ source: button }).

<button class="button" type="button" popovertarget="share">Share</button>

<div class="popover" id="share" popover>
    <h3>Share this page</h3>
    <p>Anyone with the link can read it.</p>
    <button class="button" type="button">Copy link</button>
</div>

What it holds is ordinary flow content. Links look like links and buttons like buttons, and the panel spaces its own children: the medium space between siblings, and the smaller heading space after a heading. The panel fades in and closes at once.

The panel's background is --yeti-popover-surface, its corner is --yeti-popover-radius, and its padding is --yeti-popover-padding.

Placement

By default the panel opens below its button, lined up with the button's start edge, with a small gap between them. Pick another side with data-placement: bottom (the default), top, start or end. start and end follow the writing direction, so in a right-to-left page end is to the button's left.

Where there is no room on that side, the panel flips to the other. Where it would run past the end of the screen, it lines up with the button's other edge instead, and it shifts to stay on the screen when neither fits.

View Code
<button class="button" type="button" popovertarget="hint">What is this?</button>

<div class="popover" id="hint" popover data-placement="end">
    <p>Your plan renews on the first of each month.</p>
</div>

Where anchor positioning is missing the browser centers the panel on the screen. It still opens, closes and reads correctly.

Width

The panel is as wide as its content, up to data-max, from the width scale. The default is xs. On a narrow screen it is never wider than the screen less a margin each side.

A panel that holds a form

Make the panel a dialog element when it holds a form. A screen reader then announces a named dialog, and autofocus on a field works. It is still not modal: the page behind stays usable, and a click outside closes it.

View Code
<button class="button" type="button" popovertarget="rename">Rename</button>

<dialog class="popover" id="rename" popover aria-labelledby="rename-title">
    <h3 id="rename-title">Rename</h3>
    <div class="field">
        <label for="rename-name">Name</label>
        <input id="rename-name" type="text" autofocus>
    </div>
</dialog>

What it is not

There is no arrow pointing at the button, because an arrow cannot follow the panel when it flips. It does not open on hover: that is the tooltip's job, or the dropdown's hover.js. A menu of links belongs in a dropdown.

Accessibility

Give the button a name. The browser sets its expanded state from popovertarget, so a screen reader hears whether the panel is open. Escape closes the panel and returns focus to the button.

In Chromium and Firefox, Tab moves from the button into the panel and, from the panel's last control, on to whatever follows the button, wherever the panel sits in the markup. WebKit was not measured, because its Tab key skips links and buttons by default. A screen reader reading the page in order still meets the panel where it sits, so keep it close to its button, and inside the same landmark.

Attributes

Attribute Type Values Default Description
data-placement enum top, bottom, start, end bottom Which side of its button the panel opens on. start and end follow the writing direction. Where there is no room on that side it flips to the other.
data-max enum 3xs, 2xs, xs, sm, md, lg, xl, 2xl xs The widest the panel may grow, from the width scale; it is always narrower than the viewport by a gutter each side.

Children

  • > *: at least 1. The contents: ordinary flow content, such as a heading, a sentence, a form or a button.

Tokens

Token Description
--yeti-border-width Width of the edge the panel keeps in forced colors.
--yeti-popover-surface Background of the panel.
--yeti-popover-radius Corner of the panel.
--yeti-popover-padding Padding inside the panel.
--yeti-color-text The panel's text.
--yeti-shadow-md The panel's shadow.
--yeti-width-xs The default maximum, when data-max is absent.
--yeti-space-md The least margin kept between the panel and each side of the viewport, and the space between its children.
--yeti-space-xs The gap between the button and the panel.
--yeti-heading-space-after The space after a heading inside the panel.
--yeti-duration-fast How long the panel takes to fade in.
--yeti-ease The curve of that fade.
Internal tokens (may change between minor versions) - `--_yeti-max`

Accessibility

  • The button needs a name, and the browser sets its expanded state from popovertarget. In Chromium and Firefox the browser puts the panel straight after its button in the tab order, wherever the panel sits in the markup; WebKit was not measured, because its Tab key skips links and buttons by default. A panel is not modal: the page behind stays usable, and a click outside closes it. For a panel that holds a form, make it a dialog element with aria-labelledby naming its heading, so a screen reader announces a named dialog and autofocus on a field works. Do not open it on hover, and keep a menu of links in a dropdown.
Key Action
Enter / Space On the button, opens or closes the panel.
Tab From the button, moves into the panel; from the panel's last control, moves to what follows the button.
Escape Closes the panel and returns focus to its button.

Browser support

  • Used without guards: popover, @starting-style
  • Behind @supports: anchor positioning, position-try (fallback: the panel is centered by the user agent)

JavaScript

None. This component is CSS only.

Available since 7.0.0.