How can I build a show-and-hide disclosure button that stays in sync?
Quick answer
Use a button to control a specific content section, and keep the button’s aria-expanded value matched to whether that section is visible: true when open, false when closed. Make one function update both the section’s visibility and the button state, and use it for every open, close, or reset action. Test toggling with Enter and Space, then verify the state again after other code changes the disclosure.
The question, the details, the next step.
A practical checklist
Use a button for the control and identify the section it controls.
Set
aria-expandedtotruewhen the section is visible andfalsewhen hidden.Use one update function or render path to change visibility and expanded state together.
Route resets and other visibility changes through that same update path.
Test open and close with both a pointer and the Enter and Space keys.
A few more useful answers
Is `aria-controls` required for the disclosure to work?
The pattern guide says aria-controls may identify the controlled content; it does not describe it as mandatory. The essential state relationship is that aria-expanded matches whether the content is visible. [S1]
Should I change `aria-expanded` when only the panel’s text changes?
Not if the panel remains open or remains closed. Update the expanded state when visibility changes, not merely because the content within the panel is edited.
Sources
References used for this guide.
- [S1] W3C ARIA APG: Disclosure (Show/Hide) Pattern w3.org · Checked
