npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-file-upload-button

v1.0.6

Published

A customizable React component for file upload with drag-and-drop functionality, progress bar, and file management.

Downloads

881

Readme

React File Upload Button

NPM version NPM downloads GitHub stars

A customizable React component for file uploads with drag-and-drop functionality, progress bar, and file type restrictions. This component allows users to click or drag a file to upload, shows upload progress, supports custom icons, and lets users delete files after a successful upload.

Features

  • Drag-and-Drop or Click-to-Upload functionality
  • File type and size restrictions with custom error messages
  • Progress bar for showing upload progress
  • Custom icons for different file types
  • Callbacks for handling errors and file upload events
  • CSS customization through props

Installation

Install the package via NPM:

npm install react-file-upload-button

Usage

The UploadButton component is designed to be used out-of-the-box with zero configuration, but you can also customize it fully with various props.

Here’s a basic example of how to use the UploadButton component:

For a simple implementation, just add the component, and it will render with default settings. No additional configuration or CSS imports are needed.

import React from 'react'
import UploadButton from 'react-file-upload-button'

const App = () => (
    <div>
        <h1>Upload Your File</h1>
        <UploadButton />
    </div>
)

export default App

By default, the component will:

  • Allow all file types for upload.
  • Set a maximum file size of 10 MB (this can be changed).
  • Show a basic drag-and-drop interface with upload progress.

Advanced Usage with Custom Props

To customize the upload experience, you can pass various props to control file type restrictions, size limits, styles, custom icons, and callbacks for handling events like errors and file uploads.

Here’s an example with all possible props:

import React from 'react'
import UploadButton from 'react-file-upload-button'

const App = () => (
    <UploadButton
        maxSizeMB={5}
        restrictFileSize={true}
        allowedFileTypes={[".pdf", ".jpg", ".png"]}
        uploadText={<strong style={{ color: 'blue' }}>Click or Drag to Upload Files</strong>}
        subText="Only PDF, JPG, and PNG files are allowed"
        uploadStyles={{ border: '2px dashed green', padding: '20px' }}
        progressStyles={{ backgroundColor: 'lightblue' }}
        errorStyles={{ fontStyle: 'italic', color: 'darkred' }}
        fileIcons={{
            pdf: <img src="pdf-icon.png" alt="PDF" style={{ width: '24px', height: '24px' }} />,
            image: <img src="image-icon.png" alt="Image" style={{ width: '24px', height: '24px' }} />,
            excel: <img src="excel-icon.png" alt="Excel" style={{ width: '24px', height: '24px' }} />,
            default: <img src="default-icon.png" alt="File" style={{ width: '24px', height: '24px' }} />
        }}
        fileTypeNotAllowedText="File type not supported. Please use PDF, JPG, or PNG."
        restrictFileSizeText="File too large. Max size is 5MB."
        FileSizeExceededButAllowedText="File size exceeds 5MB but will be uploaded."
        progressBarInterval={200}
        uploadFileIcon={<svg width="32" height="32" viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
         <path d="M12 2L6.5 7.5H10V15H14V7.5H17.5L12 2Z" />
         <path d="M18 18H6V20H18V18Z" />
        </svg>}
        onError={(error) => console.log("Error:", error)}
        onFileUpload={(file) => console.log("File uploaded:", file)}
    />
)

export default App

In this example:

  • File Restrictions: Limits uploads to PDF, JPG, and PNG files and restricts the maximum file size to 5MB.
  • Custom Styling: Applies custom styles to the upload area, progress bar, and error messages.
  • Icons: Displays different icons for each file type (PDF, image, Excel) and a default icon for others.
  • Custom Text: Changes the default upload text and subtext messages.
  • Callbacks:
    • onError: Logs errors when there is an issue with the file upload.
    • onFileUpload: Logs details when a file is successfully uploaded.

Props

| Prop | Type | Default | Description | |---------------------------|------------------------|---------------------------------------------------|-------------| | maxSizeMB | number | 10 | Maximum file size in MB. | | restrictFileSize | boolean | false | If true, restricts file uploads to maxSizeMB. | | allowedFileTypes | Array<string> | undefined | List of allowed file types (e.g., [".pdf", ".jpg"]). | | uploadText | ReactNode | Drag and Drop your file or Upload | Custom upload instruction text. | | subText | ReactNode | You can upload up to ${maxSizeMB} MB | Custom subtext below upload instruction. | | uploadStyles | CSSProperties | undefined | Custom styles for the upload area. | | progressStyles | CSSProperties | undefined | Custom styles for the progress bar. | | errorStyles | CSSProperties | undefined | Custom styles for error messages. | | fileIcons | object | {} | Custom icons for file types (pdf, image, excel, default). | | onError | (error: string) => void | undefined | Callback when an error occurs. | | onFileUpload | (file: File) => void | undefined | Callback when a file is uploaded. | | fileTypeNotAllowedText | string | File type not allowed. Please upload one of the following: ${allowedFileTypes?.join(', ')} | Message when file type is not allowed. | | restrictFileSizeText | string | File size exceeds the allowed limit of ${maxSizeMB} MB. Please select a smaller file. | Message when file size exceeds limit and is restricted. | | FileSizeExceededButAllowedText | string | File size exceeds the allowed limit of ${maxSizeMB} MB. File will still be uploaded. | Message when file size exceeds limit but is still uploaded. | | progressBarInterval | number | 100 | Interval (in ms) for upload progress increments. | | uploadFileIcon | ReactNode | 📤 | Custom icon for the upload area. | | showPreview | boolean | false | if true shows file preview | | showFileIcon | boolean | true | if true shows file icon |

Customization

  • File Type Icons: Provide custom icons for different file types (e.g., PDF, images) by passing SVG or image elements in fileIcons.
  • Error Messages: Customize error messages for unsupported file types and oversized files.
  • Style Overrides: Apply custom styles directly to the upload area, progress bar, and error messages.
  • Callbacks: Use onError and onFileUpload to handle errors and get the uploaded file data.

Development

To work on the component locally:

  1. Clone the repository:

    git clone https://github.com/gsteja2307/react-file-upload-button.git
  2. Install dependencies:

    npm install
  3. Run the TypeScript compiler in watch mode:

    npm run build
  4. Run the project (if using Vite or another dev server):

    npm run dev

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contributing

Feel free to submit issues or pull requests! If you’d like to improve this component, please fork the repository and make your changes in a feature branch.

Author

Developed by G Surya Teja.