OddBird’s Popover Attribute Polyfill – built in collaboration with
Keith Cirkel
and used in production by GitHub – lets developers preview the
upcoming mechanism for
displaying popover content
on top of other page content, drawing the user’s attention to
specific important information or actions that need to be taken.
This polyfills the HTML popover attribute and
showPopover, hidePopover, and
togglePopover methods on HTMLElement, as
well as the popovertarget and
popovertargetaction attributes on
<button> elements.
Popovers take a state of “auto” or “manual”. If no state is
provided, the popover takes on the behavior of its default state,
which is “auto”. Auto popovers can be “light dismissed” by selecting
anywhere on the page, clicking the popover control button, or
opening another popover on the page.
Popover button controls can be set to “show” or “hide” with the
popovertargetaction attribute. This
popover also has a link with an
autofocus attribute, which receives focus when the
popover is opened.
<button popovertargetaction="show" popovertarget="showHidePopover">
Show Popover with Explicit Action
</button>
<button popovertargetaction="hide" popovertarget="showHidePopover">
Hide Popover with Explicit Action
</button>
<div id="showHidePopover" popover="manual">
Popover with Explicit Action. This popover has an <a href="#" autofocus>autofocus link.</a>
</div>
Since the value of the popovertargetaction attribute is
set to “show”, clicking the button will only open the popover. The
popover can be closed by selecting elsewhere on the page, or opening
another popover. This popover also has a link with an
autofocus attribute, which receives focus when the
popover is opened.
<button popovertargetaction="show" popovertarget="singleActionShowPopover">
Show Single Action Popover
</button>
<div id="singleActionShowPopover" popover>
Single Action Popover. This popover has an <a href="#" autofocus>autofocus link.</a>
</div>
When supported, the polyfill creates a cascade layer named
popover-polyfill. If author styles are not in layers
then this should have no impact. If layers are used, authors will
need to ensure the polyfill layer is declared first (e.g.
@layer popover-polyfill, other, layers;).
<button popovertarget="layeredStylesPopover">
Toggle Popover with Layered Styles
</button>
<div id="layeredStylesPopover" data-popover="layered-styles" popover>
Popover with Layered Styles
(should be red if browser has <code>@layer</code> support)
</div>/* CSS */
@layer popover-polyfill;
@layer consumer {
[data-popover~='layered-styles'] {
background-color: #d00d1e;
}
}
🔗
Popovers and Controls Created Fully in the Shadow Root
Here, both the popover control button and the popover are created in
the Shadow DOM.