Sub topic 14
Feature detection and fallbacks
This chapter assumes you know the popover attribute, invoker buttons, and the JavaScript methods showPopover, hidePopover, and togglePopover. You also need basic CSS selectors and event listeners. Here you will learn how to find out whether a browser supports popovers and what to do when it does not.
Why detection matters
Browsers that do not support popovers ignore the popover and popovertarget attributes. An element with popover then behaves like any other div. It stays visible on the page, its content shows up in the normal flow, and the invoker button does nothing.
Detection lets you notice this case and choose a different behavior. Check for the feature itself. Do not guess from the browser name or version, because user-agent strings are unreliable and support changes over time.
Detecting support in JavaScript
Support is exposed as a popover property on HTMLElement.prototype. If the property exists, the browser understands the attribute.
const supportsPopover =
typeof HTMLElement !== 'undefined' &&
Object.prototype.hasOwnProperty.call(HTMLElement.prototype, 'popover');
if (!supportsPopover) {
document.documentElement.classList.add('no-popover');
}The typeof HTMLElement check keeps the code from throwing in environments without a DOM, such as a server-side render. The no-popover class on the root element gives your CSS a hook for fallback styles.
Do not test with 'showPopover' in document.body. It can work, but the popover property is the check that matches the attribute itself.
Detecting support in CSS
Use @supports selector() to test the pseudo-class that popovers add.
@supports not selector(:popover-open) {
[popover] {
display: none;
}
}This rule applies only in browsers that lack :popover-open. It hides every popover by default, so unsupported browsers do not show them on load. Without it, the "hidden until opened" behavior is missing.
A simple fallback
A fallback needs to restore the open and close behavior that the browser would normally provide. The smallest version toggles a class from the invoker's click.
<button popovertarget="help" id="help-btn">Show help</button>
<div id="help" popover>
<p>Press Escape or click outside this box to close it.</p>
</div>
<style>
.no-popover [popover] { display: none; }
.no-popover [popover].is-open { display: block; }
</style>
<script>
const supportsPopover =
typeof HTMLElement !== 'undefined' &&
Object.prototype.hasOwnProperty.call(HTMLElement.prototype, 'popover');
if (!supportsPopover) {
document.documentElement.classList.add('no-popover');
document.querySelectorAll('[popovertarget]').forEach((button) => {
button.addEventListener('click', () => {
const target = document.getElementById(button.getAttribute('popovertarget'));
if (target) target.classList.toggle('is-open');
});
});
}
</script>Native browsers never run the fallback branch, so there is no double handling.
What a fallback does not give you
A small fallback is not a full copy of native behavior. It lacks the following:
- The top layer, so the content can still be clipped or covered by other elements.
- Light dismiss and the Escape key.
- Automatic closing of other popovers.
- The
beforetoggleandtoggleevents.
You can add these features by hand, but the code grows quickly. Decide how much fidelity you need. For many pages, showing the content in the normal flow is an acceptable fallback.
Common mistakes
- Calling
showPopover()without checking support. In an unsupported browser this throws aTypeError, because the method does not exist. - Hiding popovers with CSS that also overrides
:popover-openin supported browsers. Put fallback rules behind.no-popoveror@supports not selector(...). - Detecting by browser name. Test for the feature.
Exercise
Why is @supports not selector(:popover-open) { [popover] { display: none; } } safe to ship, and what problem does it solve?
Show answer
Browsers that support popovers understand :popover-open, so the not condition is false and the rule never applies there. Browsers without support skip the native hiding behavior, so the rule hides the popover elements by default instead of showing them on the page.
Exercise
Write a JavaScript check that is safe to run outside a browser and returns true only if the popover attribute is supported.
Show answer
typeof HTMLElement !== 'undefined' && Object.prototype.hasOwnProperty.call(HTMLElement.prototype, 'popover')