lace:transition
Animate an element's change smoothly instead of snapping, through the browser's native View Transitions API.
Put lace:transition on keyed list items (with lace:key) and each item animates on its own: an
insert pushes the others down, a remove collapses the gap, a shuffle slides items to new positions:
- Write the docs
- Review the PR
- Ship it
import random
class TransitionListDemo(Component):
items: list = []
def add(self):
new_id = max((i["id"] for i in self.items), default=0) + 1
self.items = [{"id": new_id, "text": f"Task {new_id}"}] + self.items
def remove(self, item_id):
self.items = [i for i in self.items if i["id"] != item_id]
def shuffle(self):
items = list(self.items)
random.shuffle(items)
self.items = items
def inline_template(self):
return """
<button lace:click="add">Add</button>
<button lace:click="shuffle">Shuffle</button>
<ul>
{% for item in items %}
<li lace:key="{{ item.id }}" lace:transition> {# each item animates individually #}
{{ item.text }}
<button lace:click="remove({{ item.id }})">×</button>
</li>
{% endfor %}
</ul>
"""
Interlace gives each keyed item a unique view-transition-name, which is what lets the browser track
and animate them individually. On a plain container (no keys), lace:transition crossfades the
whole region instead.
Notes
-
Safe by default. Where the View Transitions API is unavailable (some browsers), it falls back to a plain morph, no animation, same result.
-
Keep lists reasonable. View Transitions snapshot each named element, so very long lists or heavy per-item content get expensive.