CORE JSC

International Technology Partnership

UI/UX

Fixing Accordion Components Whose aria-expanded State Doesn't Match What's Actually Visible

A custom accordion panel opens and closes smoothly on screen, but a screen reader announces it as "expanded" while it's visibly collapsed, or the other way around. Sighted users never notice, because the bug lives entirely in an attribute nobody sees — and it usually comes from updating aria-expanded on a different tick than the one that actually changes what's visible.

Core JSC Team·December 3, 2026
AccessibilityARIAUI ComponentsUXWeb Development

The Problem

A custom accordion component — a FAQ list, a settings panel, a filter sidebar — opens and closes correctly as far as any sighted user can tell: clicking a header expands its panel, clicking it again collapses it, and the animation looks clean. A screen reader user navigating the same component hears something that contradicts what's on screen: the header is announced as "expanded" after a panel has visibly collapsed, or "collapsed" while the panel is still open and showing its content. Nothing in the visual behavior is wrong; the mismatch is entirely in the aria-expanded attribute screen readers rely on to describe the control's state.

Why It Happens

The attribute gets set from the state that triggers the animation, not the state that reflects what's actually rendered once the animation finishes

A common implementation toggles a boolean the instant a header is clicked, and binds aria-expanded directly to that boolean. If the panel's visual open/close is driven by a CSS transition — height, max-height, or opacity — that takes a few hundred milliseconds, the attribute flips the moment the click happens, well before the panel has actually finished animating into its new visible state. For most of that transition window, and for anyone whose assistive technology announces state on interaction, the attribute describes where the panel is heading, not where it currently is.

A closing animation that's interrupted by a second quick click leaves the attribute in whatever state the last click set, disconnected from the panel's actual mid-animation visual state

If a user clicks a header, then clicks it again before the first transition finishes, a naive implementation flips the boolean on every click without checking what the panel is actually doing visually at that moment. The attribute ends up reflecting the last click's intent, which can land completely out of sync with a panel that's still animating from an earlier, already-overridden transition — something no sighted user would notice because the eye just sees the final visual result, not the intermediate desync.

Some accordion implementations only set aria-expanded on the panel's trigger, forgetting that the attribute's presence or absence also has to be paired correctly with the panel's own hidden state

The two halves of a correct accordion state are the trigger's aria-expanded and the panel's own visibility to assistive technology, usually via the hidden attribute or an equivalent. A header can correctly report aria-expanded="true" while the panel element itself retains a stale hidden attribute from before, or vice versa — both pieces have to be updated together, from the same state change, or a screen reader can end up with a trigger and a panel that disagree with each other.

The Fix

1. Drive aria-expanded from the same single source of state that determines what's actually rendered, never from the click event itself

function AccordionItem({ isOpen, onToggle, title, children }) {
  return (
    
); }

Both aria-expanded on the trigger and hidden on the panel read from the exact same isOpen value in the same render. There's no separate "animation state" and "ARIA state" to keep in sync — they're both derived from one variable, so they can never disagree with each other by construction.

2. If an animation needs to finish before the panel is fully removed from the accessibility tree, delay only the hidden attribute, not aria-expanded

function closePanel() {
  setIsOpen(false); // aria-expanded flips immediately — correct
  // Let the CSS transition play, then finish removing
  // the panel from the accessibility tree once it's
  // visually gone, not before:
  panelEl.addEventListener("transitionend", () => {
    panelEl.hidden = true;
  }, { once: true });
}

aria-expanded should flip the instant the toggle happens, since that's the moment the control's state truly changes from the user's perspective. The panel's hidden attribute, by contrast, can reasonably wait until a closing transition visually finishes — removing it from the accessibility tree too early, while it's still visibly sliding shut, pulls focus and reading order away from content a sighted user can still see on screen.

3. Guard against rapid double-clicks by keying off current state rather than blindly flipping a boolean on every click

function handleToggleClick() {
  // Read the current state fresh each time, don't
  // just invert a value that might already be stale
  // from a previous click mid-animation:
  setIsOpen(prev => !prev);
}

Using the updater-function form that reads the latest state at the moment it actually applies, rather than capturing a possibly-stale boolean from the click handler's closure, prevents a rapid double-click from producing an aria-expanded value that doesn't match where the component's state genuinely ends up.

4. Test the accordion by listening to it, not just by looking at it

- Turn on VoiceOver (Cmd+F5 on macOS) or NVDA (Windows)
- Tab to each accordion trigger and activate it with
  Enter or Space, listening for "expanded"/"collapsed"
- Click rapidly twice on the same trigger and confirm
  the announced state still matches the final visual
  state, not an intermediate one

This class of bug is invisible to a purely visual review, since the broken part of the experience is an announcement nobody sighted ever hears. The only reliable way to catch it during development is to actually turn on a screen reader and listen to the same interaction a sighted developer would normally only watch.

Why This Works

Deriving aria-expanded and the panel's hidden state from the same underlying variable, in the same render, removes the possibility of the two ever disagreeing — there's no second, independently-updated "ARIA state" that can drift out of sync with the real one. Flipping the ARIA attribute immediately on toggle, while letting the visual transition and the accessibility-tree removal lag slightly behind it, matches what a screen reader user actually needs: an instant, accurate signal that their action registered, not a delayed echo of an animation they can't see.

Conclusion

An accordion whose aria-expanded state disagrees with what's visibly open or closed almost always traces back to the attribute being driven by something other than the single piece of state that also controls what's actually rendered — a click event fired in isolation, a stale closure from a rapid double-click, or an animation callback that updates one half of the state without the other. Binding both the trigger's ARIA attribute and the panel's visibility to one shared state value, flipping that value immediately on toggle, and verifying the result by actually listening with a screen reader turns an invisible-to-sighted-review bug into one that's caught before it ships.