This is really great, thanks for the share! I echo exactly what Andrew Mason said too.
One thing was missing for me and that was clicking away on the window to close the dropdown. In AlpineJS it is handled with @click.away=. I wish I knew more about javascript and events, but I did find adding an event listener when the button is clicked and removing it upon close to work well. Supports clicking in the dropdown, the original button and outside (somewhere on the window).
Here's my updated dropdown_controller.js for you or anyone else listening:
For sure! That's a really cool solution! I agree that handling clicks outside is important. I didn't go down that road because it strayed from the purpose of the article and some UI's don't implement it. ie. npmjs.com.
FWIW I am/was doing something like this, a pattern I see in React.
This is really great, thanks for the share! I echo exactly what Andrew Mason said too.
One thing was missing for me and that was clicking away on the window to close the dropdown. In AlpineJS it is handled with
@click.away=
. I wish I knew more about javascript and events, but I did find adding an event listener when the button is clicked and removing it upon close to work well. Supports clicking in the dropdown, the original button and outside (somewhere on the window).Here's my updated
dropdown_controller.js
for you or anyone else listening:For sure! That's a really cool solution! I agree that handling clicks outside is important. I didn't go down that road because it strayed from the purpose of the article and some UI's don't implement it. ie. npmjs.com.
FWIW I am/was doing something like this, a pattern I see in React.
Heck yah, that works great! Thanks for following up.
First, thanks for a good post. And, i believe, you can doing something like