I'm building a button group using an alpinejs x-for loop, where the first and last buttons should have rounded-l-md and rounded-r-md using first:rounded-l-md
and last:rounded-r-md
. The last:rounded-r-md
works fine, however the first:rounded-l-md
doesn't work, because the <template>
used for the loop is seen as the first element, i presume.
I have added a jsfiddle: https://jsfiddle.net/20qega7d/
I think is because of the first element in the template tag, but I did a twist to get what you want, I added a key
since it is recommended in x-for
.
Now I returned the array index in the for in loop
, then bind a class to check if the index is nothing it should add rounded-l-md
to it.
<!-- Include CDN JavaScript -->
<script src="https://unpkg.com/tailwindcss-jit-cdn"></script>
<script defer src="https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js"></script>
<!-- Specify a custom TailwindCSS configuration -->
<script type="tailwind-config">
{
variants: {
extend: {
borderRadius: ['last, first'],
},
},
}
</script>
<div class="p-10" x-data="{ timeframes: [{id: 1, n: '1d'}, {id: 2, n: '1w'}, {id: 3, n: '1y'}] }">
<span class="relative z-0 inline-flex shadow-sm rounded-md">
<template x-for="(timeframe, index) in timeframes" :key="timeframe.id">
<button type="button" class="-ml-px relative inline-flex items-center px-4 py-2 last:rounded-r-md border border-gray-300 bg-white text-sm font-medium text-gray-700 hover:bg-gray-50 focus:z-10 focus:outline-none focus:ring-1 focus:ring-indigo-500 focus:border-indigo-500"
:class="{'rounded-l-md' : !index}"
>
<span x-text="timeframe.n"></span>
</button>
</template>
</span>
</div>