diff --git a/site/content/docs/02-template-syntax.md b/site/content/docs/02-template-syntax.md index 917673a7b0..a69400f8e3 100644 --- a/site/content/docs/02-template-syntax.md +++ b/site/content/docs/02-template-syntax.md @@ -895,7 +895,138 @@ Local transitions only play when the block they belong to is created or destroye ### Animations -TODO i can't remember how any of this works +```sv +animate:name +``` + +```sv +animate:name={params} +``` + +```js +animation = (node: HTMLElement, { from: DOMRect, to: DOMRect } , params: any) => { + delay?: number, + duration?: number, + easing?: (t: number) => number, + css?: (t: number, u: number) => string, + tick?: (t: number, u: number) => void +} +``` + +```js +DOMRect { + bottom: number, + height: number, + ​​left: number, + right: number, + ​top: number, + width: number, + x: number, + y:number +} +``` + +--- + +An animation is triggered when the contents of a [keyed each block](docs#Each_blocks) are re-ordered. Animations do not run when an element is removed, only when the each block's data is reordered. Animate directives must be on an element that is an *immediate* child of a keyed each block. + +Animations can be used with Svelte's [built-in animation functions](docs#svelte_animate) or [custom animation functions](docs#Custom_animation_functions). + +```html + +{#each list as item, index (item)} +
  • {item}
  • +{/each} +``` + +#### Animation Parameters + +--- + +As with actions and transitions, animations can have parameters. + +(The double `{{curlies}}` aren't a special syntax; this is an object literal inside an expression tag.) + +```html +{#each list as item, index (item)} +
  • {item}
  • +{/each} +``` + +#### Custom animation functions + +--- + +Animations can use custom functions that provide the `node`, an `animation` object and any `paramaters` as arguments. The `animation` parameter is an object containing `from` and `to` properties each containing a [DOMRect](https://developer.mozilla.org/en-US/docs/Web/API/DOMRect#Properties) describing the geometry of the element in its `start` and `end` positions. The `from` property is the DOMRect of the element in its starting position, the `to` property is the DOMRect of the element in its final position after the list has been reordered and the DOM updated. + +If the returned object has a `css` method, Svelte will create a CSS animation that plays on the element. + +The `t` argument passed to `css` is a value that goes from `0` and `1` after the `easing` function has been applied. The `u` argument is equal to `1 - t`. + +The function is called repeatedly *before* the animation begins, with different `t` and `u` arguments. + + +```html + + +{#each list as item, index (item)} +
    {item}
    +{/each} +``` + +--- + + +A custom animation function can also return a `tick` function, which is called *during* the animation with the same `t` and `u` arguments. + +> If it's possible to use `css` instead of `tick`, do so — CSS animations can run off the main thread, preventing jank on slower devices. + +```html + + +{#each list as item, index (item)} +
    {item}
    +{/each} +``` + ### Slots