0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-12-01 09:21:21 +01:00
svelte/site/content/tutorial
Simon H d083f8a3f2
feat: custom elements rework (#8457)
This is an overhaul of custom elements in Svelte. Instead of compiling to a custom element class, the Svelte component class is mostly preserved as-is. Instead a wrapper is introduced which wraps a Svelte component constructor and returns a HTML element constructor. This has a couple of advantages:

- component can be used both as a custom element as well as a regular component. This allows creating one wrapper custom element and using regular Svelte components inside. Fixes #3594, fixes #3128, fixes #4274, fixes #5486, fixes #3422, fixes #2969, helps with https://github.com/sveltejs/kit/issues/4502
- all components are compiled with injected styles (inlined through Javascript), fixes #4274
- the wrapper instantiates the component in `connectedCallback` and disconnects it in `disconnectedCallback` (but only after one tick, because this could be a element move). Mount/destroy works as expected inside, fixes #5989, fixes #8191
- the wrapper forwards `addEventListener` calls to `component.$on`, which allows to listen to custom events, fixes #3119, closes #4142 
- some things are hard to auto-configure, like attribute hyphen preferences or whether or not setting a property should reflect back to the attribute. This is why `<svelte:options customElement={..}>` can also take an object to modify such aspects. This option allows to specify whether setting a prop should be reflected back to the attribute (default `false`), what to use when converting the property to the attribute value and vice versa (through `type`, default `String`, or when `export let prop = false` then `Boolean`), and what the corresponding attribute for the property is (`attribute`, default lowercased prop name). These options are heavily inspired by lit: https://lit.dev/docs/components/properties. Closes #7638, fixes #5705
- adds a `shadowdom` option to control whether or not encapsulate the custom element. Closes #4330, closes #1748 

Breaking changes:
- Wrapped Svelte component now stays as a regular Svelte component (invokeing it like before with `new Component({ target: ..})` won't create a custom element). Its custom element constructor is now a static property named `element` on the class (`Component.element`) and should be regularly invoked through setting it in the html.
- The timing of mount/destroy/update is different. Mount/destroy/updating a prop all happen after a tick, so `shadowRoot.innerHTML` won't immediately reflect the change (Lit does this too). If you rely on it, you need to await a promise
2023-05-02 12:39:23 +02:00
..
01-introduction [docs] move @html tutorial to end (partly fixing #7253) (#7254) 2023-02-21 20:00:15 +01:00
02-reactivity [docs] fix typo (#8180) 2023-01-10 09:38:47 +01:00
03-props site: change "on to" to "onto" in tutorial 03 (#5386) 2020-09-11 17:51:59 -04:00
04-logic docs: add console output instruction to tutorial (#8336) 2023-02-28 09:44:39 +01:00
05-events [docs] absolute and future proof links (#7038) 2021-12-21 09:46:39 -08:00
06-bindings feat: bind:innerText for contenteditable (#4291) 2023-03-16 10:58:49 +01:00
07-lifecycle update api 2022-04-20 12:52:22 +08:00
08-stores [docs] Rename a variable count_value to countValue in the Stores section (#7180) 2022-01-27 11:13:25 +01:00
09-motion [docs] fix inconsistent margin in svelte/motion spring tutorial (#8081) 2022-12-06 00:14:11 +09:00
10-transitions [docs] absolute and future proof links (#7038) 2021-12-21 09:46:39 -08:00
11-animations [docs] absolute and future proof links (#7038) 2021-12-21 09:46:39 -08:00
12-actions [docs] clear timeout on destroy 2022-12-22 17:47:52 +01:00
13-advanced-styling [docs] style directive tutorial (#7161) 2022-04-12 11:01:32 -07:00
14-composition [docs] Typescript -> TypeScript (#7797) 2022-08-19 13:30:22 +09:00
15-context [docs] Use symbols for context keys in tutorial (#7046) 2022-01-16 09:32:13 +01:00
16-special-elements feat: custom elements rework (#8457) 2023-05-02 12:39:23 +02:00
17-module-context [docs] fix typo (#7344) 2022-03-06 16:46:48 +01:00
18-special-tags [docs] move @html tutorial to end (partly fixing #7253) (#7254) 2023-02-21 20:00:15 +01:00
19-next-steps [docs] create getting started guide outside blog (#7812) 2022-10-04 21:38:58 -07:00