1.0.0 • Published 1 year ago

solid-dzone v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

solidjs card

pnpm NPM package bundle size package version npm downloads

Simple, high-quality drag'n'drop primitive for Solid JS

Quick start

Install it:

npm i solid-dzone
# or
yarn add solid-dzone
# or
pnpm add solid-dzone

Usage

import createDropzone from "solid-dzone"

const MyDropZone = () => {
  const { 
    getRootProps,
    getInputProps,
    setRefs,
  } = createDropzone<HTMLDivElement>() // You MUST specify the type of the root element for proper typing

  let inputRef!: HTMLInputElement
  let rootRef!: HTMLDivElement

  setTimeout(() => {
    // Refs must be set inside a timeout to ensure the elements have been mounted
    setRefs(rootRef, inputRef)
  })

  return (
    <div {...getRootProps()} ref={rootRef}>
      <input {...getInputProps()} ref={inputRef}/>
    </div>
  )
}

Dropzone Props Getters

The dropzone property getters are just two functions that return objects with properties you need to create the drag'n'drop zone. The root properties can be applied to whatever ellement you want, whereas the input properties must be applied to an <input>.

Note that whatever other props you want to add to the elements, you should always pass them through the functions, rather than applying them directly to avoid conflicts and unexpected behaviours.

<div
  {...getRootProps({
    role: "button",
    onDrop: e => console.log("FILE WAS DROPPED")
  })}
>

createDropzone options

NameTypeNotes
disabledbooleanDisable the dropzone
maxSizenumberMaximum file size in bytes
minSizenumberMinimum file size in bytes
multiplebooleanAllow multiple files
maxFilesnumberMaximum number of files
acceptstring[]Array of allowed file types, see link for possible values
noClickbooleanDisable click to open file dialog
noKeyboardbooleanDisable keyboard to open file dialog
noDragbooleanDisable drag'n'drop
validatorfunctionCustom file validation, overrides default

createDropzone return values

NameTypeNotes
isFocusedfunctionBoolean signal that indicates whether the dropzone is focused or not
isFileDialogActivefunctionBoolean signal that indicates whether the file dialog is open or not
isDraggingfunctionBoolean signal that indicates whether the a file is being dragged over the root or not
getRootPropsfunctionFunction that returns an object of props for the root element
getInputPropsfunctionFunction that returns an object of props for the input element
openFileDialogfunctionFunction to programmatically open the file dialog
setRefsfunctionSet the root and input refs. Must be called inside a setTimeout to ensure elements are mounted
filesfunctionSignal that returns an array of files
errorsfunctionObject containing errors. Each key is a file name and the value is an object containing the error message and the file
removeFilefunctionFunction to remove a specific file by name
removeErrorfunctionFunction to remove a specific error by file name
clearFilesfunctionFunction to clear all files

File type definition

NameType
sourcestring
namestring
sizenumber
fileFile

Contributors

contributors

Licence

MIT

1.0.0

1 year ago