How to have transition working with dynamic list on Livewire

Published at Feb 11, 2024

If you have tried to use wire:transition in a loop in Livewire, you may have seen some weird behaiour. It's because of limitation recently documented on Livewire. But no worries, I have a workaround for you!

The solution is simple, we are gonna use AutoAnimate.js and to be more precise alpine-auto-animate wich is a Alpine.js wrapper.


The easiest way is to use the cdn to install the library

<script src="" defer></script>

But I didn't go the easy way, I prefer to package all in my own project. So we will have to follow Livewire documentation on manually bundling Livewire and Alpine, then add alpine auto animate plugin.

Your app.js will now import Livewire and Alpine manually so you can add one or more plugins before Livewire start.

import { Livewire, Alpine } from '../../vendor/livewire/livewire/dist/livewire.esm';
import AutoAnimate from '@marcreichel/alpine-auto-animate';

And your blade layout should include two new blade tags

{{ $slot }}

Using auto animate with Livewire

So before, you probably tried something like that

@foreach($list as $model)
<li wire:key="{{ $model->id }}" wire:transition.duration.175ms>
{{ $model->name }}

Remove wire:transition from the loop and add x-auto-animate attribute to the parent.

<ul x-auto-animate.175ms
@foreach($list as $model)
<li wire:key="{{ $model->id }}"
{{ $model->name }}

Full documentation on how to cusotmize annimations on Github.

And voilĂ !

#livewire #javascript #transition

Syntax highlighting provided by