Skip to content

Commit

Permalink
feat: make transitions local by default
Browse files Browse the repository at this point in the history
To make them global, add the |global modifier
This is a breaking change
closes #6686
  • Loading branch information
dummdidumm committed May 25, 2023
1 parent a40af4d commit c6e2332
Show file tree
Hide file tree
Showing 18 changed files with 59 additions and 38 deletions.
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}
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 @@ -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}

0 comments on commit c6e2332

Please sign in to comment.