File size: 1,706 Bytes
b2ecf7d
9d298eb
 
76d4920
 
b2ecf7d
 
 
 
 
 
 
 
 
 
76d4920
b2ecf7d
 
76d4920
 
 
 
 
b2ecf7d
 
 
 
 
 
 
 
76d4920
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<script lang="ts">
	import IconSpin from "../../../Icons/IconSpin.svelte";
	import IconFile from "../../../Icons/IconFile.svelte";
	import { isLoggedIn } from "../../stores.js";
	import LogInPopover from "../../../LogInPopover/LogInPopover.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;
	let popOverOpen = false;

	function onChange() {
		if (!$isLoggedIn) {
			popOverOpen = true;
			return;
		}

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

{#if !isDisabled}
	<LogInPopover bind:open={popOverOpen}>
		<button
			class={classNames}
			on:click={(e) => {
				if (!$isLoggedIn) {
					popOverOpen = true;
					e.preventDefault();
					return;
				}
			}}
			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>
		</button>
	</LogInPopover>
{/if}