-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSingleFileInput.svelte
More file actions
32 lines (26 loc) · 873 Bytes
/
Copy pathSingleFileInput.svelte
File metadata and controls
32 lines (26 loc) · 873 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<script lang="ts">
import { useId } from "bits-ui";
import { type RestProps } from "$lib/types";
import { type Snippet } from "svelte";
import { watch } from "runed";
type Props = {
children?: Snippet<[{ file?: File }]>;
file?: File;
} & RestProps;
let { children, file = $bindable<File | undefined>(undefined), ...restProps }: Props = $props();
let files = $state<FileList | undefined>();
watch(
() => files,
(newFiles) => {
if (newFiles && newFiles.length > 0) {
file = newFiles[0];
}
},
);
const labelId = useId();
const inputId = useId();
</script>
<label id={labelId} for={inputId} {...restProps}>
{@render children?.({ file })}
<input id={inputId} aria-labelledby={labelId} type="file" bind:files class="sr-only" />
</label>