$host rune provides access to the host element, allowing you to (for example) dispatch custom events.
This rune is only available inside custom element components, and only on the client-side.
Signature
Basic usage
Here’s an example of a stepper component that dispatches custom events:Dispatching events with detail
You can include data in your custom events using thedetail property:
Accessing host attributes
You can also use$host() to access attributes set on the custom element:
Use cases
$host is particularly useful for:
- Dispatching custom events from web components
- Reading attributes from the host element
- Calling methods on the host element
- Integrating with web component lifecycle hooks
- Communicating with non-Svelte code
- Custom events
- Reading attributes
- Lifecycle integration