-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileBrowser.svelte
96 lines (86 loc) · 1.8 KB
/
FileBrowser.svelte
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<style>
label,
label > span {
width: 100%;
display: flex;
flex-direction: column;
justify-content: flex-start;
align-items: stretch;
cursor: pointer;
}
label > span {
flex-direction: row;
flex-grow: 1;
}
input {
display: none;
}
.boxed {
flex-grow: 1;
border: 1px solid var(--fg-color);
box-shadow: 3px 3px 0 0 var(--fg-color);
padding: 0.2em 0.5em;
/* Space for the shadow so it doesn't get cut off */
margin-bottom: 3px;
}
.filename {
margin-left: -4px;
overflow: hidden;
text-overflow: ellipsis;
flex-shrink: 1;
white-space: pre;
user-select: none;
-webkit-user-select: none;
}
.filename:hover {
outline: 1px solid var(--fg-color);
}
.filename:active {
box-shadow: 2px 2px 0 0 var(--fg-color);
}
</style>
<script>
import Button from "./Button.svelte";
let { files = $bindable(), children, ...restProps } = $props();
let dragging = $state(false);
let input = $state();
</script>
<label
ondrop={(e) => {
e.preventDefault();
e.stopPropagation();
dragging = false;
if (e.dataTransfer.files) {
files = e.dataTransfer.files;
}
}}
ondragover={(e) => {
// Prevent default here is necessary to allow drop
e.preventDefault();
dragging = true;
}}
ondragend={(e) => {
dragging = false;
}}
ondragleave={(e) => {
dragging = false;
}}
>
{@render children?.()}
{#if dragging}
<div class="boxed">Drop file</div>
{:else}
<span>
<Button
style="padding: 0.2em 0.5em;"
onclick={() => {
input?.click();
}}
>
Browse...
</Button>
<span class="boxed filename">{files?.[0]?.name}</span>
</span>
{/if}
<input type="file" bind:files bind:this={input} {...restProps} />
</label>