You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
svelte/documentation/docs/02-template-syntax/07-actions.md

104 lines
2.3 KiB

---
title: Actions
---
- template syntax
- how to write
- typings
- adjust so that `$effect` is used instead of update/destroy?
```svelte
<!--- copy: false --->
use:action
```
```svelte
<!--- copy: false --->
use:action={parameters}
```
```ts
/// copy: false
// @noErrors
action = (node: HTMLElement, parameters: any) => {
update?: (parameters: any) => void,
destroy?: () => void
}
```
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:
```svelte
<!--- file: App.svelte --->
<script>
/** @type {import('svelte/action').Action} */
function foo(node) {
// the node has been mounted in the DOM
return {
destroy() {
// the node has been removed from the DOM
}
};
}
</script>
<div use:foo />
```
An action can have a parameter. If the returned value has an `update` method, it will be called immediately after Svelte has applied updates to the markup whenever that parameter changes.
> [!NOTE] Don't worry 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.
```svelte
<!--- file: App.svelte --->
<script>
/** @type {string} */
export let bar;
/** @type {import('svelte/action').Action<HTMLElement, string>} */
function foo(node, bar) {
// the node has been mounted in the DOM
return {
update(bar) {
// the value of `bar` has changed
},
destroy() {
// the node has been removed from the DOM
}
};
}
</script>
<div use:foo={bar} />
```
## Attributes
Sometimes actions emit custom events and apply custom attributes to the element they are applied to. To support this, actions typed with `Action` or `ActionReturn` type can have a last parameter, `Attributes`:
```svelte
<!--- file: App.svelte --->
<script>
/**
update type generation script (#8712) * chore: playground (#8648) * initialize playground * pnpm up * tidy up git ignore * remove fluff * format * rm readme * fix jsconfig error * add skip-worktree instructions * reload hack * simplify * use rollup * ughh * add flag for SSR * ... * simplify further * configure launch.json * add debugger info to readme * remove vm modules flag * use replaceAll instead of replace * tidy up * fix: make it run * add watch to launch config * Generate type declarations with `dts-buddy` (#8702) * use dts-buddy * remove debug output * remove existing type generation script * fix package.json * update gitignore * bump dts-buddy * remove unused action entry point * add svelte/compiler and svelte/types/compiler/preprocess modules * bump dts-buddy * annoying * changeset * bump dts-buddy * get rid of .d.ts files * another one * Update packages/svelte/package.json Co-authored-by: gtmnayan <50981692+gtm-nayan@users.noreply.github.com> --------- Co-authored-by: Rich Harris <git@rich-harris.dev> Co-authored-by: gtmnayan <50981692+gtm-nayan@users.noreply.github.com> * fix: export ComponentType (#8694) * fix: export ComponentType * ughh * changeset * fix: derived types (#8700) * fix: derived store types * changeset * Version Packages (next) (#8709) Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> * update type generation script * remove unused stuff * fix: changeset publish script isn't called release anymore (#8711) * chore: remove prepare script (#8713) * chore: fix release workflow (#8716) * More readable, Fix $$_attributes * Fix types (#8727) * put comment in right place * bump dts-buddy --------- Co-authored-by: Rich Harris <git@rich-harris.dev> * build types * add svelte/compiler types * remove prepare script * fix * typo * squelch errors * Add svelte and kit to twoslash's types field * squelch more stuff * Add errors to account for new types * Remove deps * formatting tweak * fix linting, maybe * the hell * gah * Fix types a bit * bump dts-buddy * pnpm generate in dev mode * Cache again * reduce index * bump dts-buddy * remove comment --------- Co-authored-by: gtmnayan <50981692+gtm-nayan@users.noreply.github.com> Co-authored-by: Rich Harris <git@rich-harris.dev> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Dominik G <dominik.goepel@gmx.de> Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> Co-authored-by: Puru Vijay <devpuruvj@gmail.com>
1 year ago
* @type {import('svelte/action').Action<HTMLDivElement, { prop: any }, { 'on:emit': (e: CustomEvent<string>) => void }>}
*/
function foo(node, { prop }) {
// the node has been mounted in the DOM
//...LOGIC
node.dispatchEvent(new CustomEvent('emit', { detail: 'hello' }));
return {
destroy() {
// the node has been removed from the DOM
}
};
}
</script>
<div use:foo={{ prop: 'someValue' }} onemit={handleEmit} />
```