[Bug Fix] Accordion: hide closed content properly instead of zero height (#168)

Merged
DJ
5mo ago
+114-8

Closes #168

How to reproduce

  1. Place a FormField with a FormFieldError inside a closed AccordionItem.
  2. Submit the form without filling the field.
  3. Observe the validation error is invisible — the content is clipped to height: 0 with overflow: 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:

  • AccordionContent now renders with hidden attribute and data-state="closed" by default, completely removing it from layout and form-focus.
  • The Stimulus controller (accordion_controller.js) now:
    • On open: removes hidden and sets data-state="open" before animating height, so scrollHeight is measurable.
    • On close: animates height to 0, then calls .finished.then(...) to set hidden after the animation completes (guarded by data-state === "closed" to avoid racing a rapid re-open).
  • Animation remains smooth — the motion library 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

  1. Open an accordion page (e.g. https://rubyui.com/docs/accordion or your local dev server).
  2. Open DevTools → Elements. Inspect a closed AccordionContent — verify it has hidden attribute and data-state="closed".
  3. Click the trigger to open it — verify hidden is removed, data-state becomes "open", and the animation is smooth.
  4. Close it again — verify the height animates to 0, then hidden reappears on the element.
  5. Form error scenario: wrap an <input required> and an error <span> inside a closed AccordionContent, 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
    • AccordionContent now renders with hidden and data-state="closed".
    • On open: remove hidden, set data-state="open", then animate to measured height.
    • On close: animate to height: 0, then set hidden; guarded by data-state to avoid races.
    • Tests clarified and renamed with an extra visible-content assertion; rebuilt mcp/data/registry.json for CI; public API unchanged.