In Svelte 5.29 and newer, consider using attachments instead, as they are more flexible and composable.
Actions are functions that are called when an element is mounted. They are added with the use:
directive, and will typically use an $effect
so that they can reset any state when the element is unmounted:
App
<script>
/** @type {import('svelte/action').Action} */
function myaction(node) {
// the node has been mounted in the DOM
$effect(() => {
// setup goes here
return () => {
// teardown goes here
};
});
}
</script>
<div use:myaction>...</div>
<script lang="ts">
import type { Action } from 'svelte/action';
const myaction: Action = (node) => {
// the node has been mounted in the DOM
$effect(() => {
// setup goes here
return () => {
// teardown goes here
};
});
};
</script>
<div use:myaction>...</div>
An action can be called with an argument:
App
<script>
/** @type {import('svelte/action').Action} */
function myaction(node, data) {
// ...
}
</script>
<div use:myaction={data}>...</div>
<script lang="ts">
import type { Action } from 'svelte/action';
const myaction: Action = (node, data) => {
// ...
};
</script>
<div use:myaction={data}>...</div>
The action is only called once (but not during server-side rendering) — it will not run again if the argument changes.
Legacy modeTypingPrior to the
$effect
rune, actions could return an object withupdate
anddestroy
methods, whereupdate
would be called with the latest value of the argument if it changed. Using effects is preferred.
The Action
interface receives three optional type arguments — a node type (which can be Element
, if the action applies to everything), a parameter, and any custom event handlers created by the action:
App
<script>
/**
* @type {import('svelte/action').Action<
* HTMLDivElement,
* undefined,
* {
* onswiperight: (e: CustomEvent) => void;
* onswipeleft: (e: CustomEvent) => void;
* // ...
* }
* >}
*/
function gestures(node) {
$effect(() => {
// ...
node.dispatchEvent(new CustomEvent('swipeleft'));
// ...
node.dispatchEvent(new CustomEvent('swiperight'));
});
}
</script>
<div
use:gestures
onswipeleft={next}
onswiperight={prev}
>...</div>
<script lang="ts">
import type { Action } from 'svelte/action';
const gestures: Action<
HTMLDivElement,
undefined,
{
onswiperight: (e: CustomEvent) => void;
onswipeleft: (e: CustomEvent) => void;
// ...
}
> = (node) => {
$effect(() => {
// ...
node.dispatchEvent(new CustomEvent('swipeleft'));
// ...
node.dispatchEvent(new CustomEvent('swiperight'));
});
};
</script>
<div
use:gestures
onswipeleft={next}
onswiperight={prev}
>...</div>
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4