🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

react-files-preview

Package Overview
Dependencies
Maintainers
1
Versions
47
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-files-preview

A React file preview component with built-in support for image editing, image carousel/slider, and download functionality, making it easy to display and manage files in your application

3.0.0
latest
Source
npm
Version published
Weekly downloads
844
-11.81%
Maintainers
1
Weekly downloads
 
Created
Source

react-files-preview

react-files-preview

NPM Version NPM Downloads License CodeQL

A versatile React component to display and manage file previews, supporting various customization options.

📋 Table of Contents

✨ Features

  • 🖼️ Visual File Representation: Displays previews for various image file types.
  • ✏️ Integrated Image Editing: Allows users to edit images using the features of react-photo-editor (brightness, contrast, rotate, flip, draw, etc.).
  • 📤 Drag and Drop Support: Allows users to easily add files by dragging and dropping.
  • 🖱️ Click to Browse: Enables file selection through a standard file input dialog.
  • 🗑️ Remove Files: Option to display a remove icon for individual files.
  • ⬇️ Download Files: Functionality to enable downloading of displayed files.
  • 🔢 Slider Count: Shows the current slide number and total count for image sliders.
  • 📏 File Size Display: Option to show the size of each file.
  • ⚙️ Customizable Styling: Offers props for adjusting width, height, and rounded corners using Tailwind CSS classes.
  • 🚫 Disable Input: Option to disable file selection.
  • 📄 Accept Specific Types: Control which file types are accepted.
  • 🔢 Maximum File Limits: Set constraints on the number and size of files.
  • 🔄 Controlled Component: Accepts an array of files as a prop for controlled behavior.
  • 👂 Event Callbacks: Provides callbacks for onChange, onRemove, onError, onClick, and onDrop.

📦 Installation

# Using npm
npm install react-files-preview

# Using yarn
yarn add react-files-preview

🚀 Basic Usage

import React, { useState } from "react";
import { ReactFilesPreview } from "./components/ReactFilesPreview";

function App() {
  const [files, setFiles] = useState<File[]>([]);

  const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
    const newFiles = Array.from(event.target.files || []);
    setFiles((prevFiles) => [...prevFiles, ...newFiles]);
    console.log("Selected files:", newFiles);
    console.log("All files:", [...files, ...newFiles]);
  };

  const handleFileRemove = (removedFile: File) => {
    setFiles((prevFiles) => prevFiles.filter((file) => file !== removedFile));
    console.log("Removed file:", removedFile);
  };

  return (
    <div>
      <ReactFilesPreview
        files={files}
        onChange={handleFileChange}
        onRemove={handleFileRemove}
        allowEditing
        multiple
      />
    </div>
  );
}

export default App;

📱 Live Demo

See it in action on Stackblitz

Props

NameTypeDefaultDescription
filesFile[][]Pass array of file objects for default files
urlstringnullSet image by passing image URL
downloadFilebooleantrueEnables file download
removeFilebooleantrueShow file remove icon on file hover
showFileSizebooleantrueShow file size under files
showSliderCountbooleantrueShow slides count under file slider
disabledbooleanfalseIf true, prevents user to add files by disabling the component
multiplebooleantrueAccepts one or more files
acceptstringComma-separated lists of file types. See MIME Types
maxFileSizenumberMaximum allowed file size in bytes e.g. 1024 x 1024 x 5 == 5MB
maxFilesnumberMaximum files allowed to be added
widthstringrfp-basis-11/12Tailwind CSS flex-basis class https://tailwindcss.com/docs/flex-basis
heightstringTailwind CSS height class https://tailwindcss.com/docs/height
fileWidthstringrfp-w-44Tailwind CSS width class https://tailwindcss.com/docs/width
fileHeightstringrfp-h-32Tailwind CSS height class https://tailwindcss.com/docs/height
getFilefuncReturns all current files
onChangefuncReturns selected file(s)
onRemovefuncReturns the removed file
onErrorfuncReturns error message as string
onClickfuncReturns file on click

🤝 Contributing

Contributions to react-files-preview are welcome! If you have any issues, feature requests, or improvements, please open an issue or submit a pull request on the GitHub repository.

How to Contribute

  • Fork the repository
  • Create your feature branch: git checkout -b feature/amazing-feature
  • Commit your changes: git commit -m 'Add some amazing feature'
  • Push to the branch: git push origin feature/amazing-feature
  • Open a pull request

Reporting Issues

When reporting issues, please provide:

  • A clear description of the problem
  • Steps to reproduce
  • Expected vs actual behavior
  • Screenshots if applicable
  • Environment details (browser, OS, etc.)

📄 License

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

Keywords

react

FAQs

Package last updated on 10 Apr 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts