A place to cache linked articles (think custom and personal wayback machine)
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

title: Popover API (Explainer) url: https://open-ui.org/components/popover.research.explainer/ hash_url: bf61b62532 archive_date: 2024-02-03 og_image: description: A very common UI pattern on the Web, for which there is no native API, is “popover UI”, also sometimes called “popovers”, “pop up UI”, or “popovers”. Popovers are a general class of UI that have three common behaviors favicon: https://open-ui.org/images/favicon-32x32.png language: en_US

NOTE: This Popover API explainer was mostly useful during the development of the feature. While it is roughly still in line with the actual feature, it might be more informative to look at either of these two sources of documentation:

  1. The MDN page documenting the Popover API
  2. The HTML spec for the popover attribute

A very common UI pattern on the Web, for which there is no native API, is “popover UI”, also sometimes called “popovers”, “pop up UI”, or “popovers”. Popovers are a general class of UI that have three common behaviors:

  1. Popovers always appear on top of other page content.
  2. Popovers are ephemeral. When the user “moves on” to another part of the page (e.g. by clicking elsewhere, or hitting ESC), the popover hides.
  3. Popovers (of a particular type) are generally “one at a time” - opening one popover closes others.

This document proposes a set of APIs to make this type of UI easy to build.

Here are the goals for this API:

  • Allow any element and its (arbitrary) descendants to be rendered on top of all other content in the host web application.
  • Include “light dismiss” management functionality, to remove the element/descendants from the top-layer upon certain actions such as hitting Esc (or any close signal) or clicking outside the element bounds.
  • Allow this “top layer” content to be fully styled, sized, and positioned at the author’s discretion, including properties which require compositing with other layers of the host web application (e.g. the box-shadow or backdrop-filter CSS properties).
  • Allow these top layer elements to reside at semantically-relevant positions in the DOM. I.e. it should not be required to re-parent a top layer element as the last child of the document.body simply to escape ancestor containment and transforms.
  • Include an appropriate user input and focus management experience, with flexibility to modify behaviors such as initial focus.
  • Accessible by default, with the ability to further extend semantics/behaviors as needed for the author’s specific use case.
  • Avoid developer footguns, such as improper stacking of dialogs and popovers, and incorrect accessibility mappings.
  • Avoid the need for any JavaScript for most common cases.
  • Easy animation of show/hide operations.

See the original <popup> element explainer, and also the comments on Issue 410 and Issue 417. See also this CSSWG discussion which has mostly been about a CSS alternative for top layer.

This proposal was discussed in OpenUI on Issue 455, which was closed as resolved. In WHATWG/html, Issue 7785 tracks the addition of this feature.

There have been many discussions and resolutions at OpenUI of various aspects of this proposal, and those are listed in the Design Decisions section. Requests for standards positions have been requested from Gecko and WebKit.

There have been three TAG reviews for this set of functionality: #599 (closed waiting for anchor positioning), #680 (closed recommending change from <popup> to popup attribute), #743 (open).

A natural question that commonly arises is: what’s the difference between a “popover” and a “dialog”? There are two ways to interpret this question:

  1. What are the difference between these general UX patterns and words?
  2. What are the technical implementation differences between <div popover> and <dialog>?

In both cases, an important distinction needs to be made between modal and non-modal (or modeless) dialogs. With a modal dialog, the rest of the page (outside the dialog) is rendered inert, so that only the contents of the dialog are interactable. Importantly, a popover is non-modal. Almost by definition, a popover is not permanent: it goes away (via light dismiss) when the user changes their attention to something else, by clicking or tabbing to something else. For these reasons, if the dialog in question needs to be modal, then the <dialog> element is the way to go.

Having said that, dialogs can also be non-modal, and popovers can be set to not light-dismiss (via popover=manual). There is a significant area of overlap between the two Web features. Some use cases that lie in this area of overlap include:

  1. “Toasts” or asynchronous notifications, which stay onscreen until dismissed manually or via a timer.
  2. Persistent UI, such as teaching-UI, that needs to stay on screen while the user interacts with the page.
  3. Custom components that need to “manually” control popover behavior.

Given these use cases, it’s important to call out the technical differences between a non-modal <dialog> and a <div popover=manual>:

  • A <div popover=manual> is in the top layer, so it draws on top of other content. The same is not true for a non-modal <dialog>. This is likely the most impactful difference, as it tends to be difficult to ensure that a non-modal <dialog> is painted on top of other page content.
  • A <dialog> element always has role=dialog, while the popover attribute can be applied to the most-semantically-relevant HTML element, including the <dialog> element itself: <dialog popover>.
  • The popover API comes with some “nice to haves”:
    • popovers are easy to animate both the show and hide transitions, via pure CSS. In contrast, JS is required in order to animate dialog.close().
    • popovers work with the invoking attributes (e.g. popovertarget) to declaratively show/hide them with pure HTML. In contrast, JS is required to show/close a <dialog>.
    • popovers fire both a “popovershow” and a “popoverhide” event. In contrast, a <dialog> only fires cancel and close, but no event is fired when it is shown.

For the above reasons, it seems clear that for use cases that need a non-modal dialog which has popover behavior, a <dialog popover> (with the most appropriate value for the popover attribute) should be preferred. Importantly, if the use case is not meant to be exposed as a “dialog”, then another (non-<dialog>) element should be used with the popover attribute, or a generic <div popover role=something> should be used with the appropriate role added.

This section lays out the full details of this proposal. If you’d prefer, you can skip to the examples section to see the code.

A new content attribute, popover, controls both the top layer status and the dismiss behavior. There are several allowed values for this attribute:

  • popover=auto - A top layer element following “Auto” dismiss behaviors (see below).
  • popover=manual - A top layer element following “Manual” dismiss behaviors (see below).

Additional values for the popover attribute may become available in the future.

So this markup represents popover content:

<div popover=“auto”>I am a popover</div>

As written above, the <div> will be rendered display:none by the UA stylesheet, meaning it will not be shown when the page is loaded. To show the popover, one of several methods can be used: declarative triggering, JavaScript triggering.

Additionally, the popover attribute can be used without a value (or with an empty string ”” value), and in that case it will behave identically to popover=auto:

<div popover=“auto”>I am a popover</div>
<div popover>I am also an “auto” popover</div>
<div popover=””>So am I</div>

For convenience and brevity, the remainder of this explainer will use this boolean-like syntax in most cases, e.g. <div popover>.

There are several ways to “show” a popover, and they are discussed in this section. When any of these methods are used to show a popover, it will be made visible and moved (by the UA) to the top layer. The top layer is a layer that paints on top of all other page content, with the exception of other elements currently in the top layer. This allows, for example, a “stack” of popovers to exist.

Note: It is intended that invoketarget will replace popovertarget leading to popovertarget’s eventual deprecation.

A common design pattern is to have a button which makes a popover visible. To facilitate this pattern, and avoid the need for JavaScript in this common case, two content attributes (popovertarget and popovertargetaction) allow the developer to declaratively toggle, show, or hide a popover. To do so, the attribute’s value should be set to the idref of another element:

<button popovertarget=“foo”>Toggle the popover</button>
<div id=“foo” popover>Popover content</div>

When the button in this example is activated, the UA will call .showPopover() on the <div id=foo popover> element if it is currently hidden, or hidePopover() if it is showing. In this way, no JavaScript will be necessary for this use case.

If the desire is to have a button that only shows or only hides a popover, the following markup can be used:

<button popovertarget=“foo”>Toggle the popover</button>
<button popovertarget=“foo” popovertargetaction=“show”>This button only shows the popover</button>
<button popovertarget=“foo” popovertargetaction=“hide”>This button only hides the popover</button>
<div id=“foo” popover>Popover content</div>

Note that the default value for popovertargetaction is “toggle” which can also be explicitly specified.

When the popovertarget attribute is applied to an activating element, the UA will automatically map this element with the appropriate accessibility semantics. For instance, the initial implementation will expose the appropriate aria-expanded state based on whether the popover is shown or hidden. As the popover API matures, there may need to be further discussion with the ARIA working group to determine if additional ARIA semantics, if any, are necessary.

These attributes are only supported on buttons (including <button>, <input type=button>, etc.) as long as the button would not otherwise submit a form. For example, this is not supported: <form><input type=submit popovertarget=foo></form>. In that case, the form would be submitted, and the popover would not be toggled.

The declarative trigger attributes can also be accessed via IDL:

// Note that popoverTargetElement directly sets an element reference:
myButton.popoverTargetElement = myElement
myButton.popoverTargetAction = ‘show’

To show and hide the popover via JavaScript, there are three methods on HTMLElement:

const popover = document.querySelector(‘[popover]’)
popover.showPopover() // Show the popover
popover.hidePopover() // Hide a visible popover
popover.togglePopover() // Toggle the open/closed state of a popover

Calling showPopover() on an element that has a valid value for the popover attribute will cause the UA to remove the display:none rule from the element and move it to the top layer. Calling hidePopover() on a showing popover will remove it from the top layer, and re-apply display:none.

There are several conditions that will cause showPopover(), hidePopover(), and togglePopover() to throw an exception:

  1. Calling showPopover() on a valid popover that is already in the showing state. This will throw an InvalidStateError DOMException.
  2. Calling hidePopover() on a valid popover that is not currently showing. This will throw an InvalidStateError DOMException.
  3. Calling any of the three methods on an element that does not contain a valid value of the popover attribute. This will throw a NotSupportedError DOMException.
  4. Calling any of the three methods on a valid popover that is not connected to a document. This will throw an InvalidStateError DOMException.

When a popover is open, it will match the :popover-open pseudo class:

const popover = document.createElement(‘div’)
popover.popover = ‘auto’
popover.matches(’:popover-open’) === false
popover.showPopover()
popover.matches(’:popover-open’) === true

The UA stylesheet for popovers will look like this:

[popover] {
  position: fixed;
  inset: 0;
  width: fit-content;
  height: fit-content;
  margin: auto;
  border: solid;
  padding: 0.25em;
  overflow: auto;
  color: CanvasText;
  background-color: Canvas;
}

[popover]::backdrop {
    pointer-events: none !important;
}

[popover]{match this when popover is hidden} {
  display: none;
}

As seen above, the styling rules manage “showing” vs. “hidden” via these UA stylesheet rules:

[popover]{when hidden}: {
  display: none;
}
[popover] {
  position: fixed;
}

The above rules mean that a popover, when not “shown”, has display:none applied, and that style is removed when one of the methods above is used to show the popover. Note that the display:none UA stylesheet rule is not !important. In other words, developer style rules can be used to override this UA style to make a not-showing popover visible in the page. In this case, the popover will not be displayed in the top layer, but rather at its ordinary z-index position within the document. This can be used, for example, to make popover content “return to the page” instead of becoming hidden. Care must be taken, if the display property is set by developer CSS, to ensure the popover visibility isn’t adversely affected. For example, developer CSS could do this:

[popover] {
  /* Make the popover a flexbox: /
  display: flex;
}
[popover]:not(:popover-open) {
  / But make sure not to show it unless it’s open: /
  display: none;
}

Be mindful when using other stylesheets that you haven’t authored. These may set the display value on your popover elements. For example, if you use a menu element as a popover and you use earlier versions of normalize.css. In this case, normalize.css has a rule that sets display: flex on menu elements.

An early version of the Popover API included custom behaviors to make animations and transitions of popovers trivially easy. During the standardization process, however, this functionality was removed, with the intention to replace it with a more general capability for the web platform that also applies to other top layer elements. This section describes how that new set of capabilities will work together to help animate popovers (and modal <dialog>s). There is also a separate explainer for this set of features.

The new capabilities include the ones described in these five CSSWG issues:

Assuming all five of the above features land in specs and browsers in roughly the form they’re currently being discussed, then to animate a popover’s show/hide transitions you would use:

[popover] {
  --transition-time: 0.5s;
  transition: display var(--transition-time), overlay-behavior var(--transition-time),
    opacity var(--transition-time);
}
[popover]:popover-open {
  @initial {
    opacity: 0;
  }
  opacity: 1;
}

The above CSS will result in the opacity (and also display and top-layer-ness) being transitioned during both show and hide transitions.

This can also be achieved with the equivalent CSS animations:

[popover]:popover-open {
  animation: fadein 0.5s;
  transition: overlay-behavior 0.5s;
  opacity: 1; / UA default /
}
@keyframes fadein {
  from {
    opacity: 0;
  }
}
[popover]:closed {
  animation: fadeout 0.5s;
}
@keyframes fadeout {
  from {
    display: block;
  }
  to {
    opacity: 0;
  }
}

Again, the above descriptions depend on all five CSSWG issues above landing in standards and browsers.

The popover content attribute will be reflected as a nullable IDL attribute:

[Exposed=Window]
partial interface Element {
  attribute DOMString? popover;
}

This not only allows developer ease-of-use from JavaScript, but also allows for a feature detection mechanism:

function supportsPopover() {
  return HTMLElement.prototype.hasOwnProperty(‘popover’)
}

Further, only valid values of the content attribute will be reflected to the IDL property, with invalid values being reflected as the value manual. For example:

const div = document.createElement(‘div’)
div.setAttribute(‘popover’, ‘AUTO’)
div.popover === ‘auto’ // true (note lowercase)
div.setAttribute(‘popover’, ‘invalid!’)
div.popover === ‘manual’ // true
div.removeAttribute(‘popover’)
div.popover === null // true

This allows feature detection of the values, for forward compatibility:

function supportsPopoverType(type) {
  if (!HTMLElement.prototype.hasOwnProperty(‘popover’)) return false // Popover API not supported
  const testElement = document.createElement(‘div’)
  testElement.popover = type
  return testElement.popover == type.toLowerCase()
}
supportsPopoverType(‘manual’) === true
supportsPopoverType(‘invalid!’) === false

An event is fired synchronously when a popover is shown or hidden. This event can be used, for example, to populate content for the popover just in time before it is shown, or update server data when it closes. The event provides a currentState and newState for the popover. The value of these properties can either be “open” or “closed”. The events looks like this:

const popover = Object.assign(document.createElement(‘div’), { popover: ‘auto’ })
popover.addEventListener(‘beforetoggle’, ({ currentState, newState }) => {
  if (currentState === ‘closed’) console.info(‘Popover is closed’)
  if (newState === ‘open’) console.info(‘Popover is being shown’)
})

The beforetoggle event is cancellable when the newState is equal to “open”. Doing so keeps the popover from being shown. You can’t cancel the hiding of a popover as that would interfere with the one-at-a-time and light-dismiss behaviors. The beforetoggle event is fired synchronously.

Elements that move into the top layer may require focus to be moved to that element, or a descendant element. However, not all elements in the top layer will require focus. For example, a modal <dialog> will have focus set to its first interactive element, if not the dialog element itself, because a modal dialog is something that requires immediate attention. On the other hand, a <div popover=manual>, which may represent a dynamic notification message (commonly referred to as a “notification”, “toast”, or “alert”), or potentially a persistent chat widget, should not immediately receive focus, even if it contains focusable elements. This is because such popovers are meant for out-of-band communication of state, and are not meant to interrupt a user’s current action. Additionally, if the top layer element should receive immediate focus, there is a question about which part of the element gets that initial focus. For example, the element itself could receive focus, or one of its focusable descendants could receive focus.

To provide control over these behaviors, the autofocus attribute can be used on or within popovers. When present on a popover or one of its descendants, it will result in focus being moved to the popover or the specified element when the popover is shown. Note that autofocus is already a global attribute, but the existing behavior applies to element focus on page load. This proposal extends that definition to be used within popovers, and the focus behavior happens when they are shown. Note that adding autofocus to a popover descendant does not cause the popover to be shown on page load, and therefore it does not cause focus to be moved into the popover on page load, unless the defaultopen attribute is also used.

The autofocus attribute allows control over the focus behavior when the popover is shown. When the popover is hidden, often the most user friendly thing to do is to return focus to the previously-focused element. The <dialog> element currently behaves this way. However, for popovers, there are some nuances. For example, if the popover is being hidden via light dismiss, because the user clicked on another element outside the popover, the focus should not be returned to another element, it should go to the clicked element (if focusable, or <body> if not). There are a number of other such considerations. The behavior on hiding the popover is:

  • A popover element has a previously focused element, initially null, which is set equal to document.activeElement when the popover is shown, if a) the popover is a auto popover, and b) if the popover stack is currently empty. The previously focused element is set back to null when a popover is hidden.

  • When a popover is hidden, focus is set back to the previously focused element, if it is non-null, in the following cases:

    1. Light dismiss via close signal (e.g. Escape key pressed).
    2. Hide popover from JavaScript via hidePopover().
    3. Hide popover via a popover-contained triggering element with popovertargetaction=“hide” or “toggle”. The triggering element must be popover-contained, otherwise the keyboard or mouse activation of the triggering element should have already moved focus to that element.</li><li>Hide popover because its <code>popover</code> type changes (e.g. via <code>myPopover.popover='something else';</code>).</li></ol></li><li><p>Any other actions which hide the popover will <strong>not</strong> cause the focus to be changed when the popover hides. In these cases, the “normal” behavior happens for each action. Some examples include:</p><ol><li>Click outside the popover (focus the clicked thing).</li><li>Click on a <strong>non-popover-contained</strong>* triggering element to close popover (focus the triggering element). This is a special case of the point just above.</li><li>Tab-navigate (focus the next tabbable element in the document’s focus order).</li><li>Hide popover because it was removed from the document (event handlers not allowed while removing elements).</li><li>Hide popover when a modal dialog or fullscreen element is shown (follow dialog/fullscreen focusing steps).</li><li>Hide popover via <code>showPopover()</code> on another popover that hides this one (follow new popover focusing steps).</li></ol></li></ul><p>The intention of the above set of behaviors is to return focus to the previously focused element when that makes sense, and not do so when the user’s intention is to move focus elsewhere or when it would be confusing.</p><p>* In the above, “a popover contained triggering element” means the triggering element is contained within <strong>the popover being hidden</strong>, not any arbitrary popover.</p><p>A new attribute, <code>anchor</code>, can be used on a popover element to refer to the popover’s “anchor”. The value of the attribute is a string idref corresponding to the <code>id</code> of another element (the “anchor element”). This anchoring relationship is used for two things:</p><ol><li>Establish the provided anchor element as an <a href="#nearest-open-ancestral-popover">“ancestor”</a> of this popover, for light-dismiss behaviors. In other words, the <code>anchor</code> attribute can be used to form nested popovers.</li><li>The referenced anchor element could be used by the <a href="https://drafts.csswg.org/css-anchor-1/">Anchor Positioning feature</a>.</li></ol><p>The anchor attribute can also be accessed via IDL:</p><pre class="astro-code github-dark" tabindex="0"><code><span class="line"><span>// This sets the IDREF for the anchor element, and not the element itself:</span></span> <span class="line"><span>myPopover.anchor </span><span>=</span><span> idref</span></span></code></pre><p>Akin to modal <code>&lt;dialog&gt;</code> and fullscreen elements, popovers allow access to a <code>::backdrop</code> pseudo element, which is a full-screen element placed directly behind the popover in the top layer. This allows the developer to do things like blur out the background when a popover is showing:</p><pre class="astro-code github-dark" tabindex="0"><code><span class="line"><span>&lt;</span><span>div</span><span> popover</span><span>&gt;I'm a popover&lt;/</span><span>div</span><span>&gt;</span></span> <span class="line"><span>&lt;</span><span>style</span><span>&gt;</span></span> <span class="line"><span> [</span><span>popover</span><span>]</span><span>::backdrop</span><span> {</span></span> <span class="line"><span> backdrop-filter</span><span>: </span><span>blur</span><span>(</span><span>3</span><span>px</span><span>);</span></span> <span class="line"><span> }</span></span> <span class="line"><span>&lt;/</span><span>style</span><span>&gt;</span></span></code></pre><p>Note that in contrast to the <code>::backdrop</code> pseudo element for modal dialogs and fullscreen elements, the <code>::backdrop</code> for a popover is styled by a UA stylesheet rule <code>pointer-events: none !important</code>, which means it cannot trap clicks outside the popover. This ensures the “click outside” light dismiss behavior continues to function. For this reason, and because <code>popover=manual</code> popovers do not have light-dismiss behavior, it is not recommended that the <code>::backdrop</code> be styled in a non-transparent way for <code>popover=manual</code> popovers. Doing so would obscure the rest of the page, while still allowing the user to click or keyboard navigate to obscured elements. In this case, a better approach might be to use a modal <code>&lt;dialog&gt;</code> element, which will trap focus within the <code>&lt;dialog&gt;</code>.</p><p>For elements that are displayed on the top layer via this API, there are a number of things that can cause the element to be removed from the top-layer, besides the ones <a href="#showing-and-hiding-a-popover">described above</a>. These fall into three main categories:</p><ul><li><strong>One at a Time.</strong> Another element being added to the top-layer causes existing top layer elements to be removed. This is typically used for “one at a time” type elements: when one popover is shown, other popovers should be hidden, so that only one is on-screen at a time. This is also used when “more important” top layer elements get added. For example, fullscreen elements should close all open popovers.</li><li><strong>Light Dismiss.</strong> User action such as clicking outside the element, hitting Escape, or causing keyboard focus to leave the element should all cause a displayed popover to be hidden. This is typically called “light dismiss”, and is discussed in more detail in <a href="#light-dismiss">this section</a>.</li><li><strong>Other Reasons.</strong> Because the top layer is a UA-managed resource, it may have other reasons (for example a user preference) to forcibly remove elements from the top layer.</li></ul><p>In all such cases, the UA is allowed to forcibly remove an element from the top layer and re-apply the <code>display:none</code> popover UA rule. The rules the UA uses to manage these interactions depends on the element types, and this is described in more detail in <a href="#classes-of-top-layer-ui">this section</a>.</p><p>The term “light dismiss” for a popover is used to describe the user “moving on” to another part of the page, or generally being done interacting with the popover. When this happens, the popover should be hidden. Several actions trigger light dismiss of a popover:</p><ol><li>Clicking or tapping outside the bounds of the popover. Any <code>mousedown</code> event will trigger <strong>all open popovers</strong> to be hidden, starting from the top of <a href="#the-popover-stack">the popover stack</a>, and ending with the <a href="#nearest-open-ancestral-popover">nearest open ancestral popover</a> of the <code>mousedown</code> event’s <code>target</code> <code>Node</code>. This means clicking on a popover or its trigger or anchor elements will not hide that popover.</li><li>Hitting the Escape key, or generally any <a href="#close-signal">“close signal”</a>. This will cause the topmost popover on <a href="#the-popover-stack">the popover stack</a> to be hidden.</li></ol><p>For <code>popover=auto</code> only, it is possible to have “nested” popovers. I.e. two popovers that are allowed to both be open at the same time, due to their relationship with each other. A simple example where this would be desired is a popover menu that contains sub-menus: it is commonly expected to support this pattern, and keep the main menu showing while the sub-menu is shown.</p><p>Popover nesting is not possible/applicable to the other popover types, such as <code>popover=manual</code>.</p><p>The <code>Document</code> contains a “stack of open popovers”, which is initially empty. When a <code>popover=auto</code> element is shown, that popover is pushed onto the top of the stack, and when a <code>popover=auto</code> element is hidden, it is popped from the top of the stack.</p><p>The “nearest open ancestral popover” <code>P</code> to a given <code>Node</code> N is defined in this way:</p><blockquote><p><code>P</code> is the topmost popover in <a href="#the-popover-stack">the popover stack</a> for which any one of the following is true:</p><ol><li><code>P</code> is a flat-tree DOM ancestor of <code>N</code>.</li><li><code>N</code> is a descendant of another popover <code>P2</code>, which has an <code>anchor</code> attribute whose value is equal to <code>P</code>’s <code>id</code> or any of <code>P</code>’s flat-tree descendant’s <code>id</code>s.</li><li><code>P</code> contains a <a href="#declarative-triggers">triggering element</a> whose target is another popover <code>P2</code>, which contains <code>N</code>.</li></ol><p>If none of the popovers in <a href="#the-popover-stack">the popover stack</a> match the above conditions, then <code>P</code> is <code>null</code>.</p></blockquote><p>The purpose of the algorithm above is to allow “nesting” of <code>popover=auto</code> popovers in several natural ways:</p><ol><li><strong>Using traditional DOM tree descendants:</strong> when one popover is a descendant of another popover, these two are “nested”.</li><li><strong>Using the <code>anchor</code> attribute:</strong> when a popover has an <code>anchor</code> attribute that points to another popover (or descendant of another popover), these two are “nested”. This allows the <code>anchor</code> attribute to be used with the <a href="https://tabatkins.github.io/specs/css-anchor-position/">Anchor Positioning API</a>, transparently defining a clear “nested” relationship between the popovers. It is not necessary to use the Anchor Positioning API.</li><li><strong>Using a <a href="#declarative-triggers">declarative triggering element</a> (i.e. one using the <code>popovertarget</code> attribute):</strong> when a triggering element is contained in a popover, and can therefore trigger another popover, the second popover is “nested” within the first.</li></ol><p>Note that because <a href="#the-popover-stack">the popover stack</a> can only contain <code>popover=auto</code> popovers, it is only possible to “nest” popovers within <code>popover=auto</code> popovers.</p><p>The “close signal” <a href="https://wicg.github.io/close-watcher/#close-signal">proposal</a> attempts to unify the concept of “closing” something. Most typically, the Escape key is the standard close signal, but there are others, including the Android back button, Accessibility Tools dismiss gestures, and the Playstation square button. Any of these close signals is a light dismiss trigger for the topmost popover.</p><p>As described in this section, the popover types (<code>auto</code> and <code>manual</code>) each have slightly different interactions with each other. For example, <code>auto</code>s hide other <code>auto</code>s, but <code>manual</code>s do not close each other. Additionally, there are other (non-popover) elements that participate in the top layer. This section describes the general interactions between the various top layer element types, including the various flavors of popover:</p><ul><li>Popover (<strong><code>popover=auto</code></strong>)<ul><li>When opened, force-closes other <code>popover=auto</code>s, except for <a href="#nearest-open-ancestral-popover">ancestor popovers</a>.</li><li>It would generally be expected that a popover of this type would either receive focus, or a descendant element would receive focus when invoked.</li><li>Dismisses on <a href="https://wicg.github.io/close-watcher/#close-signal">close signal</a> or a click outside the element.</li></ul></li><li>Manual (<strong><code>popover=manual</code></strong>)<ul><li>Does not force-close any other element type.</li><li>Does not light-dismiss - closes via timer or explicit close action.</li></ul></li><li>Dialog (<strong><code>&lt;dialog&gt;.showModal()</code></strong>)<ul><li>When opened, force-closes <code>popover=auto</code>.</li><li>Dismisses on <a href="https://wicg.github.io/close-watcher/#close-signal">close signal</a></li></ul></li><li>Fullscreen (<strong><code>&lt;div&gt;.requestFullscreen()</code></strong>)<ul><li>When opened, force-closes <code>popover=auto</code>, and (with spec changes) dialog</li><li>Dismisses on <a href="https://wicg.github.io/close-watcher/#close-signal">close signal</a></li></ul></li></ul><p>Since the <code>popover</code> content attribute can be applied to any element, and this only impacts the element’s presentation (top layer vs not top layer), this addition does not have any direct semantic or accessibility impact. The element with the <code>popover</code> attribute will keep its existing semantics and AOM representation. For example, <code>&lt;article popover&gt;...&lt;/article&gt;</code> will continue to be exposed as an implicit <code>role=article</code>, but will be able to be displayed on top of other content. Similarly, ARIA can be used to modify accessibility mappings in <a href="https://w3c.github.io/html-aria/">the normal way</a>, for example <code>&lt;div popover role=note&gt;...&lt;/div&gt;</code>.</p><p>As mentioned in the <a href="#declarative-triggers">Declarative Triggers</a> section, accessibility mappings will be automatically configured to associate the popover with its trigger element, as needed.</p><p>While the popover API can be used on most elements, there are some limitations. It is legal to apply the <code>popover</code> attribute to a <code>&lt;dialog&gt;</code> element, for example. However, doing so causes <code>dialog.showModal()</code> (the <code>&lt;dialog&gt;</code> API to show it modally) to throw an exception. This is because it is confusing that an element with <code>popover</code> can be shown in a modal fashion. Similarly, calling <code>element.requestFullscreen()</code> on an element that has the <code>popover</code> attribute will return a rejected promise.</p><p>This section contains several HTML examples, showing how various UI elements might be constructed using this API.</p><p><strong>Note:</strong> these examples are for demonstrative purposes of how to use the <code>popovertarget</code> and <code>popover</code> attributes. They may not represent all necessary HTML, ARIA or JavaScript features needed to fully create such components.</p><pre class="astro-code github-dark" tabindex="0"><code><span class="line"><span>&lt;</span><span>button</span><span> popovertarget</span><span>=</span><span>"datepicker"</span><span>&gt;Pick a date&lt;/</span><span>button</span><span>&gt;</span></span> <span class="line"><span>&lt;</span><span>my-date-picker</span><span> role</span><span>=</span><span>"dialog"</span><span> id</span><span>=</span><span>"datepicker"</span><span> popover</span><span>&gt;...date picker contents...&lt;/</span><span>my-date-picker</span><span>&gt;</span></span> <span class="line"></span> <span class="line"><span>&lt;!-- No script - the popovertarget attribute takes care of activation, and</span></span> <span class="line"><span> thepopoverattribute takes care of the popover behavior. --&gt;</span></span></code></pre><pre class="astro-code github-dark" tabindex="0"><code><span class="line"><span>&lt;</span><span>selectmenu</span><span>&gt;</span></span> <span class="line"><span> &lt;</span><span>div</span><span> popover</span><span> slot</span><span>=</span><span>"listbox"</span><span> behavior</span><span>=</span><span>"listbox"</span><span>&gt;</span></span> <span class="line"><span> &lt;</span><span>option</span><span>&gt;Option 1&lt;/</span><span>option</span><span>&gt;</span></span> <span class="line"><span> &lt;</span><span>option</span><span>&gt;Option 2&lt;/</span><span>option</span><span>&gt;</span></span> <span class="line"><span> &lt;/</span><span>div</span><span>&gt;</span></span> <span class="line"><span>&lt;/</span><span>selectmenu</span><span>&gt;</span></span> <span class="line"></span> <span class="line"><span>&lt;!-- No script -<selectmenu>'s listbox is provided by a<div popover>` element, which takes care of popover behavior -->
      <div role=“alert”>
        <my-manual-container popover=“manual”></my-manual-container>
      </div>
      
      <script>
        window.addEventListener(‘message’, (e) => {
          const container = document.querySelector(‘my-manual-container’)
          container.appendChild(document.createTextNode(‘Msg: ‘ + e.data))
          container.showPopover()
        })
      </script>

      Allowing a popover/top-layer element to exceed the bounds of its containing frame poses a serious security risk: such an element could spoof browser UI or containing-page content. While the original <popup> proposal did not discuss this issue, the <selectmenu> proposal does have a specific section at least mentioning this issue. Some top-layer APIs (e.g. the fullscreen API) make it possible for an element to exceed the frame bounds in some cases, great care must be taken in these cases to ensure user safety. Given the complete flexibility offered by the popover API (any element, arbitrary content, etc.), there would be no way to ensure the safety of this feature if it were allowed to exceed frame bounds.

      For completeness, several use counters were added to Chromium to measure how often this type of behavior (content exceeding the frame bounds) might be needed. These are approximations, as they merely measure the total number of times one of the built-in “popover” windows, which can exceed frame bounds because of their carefully-controlled content, is shown. The popovers included in this count include the <select> popover, the <input type=color> color picker, and the <input type=date/etc> date/time picker. Data can be found here:

      So about 11% of all popovers currently exceed their owner frame bounds. That should be considered a rough upper bound, as it is possible that some of those popovers could have fit within their frame if an attempt was made to do so, but they just happened to exceed the bounds anyway.

      In any case, it is important to note that this API cannot be used to render content outside the containing frame.

      Note that using the API described in this explainer, it is possible for elements contained within a shadow root to be popovers. For example, it is possible to construct a custom element that wraps a popover type UI element, such as a <my-popover>, with this DOM structure:

      <my-popover>
        <template shadowroot=“closed”>
          <div popover=“auto”>
            This is a popover:
            <slot></slot>
          </div>
        </template>
        Popover text here!
      </my-popover>

      In this case, the (closed) shadow root contains a <div> that has popover=auto and that element will be shown on the top layer when the custom element calls div.showPopover().

      This is “normal”, and the only point of this section is to point out that even shadow dom children can be promoted to the top layer, in the same way that a shadow root can contain a <dialog> that can be showModal()‘d, or a <div> that can be requestFullscreen()‘d.

      There might come a time, sooner or later, where a new popover-type HTML element is desired which combines strong semantics and purpose-built behaviors. For example, a <notification> or <listbox> element. Those elements could be relatively easily built via the APIs proposed in this document. For example, a <notification> element could be defined to have role=alert and popover=manual, and therefore re-use this popover API for always-on-top rendering. In other words, these new elements could be explained in terms of the lower-level primitives being proposed for this API.

      Many decisions and choices were made in the design of this API, and those decisions were made via numerous discussions (live and on issues) in OpenUI, a WHATWG Community Group.

      To achieve the goals of this project, a number of approaches could have been used:

      • An HTML content attribute (this proposal).
      • A dedicated <popup> element.
      • A CSS property.
      • A JavaScript API.

      Each of these options is significantly different from the others. To properly evaluate them, each option was fleshed out in some detail. Please see this document for the details of that effort:

      That document discusses the pros and cons for each alternative. After exploring these options, the HTML content attribute approach was resolved by OpenUI to be the best overall.

      Again, refer to the Other Alternatives Considered document for an exhaustive look at the other alternatives. That document runs through a fairly complete design of the other alternatives, to see what they would look like.

      This section simply tries to summarize the primary reason that a content attribute was chosen to enable the popover behavior: a content attribute allows behavior to be applied to any element. That is useful:

      <dialog popover=“auto”>
        This is a “light dismiss” dialog
        <button>Ok</button>
        <button>Cancel</button>
      </dialog>

      Here, the developer is building a dialog. Since HTML strongly encourages the use of the correct element that properly represents the semantics of the content, the proper element in this case is a <dialog>.

      <menu popover=“auto”>
        <li><button>Item 1</button></li>
        <li><button>Item 2</button></li>
        <li><button>Item 3</button></li>
      </menu>

      Similarly here, the developer is building a menu, so they should use the <menu> element. This adheres to the semantic definition of a <menu> element, and allows the content to be programmatically exposed as a listing of buttons.

      In both cases, not only does the use of the semantically-correct element make it easier to understand the content, it also enforces the appropriate content rules and behaviors that are specific to each element. Further, using the correct element allows the UA to correctly represent the content in the accessibility tree, making assistive technologies better able to assist users in navigating the content.

      In an alternative proposal where the popover behavior is enabled via a special element, e.g. <popup>, all of the above would need to be carefully managed by the developer via ARIA roles and attributes, as there is no single role that can consistently be used to identify a <popup> element:

      <popup role=“dialog”>
        <popup role=“list”>…etc…</popup>
      </popup>

      and that violates the first rule of ARIA, which is essentially that if there’s an element that properly represents the content, use that, and don’t use ARIA.

      By having popover be a content attribute that purely confers behavior upon an existing element, the above problems are nicely resolved. Semantics are provided by elements, and behaviors are confered on those elements via attributes. This situation is exactly analogous to contenteditable or tabindex, which confer specific behaviors on any element. Imagine a Web in which those two attributes were instead elements: <contenteditable> and <tabindex index=0>. In that Web, many common patterns would either be very convoluted or simply not possible.

      Many small (and large!) behavior questions were answered via discussions at OpenUI. This section contains links to some of those:

      Here are all non-spec-text related OpenUI popover issues, both open and closed:

      https://github.com/openui/open-ui/issues?q=is%3Aissue+label%3Apopover+-label%3Apopover-spec

      Here are all current, open, non-spec-text related OpenUI popover issues:

      https://github.com/openui/open-ui/issues?q=is%3Aissue+is%3Aopen+label%3Apopover+-label%3Apopover-spec+-label%3Apopover-v2