Skip to main content
When compiling a component as a custom element, the $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

Returns the custom element host instance. You can specify a type parameter to get better type safety:

Basic usage

Here’s an example of a stepper component that dispatches custom events:
The parent component can then listen to these events:

Dispatching events with detail

You can include data in your custom events using the detail 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
Remember that $host() only works in components compiled with customElement option and only runs on the client-side.