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.
Here, both the popover control button and the popover are created in
the Shadow DOM, but an element is slotted in the popover.
I’m a slotted element
<!-- Light DOM -->
<div id="shadowHostWithSlot">
<div><span>I’m a slotted element</span></div>
</div>
<!-- Shadow DOM -->
<button popovertarget="shadowedPopoverWithSlot">
Toggle Shadowed Popover
</button>
<div id="shadowedPopoverWithSlot" popover>
<slot></slot>
</div>
Sponsor OddBird’s OSS Work
At OddBird, we love contributing to the languages & tools developers
rely on. We’re currently working on polyfills for new Popover &
Anchor Positioning functionality, as well as CSS specifications for
functions, mixins, and responsive typography. Help us keep this work
sustainable and centered on your needs as a developer! We display
sponsor logos and avatars on our
website.