Close one accordion item when another opens #778
-
We managed to build a working basic accordion. Now we want to implement a behaviour that only one item can be open at a time. We lifted up state from the accordion component to the faq page and we also saved the whole accordion information in a state object. When the user clicks an accordion item, the key-value pair in the state object called "is open" should change from true to false. Ty for your help ... We have the following code in place:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Firstly, kudos on your work so far—it's a commendable effort! I'd like to introduce you to some hidden gems in HTML— the To implement this, you just need to keep track of an identifier (like an id or index) for the currently open detail. I've put together an example for you to take a look at here. It's worth noting that there's an exciting feature on the horizon (see here), which promises to make this process even more straightforward. However, keep in mind that, as of now, this functionality is not universally available in browsers, except in previews and experimental versions. |
Beta Was this translation helpful? Give feedback.
-
Hi Pete, here is what we finally came up with. A mixture of your approach and ours.
|
Beta Was this translation helpful? Give feedback.
Firstly, kudos on your work so far—it's a commendable effort!
I'd like to introduce you to some hidden gems in HTML— the
details
andsummary
tags. These elements offer built-in functionality for creating accordions without the need for javascript. They handle a significant part of the heavy lifting.To implement this, you just need to keep track of an identifier (like an id or index) for the currently open detail. I've put together an example for you to take a look at here.
It's worth noting that there's an exciting feature on the horizon (see here), which promises to make this process even more straightforward. However, keep in mind that, as of now, this functionality is not universally …