1.3.5 • Published 6 months ago

react-muntaha-uploader v1.3.5

Weekly downloads
-
License
ISC
Repository
github
Last release
6 months ago

useMuntahaDrop Hook

Overview

useMuntahaDrop is a custom React hook for handling file uploads via drag-and-drop or input selection. It provides a flexible API to manage file uploads efficiently.


AcceptFileTypes

Defines the supported file types for uploads.

Supported Categories

  • Images: image/png, image/jpeg, image/svg+xml, etc.
  • Videos: video/mp4, video/webm, etc.
  • Audio: audio/mp3, audio/wav, etc.
  • Documents: application/pdf, text/plain, etc.
  • Archives & Compressed Files: application/zip, application/x-rar-compressed, etc.
  • Fonts: font/ttf, font/woff2, etc.
  • Programming & Code Files: application/javascript, application/x-python-code, etc.

FilePayload

Represents the structure of an uploaded file.

PropertyTypeDescription
bufferstring \| ArrayBuffer \| nullFile data buffer.
altstring \| nullAlternative text for images (optional).
srcstring \| nullSource URL or base64 data.

FileUploadState

Manages file upload state and actions.

PropertyTypeDescription
onChange(event: React.ChangeEvent<HTMLInputElement>) => voidHandles file input change event.
inputRefReact.RefObject<HTMLInputElement \| null>Ref for the file input element.
dropRefReact.RefObject<HTMLDivElement \| null>Ref for the drop area element.
onClick() => voidTriggers the file input click event.
onDrop(event: React.DragEvent<HTMLDivElement>) => voidHandles file drop event.
onRemove(index?: number) => voidRemoves a file from the list.
errorstring \| nullError message if validation fails.
payloadT extends true ? FilePayload[] : FilePayloadUploaded file(s) data.
draggingbooleanIndicates if a file is being dragged over the drop area.
progressnumber \| nullUpload progress percentage (if applicable).

useMuntahaDrop(options: UseMuntahaDropOptions)

A custom React hook for handling file uploads.

Parameters

PropertyTypeDefaultDescription
acceptsAcceptFileTypes[][]Allowed MIME types.
minSizenumberundefinedMinimum file size in bytes.
maxSizenumber10MBMaximum file size in bytes.
maxFilesnumberundefinedMaximum number of files.
multiplebooleanfalseAllows multiple file selection.
disabledbooleanfalseDisables file input.

Usage Example

Single File Upload Example:

import React from 'react'
import { useMuntahaDrop } from 'react-muntaha-uploader'

const SingleFileUpload = () => {
  const {
    onChange,
    inputRef,
    dropRef,
    onClick,
    onDrop,
    onRemove,
    error,
    payload,
    dragging,
  } = useMuntahaDrop({
    accepts: ['image/png', 'image/jpeg'],
    maxSize: 5 * 1024 * 1024, // 5MB
    multiple: false,
  })

  return (
    <div>
      <div
        ref={dropRef}
        onDrop={onDrop}
        onClick={onClick}
        style={{
          border: '2px dashed gray',
          padding: '20px',
          textAlign: 'center',
          cursor: 'pointer',
          backgroundColor: dragging ? '#f0f0f0' : 'white',
        }}
      >
        Drag & Drop an image or click to upload
      </div>

      <input
        ref={inputRef}
        type="file"
        onChange={onChange}
        style={{ display: 'none' }}
      />

      {error && <p style={{ color: 'red' }}>{error}</p>}

      {payload?.src && (
        <div>
          <img src={payload.src} alt={payload.alt || 'Preview'} width={100} />
          <button onClick={onRemove}>Remove</button>
        </div>
      )}
    </div>
  )
}

export default SingleFileUpload

Multiple File Upload Example:

import React from 'react'
import { useMuntahaDrop } from 'react-muntaha-uploader'

const MultiFileUpload = () => {
  const {
    onChange,
    inputRef,
    dropRef,
    onClick,
    onDrop,
    onRemove,
    error,
    payload,
    dragging,
  } = useMuntahaDrop({
    accepts: ['image/png', 'image/jpeg', 'application/pdf'],
    maxSize: 5 * 1024 * 1024, // 5MB
    maxFiles: 5,
    multiple: true,
  })

  return (
    <div>
      <div
        ref={dropRef}
        onDrop={onDrop}
        onClick={onClick}
        style={{
          border: '2px dashed gray',
          padding: '20px',
          textAlign: 'center',
          cursor: 'pointer',
          backgroundColor: dragging ? '#f0f0f0' : 'white',
        }}
      >
        Drag & Drop files here or click to upload
      </div>

      <input
        ref={inputRef}
        type="file"
        multiple
        onChange={onChange}
        style={{ display: 'none' }}
      />

      {error && <p style={{ color: 'red' }}>{error}</p>}

      {Array.isArray(payload) && payload.length > 0 && (
        <ul>
          {payload.map((file, index) => (
            <li key={index}>
              {file.src && (
                <img src={file.src} alt={file.alt || 'Preview'} width={50} />
              )}
              <button onClick={() => onRemove(index)}>Remove</button>
            </li>
          ))}
        </ul>
      )}
    </div>
  )
}

export default MultiFileUpload
1.3.5

6 months ago

1.3.4

6 months ago

1.3.3

6 months ago

1.3.2

6 months ago

1.3.1

6 months ago

1.2.9

6 months ago

1.2.8

6 months ago

1.2.7

6 months ago

1.2.6

6 months ago

1.2.5

6 months ago

1.2.4

8 months ago

1.2.3

8 months ago

1.2.2

8 months ago

1.2.1

8 months ago

1.1.9

8 months ago

1.1.8

8 months ago

1.1.7

8 months ago

1.1.6

8 months ago

1.1.5

8 months ago

1.1.4

10 months ago

1.1.3

10 months ago

1.1.2

10 months ago

1.1.1

10 months ago

1.1.0

10 months ago

1.0.9

10 months ago

1.0.8

10 months ago

1.0.7

10 months ago

1.0.6

10 months ago

1.0.5

10 months ago

1.0.4

10 months ago

1.0.3

10 months ago

1.0.2

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago