Action Interface
The type of element this action works on (e.g.,
HTMLDivElement, HTMLButtonElement)The type of parameter the action accepts. Use
undefined if the action takes no parametersAdditional attributes and events the action enables on the element (TypeScript only)
ActionReturn Interface
Called whenever the action’s parameter changes, immediately after Svelte applies markup updates
Called when the element is unmounted from the DOM
Usage
Basic Action
Create a simple action that runs when an element is added to the DOM:Action with Parameters
Pass parameters to customize the action’s behavior:Action with Update
Handle parameter changes with theupdate method:
TypeScript Example
Type your actions for better IDE support:Adding Type-Safe Attributes
Define custom attributes and events that the action enables:Common Patterns
Click Outside
Focus Trap
Notes
- Actions are called when the element is created
- The
destroymethod is called when the element is removed from the DOM - The
updatemethod is called whenever the parameter changes - Multiple actions can be used on the same element
- Actions run after the element is added to the DOM
- Actions are useful for integrating third-party libraries
- The
Attributesgeneric is TypeScript-only and has no runtime effect
See Also
- Event handlers - Handle DOM events
- Bindings - Bind to element properties
- use: directive - Element directive syntax