lustre-labs / lustre

An Elm-inspired framework for building HTML templates, single page applications, and server-rendered components in Gleam!
https://hexdocs.pm/lustre
MIT License
737 stars 52 forks source link

Add support for elements keyed by their `id` attribute. #82

Closed hayleigh-dot-dev closed 3 months ago

hayleigh-dot-dev commented 3 months ago

Most frameworks have the concept of a "keyed" node that lets the DOM patching do an in-place update of elements with a stable id even if their position in a list changes. This is important for two reasons:

This was brought up by #79.

ghivert commented 3 months ago

I'm not sure I can really be of any help on the subject (because I have no idea on how the VDOM on Lustre is working actually), but if you think I can help on the subject, let me think, I'd be happy to do it 🙂

hayleigh-dot-dev commented 3 months ago

It's almost done! 😅

hayleigh-dot-dev commented 3 months ago

Actually it would be super helpful if you had a minimal repo that demonstrated the issue currently so I can verify things are working now!

ghivert commented 3 months ago

Here's a minimal example, where the color is not following the update 🙂

hayleigh-dot-dev commented 3 months ago

We're close!

https://github.com/lustre-labs/lustre/assets/9001354/b7013490-fe38-4e2d-9179-1517611cc582

But removing elements messes everything up:

https://github.com/lustre-labs/lustre/assets/9001354/18220ed7-4f0a-472f-a94f-aada53eb6011

hayleigh-dot-dev commented 2 months ago

@ghivert go grab v4.1.0 and let me know if it works for you!

ghivert commented 2 months ago

https://github.com/lustre-labs/lustre/assets/7314118/b4d5ae8a-6ed8-40ac-b861-892f9389b4e0

I had a little hard time to understand how to use element.keyed, but it's working great! Thank you very much! You're the best!