[Bug Fix] Accordion: hide closed content properly instead of zero height (#168)
Merged
DJ
5mo ago+114+114+012345678901234567890123456789-8-8-0123456789
Closes #168
How to reproduce
- Place a
FormFieldwith aFormFieldErrorinside a closedAccordionItem. - Submit the form without filling the field.
- Observe the validation error is invisible — the content is clipped to
height: 0withoverflow: hidden, so the error message can't be seen and the browser cannot scroll to / focus the invalid field.
What changed
Before: AccordionContent started with style="height: 0px;" and overflow-y-hidden. The Stimulus controller only animated height between 0 and scrollHeight. The element was always in the layout (and form-focus) tree, just visually hidden.
After:
AccordionContentnow renders withhiddenattribute anddata-state="closed"by default, completely removing it from layout and form-focus.- The Stimulus controller (
accordion_controller.js) now:- On open: removes
hiddenand setsdata-state="open"before animating height, soscrollHeightis measurable. - On close: animates height to
0, then calls.finished.then(...)to sethiddenafter the animation completes (guarded bydata-state === "closed"to avoid racing a rapid re-open).
- On open: removes
- Animation remains smooth — the
motionlibrary and existing duration/easing values are unchanged. - Public Ruby API is identical:
AccordionContent,AccordionItem,AccordionTrigger, targets, and values are all preserved.
Testing instructions
Automated tests
cd gem
bundle exec rake test TEST=test/ruby_ui/accordion_test.rb # accordion-specific
bundle exec rake # full suite + lint
Manual browser verification
- Open an accordion page (e.g. https://rubyui.com/docs/accordion or your local dev server).
- Open DevTools → Elements. Inspect a closed
AccordionContent— verify it hashiddenattribute anddata-state="closed". - Click the trigger to open it — verify
hiddenis removed,data-statebecomes"open", and the animation is smooth. - Close it again — verify the height animates to
0, thenhiddenreappears on the element. - Form error scenario: wrap an
<input required>and an error<span>inside a closedAccordionContent, submit the form empty, then open the accordion — confirm the validation error is visible and the field is focusable.
Summary by cubic
Fixes the Accordion so closed content is truly removed from layout and focus using the hidden attribute instead of height: 0 clipping. Prevents invisible validation errors while keeping open/close animations smooth.
- Bug Fixes
AccordionContentnow renders withhiddenanddata-state="closed".- On open: remove
hidden, setdata-state="open", then animate to measured height. - On close: animate to
height: 0, then sethidden; guarded bydata-stateto avoid races. - Tests clarified and renamed with an extra visible-content assertion; rebuilt
mcp/data/registry.jsonfor CI; public API unchanged.