Skip to content

Commit c6f51d8

Browse files
committed
more
1 parent 841f2e5 commit c6f51d8

11 files changed

+166
-198
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
title: <svelte:window>
3+
---
4+
5+
```svelte
6+
<svelte:window onevent={handler} />
7+
```
8+
9+
```svelte
10+
<svelte:window bind:prop={value} />
11+
```
12+
13+
The `<svelte:window>` element allows you to add event listeners to the `window` object without worrying about removing them when the component is destroyed, or checking for the existence of `window` when server-side rendering.
14+
15+
This element may only appear at the top level of your component — it cannot be inside a block or element.
16+
17+
```svelte
18+
<script>
19+
function handleKeydown(event) {
20+
alert(`pressed the ${event.key} key`);
21+
}
22+
</script>
23+
24+
<svelte:window onkeydown={handleKeydown} />
25+
```
26+
27+
You can also bind to the following properties:
28+
29+
- `innerWidth`
30+
- `innerHeight`
31+
- `outerWidth`
32+
- `outerHeight`
33+
- `scrollX`
34+
- `scrollY`
35+
- `online` — an alias for `window.navigator.onLine`
36+
- `devicePixelRatio`
37+
38+
All except `scrollX` and `scrollY` are readonly.
39+
40+
```svelte
41+
<svelte:window bind:scrollY={y} />
42+
```
43+
44+
> [!NOTE] Note that the page will not be scrolled to the initial value to avoid accessibility issues. Only subsequent changes to the bound variable of `scrollX` and `scrollY` will cause scrolling. If you have a legitimate reason to scroll when the component is rendered, call `scrollTo()` in an `$effect`.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
title: <svelte:document>
3+
---
4+
5+
```svelte
6+
<svelte:document onevent={handler} />
7+
```
8+
9+
```svelte
10+
<svelte:document bind:prop={value} />
11+
```
12+
13+
Similarly to `<svelte:window>`, this element allows you to add listeners to events on `document`, such as `visibilitychange`, which don't fire on `window`. It also lets you use [actions](use) on `document`.
14+
15+
As with `<svelte:window>`, this element may only appear the top level of your component and must never be inside a block or element.
16+
17+
```svelte
18+
<svelte:document onvisibilitychange={handleVisibilityChange} use:someAction />
19+
```
20+
21+
You can also bind to the following properties:
22+
23+
- `activeElement`
24+
- `fullscreenElement`
25+
- `pointerLockElement`
26+
- `visibilityState`
27+
28+
All are readonly.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
title: <svelte:body>
3+
---
4+
5+
```svelte
6+
<svelte:body onevent={handler} />
7+
```
8+
9+
Similarly to `<svelte:window>`, this element allows you to add listeners to events on `document.body`, such as `mouseenter` and `mouseleave`, which don't fire on `window`. It also lets you use [actions](use) on the `<body>` element.
10+
11+
As with `<svelte:window>` and `<svelte:document>`, this element may only appear the top level of your component and must never be inside a block or element.
12+
13+
```svelte
14+
<svelte:body onmouseenter={handleMouseenter} onmouseleave={handleMouseleave} use:someAction />
15+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: <svelte:head>
3+
---
4+
5+
```svelte
6+
<svelte:head>...</svelte:head>
7+
```
8+
9+
This element makes it possible to insert elements into `document.head`. During server-side rendering, `head` content is exposed separately to the main `body` content.
10+
11+
As with `<svelte:window>`, `<svelte:document>` and `<svelte:body>`, this element may only appear at the top level of your component and must never be inside a block or element.
12+
13+
```svelte
14+
<svelte:head>
15+
<title>Hello world!</title>
16+
<meta name="description" content="This is where the description goes for SEO" />
17+
</svelte:head>
18+
```
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
title: <svelte:element>
3+
---
4+
5+
```svelte
6+
<svelte:element this={expression} />
7+
```
8+
9+
The `<svelte:element>` element lets you render an element that is unknown at author time, for example because it comes from a CMS. Any properties and event listeners present will be applied to the element.
10+
11+
The only supported binding is `bind:this`, since Svelte's built-in bindings do not work with generic elements.
12+
13+
If `this` has a nullish value, the element and its children will not be rendered.
14+
15+
If `this` is the name of a [void element](https://developer.mozilla.org/en-US/docs/Glossary/Void_element) (e.g., `br`) and `<svelte:element>` has child elements, a runtime error will be thrown in development mode:
16+
17+
```svelte
18+
<script>
19+
let tag = $state('hr');
20+
</script>
21+
22+
<svelte:element this={tag}>
23+
This text cannot appear inside an hr element
24+
</svelte:element>
25+
```
26+
27+
Svelte tries its best to infer the correct namespace from the element's surroundings, but it's not always possible. You can make it explicit with an `xmlns` attribute:
28+
29+
```svelte
30+
<svelte:element this={tag} xmlns="http://www.w3.org/2000/svg" />
31+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
title: <svelte:element>
3+
---
4+
5+
```svelte
6+
<svelte:options option={value} />
7+
```
8+
9+
The `<svelte:options>` element provides a place to specify per-component compiler options, which are detailed in the [compiler section](svelte-compiler#compile). The possible options are:
10+
11+
- `immutable={true}` — you never use mutable data, so the compiler can do simple referential equality checks to determine if values have changed
12+
- `immutable={false}` — the default. Svelte will be more conservative about whether or not mutable objects have changed
13+
- `accessors={true}` — adds getters and setters for the component's props
14+
- `accessors={false}` — the default
15+
- `runes={true}` — forces a component into _runes mode_ (see the [Legacy APIs](legacy-overview) section)
16+
- `runes={false}` — forces a component into _legacy mode_
17+
- `namespace="..."` — the namespace where this component will be used, most commonly "svg"; use the "foreign" namespace to opt out of case-insensitive attribute names and HTML-specific warnings
18+
- `customElement={...}` — the [options](custom-elements#Component-options) to use when compiling this component as a custom element. If a string is passed, it is used as the `tag` option
19+
20+
```svelte
21+
<svelte:options customElement="my-custom-element" />
22+
```

documentation/docs/05-special-elements/09-special-elements.md

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

documentation/docs/06-runtime/01-stores.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
title: Stores
33
---
44

5-
- how to use
5+
<!-- - how to use
66
- how to write
7-
- TODO should the details for the store methods belong to the reference section?
7+
- TODO should the details for the store methods belong to the reference section? -->
88

99
A _store_ is an object that allows reactive access to a value via a simple _store contract_. The [`svelte/store` module](../svelte-store) contains minimal store implementations which fulfil this contract.
1010

documentation/docs/06-runtime/02-context.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
title: Context
33
---
44

5-
- get/set/hasContext
6-
- how to use, best practises (like encapsulating them)
5+
<!-- - get/set/hasContext
6+
- how to use, best practises (like encapsulating them) -->
77

88
Most state is component-level state that lives as long as its component lives. There's also section-wide or app-wide state however, which also needs to be handled somehow.
99

documentation/docs/06-runtime/03-lifecycle-hooks.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
title: Lifecycle hooks
33
---
44

5-
- onMount/onDestroy
5+
<!-- - onMount/onDestroy
66
- mention that `$effect` might be better for your use case
77
- beforeUpdate/afterUpdate with deprecation notice?
8-
- or skip this entirely and only have it in the reference docs?
8+
- or skip this entirely and only have it in the reference docs? -->
99

1010
In Svelte 5, the component lifecycle consists of only two parts: Its creation and its destruction. Everything in-between - when certain state is updated - is not related to the component as a whole, only the parts that need to react to the state change are notified. This is because under the hood the smallest unit of change is actually not a component, it's the (render) effects that the component sets up upon component initialization. Consequently, there's no such thing as a "before update"/"after update" hook.
1111

0 commit comments

Comments
 (0)