Skip to content

Support keyed #each #81

Closed
Closed
@Swatinem

Description

@Swatinem

This would fix #77 along with all the other benefits that keyed iteration would bring.

What we definitely would need is a way to move fragments around in the DOM.

To achieve this, I would split the render (constructor) method into separate create and mount/insert methods.

  • create would do createElement etc.
  • mount (or insert, pending bikeshed) would actually mount the elements to the DOM. Instead of making a distinction for useAnchor, I would just always generate code such as parent.insertBefore( elem, anchor ), since insertBefore is the same as appendChild when anchor is undefined.

[this would also go in the direction I was thinking of in #37]

As for the #each itself, it would be as simple as iterating the list and create fragments that do not exist yet, then mount the fragment to the anchor, effectively moving already existing fragments. And then just unmount/teardown all the leftovers.

That should do as a first naive solution. Not moving existing fragments at all would require a more elaborate list diff, using lcs (longest common subsequence) and friends.

Any ideas on that @Rich-Harris?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions