File size: 1,285 Bytes
b2ecf7d
9d298eb
 
b2ecf7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<script lang="ts">
	import IconSpin from "../../../Icons/IconSpin.svelte";
	import IconFile from "../../../Icons/IconFile.svelte";

	export let accept: string | undefined;
	export let classNames = "";
	export let isLoading = false;
	export let isDisabled = false;
	export let label = "Browse for file";
	export let onSelectFile: (file: File | Blob) => void;

	let fileInput: HTMLInputElement;
	let isDragging = false;

	function onChange() {
		const file = fileInput.files?.[0];
		if (file) {
			onSelectFile(file);
		}
	}
</script>

{#if !isDisabled}
	<div
		class={classNames}
		on:dragenter={() => {
			isDragging = true;
		}}
		on:dragover|preventDefault
		on:dragleave={() => {
			isDragging = false;
		}}
		on:drop|preventDefault={(e) => {
			isDragging = false;
			fileInput.files = e.dataTransfer?.files ?? null;
			onChange();
		}}
	>
		<label class="btn-widget {isDragging ? 'ring' : ''} {isLoading ? 'text-gray-600' : ''}">
			{#if isLoading}
				<IconSpin classNames="-ml-1 mr-1.5 text-gray-600 animate-spin" />
			{:else}
				<IconFile classNames="-ml-1 mr-1.5" />
			{/if}
			<input
				{accept}
				bind:this={fileInput}
				on:change={onChange}
				disabled={isLoading || isDisabled}
				style="display: none;"
				type="file"
			/>
			{label}
		</label>
	</div>
{/if}