Skip to content
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.

## 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>