svelte icon indicating copy to clipboard operation
svelte copied to clipboard

fix: bind:this on changing transitioning element

Open robertadamsonsmith opened this issue 1 year ago • 0 comments

Fixes https://github.com/sveltejs/svelte/issues/8891

When an outro starts, a bind:this variable is now nulled immediately, instead of when the element is destroyed. This is a breaking change.

This makes the behaviour consistent with, and appropriate for, the case where the bind:this value is swapping from one transitioning element to another (such as in an if/else block), and avoids the bug in that situation where the outroing component nulls the bind:this variable which has since been bound to another element. This bug specifically breaks snapshotting in SvelteKit when using transitions on pages.

The new binding-this-transition-js-if-else-block test highlights the bug most simply (in practice the bug is more subtle - since the unwanted nulling doesn't cause the element to be invalidated, it isn't usually until some other change occurs that the change in the value is reactively apparent).

Things to do still (if appropriate):

  • Three existing transition tests, that relied on bind:this not being nulled for an outroing component, have been modified so that they succeed. They are less thorough as a result though, and so further changes may be appropriate to those tests.
  • It may also be suitable to add a test that proves that unmounting an element with a local transition, but such that the transition doesn't fire, also causes bind:this to be nulled appropriately (I am not sure if any existing tests would prove that.)
  • Also, in bind_this.js, the branch for when there are contextual_dependencies, would likely need similar changes if this approach is appropriate.

Because this pull request affects code that is possibly undergoing considerable change for Svelte 5, as well as it being a breaking change, it may be appropriate for this pull request to inform that process rather than be used directly.

robertadamsonsmith avatar Jul 06 '23 16:07 robertadamsonsmith