Skip to content

Commit 2a804a4

Browse files
authored
docs: sync content (#115)
preserve local changes showing legacy blocks
1 parent 149b416 commit 2a804a4

File tree

7 files changed

+280
-26
lines changed

7 files changed

+280
-26
lines changed

apps/svelte.dev/.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@
99
/static/svelte-app.json
1010

1111
# git-repositories of synced docs go here
12-
/repos/
12+
/repos/
13+
14+
# TODO remove this after https://github.com/sveltejs/kit/pull/12718 is merged
15+
/content/docs/kit/50-reference

apps/svelte.dev/content/docs/svelte/02-template-syntax/02-basic-markup.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ Because events are just attributes, the same rules as for attributes apply:
112112

113113
Timing-wise, event attributes always fire after events from bindings (e.g. `oninput` always fires after an update to `bind:value`). Under the hood, some event handlers are attached directly with `addEventListener`, while others are _delegated_.
114114

115-
When using `onwheel`, `onmousewheel`, `ontouchstart` and `ontouchmove` event attributes, the handlers are [passive](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#using_passive_listeners) to align with browser defaults. This greatly improves responsiveness by allowing the browser to scroll the document immediately, rather than waiting to see if the event handler calls `event.preventDefault()`.
115+
When using `ontouchstart` and `ontouchmove` event attributes, the handlers are [passive](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#using_passive_listeners) for better performance. This greatly improves responsiveness by allowing the browser to scroll the document immediately, rather than waiting to see if the event handler calls `event.preventDefault()`.
116116

117117
In the very rare cases that you need to prevent these event defaults, you should use [`on`](https://svelte-5-preview.vercel.app/docs/imports#svelte-events) instead (for example inside an action).
118118

apps/svelte.dev/content/docs/svelte/02-template-syntax/08-bindings.md

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
---
1+
---
22
title: Bindings
33
---
44

@@ -42,25 +42,34 @@ Numeric input values are coerced; even though `input.value` is a string as far a
4242
<input type="range" bind:value={num} />
4343
```
4444

45-
On `<input>` elements with `type="file"`, you can use `bind:files` to get the [`FileList` of selected files](https://developer.mozilla.org/en-US/docs/Web/API/FileList). It is readonly.
45+
On `<input>` elements with `type="file"`, you can use `bind:files` to get the [`FileList` of selected files](https://developer.mozilla.org/en-US/docs/Web/API/FileList). When you want to update the files programmatically, you always need to use a `FileList` object. Currently `FileList` objects cannot be constructed directly, so you need to create a new [`DataTransfer`](https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer) object and get `files` from there.
4646

4747
```svelte
48+
<script>
49+
let files = $state();
50+
51+
function clear() {
52+
files = new DataTransfer().files; // null or undefined does not work
53+
}
54+
</script>
55+
4856
<label for="avatar">Upload a picture:</label>
4957
<input accept="image/png, image/jpeg" bind:files id="avatar" name="avatar" type="file" />
58+
<button onclick={clear}>clear</button>
5059
```
5160

52-
If you're using `bind:` directives together with `on:` directives, the order that they're defined in affects the value of the bound variable when the event handler is called.
61+
`FileList` objects also cannot be modified, so if you want to e.g. delete a single file from the list, you need to create a new `DataTransfer` object and add the files you want to keep.
62+
63+
> `DataTransfer` may not be available in server-side JS runtimes. Leaving the state that is bound to `files` uninitialized prevents potential errors if components are server-side rendered.
64+
65+
If you're using `bind:` directives together with `on` event attributes, the binding will always fire before the event attribute.
5366

5467
```svelte
5568
<script>
5669
let value = 'Hello World';
5770
</script>
5871
59-
<input
60-
on:input={() => console.log('Old value:', value)}
61-
bind:value
62-
on:input={() => console.log('New value:', value)}
63-
/>
72+
<input oninput={() => console.log('New value:', value)} bind:value />
6473
```
6574

6675
Here we were binding to the value of a text input, which uses the `input` event. Bindings on other elements may use different events such as `change`.

apps/svelte.dev/content/docs/svelte/02-template-syntax/09-special-elements.md

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ It cannot appear at the top level of your markup; it must be inside an if or eac
1414

1515
```svelte
1616
<script>
17-
/** @type {number} */
18-
export let count;
17+
let { count } = $props();
1918
</script>
2019
2120
{#if count > 0}
@@ -56,12 +55,12 @@ If `this` is the name of a [void element](https://developer.mozilla.org/en-US/do
5655

5756
```svelte
5857
<script>
59-
let tag = 'div';
58+
let { handler } = $props();
6059
61-
export let handler;
60+
let tag = $state('div');
6261
</script>
6362
64-
<svelte:element this={tag} on:click={handler}>Foo</svelte:element>
63+
<svelte:element this={tag} onclick={handler}>Foo</svelte:element>
6564
```
6665

6766
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:
@@ -73,7 +72,7 @@ Svelte tries its best to infer the correct namespace from the element's surround
7372
## `<svelte:window>`
7473

7574
```svelte
76-
<svelte:window on:event={handler} />
75+
<svelte:window onevent={handler} />
7776
```
7877

7978
```svelte
@@ -86,13 +85,12 @@ Unlike `<svelte:self>`, this element may only appear at the top level of your co
8685

8786
```svelte
8887
<script>
89-
/** @param {KeyboardEvent} event */
9088
function handleKeydown(event) {
9189
alert(`pressed the ${event.key} key`);
9290
}
9391
</script>
9492
95-
<svelte:window on:keydown={handleKeydown} />
93+
<svelte:window onkeydown={handleKeydown} />
9694
```
9795

9896
You can also bind to the following properties:
@@ -117,7 +115,7 @@ All except `scrollX` and `scrollY` are readonly.
117115
## `<svelte:document>`
118116

119117
```svelte
120-
<svelte:document on:event={handler} />
118+
<svelte:document onevent={handler} />
121119
```
122120

123121
```svelte
@@ -129,7 +127,7 @@ Similarly to `<svelte:window>`, this element allows you to add listeners to even
129127
As with `<svelte:window>`, this element may only appear the top level of your component and must never be inside a block or element.
130128

131129
```svelte
132-
<svelte:document on:visibilitychange={handleVisibilityChange} use:someAction />
130+
<svelte:document onvisibilitychange={handleVisibilityChange} use:someAction />
133131
```
134132

135133
You can also bind to the following properties:
@@ -144,15 +142,15 @@ All are readonly.
144142
## `<svelte:body>`
145143

146144
```svelte
147-
<svelte:body on:event={handler} />
145+
<svelte:body onevent={handler} />
148146
```
149147

150148
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](/docs/element-directives#use-action) on the `<body>` element.
151149

152150
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.
153151

154152
```svelte
155-
<svelte:body on:mouseenter={handleMouseenter} on:mouseleave={handleMouseleave} use:someAction />
153+
<svelte:body onmouseenter={handleMouseenter} onmouseleave={handleMouseleave} use:someAction />
156154
```
157155

158156
## `<svelte:head>`

apps/svelte.dev/content/docs/svelte/05-misc/03-typescript.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ Components can declare a generic relationship between their properties. One exam
118118
select: Item;
119119
}
120120
121-
let { items, select } = $props();
121+
let { items, select }: Props = $props();
122122
</script>
123123
124124
{#each items as item}
@@ -210,8 +210,8 @@ declare namespace svelteHTML {
210210
}
211211
// enhance attributes
212212
interface HTMLAttributes<T> {
213-
// If you want to use on:beforeinstallprompt
214-
'on:beforeinstallprompt'?: (event: any) => any;
213+
// If you want to use the beforeinstallprompt event
214+
'onbeforeinstallprompt'?: (event: any) => any;
215215
// If you want to use myCustomAttribute={..} (note: all lowercase)
216216
mycustomattribute?: any; // You can replace any with something more specific if you like
217217
}

apps/svelte.dev/content/docs/svelte/98-reference/21-svelte-events.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,52 @@ import { on } from 'svelte/events';
1111

1212
## on
1313

14+
Attaches an event handler to the window and returns a function that removes the handler. Using this
15+
rather than `addEventListener` will preserve the correct order relative to handlers added declaratively
16+
(with attributes like `onclick`), which use event delegation for performance reasons
17+
18+
<div class="ts-block">
19+
20+
```ts
21+
// @noErrors
22+
function on<Type extends keyof WindowEventMap>(
23+
window: Window,
24+
type: Type,
25+
handler: (
26+
this: Window,
27+
event: WindowEventMap[Type]
28+
) => any,
29+
options?: AddEventListenerOptions | undefined
30+
): () => void;
31+
```
32+
33+
</div>
34+
35+
## on
36+
37+
Attaches an event handler to the document and returns a function that removes the handler. Using this
38+
rather than `addEventListener` will preserve the correct order relative to handlers added declaratively
39+
(with attributes like `onclick`), which use event delegation for performance reasons
40+
41+
<div class="ts-block">
42+
43+
```ts
44+
// @noErrors
45+
function on<Type extends keyof DocumentEventMap>(
46+
document: Document,
47+
type: Type,
48+
handler: (
49+
this: Document,
50+
event: DocumentEventMap[Type]
51+
) => any,
52+
options?: AddEventListenerOptions | undefined
53+
): () => void;
54+
```
55+
56+
</div>
57+
58+
## on
59+
1460
Attaches an event handler to an element and returns a function that removes the handler. Using this
1561
rather than `addEventListener` will preserve the correct order relative to handlers added declaratively
1662
(with attributes like `onclick`), which use event delegation for performance reasons
@@ -43,6 +89,32 @@ rather than `addEventListener` will preserve the correct order relative to handl
4389

4490
<div class="ts-block">
4591

92+
```ts
93+
// @noErrors
94+
function on<
95+
Element extends MediaQueryList,
96+
Type extends keyof MediaQueryListEventMap
97+
>(
98+
element: Element,
99+
type: Type,
100+
handler: (
101+
this: Element,
102+
event: MediaQueryListEventMap[Type]
103+
) => any,
104+
options?: AddEventListenerOptions | undefined
105+
): () => void;
106+
```
107+
108+
</div>
109+
110+
## on
111+
112+
Attaches an event handler to an element and returns a function that removes the handler. Using this
113+
rather than `addEventListener` will preserve the correct order relative to handlers added declaratively
114+
(with attributes like `onclick`), which use event delegation for performance reasons
115+
116+
<div class="ts-block">
117+
46118
```ts
47119
// @noErrors
48120
function on(

0 commit comments

Comments
 (0)