MapComplete/src/UI/Base/FileSelector.svelte

51 lines
1.3 KiB
Svelte
Raw Normal View History

<script lang="ts">
2023-09-28 23:50:27 +02:00
import { createEventDispatcher } from "svelte"
import { twMerge } from "tailwind-merge"
2023-09-28 23:50:27 +02:00
export let accept: string
export let multiple: boolean = true
2023-09-28 23:50:27 +02:00
const dispatcher = createEventDispatcher<{ submit: FileList }>()
export let cls: string = ""
let drawAttention = false
let inputElement: HTMLInputElement
let id = Math.random() * 1000000000 + ""
</script>
2023-10-09 01:53:27 +02:00
<form on:change|preventDefault={() => {
2023-09-28 23:50:27 +02:00
drawAttention = false
dispatcher("submit", inputElement.files)
}}
2023-10-09 01:53:27 +02:00
on:dragend={() => {
console.log("Drag end")
2023-09-28 23:50:27 +02:00
drawAttention = false
}}
2023-10-09 01:53:27 +02:00
on:dragenter|preventDefault|stopPropagation={(e) => {
console.log("Dragging enter")
2023-09-28 23:50:27 +02:00
drawAttention = true
e.dataTransfer.drop = "copy"
}}
2023-10-09 01:53:27 +02:00
on:dragstart={() => {
console.log("DragStart")
2023-09-28 23:50:27 +02:00
drawAttention = false
}}
2023-10-09 01:53:27 +02:00
on:drop|preventDefault|stopPropagation={(e) => {
2023-09-28 23:50:27 +02:00
console.log("Got a 'drop'")
drawAttention = false
dispatcher("submit", e.dataTransfer.files)
2023-10-09 01:53:27 +02:00
}}>
<label class={twMerge(cls, drawAttention ? "glowing-shadow" : "")} for={"fileinput" + id}>
<slot />
</label>
<input
{accept}
bind:this={inputElement}
class="hidden"
id={"fileinput" + id}
{multiple}
name="file-input"
2023-09-28 23:50:27 +02:00
type="file"
/>
</form>