Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: parameter for running outro transitions on $destroy #9056

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,20 @@ import { SvelteComponent, ComponentConstructorOptions } from 'svelte';
declare global {
class Component extends SvelteComponent {}
var component: Component;
var run_outro: boolean;
}

export {}

// @filename: index.ts
// ---cut---
component.$destroy();
component.$destroy(run_outro);
```

Removes a component from the DOM and triggers any `onDestroy` handlers.

If `run_outro` is `true`, any outro transitions will play before the component is destroyed.
benmccann marked this conversation as resolved.
Show resolved Hide resolved

## Component props

```js
Expand Down
22 changes: 17 additions & 5 deletions packages/svelte/src/runtime/internal/Component.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
element,
attr
} from './dom.js';
import { transition_in } from './transitions.js';
import { check_outros, group_outros, transition_in, transition_out } from './transitions.js';

/** @returns {void} */
export function bind(component, name, callback) {
Expand Down Expand Up @@ -455,10 +455,22 @@ export class SvelteComponent {
*/
$$set = undefined;

/** @returns {void} */
$destroy() {
destroy_component(this, 1);
this.$destroy = noop;
/**
* @param {boolean} [run_outro]
* @returns {void}
*/
$destroy(run_outro) {
if (run_outro && this.$$.fragment && this.$$.fragment.o) {
group_outros();
transition_out(this.$$.fragment, 0, 0, () => {
destroy_component(this, 1);
this.$destroy = noop;
});
check_outros();
} else {
destroy_component(this, 1);
this.$destroy = noop;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export default {
skip_if_ssr: true,
skip_if_hydrate: true,
skip_if_hydrate_from_ssr: true,
test({ assert, component, target, raf }) {
component.$destroy(true);

return Promise.resolve().then(() => {
const div = target.querySelector('div');

raf.tick(50);
assert.equal(div.transitioned, 0.5);
});
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script>
function transitionOut(node) {
return () => {
return {
duration: 100,
tick: t => {
node.transitioned = t;
}
};
};
}
</script>

<div out:transitionOut|global></div>