Skip to content

Commit 9204d69

Browse files
authored
docs: fixes relating to bind:files & note on FileList API (#13235)
1 parent e3ad1ac commit 9204d69

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

documentation/docs/02-template-syntax/08-bindings.md

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

@@ -42,14 +42,14 @@ 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). When you want to update the files programmatically, you always need to use a `FileList` object.
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
4848
<script>
4949
let files = $state();
5050
5151
function clear() {
52-
files = new FileList(); // null or undefined doesn't work
52+
files = new DataTransfer().files; // null or undefined does not work
5353
}
5454
</script>
5555
@@ -58,6 +58,10 @@ On `<input>` elements with `type="file"`, you can use `bind:files` to get the [`
5858
<button onclick={clear}>clear</button>
5959
```
6060

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+
6165
If you're using `bind:` directives together with `on` event attributes, the binding will always fire before the event attribute.
6266

6367
```svelte

0 commit comments

Comments
 (0)