Collapse/expand UI with Stimulus that survives Turbo swaps

13942
0

Simple disclosure components are everywhere: FAQ, details panels, advanced filters. I keep them as progressive enhancement: the HTML is valid and readable, and Stimulus adds toggling behavior. The controller toggles a hidden class and updates aria-expanded so assistive tech stays accurate. In Turbo apps, this controller reconnects after frame replacements automatically, which means you can safely render collapsible partials via Turbo Streams. I also prefer using data-controller on a wrapper rather than on individual buttons; it’s easier to reuse. If state needs to persist across navigation, consider data-turbo-permanent or storing state in the URL, but for most collapses, local state is fine.