@itrocks/file-drop
v0.1.0
Published
Drag-and-drop enhancement for native file inputs
Downloads
39
Maintainers
Readme
file-drop
Drag-and-drop enhancement for native file inputs.
Installation
npm install @itrocks/file-dropUsage
Basic usage
import { fileDrop } from '@itrocks/file-drop/file-drop.js'
const input = document.querySelector('input[type=file]')
fileDrop(input)HTML example with default styling
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="/@itrocks/file-drop/file-drop.css">
</head>
<body>
<input type="file" id="invoice">
<script type="module">
import { fileDrop } from '/@itrocks/file-drop/file-drop.js'
fileDrop(document.getElementById('invoice'))
</script>
</body>
</html>Multiple inputs
document.querySelectorAll('input[type=file]').forEach(fileDrop)Dynamic DOM support (optional)
If file inputs are added dynamically, use the optional integration with @itrocks/build.
import '@itrocks/file-drop/file-drop.css'
import '@itrocks/file-drop/build.js'This automatically applies drag-and-drop behaviour to:
- dynamically inserted inputs
- inputs loaded via fetch / XHR
- inputs created via JavaScript
Custom build integration
If you already use your own @itrocks/build setup, you can integrate manually.
Example applying only to inputs having a specific class:
import { build } from '@itrocks/build'
import { fileDrop } from '@itrocks/file-drop/file-drop.js'
build('input[type=file].drop-zone', element => fileDrop(element))Example applying inside a specific container:
build('#invoices input[type=file]', element => fileDrop(element))Behaviour
- Enables drag-and-drop on native file inputs
- Works with single and multiple file inputs
- Adds
drag-overclass during drag interaction - Triggers native
changeevent after drop - Does not replace native file selection behaviour
Styling
Default styling is provided in:
file-drop.cssYou may:
- override the
.drag-overclass - provide your own styles
- omit the default stylesheet
Browser support
Requires modern browsers supporting:
- DataTransfer API
- File API
- ES modules
No legacy browser support is provided.
