Skip to content

Commit af580f2

Browse files
committed
docs: Svelte 3/4 differences
note some inline, drive-by-fix in custom element docs closes #8703
1 parent 62bef80 commit af580f2

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

documentation/docs/02-template-syntax/03-logic-blocks.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ An each block can also have an `{:else}` clause, which is rendered if the list i
120120
{/each}
121121
```
122122

123-
It is possible to iterate over iterables like `Map` or `Set`. Iterables need to be finite and static (they shouldn't change while being iterated over). Under the hood, they are transformed to an array using `Array.from` before being passed off to rendering. If you're writing performance-sensitive code, try to avoid iterables and use regular arrays as they are more performant.
123+
Since Svelte 4 it is possible to iterate over iterables like `Map` or `Set`. Iterables need to be finite and static (they shouldn't change while being iterated over). Under the hood, they are transformed to an array using `Array.from` before being passed off to rendering. If you're writing performance-sensitive code, try to avoid iterables and use regular arrays as they are more performant.
124124

125125
## {#await ...}
126126

documentation/docs/04-compiler-and-api/01-svelte-compiler.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ The following options can be passed to the compiler. None are required:
7878
| `cssOutputFilename` | `null` | A `string` used for your CSS sourcemap. |
7979
| `sveltePath` | `"svelte"` | The location of the `svelte` package. Any imports from `svelte` or `svelte/[module]` will be modified accordingly. |
8080
| `namespace` | `"html"` | The namespace of the element; e.g., `"mathml"`, `"svg"`, `"foreign"`. |
81+
| `format` | `"esm"` | This option only exists in Svelte 3. If `"esm"`, creates a JavaScript module (with import and export). If `"cjs"`, creates a CommonJS module (with require and module.exports). |
8182

8283
The returned `result` object contains the code for your component, along with useful bits of metadata.
8384

documentation/docs/04-compiler-and-api/04-custom-elements-api.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ title: 'Custom elements API'
55
Svelte components can also be compiled to custom elements (aka web components) using the `customElement: true` compiler option. You should specify a tag name for the component using the `<svelte:options>` [element](/docs/special-elements#svelte-options).
66

77
```svelte
8+
<svelte:options customElement="my-element" />
9+
10+
<!-- in Svelte 3, do this instead:
811
<svelte:options tag="my-element" />
12+
-->
913
1014
<script>
1115
export let name = 'world';
@@ -21,7 +25,9 @@ You can leave out the tag name for any of your inner components which you don't
2125
// @noErrors
2226
import MyElement from './MyElement.svelte';
2327

24-
customElements.define('my-element', MyElement);
28+
customElements.define('my-element', MyElement.element);
29+
// In Svelte 3, do this instead:
30+
// customElements.define('my-element', MyElement);
2531
```
2632

2733
Once a custom element has been defined, it can be used as a regular DOM element:
@@ -49,6 +55,8 @@ console.log(el.name);
4955
el.name = 'everybody';
5056
```
5157

58+
## Component options
59+
5260
When constructing a custom element, you can tailor several aspects by defining `customElement` as an object within `<svelte:options>`. This object comprises a mandatory `tag` property for the custom element's name, an optional `shadow` property that can be set to `"none"` to forgo shadow root creation (note that styles are then no longer encapsulated, and you can't use slots), and a `props` option, which offers the following settings:
5361

5462
- `attribute: string`: To update a custom element's prop, you have two alternatives: either set the property on the custom element's reference as illustrated above or use an HTML attribute. For the latter, the default attribute name is the lowercase property name. Modify this by assigning `attribute: "<desired name>"`.
@@ -73,6 +81,10 @@ When constructing a custom element, you can tailor several aspects by defining `
7381
...
7482
```
7583

84+
These options don't exist in Svelte 3.
85+
86+
## Caveats and limitations
87+
7688
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:
7789

7890
- Styles are _encapsulated_, rather than merely _scoped_ (unless you set `shadow: "none"`). This means that any non-component styles (such as you might have in a `global.css` file) will not apply to the custom element, including styles with the `:global(...)` modifier

0 commit comments

Comments
 (0)