Toggles and Dropdowns
index.html
To create interactive toggles and dropdown menus with Alpine.js, you combine several core directives to manage state and user interaction. The example above uses x-data to define state, specifically an open property that tracks whether the dropdown is visible. When you click the button, the x-on:click directive toggles the open state. The dropdown menu itself uses x-show to only display when open is true, and x-transition provides a simple animation effect.
Accessibility is enhanced by using ARIA attributes. The button uses aria-haspopup="true" to indicate it controls a menu, and :aria-expanded="open" to reflect the open state. The dropdown menu uses role="menu" and its items have role="menuitem" with tabindex="0" for keyboard navigation. The @click.away directive automatically closes the menu if you click outside of it, mimicking native dropdown behavior and improving usability. This combination of Alpine.js directives and semantic HTML ensures that your toggles and dropdowns are both interactive and accessible.
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Can you show me a complete example of the code for this dropdown?
How do I add more menu items to the dropdown?
What are some best practices for making Alpine.js components accessible?
Geweldig!
Completion tarief verbeterd naar 6.67
Toggles and Dropdowns
Veeg om het menu te tonen
index.html
To create interactive toggles and dropdown menus with Alpine.js, you combine several core directives to manage state and user interaction. The example above uses x-data to define state, specifically an open property that tracks whether the dropdown is visible. When you click the button, the x-on:click directive toggles the open state. The dropdown menu itself uses x-show to only display when open is true, and x-transition provides a simple animation effect.
Accessibility is enhanced by using ARIA attributes. The button uses aria-haspopup="true" to indicate it controls a menu, and :aria-expanded="open" to reflect the open state. The dropdown menu uses role="menu" and its items have role="menuitem" with tabindex="0" for keyboard navigation. The @click.away directive automatically closes the menu if you click outside of it, mimicking native dropdown behavior and improving usability. This combination of Alpine.js directives and semantic HTML ensures that your toggles and dropdowns are both interactive and accessible.
Bedankt voor je feedback!