minaremonshaker's avatar

sidebar dose not close when i added event listener to close it when i click on and <li> inside it

Hello, I’m having an issue with the sidebar. I added an event listener to close the sidebar on small screens whenever I click any link inside it. The sidebar also has a toggle button. The problem is that when I click a link, the sidebar closes without any issues, but afterward, I have to click the toggle button twice to reopen the sidebar. note i am using the attribues approch for the sidebar , can any one help link to me code https://gist.github.com/mina20088/4b895e3f89e293c6f127503e48a40962

Note : I am using tailwindcss and flowbite

0 likes
6 replies
Tray2's avatar

Have you checked the console for errors?

1 like
N1-San's avatar

hey, in your code you are calling drawer.hide in line 26 of your dashboard.js which manually hides the drawer without updating flowbite's internal state, you can instead simulate a click on the toggle button like this

if (event.target.tagName === 'A' || event.target.closest('a')) {
        toggleButton.click(); 
    }

this should sync your flowbite internal state and fix your issue

Hope that helps!

1 like
minaremonshaker's avatar

hi thank you it works well but can you explain to me what dose this do in the code

N1-San's avatar
N1-San
Best Answer
Level 2

@minaremonshaker The issue was that you were directly calling drawer.hide() when clicking a link, which closes the sidebar visually, but doesn't update Flowbite's internal toggle state.

So when you click the toggle button again, Flowbite thinks the sidebar is still open and does nothing on the first click — then on the second click it works. That's why you’re seeing the "double-click" behavior.

Using toggleButton.click(); keeps Flowbite’s internal state in sync and fixes the issue

I hope this clears it out

Please or to participate in this conversation.