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 all commits
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 options: { runOutro: boolean } | undefined;
}

export {}

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

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

`options` may contain the property `runOutro` which indicates whether to play any outro transitions 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 {import('./private.js').ComponentDestroyOptions} [options]
* @returns {void}
*/
$destroy(options) {
if (options?.runOutro && 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
9 changes: 6 additions & 3 deletions packages/svelte/src/runtime/internal/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,12 @@ export class SvelteComponentDev extends SvelteComponent {
super();
}

/** @returns {void} */
$destroy() {
super.$destroy();
/**
* @param {import('./private.js').ComponentDestroyOptions} [options]
* @returns {void}
*/
$destroy(options) {
super.$destroy(options);
this.$destroy = () => {
console.warn('Component was already destroyed'); // eslint-disable-line no-console
};
Expand Down
4 changes: 4 additions & 0 deletions packages/svelte/src/runtime/internal/private.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ export interface StyleInformation {
rules: Record<string, true>;
}

export interface ComponentDestroyOptions {
runOutro?: boolean;
}

export type TaskCallback = (now: number) => boolean | void;

export type TaskEntry = { c: TaskCallback; f: () => void };
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({ runOutro: 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>