Template Syntax
Element Directives
As well as attributes, elements can have directives, which control the element's behaviour in some way.
on:_eventname_
Use the on:
directive to listen to DOM events.
Handlers can be declared inline with no performance penalty. As with attributes, directive values may be quoted for the sake of syntax highlighters.
Add modifiers to DOM events with the |
character.
The following modifiers are available:
preventDefault
— callsevent.preventDefault()
before running the handlerstopPropagation
— callsevent.stopPropagation()
, preventing the event reaching the next elementpassive
— improves scrolling performance on touch/wheel events (Svelte will add it automatically where it's safe to do so)nonpassive
— explicitly setpassive: false
capture
— fires the handler during the capture phase instead of the bubbling phaseonce
— remove the handler after the first time it runsself
— only trigger handler ifevent.target
is the element itselftrusted
— only trigger handler ifevent.isTrusted
istrue
. I.e. if the event is triggered by a user action.
Modifiers can be chained together, e.g. on:click|once|capture={...}
.
If the on:
directive is used without a value, the component will forward the event, meaning that a consumer of the component can listen for it.
It's possible to have multiple event listeners for the same event:
bind:_property_
Data ordinarily flows down, from parent to child. The bind:
directive allows data to flow the other way, from child to parent. Most bindings are specific to particular elements.
The simplest bindings reflect the value of a property, such as input.value
.
If the name matches the value, you can use a shorthand.
Numeric input values are coerced; even though input.value
is a string as far as the DOM is concerned, Svelte will treat it as a number. If the input is empty or invalid (in the case of type="number"
), the value is undefined
.
On <input>
elements with type="file"
, you can use bind:files
to get the FileList
of selected files. It is readonly.
bind:
can be used together with on:
directives. The order that they are defined in determines the value of the bound variable when the event handler is called.
Select Element Bindings
A <select>
value binding corresponds to the value
property on the selected <option>
, which can be any value (not just strings, as is normally the case in the DOM).
A <select multiple>
element behaves similarly to a checkbox group.
When the value of an <option>
matches its text content, the attribute can be omitted.
Elements with the contenteditable
attribute support innerHTML
and textContent
bindings.
<details>
elements support binding to the open
property.
Media Element Bindings
Media elements (<audio>
and <video>
) have their own set of bindings — six readonly ones...
duration
(readonly) — the total duration of the video, in secondsbuffered
(readonly) — an array of{start, end}
objectsplayed
(readonly) — dittoseekable
(readonly) — dittoseeking
(readonly) — booleanended
(readonly) — boolean
...and five two-way bindings:
currentTime
— the current playback time in the video, in secondsplaybackRate
— how fast or slow to play the video, where 1 is 'normal'paused
— this one should be self-explanatoryvolume
— a value between 0 and 1muted
— a boolean value indicating whether the player is muted
Videos additionally have readonly videoWidth
and videoHeight
bindings.
Block Element Bindings
Block-level elements have 4 readonly bindings, measured using a technique similar to this one:
clientWidth
clientHeight
offsetWidth
offsetHeight
bind:group
Inputs that work together can use bind:group
.
bind:this
To get a reference to a DOM node, use bind:this
.
class:_name_
A class:
directive provides a shorter way of toggling a class on an element.
style:_property_
The style:
directive provides a shorthand for setting multiple styles on an element.
When style:
directives are combined with style
attributes, the directives will take precedence:
use:_action_
Actions are functions that are called when an element is created. They can return an object with a destroy
method that is called after the element is unmounted:
An action can have a parameter. If the returned value has an update
method, it will be called whenever that parameter changes, immediately after Svelte has applied updates to the markup.
Don't worry about the fact that we're redeclaring the foo
function for every component instance — Svelte will hoist any functions that don't depend on local state out of the component definition.
transition:_fn_
A transition is triggered by an element entering or leaving the DOM as a result of a state change.
When a block is transitioning out, all elements inside the block, including those that do not have their own transitions, are kept in the DOM until every transition in the block has completed.
The transition:
directive indicates a bidirectional transition, which means it can be smoothly reversed while the transition is in progress.
By default intro transitions will not play on first render. You can modify this behaviour by setting intro: true
when you create a component.
Transition Parameters
Like actions, transitions can have parameters.
(The double {{curlies}}
aren't a special syntax; this is an object literal inside an expression tag.)
Custom Transition Functions
Transitions can use custom functions. If the returned object has a css
function, Svelte will create a CSS animation that plays on the element.
The t
argument passed to css
is a value between 0
and 1
after the easing
function has been applied. In transitions run from 0
to 1
, out transitions run from 1
to 0
— in other words 1
is the element's natural state, as though no transition had been applied. The u
argument is equal to 1 - t
.
The function is called repeatedly before the transition begins, with different t
and u
arguments.
A custom transition function can also return a tick
function, which is called during the transition 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.
If a transition returns a function instead of a transition object, the function will be called in the next microtask. This allows multiple transitions to coordinate, making crossfade effects possible.
Transition Events
An element with transitions will dispatch the following events in addition to any standard DOM events:
introstart
introend
outrostart
outroend
Local transitions only play when the block they belong to is created or destroyed, not when parent blocks are created or destroyed.
in:_fn_/out:_fn_
Similar to transition:
, but only applies to elements entering (in:
) or leaving (out:
) the DOM.
Unlike with transition:
, transitions applied with in:
and out:
are not bidirectional — an in transition will continue to 'play' alongside the out transition, rather than reversing, if the block is outroed while the transition is in progress. If an out transition is aborted, transitions will restart from scratch.
animate:_fn_
An animation is triggered when the contents of a keyed each block are re-ordered. Animations do not run when an element is added or removed, only when the index of an existing data item within the each block changes. 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 or custom animation functions.
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.)
Custom Animation Functions
Animations can use custom functions that provide the node
, an animation
object and any parameters
as arguments. The animation
parameter is an object containing from
and to
properties each containing a DOMRect 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.
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.