I am using mulitple Headless UI dropdown menus. So when one of the dropdown is in open state and I try to click on the another dropdown menu, it just closes the current dropdown alone and doesn’t open the new dropdown. So every time I have to click twice when switching between dropdowns.
Is there any way I can switch between drop downs in single click on the new drop down
2
Answers
To achieve the desired behavior of switching between Headless UI dropdown menus in a single click, you can manage the open state of the dropdown menus at a higher level. This approach involves keeping track of which dropdown is currently open and ensuring that opening a new dropdown automatically closes the previously open one. Here’s a general approach to implement this:
Manage the open state at a higher level: Create a state variable to track the currently open dropdown.
Update the state when a dropdown is clicked: When a dropdown is clicked, update the state to the new dropdown.
Close the previous dropdown: Ensure that clicking a new dropdown closes the previously open dropdown.
Here is an example implementation using React:
Explanation
Dropdown Component:
The Dropdown component takes three props: id, currentOpenId, and setOpenId.
id is a unique identifier for each dropdown.
currentOpenId is the state that tracks which dropdown is currently open.
setOpenId is the function to update the currentOpenId state.
DropdownContainer Component:
The DropdownContainer component maintains the currentOpenId state.
It renders multiple Dropdown components, each with a unique id.
Clicking on a Menu.Button updates the currentOpenId state. If the clicked dropdown is already open, it sets currentOpenId to null, closing the dropdown. Otherwise, it sets currentOpenId to the clicked dropdown’s id, opening it and closing any previously open dropdown.