The questions on my service pages slide open and shut. Under the animation each one is a plain details element, the same one the WordPress Details block gives you. I wanted to keep it that way, and this post is about how.
Why Start With Details?
Most accordion tutorials begin with a div and a button, then add aria-expanded, aria-controls, a click handler and key handling until it behaves. Do all of it correctly and you’ve rebuilt something the browser already ships.
<details class="faq__item">
<summary>Can you take over a site another developer built?</summary>
<p>Yes. I start by reading through the theme and the plugin list.</p>
</details>With no JavaScript at all, that gives you a question you can Tab to and open with Enter or Space. Screen readers announce it as collapsed or expanded. Links inside a closed answer can’t be tabbed to. Find-in-page in Chrome will even open a closed answer if the text you searched for is inside it.
What it doesn’t do is animate. It snaps open and snaps shut.
The CSS-Only Version
Newer browsers can animate it without any script. Two features make that possible. ::details-content is a pseudo-element for the part that opens, and interpolate-size lets a height animate to auto.
.faq__item {
interpolate-size: allow-keywords;
}
.faq__item::details-content {
height: 0;
overflow: clip;
transition: height 0.45s ease, content-visibility 0.45s allow-discrete;
}
.faq__item[open]::details-content {
height: auto;
}I built this first, and I liked how little there was of it. Then I watched it close in a real browser and saw a jump as it closed. Browser support was the other problem. Where those features are missing the answer simply snaps, which is a fair fallback, but it meant the animation I’d been asked for would be absent for a chunk of visitors.
So I kept the element and animated it by hand.
The Version I Shipped
The whole thing is one click listener on the document. It works for every question on the page, including ones that arrive later.
document.addEventListener('click', (e) => {
const summary = e.target.closest('.faq__item > summary');
if (!summary) return;
const item = summary.parentElement;
if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) return;
e.preventDefault();
const cs = getComputedStyle(item);
const px = (v) => parseFloat(v) || 0;
const box = px(cs.paddingTop) + px(cs.paddingBottom) +
px(cs.borderTopWidth) + px(cs.borderBottomWidth);
const extra = cs.boxSizing === 'border-box' ? 0 : box;
const from = item.offsetHeight;
if (item._slide) item._slide.cancel();
const opening = !item.open || item.dataset.closing === '1';
delete item.dataset.closing;
let to;
if (opening) {
item.open = true;
to = item.scrollHeight + px(cs.borderTopWidth) + px(cs.borderBottomWidth);
} else {
item.dataset.closing = '1';
to = summary.offsetHeight + box;
}
item.style.overflow = 'hidden';
item._slide = item.animate(
{ height: [from - extra + 'px', to - extra + 'px'] },
{ duration: 420, easing: 'cubic-bezier(0.22, 1, 0.36, 1)' }
);
item._slide.onfinish = () => {
if (!opening) {
item.open = false;
delete item.dataset.closing;
}
item.style.overflow = '';
item._slide = null;
};
});A few things in there are worth explaining.
It listens for click and nothing else. When you press Enter or Space on a focused summary, the browser fires a click event. Handling click covers the mouse, touch and the keyboard, and I never write a key handler.
Opening sets open first. The answer has to exist in the layout before its height can be measured. So the element is opened, its full height is read, and the animation runs from the old height to that one.
Closing sets open last. This was the jump. If you take the attribute away when the click happens, the answer disappears at once and there’s nothing left to slide. The attribute stays until the animation finishes, and a data-closing flag covers the time in between.
That flag also handles a change of mind. Click a question while it’s still closing and open is still true, so the script would try to close it again. Checking the flag turns that click into an open, starting from wherever the slide had got to.
Reduced motion returns early. There’s no preventDefault on that path, so the browser opens and closes the element its own way, with no movement.
The Chevron
The arrow rotates when the element is open. Because open now stays on during the closing slide, the arrow would wait until the end to turn back. The same flag fixes that in CSS.
.faq__item[open] > summary::after {
transform: rotate(180deg);
}
.faq__item[data-closing] > summary::after {
transform: none;
}Don’t Forget the Focus Ring
My FAQ sits on a near-black background, and the browser’s default focus outline was hard to see on it. A keyboard user needs to know which question they’re on, so it gets an outline of its own.
.faq__item > summary:focus-visible {
outline: 2px solid #ff6a4d;
outline-offset: 6px;
}How I Tested It
I Tab through the list and check the ring moves from question to question. I open and close with Enter, then with Space. I click one three times quickly to see whether it ends up in the right state. Then I turn on reduced motion in the system settings and check everything opens without sliding.
The last check is the one I’d skip if I were in a hurry, and it’s the reason to build it this way. With JavaScript switched off, every question still opens.
Leave a Reply