Skip to content

Commit 6cb43b9

Browse files
committed
updated the formatting
1 parent 83addfb commit 6cb43b9

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed

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

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Svelte components can also be compiled to custom elements (aka web components) u
1010
<svelte:options customElement="my-element" />
1111
1212
<script>
13-
let { name = 'world' } = $props();
13+
let { name = 'world' } = $props();
1414
</script>
1515
1616
<h1>Hello {name}!</h1>
@@ -30,9 +30,9 @@ Once a custom element has been defined, it can be used as a regular DOM element:
3030

3131
```js
3232
document.body.innerHTML = `
33-
<my-element>
34-
<p>This is some slotted content</p>
35-
</my-element>
33+
<my-element>
34+
<p>This is some slotted content</p>
35+
</my-element>
3636
`;
3737
```
3838

@@ -49,13 +49,13 @@ console.log(el.name);
4949
el.name = 'everybody';
5050
```
5151

52-
Note that you need to list out all properties explicitly, i.e. doing `let props = $props()` without declaring `props` in the [component options](#component-options) means that Svelte can't know which props to expose as properties on the DOM element.
52+
Note that you need to list out all properties explicitly, i.e. doing `let props = $props()` without declaring `props` in the [component options](#Component-options) means that Svelte can't know which props to expose as properties on the DOM element.
5353

5454
## Component lifecycle
5555

5656
Custom elements are created from Svelte components using a wrapper approach. This means the inner Svelte component has no knowledge that it is a custom element. The custom element wrapper takes care of handling its lifecycle appropriately.
5757

58-
When a custom element is created, the Svelte component it wraps is _not_ created right away. It is only created in the next tick after the `connectedCallback` is invoked. Properties assigned to the custom element before it is inserted into the DOM are temporarily saved and then set on component creation, so their values are not lost. The same does not work for invoking exported functions on the custom element though, they are only available after the element has mounted. If you need to invoke functions before component creation, you can work around it by using the [`extend` option](#component-options).
58+
When a custom element is created, the Svelte component it wraps is _not_ created right away. It is only created in the next tick after the `connectedCallback` is invoked. Properties assigned to the custom element before it is inserted into the DOM are temporarily saved and then set on component creation, so their values are not lost. The same does not work for invoking exported functions on the custom element though, they are only available after the element has mounted. If you need to invoke functions before component creation, you can work around it by using the [`extend` option](#Component-options).
5959

6060
When a custom element written with Svelte is created or updated, the shadow DOM will reflect the value in the next tick, not immediately. This way updates can be batched, and DOM moves which temporarily (but synchronously) detach the element from the DOM don't lead to unmounting the inner component.
6161

@@ -76,39 +76,39 @@ When constructing a custom element, you can tailor several aspects by defining `
7676

7777
```svelte
7878
<svelte:options
79-
customElement={{
80-
tag: 'custom-element',
81-
shadow: 'none',
82-
props: {
83-
name: { reflect: true, type: 'Number', attribute: 'element-index' }
84-
},
85-
extend: (customElementConstructor) => {
86-
// Extend the class so we can let it participate in HTML forms
87-
return class extends customElementConstructor {
88-
static formAssociated = true;
89-
90-
constructor() {
91-
super();
92-
this.attachedInternals = this.attachInternals();
93-
}
94-
95-
// Add the function here, not below in the component so that
96-
// it's always available, not just when the inner Svelte component
97-
// is mounted
98-
randomIndex() {
99-
this.elementIndex = Math.random();
100-
}
101-
};
102-
}
103-
}}
79+
customElement={{
80+
tag: 'custom-element',
81+
shadow: 'none',
82+
props: {
83+
name: { reflect: true, type: 'Number', attribute: 'element-index' }
84+
},
85+
extend: (customElementConstructor) => {
86+
// Extend the class so we can let it participate in HTML forms
87+
return class extends customElementConstructor {
88+
static formAssociated = true;
89+
90+
constructor() {
91+
super();
92+
this.attachedInternals = this.attachInternals();
93+
}
94+
95+
// Add the function here, not below in the component so that
96+
// it's always available, not just when the inner Svelte component
97+
// is mounted
98+
randomIndex() {
99+
this.elementIndex = Math.random();
100+
}
101+
};
102+
}
103+
}}
104104
/>
105105
106106
<script>
107-
let { elementIndex, attachedInternals } = $props();
108-
// ...
109-
function check() {
110-
attachedInternals.checkValidity();
111-
}
107+
let { elementIndex, attachedInternals } = $props();
108+
// ...
109+
function check() {
110+
attachedInternals.checkValidity();
111+
}
112112
</script>
113113
114114
...

0 commit comments

Comments
 (0)