Skip to content

Commit 0631011

Browse files
committed
feat: enable animate directive for snippets
1 parent 1d773ef commit 0631011

File tree

7 files changed

+10
-9
lines changed

7 files changed

+10
-9
lines changed

documentation/docs/98-reference/.generated/compile-errors.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ An element can only have one 'animate' directive
99
### animation_invalid_placement
1010

1111
```
12-
An element that uses the `animate:` directive must be the only child of a keyed `{#each ...}` block
12+
An element that uses the `animate:` directive must be the only child of a keyed `{#each ...}` block, or an only child of a snippet
1313
```
1414

1515
### animation_missing_key

packages/svelte/messages/compile-errors/template.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
## animation_invalid_placement
66

7-
> An element that uses the `animate:` directive must be the only child of a keyed `{#each ...}` block
7+
> An element that uses the `animate:` directive must be the only child of a keyed `{#each ...}` block, or an only child of a snippet
88
99
## animation_missing_key
1010

packages/svelte/src/compiler/errors.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -625,12 +625,12 @@ export function animation_duplicate(node) {
625625
}
626626

627627
/**
628-
* An element that uses the `animate:` directive must be the only child of a keyed `{#each ...}` block
628+
* An element that uses the `animate:` directive must be the only child of a keyed `{#each ...}` block, or an only child of a snippet
629629
* @param {null | number | NodeLike} node
630630
* @returns {never}
631631
*/
632632
export function animation_invalid_placement(node) {
633-
e(node, "animation_invalid_placement", `An element that uses the \`animate:\` directive must be the only child of a keyed \`{#each ...}\` block\nhttps://svelte.dev/e/animation_invalid_placement`);
633+
e(node, "animation_invalid_placement", `An element that uses the \`animate:\` directive must be the only child of a keyed \`{#each ...}\` block, or an only child of a snippet\nhttps://svelte.dev/e/animation_invalid_placement`);
634634
}
635635

636636
/**

packages/svelte/src/compiler/phases/2-analyze/visitors/shared/element.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ export function validate_element(node, context) {
9191
validate_attribute_name(attribute);
9292
} else if (attribute.type === 'AnimateDirective') {
9393
const parent = context.path.at(-2);
94-
if (parent?.type !== 'EachBlock') {
94+
if (parent?.type !== 'EachBlock' && parent?.type !== 'SnippetBlock') {
9595
e.animation_invalid_placement(attribute);
96-
} else if (!parent.key) {
96+
} else if (parent.type === 'EachBlock' && !parent.key) {
9797
e.animation_missing_key(attribute);
9898
} else if (
9999
parent.body.nodes.filter(

packages/svelte/src/internal/client/dom/blocks/each.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,14 +276,15 @@ export function each(node, flags, get_collection, get_key, render_fn, fallback_f
276276
* @returns {void}
277277
*/
278278
function reconcile(array, state, anchor, render_fn, flags, is_inert, get_key, get_collection) {
279-
var is_animated = (flags & EACH_IS_ANIMATED) !== 0;
280279
var should_update = (flags & (EACH_ITEM_REACTIVE | EACH_INDEX_REACTIVE)) !== 0;
281280

282281
var length = array.length;
283282
var items = state.items;
284283
var first = state.first;
285284
var current = first;
286285

286+
var is_animated = items.get(get_key(array[0], 0))?.a !== null;
287+
287288
/** @type {undefined | Set<EachItem>} */
288289
var seen;
289290

packages/svelte/tests/validator/samples/animation-not-in-each/errors.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[
22
{
33
"code": "animation_invalid_placement",
4-
"message": "An element that uses the `animate:` directive must be the only child of a keyed `{#each ...}` block",
4+
"message": "An element that uses the `animate:` directive must be the only child of a keyed `{#each ...}` block, or an only child of a snippet",
55
"start": {
66
"line": 5,
77
"column": 5

packages/svelte/tests/validator/samples/animation-siblings/errors.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[
22
{
33
"code": "animation_invalid_placement",
4-
"message": "An element that uses the `animate:` directive must be the only child of a keyed `{#each ...}` block",
4+
"message": "An element that uses the `animate:` directive must be the only child of a keyed `{#each ...}` block, or an only child of a snippet",
55
"start": {
66
"line": 6,
77
"column": 6

0 commit comments

Comments
 (0)