A useful way to review a popover-transition helper is to start with what its failure boundaries require.
The supplied assertions reject a transition configuration that omits display, and they require the default configuration to include both display and overlay as discrete properties. That is the key failure case behind a popover that looks correct while opening but disappears before its closing opacity or transform can be painted. The tests also require a starting-style rule, which protects the entry path from relying on an accidental initial frame.
The success path is narrower. A recording popover dependency starts closed, receives one open command, and reports that it is open afterward. Separate boundary assertions show that an already-open popover is not sent another open command, and an already-closed popover is not sent a close command. This proves that the helper guards browser state rather than forcing duplicate toggles. It does not prove rendering behavior in a browser; the dependency records calls and state transitions only.
Prerequisites are a valid Popover API element and CSS support for :popover-open, @starting-style, and discrete transitions. The implementation decision is to let the Popover API own shown/hidden state while CSS owns visual continuity: closed and open selectors define the visual endpoints, and display plus overlay remain part of the transition contract.
Expected result: opening enters from the starting frame, and closing stays observable long enough for the exit motion to complete. Avoid replacing this with a JavaScript timeout, because a delay does not express the browser lifecycle requirement.
One limitation remains: this is not a modal-dialog solution. It does not add focus management, and it cannot animate a popover after application code removes that element from the DOM.
Working example
// This module provides a small, testable popover transition helper.
// It models the article's working idea: JavaScript should only guard state,
// while CSS supplies the transition configuration that keeps the popover
// visible long enough to animate out.
const REQUIRED_PROPERTIES = new Set(['opacity', 'transform', 'display', 'overlay']);
const DISCRETE_PROPERTIES = new Set(['display', 'overlay']);
export function buildPopoverTransition({ durationMs = 700, properties } = {}) {
if (!Number.isFinite(durationMs) || durationMs <= 0) {
throw new RangeError('durationMs must be a positive finite number');
}
const selected = properties ?? ['opacity', 'transform', 'display', 'overlay'];
if (!Array.isArray(selected) || selected.some((property) => !REQUIRED_PROPERTIES.has(property))) {
throw new TypeError('properties must contain only supported popover transition properties');
}
for (const required of REQUIRED_PROPERTIES) {
if (!selected.includes(required)) {
throw new Error(`Missing required popover transition property: ${required}`);
}
}
return Object.freeze({
durationMs,
properties: Object.freeze(
selected.map((property) => Object.freeze({
property,
allowDiscrete: DISCRETE_PROPERTIES.has(property),
})),
),
});
}
export const popoverAnimationCss = `
[popover] {
opacity: 0;
transform: scaleX(0);
transition:
opacity 700ms,
transform 700ms,
display 700ms allow-discrete,
overlay 700ms allow-discrete;
}
[popover]:popover-open {
opacity: 1;
transform: scaleX(1);
}
@starting-style {
[popover]:popover-open {
opacity: 0;
transform: scaleX(0);
}
}
`;
export function createPopoverCommands(popover) {
if (
!popover ||
typeof popover.matches !== 'function' ||
typeof popover.showPopover !== 'function' ||
typeof popover.hidePopover !== 'function'
) {
throw new TypeError('popover must provide matches(), showPopover(), and hidePopover()');
}
function isOpen() {
return popover.matches(':popover-open');
}
return Object.freeze({
isOpen,
open() {
if (isOpen()) return 'already-open';
popover.showPopover();
return 'opened';
},
close() {
if (!isOpen()) return 'already-closed';
popover.hidePopover();
return 'closed';
},
});
}
Read the full article: https://chriseugenerodriguez.com/blog/popover-showpopover-exit-transition