Skip to content

Commit 4285bf9

Browse files
committed
docs: move $host docs to custom elements page
1 parent 6915c12 commit 4285bf9

File tree

2 files changed

+36
-37
lines changed

2 files changed

+36
-37
lines changed

documentation/docs/02-runes/08-$host.md

Lines changed: 0 additions & 37 deletions
This file was deleted.

documentation/docs/07-misc/04-custom-elements.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,42 @@ When constructing a custom element, you can tailor several aspects by defining `
114114
...
115115
```
116116

117+
## `$host`
118+
119+
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 ([demo](/playground/untitled#H4sIAAAAAAAAE41Ry2rDMBD8FSECtqkTt1fHFpSSL-ix7sFRNkTEXglrnTYY_3uRlDgxTaEHIfYxs7szA9-rBizPPwZOZwM89wmecqxbF70as7InaMjltrWFR3mpkQDJ8pwXVnbKkKiwItUa3RGLVtk7gTHQXRDR2lXda4CY1D0SK9nCUk0QPyfrCovsRoNFe17aQOAwGncgO2gBqRzihJXiQrEs2csYOhQ-7HgKHaLIbpRhhBG-I2eD_8ciM4KnnOCbeE5dD2P6h0Dz0-Yi_arNhPLJXBtSGi2TvSXdbpqwdsXvjuYsC1veabvvUTog2ylrapKH2G2XsMFLS4uDthQnq2t1cwKkGOGLvYU5PvaQxLsxOkPmsm97Io1Mo2yUPF6VnOZFkw1RMoopKLKAE_9gmGxyDFMwMcwN-Bx_ABXQWmOtAgAA)):
120+
121+
<!-- prettier-ignore -->
122+
```svelte
123+
/// file: Stepper.svelte
124+
<svelte:options customElement="my-stepper" />
125+
126+
<script>
127+
function dispatch(type) {
128+
+++$host()+++.dispatchEvent(new CustomEvent(type));
129+
}
130+
</script>
131+
132+
<button onclick={() => dispatch('decrement')}>decrement</button>
133+
<button onclick={() => dispatch('increment')}>increment</button>
134+
```
135+
136+
<!-- prettier-ignore -->
137+
```svelte
138+
/// file: App.svelte
139+
<script>
140+
import './Stepper.svelte';
141+
142+
let count = $state(0);
143+
</script>
144+
145+
<my-stepper
146+
ondecrement={() => count -= 1}
147+
onincrement={() => count += 1}
148+
></my-stepper>
149+
150+
<p>count: {count}</p>
151+
```
152+
117153
## Caveats and limitations
118154

119155
Custom elements can be a useful way to package components for consumption in a non-Svelte app, as they will work with vanilla HTML and JavaScript as well as [most frameworks](https://custom-elements-everywhere.com/). There are, however, some important differences to be aware of:

0 commit comments

Comments
 (0)