|
| 1 | +<script> |
| 2 | +import { isAboveMobile } from '../../mdc' |
| 3 | +import { createEventDispatcher } from 'svelte' |
| 4 | +
|
| 5 | +export let raised = false |
| 6 | +export let outlined = false |
| 7 | +export let uploading = false |
| 8 | +export let mimeType = 'application/pdf,image/*' |
| 9 | +
|
| 10 | +let fileInput = {} |
| 11 | +
|
| 12 | +let highlighted = false |
| 13 | +
|
| 14 | +const dispatch = createEventDispatcher() |
| 15 | +
|
| 16 | +function highlight() { |
| 17 | + highlighted = true |
| 18 | +} |
| 19 | +
|
| 20 | +function unhighlight() { |
| 21 | + highlighted = false |
| 22 | +} |
| 23 | +
|
| 24 | +function handleDrop(e) { |
| 25 | + unhighlight() |
| 26 | +
|
| 27 | + let dt = e.dataTransfer |
| 28 | + let files = dt.files |
| 29 | +
|
| 30 | + handleFiles(files) |
| 31 | +} |
| 32 | +
|
| 33 | +function handleFiles(files) { |
| 34 | + if (!uploading) { |
| 35 | + uploading = true |
| 36 | + files = [...files] //turns files into an array so forEach works |
| 37 | + files.forEach(uploadFile) |
| 38 | + } |
| 39 | +} |
| 40 | +
|
| 41 | +function uploadFile(file) { |
| 42 | + const formData = new FormData() |
| 43 | +
|
| 44 | + formData.append('file', file) |
| 45 | +
|
| 46 | + dispatch('upload', formData) |
| 47 | +} |
| 48 | +</script> |
| 49 | + |
| 50 | +<style> |
| 51 | +form > * { |
| 52 | + margin-top: 0; |
| 53 | +} |
| 54 | +#drop-area { |
| 55 | + border: 2px dashed #ccc; |
| 56 | +} |
| 57 | +#drop-area.highlighted { |
| 58 | + border-color: var(--mdc-theme-primary); |
| 59 | +} |
| 60 | +
|
| 61 | +#fileElem { |
| 62 | + display: none; |
| 63 | +} |
| 64 | +.disabled { |
| 65 | + cursor: progress; |
| 66 | +} |
| 67 | +.icon { |
| 68 | + color: hsla(213, 6%, 55%, 1); |
| 69 | +} |
| 70 | +</style> |
| 71 | + |
| 72 | +<div |
| 73 | + id="drop-area" |
| 74 | + class="br-8px {$$props.class}" |
| 75 | + class:highlighted |
| 76 | + on:dragenter|preventDefault|stopPropagation={highlight} |
| 77 | + on:dragleave|preventDefault|stopPropagation={unhighlight} |
| 78 | + on:dragover|preventDefault|stopPropagation={highlight} |
| 79 | + on:drop|preventDefault|stopPropagation={handleDrop} |
| 80 | +> |
| 81 | + <form class="flex justify-between align-items-center my-1 px-1" class:column={!isAboveMobile()}> |
| 82 | + {#if !uploading} |
| 83 | + <input |
| 84 | + bind:this={fileInput} |
| 85 | + type="file" |
| 86 | + id="fileElem" |
| 87 | + multiple |
| 88 | + accept={mimeType} |
| 89 | + disabled={uploading} |
| 90 | + on:change={() => handleFiles(fileInput.files)} |
| 91 | + /> |
| 92 | + {/if} |
| 93 | + <label |
| 94 | + class="mdc-button m-8px" |
| 95 | + for="fileElem" |
| 96 | + class:custom-text-button={raised} |
| 97 | + class:mdc-button--outlined={outlined} |
| 98 | + class:disabled={uploading} |
| 99 | + class:mdc-button--raised={raised}>Choose files</label |
| 100 | + > |
| 101 | + <div class="m-8px">or drop files here</div> |
| 102 | + <i class="material-icons icon m-8px" id="upload-icon">cloud_upload</i> |
| 103 | + </form> |
| 104 | +</div> |
0 commit comments