I need the x-on:mouseenter="itemActive = {{ index }}" and x-on:mouseleave="itemActive = false" states to be x-on:click="itemActive = {{ index }}" and x-on:click.away="itemActive = false" on screen smaller than 768px. The code below works perfectly with the hover part for larger screens but can't seem to figure out how to make it work with different screen sizes.
I am using Twig, Tailwind and Alpine.JS.
<div x-data="{itemActive: false}" class="w-full pb-12 mt-5 overflow-x-hidden overflow-y-scroll md:flex md:flex-wrap md:w-auto md:mt-0 md:pb-0 md:overflow-visible">
{% for item in menu.items %} {% set index = loop.index0 %}
<div x-on:mouseenter="itemActive = {{ index }}" x-on:mouseleave="itemActive = false" class="flex flex-wrap w-full md:block md:w-auto {{ item.classes|join(' ') }}">
<a :class="{'bg-gray-900' : itemActive === {{ index }}}" class="flex flex-wrap items-center justify-between w-full h-10 px-4 cursor-pointer md:h-14 md:justify-start md:w-auto" href="{{ item.link }}">
{{ item.title }}
{% if item.children %}
<span><i class="pl-2 fa fa-caret-down"></i></span>
{% endif %}
</a>
{% if item.children %}
<div x-show="itemActive === {{ index }}" class="absolute z-10 w-48 bg-gray-900">
{% for child in item.children %}
<a class="flex flex-wrap items-center h-12 px-4 transition-colors cursor-pointer md:h-14 hover:bg-gray-700" href="{{ child.link }}" >
{{ child.title }}
</a>
{% endfor %}
</div>
{% endif %}
</div>
{% endfor %}
</div>
Any suggestions?