1.1.0 • Published 3 years ago

vr-file-upload-preview v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

vr-file-upload-preview

This is a custom element (web component - can be used every where regardless the framework).

vr-file-upload aims to address the issue of showing a preview of a user's uploaded image.

Demo

Installation

There are few options on how to include/import vr-file-upload-preview into your project:

Install from NPM/YARN

We can install component from NPM/YARN

# npm
npm i vr-file-upload-preview

# yarn
yarn add vr-file-upload-preview
// import component
import 'vr-file-upload-preview';
// import styles
import 'vr-file-upload-preview/vr-file-upload-preview.min.css';

Use from CDN

If you don't want to include vr-file-upload-preview files in your project, you may use it from CDN. The following files are available:

<link rel="stylesheet" href="https://unpkg.com/vr-file-upload-preview/vr-file-upload-preview.min.css" />
<link rel="stylesheet" href="https://unpkg.com/vr-file-upload-preview/vr-file-upload-preview.css" />

<script src="https://unpkg.com/vr-file-upload-preview/vr-file-upload-preview.min.js"></script>

If you use ES modules in browser, there is a CDN version for that too:

<script type="module">
    import 'https://unpkg.com/vr-file-upload-preview/vr-file-upload-preview.umd.js'
</script>

Full Example

<!-- *.html -->
<vr-file-upload-preview
    class="custom-wrapper"
    accept=".png,.jpg,.jpeg,.gif"
    max-files="3"
    max-size="55000"
    multiple
>
    <button class="custom-button">
        Upload
    </button>
</vr-file-upload-preview>
// *.js
import 'vr-file-upload-preview';
import 'vr-file-upload-preview/vr-file-upload-preview.min.css';

const uploader = document.querySelector('vr-file-upload-preview');

uploader.addEventListener('change', (e) => {
    console.log(e.detail.files);
    // console.log(uploader.files);
});

uploader.addEventListener('error', (e) => {
    const {file, type} = e.detail;

    console.log({file, type});
});

API Reference

AttributesDescription
acceptAcceptable file types can be specified with the accept attribute, which takes a comma-separated list of allowed file extensions or MIME types. accept="image/png, image/jpeg" accept=".png, .jpg, .jpeg, .pdf"
multipleThe multiple attribute allows the user to specify multiple files at the same time in the file upload field. multiple multiple="true"
max-filesMaximum number of files. max-files="3"
max-sizeMaximum file size, the value is specified in bytes. max-size="5000"
previewAllows you to specify your own container for displaying file previews through a selector or a group of selectors. preview=".custom-preview" preview="#custom-preview" preview=".custom-preview.custom-wrapper"
dropzoneThe dropzone attribute allows the user to drag files to the drop zone. dropzone dropzone="true"
MethodsDescription
uploadUploading files. parameters: value value: Blob \| Array<Blob>
removeRemoving a file by index. parameters: index value: number
clearCleans up all files.
EventsDescription
changeTriggered each time file/files are selected. Delivers the array of files. value: event.detail {files: Array<File>}
errorTriggered each time an error occurs while selecting a file / files. Delivers file and error type. value: event.detail {type: "FORMAT" \| "MAX_FILES" \| "MAX_SIZE", file: File}

Testing

Use npm run test to run the tests.

Development

Clone the repo, then use the following to work on the project locally:

# Install dependencies
npm install

# Run demo project
npm run start

# Build library
npm run build:library