dasundev / livewire-dropzone

A Livewire Dropzone component for simple drag-and-drop file uploads.
https://dasun.dev/docs/livewire-dropzone
MIT License
161 stars 15 forks source link

Refactor Dropzone UI #23

Open dasundev opened 6 months ago

dasundev commented 6 months ago

I was wondering if someone could improve the dropzone UI.

Fsmash commented 5 months ago

I can look into this since I want to change the things for myself already. Honestly, I just want to expose all the "dz-" specific styling in a way they are easily editable.

Not sure if that is an improvement but it would allow people to more easily "improve" the UI however they want. Improve being somewhat subjective.

dasundev commented 5 months ago

Let's stick with the dz- prefix for all Tailwind classes to ensure smooth integration and prevent any potential CSS conflicts.

I'm seeking an improvement focused on the UI/UX aspect. Perhaps you could propose a different dropzone interface for a better user experience.

mbs047 commented 3 months ago

made it using some custom css class it could help

<style>
    .dz-container {
        display: flex;
        flex-wrap: wrap;
        flex-direction: column;
    }

    .dz-border {
        border-width: 1px;
        border-color: #d9dde7;
        border-style: dashed;
        border-radius: 12px;
        text-align: center;
        padding-top: 10px;
        margin-bottom: 10px;
    }

    .dz-text-primary {
        color: #1b84ff;
        font-weight: 550;
    }

    .dz-text-bold {
        font-weight: 550;
    }

    .dz-file-rules {
        display: inline-flex;
    }

    .dz-me-10 {
        margin-left: 10px;
    }
</style>

<div
    x-cloak
    x-data="dropzone({
        _this: @this,
        uuid: @js($uuid),
        multiple: @js($multiple),
    })"
    @dragenter.prevent.document="onDragenter($event)"
    @dragleave.prevent="onDragleave($event)"
    @dragover.prevent="onDragover($event)"
    @drop.prevent="onDrop"
    class="dz-w-full dz-antialiased"
>
    <div class="dz-container">
        @if(! is_null($error))
            <div>
                <div>
                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" class="">
                        <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.28 7.22a.75.75 0 00-1.06 1.06L8.94 10l-1.72 1.72a.75.75 0 101.06 1.06L10 11.06l1.72 1.72a.75.75 0 101.06-1.06L11.06 10l1.72-1.72a.75.75 0 00-1.06-1.06L10 8.94 8.28 7.22z" clip-rule="evenodd" />
                    </svg>
                    <h3 class="">{{ $error }}</h3>
                </div>
            </div>
        @endif
        <div>
            <label for="upload" class="">{{ __('Upload files') }} <span class="text-red">*</span></label>
            <div x-show="isLoading" role="status">
                <svg aria-hidden="true" class="" viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg">
                    <path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" fill="currentColor"/>
                    <path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" fill="currentFill"/>
                </svg>
                <span class="">Loading...</span>
            </div>
        </div>
        <div @click="$refs.input.click()" class="dz-border">
            <div>
                <div x-show="!isDragging">
                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" width="45px">
                        <path d="M9.25 13.25a.75.75 0 001.5 0V4.636l2.955 3.129a.75.75 0 001.09-1.03l-4.25-4.5a.75.75 0 00-1.09 0l-4.25 4.5a.75.75 0 101.09 1.03L9.25 4.636v8.614z" />
                        <path d="M3.5 12.75a.75.75 0 00-1.5 0v2.5A2.75 2.75 0 004.75 18h10.5A2.75 2.75 0 0018 15.25v-2.5a.75.75 0 00-1.5 0v2.5c0 .69-.56 1.25-1.25 1.25H4.75c-.69 0-1.25-.56-1.25-1.25v-2.5z" />
                    </svg>
                    <p>Drop here or <span class="dz-text-primary">Browse files</span></p>
                </div>
                <div x-show="isDragging">
                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor">
                        <path d="M10 2a.75.75 0 01.75.75v5.59l1.95-2.1a.75.75 0 111.1 1.02l-3.25 3.5a.75.75 0 01-1.1 0L6.2 7.26a.75.75 0 111.1-1.02l1.95 2.1V2.75A.75.75 0 0110 2z" />
                        <path d="M5.273 4.5a1.25 1.25 0 00-1.205.918l-1.523 5.52c-.006.02-.01.041-.015.062H6a1 1 0 01.894.553l.448.894a1 1 0 00.894.553h3.438a1 1 0 00.86-.49l.606-1.02A1 1 0 0114 11h3.47a1.318 1.318 0 00-.015-.062l-1.523-5.52a1.25 1.25 0 00-1.205-.918h-.977a.75.75 0 010-1.5h.977a2.75 2.75 0 012.651 2.019l1.523 5.52c.066.239.099.485.099.732V15a2 2 0 01-2 2H3a2 2 0 01-2-2v-3.73c0-.246.033-.492.099-.73l1.523-5.521A2.75 2.75 0 015.273 3h.977a.75.75 0 010 1.5h-.977z" />
                    </svg>
                    <p class="">Drop here to upload</p>
                </div>
            </div>
            <input
                x-ref="input"
                wire:model="upload"
                type="file"
                class="d-none"
                x-on:livewire-upload-start="isLoading = true"
                x-on:livewire-upload-finish="isLoading = false"
                x-on:livewire-upload-error="console.log('livewire-dropzone upload error', error)"
                @if(! is_null($this->accept)) accept="{{ $this->accept }}" @endif
                @if($multiple === true) multiple @endif
            >
        </div>

        <div class="dz-file-rules">
            @php
                $hasMaxFileSize = ! is_null($this->maxFileSize);
                $hasMimes = ! empty($this->mimes);
            @endphp

            @if($hasMaxFileSize)
                <p class="dz-me-10 dz-text-bold">{{ __('Up to :size', ['size' => \Illuminate\Support\Number::fileSize($this->maxFileSize * 1024)]) }}</p>
            @endif

            @if($hasMaxFileSize && $hasMimes)
                <span class="dz-me-10">·</span>
            @endif

            @if($hasMimes)
                <p>{{ Str::upper($this->mimes) }}</p>
            @endif
        </div>

            <!-- Dropzone Images -->
        @if(count($files) > 0)
            <div class="">
                @foreach($files as $file)
                    <!-- Dropzone Image -->
                    <div>
                        <div class="">
                            @if($this->isImageMime($file['extension']))
                                <div class="">
                                    <img src="{{ $file['temporaryUrl'] }}" class="" alt="{{ $file['name'] }}">
                                </div>
                            @else
                                <div class="">
                                    <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1" stroke="currentColor">
                                        <path stroke-linecap="round" stroke-linejoin="round" d="M19.5 14.25v-2.625a3.375 3.375 0 0 0-3.375-3.375h-1.5A1.125 1.125 0 0 1 13.5 7.125v-1.5a3.375 3.375 0 0 0-3.375-3.375H8.25m2.25 0H5.625c-.621 0-1.125.504-1.125 1.125v17.25c0 .621.504 1.125 1.125 1.125h12.75c.621 0 1.125-.504 1.125-1.125V11.25a9 9 0 0 0-9-9Z" />
                                    </svg>
                                </div>
                            @endif
                            <div>
                                <div class="">{{ $file['name'] }}</div>
                                <div class="">{{ \Illuminate\Support\Number::fileSize($file['size']) }}</div>
                            </div>
                        </div>
                        <div>
                            <button type="button" @click="removeUpload('{{ $file['tmpFilename'] }}')">
                                <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="k">
                                    <path fill-rule="evenodd" d="M5.47 5.47a.75.75 0 011.06 0L12 10.94l5.47-5.47a.75.75 0 111.06 1.06L13.06 12l5.47 5.47a.75.75 0 11-1.06 1.06L12 13.06l-5.47 5.47a.75.75 0 01-1.06-1.06L10.94 12 5.47 6.53a.75.75 0 010-1.06z" clip-rule="evenodd" />
                                </svg>
                            </button>
                        </div>
                    </div>
                @endforeach
            </div>
        @endif
    </div>

    @script
    <script>
        Alpine.data('dropzone', ({ _this, uuid, multiple }) => {
            return ({
                isDragging: false,
                isDropped: false,
                isLoading: false,

                onDrop(e) {
                    this.isDropped = true
                    this.isDragging = false

                    const file = multiple ? e.dataTransfer.files : e.dataTransfer.files[0]

                    const args = ['upload', file, () => {
                        // Upload completed
                        this.isLoading = false
                    }, (error) => {
                        // An error occurred while uploading
                        console.log('livewire-dropzone upload error', error);
                    }, () => {
                        // Uploading is in progress
                        this.isLoading = true
                    }];

                    // Upload file(s)
                    multiple ? _this.uploadMultiple(...args) : _this.upload(...args)
                },
                onDragenter() {
                    this.isDragging = true
                },
                onDragleave() {
                    this.isDragging = false
                },
                onDragover() {
                    this.isDragging = true
                },
                removeUpload(tmpFilename) {
                    // Dispatch an event to remove the temporarily uploaded file
                    _this.dispatch(uuid + ':fileRemoved', { tmpFilename })
                },
            });
        })
    </script>
    @endscript
</div>
dasundev commented 3 months ago

Please do not use custom CSS. Instead, use Tailwind utility classes. You can build the CSS using the resources from this repository: https://github.com/dasundev/livewire-dropzone/tree/main/resources/js/livewire-dropzone-styles