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: make transitions local by default #8632

Merged
merged 2 commits into from
May 26, 2023
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* **breaking** Stricter types for `onMount` - now throws a type error when returning a function asynchronously to catch potential mistakes around callback functions (see PR for migration instructions) ([#8136](https://github.com/sveltejs/svelte/pull/8136))
* **breaking** Overhaul and drastically improve creating custom elements with Svelte (see PR for list of changes and migration instructions) ([#8457](https://github.com/sveltejs/svelte/pull/8457))
* **breaking** Deprecate `SvelteComponentTyped`, use `SvelteComponent` instead ([#8512](https://github.com/sveltejs/svelte/pull/8512))
* **breaking** Make transitions local by default to prevent confusion around page navigations ([#6686](https://github.com/sveltejs/svelte/issues/6686))
* **breaking** Error on falsy values instead of stores passed to `derived` ([#7947](https://github.com/sveltejs/svelte/pull/7947))
* **breaking** Custom store implementers now need to pass an `update` function additionally to the `set` function ([#6750](https://github.com/sveltejs/svelte/pull/6750))
* **breaking** Change order in which preprocessors are applied ([#8618](https://github.com/sveltejs/svelte/pull/8618))
Expand Down
59 changes: 40 additions & 19 deletions site/content/docs/03-template-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,12 @@ transition:fn
transition:fn={params}
```
```sv
transition:fn|global
```
```sv
transition:fn|global={params}
```
```sv
transition:fn|local
```
```sv
Expand Down Expand Up @@ -1027,7 +1033,28 @@ The `transition:` directive indicates a *bidirectional* transition, which means
{/if}
```

> By default intro transitions will not play on first render. You can modify this behaviour by setting `intro: true` when you [create a component](/docs#run-time-client-side-component-api).
---

Transitions are local by default (in Svelte 3, they were global by default). Local transitions only play when the block they belong to is created or destroyed, *not* when parent blocks are created or destroyed.

```sv
{#if x}
{#if y}
<!-- Svelte 3: <p transition:fade|local> -->
<p transition:fade>
fades in and out only when y changes
</p>

<!-- Svelte 3: <p transition:fade> -->
<p transition:fade|global>
fades in and out when x or y change
</p>

{/if}
{/if}
```

> By default intro transitions will not play on first render. You can modify this behaviour by setting `intro: true` when you [create a component](/docs#run-time-client-side-component-api) and marking the transition as `global`.

##### Transition parameters

Expand Down Expand Up @@ -1153,24 +1180,6 @@ An element with transitions will dispatch the following events in addition to an
{/if}
```

---

Local transitions only play when the block they belong to is created or destroyed, *not* when parent blocks are created or destroyed.

```sv
{#if x}
{#if y}
<p transition:fade>
fades in and out when x or y change
</p>

<p transition:fade|local>
fades in and out only when y changes
</p>
{/if}
{/if}
```


#### in:*fn*/out:*fn*

Expand All @@ -1181,6 +1190,12 @@ in:fn
in:fn={params}
```
```sv
in:fn|global
```
```sv
in:fn|global={params}
```
```sv
in:fn|local
```
```sv
Expand All @@ -1194,6 +1209,12 @@ out:fn
out:fn={params}
```
```sv
out:fn|global
```
```sv
out:fn|global={params}
```
```sv
out:fn|local
```
```sv
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/compile/nodes/Transition.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default class Transition extends Node {
this.name = info.name;
component.add_reference(/** @type {any} */ (this), info.name.split('.')[0]);
this.directive = info.intro && info.outro ? 'transition' : info.intro ? 'in' : 'out';
this.is_local = info.modifiers.includes('local');
this.is_local = !info.modifiers.includes('global');
if ((info.intro && parent.intro) || (info.outro && parent.outro)) {
const parent_transition = parent.intro || parent.outro;
component.error(
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/compile/render_dom/wrappers/Element/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,10 +422,10 @@ export default class ElementWrapper extends Wrapper {
`);
}
if (this.child_dynamic_element_block.has_intros) {
block.chunks.intro.push(b`@transition_in(${this.var});`);
block.chunks.intro.push(b`@transition_in(${this.var}, #local);`);
}
if (this.child_dynamic_element_block.has_outros) {
block.chunks.outro.push(b`@transition_out(${this.var});`);
block.chunks.outro.push(b`@transition_out(${this.var}, #local);`);
}
block.chunks.destroy.push(b`if (${this.var}) ${this.var}.d(detaching)`);
if (this.node.animation) {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/transition-local/input.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@

{#if x}
{#if y}
<div in:foo|local>...</div>
<div in:foo>...</div>
{/if}
{/if}
2 changes: 1 addition & 1 deletion test/js/samples/transition-repeated-outro/input.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</script>

{#if num < 5}
<div out:fade>
<div out:fade|global>
<p>wheeee</p>
</div>
{/if}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
</script>

{#if visible}
<svelte:element this={tag} transition:foo></svelte:element>
<svelte:element this={tag} transition:foo|global></svelte:element>
{/if}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</script>

{#await promise}
<p class='pending' transition:foo>loading...</p>
<p class='pending' transition:foo|global>loading...</p>
{:then value}
<p class='then' transition:foo>{value}</p>
{:catch error}
Expand Down
2 changes: 1 addition & 1 deletion test/runtime/samples/transition-js-await-block/main.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</script>

{#await promise}
<p class='pending' transition:foo>loading...</p>
<p class='pending' transition:foo|global>loading...</p>
{:then value}
<p class='then' transition:foo>{value}</p>
{:catch error}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
</script>

{#each things as thing}
<div in:foo>{thing}</div>
<div in:foo|global>{thing}</div>
{/each}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
</script>

{#each things as thing (thing.name)}
<div transition:foo>{thing.name}</div>
<div transition:foo|global>{thing.name}</div>
{/each}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
</script>

{#each things as thing (thing.name)}
<div in:foo>{thing.name}</div>
<div in:foo|global>{thing.name}</div>
{/each}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@

{#each [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] as number}
{#if threshold >= number}
<div transition:foo>{number}</div>
<div transition:foo|global>{number}</div>
{/if}
{/each}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

export let x;

function foo(node, params) {
function foo(node) {
return {
duration: 400,
tick: t => {
Expand All @@ -17,5 +17,5 @@
{#if x}
<div bind:this={yes} in:foo>yes</div>
{:else}
<div bind:this={no} in:foo>no</div>
<div bind:this={no} in:foo|global>no</div>
{/if}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
}
</script>

<div transition:foo></div>
<div transition:foo|global></div>
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

{#if x}
{#if y}
<div transition:foo|local>snaps if x changes</div>
<div transition:foo>transitions if x changes</div>
<div transition:foo>snaps if x changes</div>
<div transition:foo|global>transitions if x changes</div>
{/if}
{/if}
2 changes: 1 addition & 1 deletion test/runtime/samples/transition-js-local/main.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@

{#if x}
{#if y}
<div transition:foo|local></div>
<div transition:foo></div>
{/if}
{/if}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@

{#if x}
{#await promise then value}
<div transition:foo></div>
<div transition:foo|global></div>
{/await}
{/if}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@

{#if x}
{#each things as thing (thing)}
<div transition:foo></div>
<div transition:foo|global></div>
{/each}
{/if}
2 changes: 1 addition & 1 deletion test/runtime/samples/transition-js-nested-each/main.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@

{#if x}
{#each things as thing}
<div transition:foo></div>
<div transition:foo|global></div>
{/each}
{/if}
2 changes: 1 addition & 1 deletion test/runtime/samples/transition-js-nested-if/main.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@

{#if x}
{#if y}
<div transition:foo></div>
<div transition:foo|global></div>
{/if}
{/if}