Skip to content

Commit

Permalink
🐛 Cleans teleported clone (#4328)
Browse files Browse the repository at this point in the history
* 🧪 Adds failing test for teleport cleanup

* 🐛 Cleans up Teleport clones
  • Loading branch information
ekwoka authored Aug 13, 2024
1 parent 1539ee9 commit ab743bc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
9 changes: 7 additions & 2 deletions packages/alpinejs/src/directives/x-teleport.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { skipDuringClone } from "../clone"
import { directive } from "../directives"
import { initTree } from "../lifecycle"
import { initTree, destroyTree } from "../lifecycle"
import { mutateDom } from "../mutation"
import { addScopeToNode } from "../scope"
import { warn } from "../utils/warn"
Expand Down Expand Up @@ -64,7 +64,12 @@ directive('teleport', (el, { modifiers, expression }, { cleanup }) => {
})
}

cleanup(() => clone.remove())
cleanup(() =>
mutateDom(() => {
clone.remove()
destroyTree(clone)
})
)
})

let teleportContainerDuringClone = document.createElement('div')
Expand Down
31 changes: 31 additions & 0 deletions tests/cypress/integration/directives/x-teleport.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,37 @@ test('removing teleport source removes teleported target',
},
)

test(
'immediately cleans up the clone when the original template is removed',
[
html`
<div x-data="{ show: true, shown: 'original' }">
<span x-text="shown"></span>
<template x-if="show">
<div>
<template x-teleport="#target">
<button x-data="{
init() { this.shown = 'cloned' },
destroy() { this.shown = 'destroyed' }
}" @click="show = false">remove</button>
</template>
</div>
</template>
<section id="target"></section>
</div>
`,
],
({ get }) => {
get('section').should(haveText('remove'));
get("button").should(exist());
get('span').should(haveText('cloned'));
get('button').click();
get('section').should(haveText(''));
get('button').should(notExist());
get('span').should(haveText('destroyed'));
}
);

test('$refs inside teleport can be accessed outside',
[html`
<div x-data="{ count: 1 }" id="a">
Expand Down

0 comments on commit ab743bc

Please sign in to comment.