Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@uploadcare/react-widget

uploadcare18.5kMIT2.4.7TypeScript support: included

React File Uploader. React component with uploading from multiple sources

uploadcare, react, component, widget, uploader, file-uploader, image-uploader, upload-component, upload file, multipart-upload, image-picker, react-image-picker, react-upload, react-upload-component, react-file-upload-component, filepicker, file-picker, file-upload, file uploading, file, upload, input, dropzone, multi, multipart, ajax, xhr, dialog, store, files, image, images, use file upload, react file upload, react hook file upload, front-end

readme

React Wrapper for jQuery File Uploader

⚠️ Notice: File Uploader Option

This uploading wrapper remains fully functional and can be a great fit for your projects, especially if you have specific needs for jQuery. However, we recommend exploring our new React File Uploader to access the latest features and improvements.


This React component helps you integrate Uploadcare jQuery File Uploader into your React app natively; props management and lazy loading are bundled.

The component allows users to upload files from any file system and device, social media, cloud storage, and more. Best-in-class for image upload. All that without any backend code that’s usually required to handle uploads.

Read Uploadcare + React Integration Guide

Build Status NPM version

Install

npm i @uploadcare/react-widget

Usage

import { Widget } from "@uploadcare/react-widget";

<Widget publicKey="YOUR_PUBLIC_KEY" />;

or

import { Panel } from "@uploadcare/react-widget";

<Panel publicKey="YOUR_PUBLIC_KEY" />;

To use this component, get an API key from your Uploadcare project.

Uploadcare account provides services for file uploads, transformations, CDN delivery, as well as APIs. After signing up, you'll see Dashboard where you can manage projects. Each Project is identified by its public key. Replace YOUR_PUBLIC_KEY with your project’s Public API Key and you are all set.

You can refer to our integration guide for more details.

Available bundles

By default, npm and other package managers import the full (all locales) CommonJS or ESM bundle.

To reduce your bundle size, you can also import one of the following:

  • The english-only bundle (saves ~27% in bundle size) as @uploadcare/react-widget/en
  • The minified all-locales bundle (saves ~44% in bundle size) as @uploadcare/react-widget/min
  • The minified english-only bundle (saves ~60% in bundle size) as @uploadcare/react-widget/en-min

Configuration

Widget component configuration

value: string[] | string | JQuery.Deferred | JQuery.Deferred[]

Set an array of file UUID/group UUID/CDN URL/File Instance/Group Instance as a value.

<Widget value='9dd2f080-cc52-442d-aa06-1d9eec7f40d1' />
<Widget value='9dd2f080-cc52-442d-aa06-1d9eec7f40d1~12' />
<Widget value={[
  '9dd2f080-cc52-442d-aa06-1d9eec7f40d1',
  'https://ucarecdn.com/fdfe4e67-f747-4993-91f5-be21d6d3c1a6/',
  '9ef9af26-7356-4428-b69c-1b920f947989~2'
]} />

onChange: (fileInfo: FileInfo) => void

Set a function to be called after a file is uploaded and ready.


onFileSelect: (fileInfo: FileInfo | FilesInfo | null) => void

Set a function to be called after a new file is selected.


onDialogOpen: (dialog: DialogApi) => void

Set a function to be called after dialog is opened.


onDialogClose: (objs: FileInfo | FilesInfo | null) => void

Set a function to be called after dialog is closed.


onTabChange: (tabName: string) => void

Set a function to be called after tab is changed.


metadata: (metadata: Record<string, string>)

The option can be used to set metadata object associated with the uploaded file.

Note that metadata supports string values only, any non-string value will be converted to string, including boolean, number, null and undefined.

WARNING: If this option is specified, option metadataCallback will be overridden (without merging). This is opposite from the uploadcare-widget's behavior.

See Metadata docs for details.


metadataCallback: () => Record<string, string>

Defines the function that specifies the actual metadata object a file uploader should use to associate with the uploaded file. It's helpful in the case of dynamic metadata object.

Note that metadata supports string values only, any non-string value will be converted to string, including boolean, number, null and undefined.

See Metadata docs for details.


customTabs: {[key: string]: CustomTabConstructor}

Add custom tabs for a widget.

function myTab(container, button, dialogApi, settings, name, uploadcare) {
  ...
}

<Widget customTabs={{ tabname: myTab }} />

Since custom tabs are global internally, any local property change will affect all the widget instances. So we're highly recommend not to redefine tab constructors and not to have different constructors under the same name.

Note that we added the fifth argument to the custom tab constructor — an uploadcare object. The widget is lazily-loaded, so you don’t have to import uploadcare-widget separately for your custom tab.

Remember that you also need to include your custom tab in the tabs prop to make it work:

<Widget customTabs={{ tabname: myTab }} tabs='tabname' />

Add Image Editor

  1. Install uploadcare-widget-tab-effects package
npm i uploadcare-widget-tab-effects
  1. Import one of the bundles:

    • uploadcare-widget-tab-effects/react for all-locales bundle
    • uploadcare-widget-tab-effects/react-en for english-only bundle
import effects from 'uploadcare-widget-tab-effects/react'
// or
// import effects from 'uploadcare-widget-tab-effects/react-en'
  1. Pass the effects object to the customTabs prop:
<Widget previewStep customTabs={{ preview: effects }} />

validators: Validator[]

Set validators for a widget. Validator is a JavaScript function that receives a fileInfo object for each uploaded file and throws an exception if that file doesn't meet validation requirements.

const fileTypeLimit = (allowedFileTypes: string) => {
  const types = allowedFileTypes.split(' ')

  return function(fileInfo: FileInfo) {
    if (fileInfo.name === null) {
      return
    }
    const extension = fileInfo.name.split('.').pop()

    if (extension && !types.includes(extension)) {
      throw new Error('fileType')
    }
  }
}

const validators = [fileTypeLimit('mp3 avi mp4')];

<Widget validators={validators} />

preloader: ComponentType

Set a custom preloader. A preloader component shows up when the widget is being loaded.


ref: widgetApiRef

Define a reference object to address the Widget API wrapper. Use it to access these methods: value, openDialog, reloadInfo and getInput.

  1. value() is the alias for widget.value()
  2. openDialog() is the alias for widget.openDialog()
  3. reloadIngo() is the alias for widget.reloadInfo()
  4. getInput() returns widget's input element instance.

Example:

const Example = () => {
 const widgetApi = useRef();
 return (
   <>
     <button onClick={() => widgetApi.current.openDialog()}>
       Click me
     </button>
     <Widget ref={widgetApi} publicKey=“demopublickey” />
   </>
 );
};

tabsCss: string

Optional. Define a custom CSS for tabs opened in iframes (e.g., Facebook, Instagram, etc.). It can be a CSS string or an absolute URL to a CSS file.

// Via URL
<Widget tabs="facebook" tabsCss="https://site.com/styles/uc.tabs.css"/>

// Via plain CSS
<Widget tabs="facebook" tabsCss=".source-facebook { background: #1877F2; }"/>

Note that all iframe tabs are opened via HTTPS, so linked CSS files should also be available over HTTPS.


Panel component configuration

value: string[] | string | JQuery.Deferred | JQuery.Deferred[]

Set an array of file UUID/group UUID/CDN URL/File Instance/Group Instance as a value.

<Panel value={[
  '9dd2f080-cc52-442d-aa06-1d9eec7f40d1',
  'https://ucarecdn.com/fdfe4e67-f747-4993-91f5-be21d6d3c1a6/',
  '9ef9af26-7356-4428-b69c-1b920f947989~2'
]} />

onChange: (fileInstanceList: FileInstance[]) => void

Set a function to be called whenever files state changes.


onProgress: (uploadInfoList: UploadInfo[]) => void

Set a function to be called whenever progress state changes.


ref: panelApiRef

Define a reference object to address the Dialog API wrapper.

Dialog API reference

interface DialogApi {
  addFiles(type: string, files: any[]): void;
  addFiles(files: Array<JQuery.Deferred<FileInfo>>): void;
  switchTab(tab: string): void;
  getFileColl(): CollectionOfPromises<FileInfo>;
  hideTab(tab: string): void;
  showTab(tab: string): void;
  isTabVisible(tab: string): boolean;
  onTabVisibility(callback: OnTabVisibilityCallback): void;
}

Those methods works exactly the same way as in Widget component:

Those methods aren't supported in Panel component:

Widget configuration

jQuery File Uploader can be deeply customized to suit your UX/UI. You can define allowed upload sources, implement file validation, and more.

All the options defined in the widget options docs are also supported in the component as props (use the camelCase notation, you can find it under “Object key” in the referenced article).

Use the live jQuery File Uploader as a starting point and consider checking out the docs on widget configuration.

Security issues

If you ran into something in Uploadcare libraries that might have security implications, please hit us up at bugbounty@uploadcare.com or Hackerone.

We’ll contact you shortly to fix and issue prior to any public disclosure.

Feedback

We want to hear your issue reports and feature requests at hello@uploadcare.com.

changelog

2.4.7 (2024-08-27)

2.4.6 (2024-08-08)

No changes

2.4.5 (2023-07-04)

  • fix CI release workflows

2.4.4 (2023-07-04)

  • update readme
  • update keywords

2.4.3 (2023-04-13)

  • bump dev dependencies
  • get rid of @uploadcare/client-suspense in favour of the original react suspense

2.4.2 (2023-03-13)

Bug Fixes

  • separate typing files for CJS and ESM modules (#374)

2.4.1 (2022-12-26)

Bug Fixes

  • add missing types and export all of them (c25a586)
  • don't pass metadataCallback to the options if metadata present (7deece8)

2.4.0 (2022-12-07)

Bug Fixes

  • restore dialog callbacks after dialog re-initialization (87867fa)
  • specify types for each module export (e52a994)
  • use .mjs extension for ESM bundles and .cjs extension for CJS bundles (e37c9ae)

Features

  • add CJS bundles for min, en, and en-min (522e0f5)

2.3.0 (2022-11-17)

Bug Fixes

  • add missing Apply button in the Effects Tab (5a0ab11)
  • panel: prevent widget reinit & clearing on re-renders (a098d48)

Features

2.2.0 (2022-09-29)

Bug Fixes

  • types: openDialog() could accept optional argument of string, null or Settings type (#349) (63938d0)

Features

  • Widget: add widgetApiRef.value method (#347) (64f8c12)

2.1.6 (2022-08-29)

Bug Fixes

  • add extension to uploadcare-widget import (#343) (714eb0d)

2.1.5 (2022-07-15)

Bug Fixes

2.1.4 (2022-07-11)

Bug Fixes

  • add exports field to the package.json (#336) (79b2579)

2.1.3 (2022-07-08)

Bug Fixes

2.1.2 (2022-07-06)

Bug Fixes

  • fix(Widget): do not update widget value if it's not changed (#329) (2c11e27)
  • fix(types): fix DialogApi.addFiles signature (#329) (2c11e27)

2.1.1 (2022-05-19)

Bug Fixes

  • remove uploadcareWidget jquery data between widget initializations in strict mode (#323) (61c6e85)

2.1.0 (2022-05-11)

Features

2.0.0 (2022-04-22)

Bug Fixes

  • fix(typing): jquery reference

Features

BREAKING CHANGES

  • Panel component was undocumented but still exported. It's API and behaviour have changed. See README for the details.

1.3.10 (2022-03-04)

Bug Fixes

  • Bump widget version (#311) (b163cbd). As a consequence, Icelandic locale added.

1.3.9 (2021-10-27)

Bug Fixes

1.3.8 (2021-07-23)

Bug Fixes

  • pass previewUrlCallback function to the global scope (#270) (32d778c)
  • reassign callbacks on widget reinitialization (#268) (90ed6f2)

1.3.7 (2021-03-31)

Bug Fixes

1.3.6 (2021-03-31)

Bug Fixes

Unreleased

Bug Fixes

  • types Export types FileUpload and FilesUpload.
  • types FileInfo type updated to account for null uuid and mime-type during validation callback
  • types Fix the type of the fileColl attibute of the dialog api
  • types Add and export the FileGroup type

1.3.5 (2021-02-26)

Chore

  • deps: update dependencies

1.3.4 (2021-01-20)

Bug Fixes

  • custom-tabs: fix using custom tabs at multiple instances (#237) (3863f60)

1.3.3 (2020-11-25)

Bug Fixes

1.3.2 (2020-08-27)

Bug Fixes

  • locale: make locale props dynamic (#200) (28fcc09)
  • types: improve onFileSelect callback typings (#204) (c365c53)

1.3.1 (2020-08-17)

Bug Fixes

  • uploader: replace IE unsupported ChildNode.remove method (#197) (6e2274c)

1.3.0 (2020-07-27)

Features

  • add onDialogClose onDialogOpen onTabChange callbacks (#186) (6822c22)
  • support tabsCss property (#187) (158602b)

Bug Fixes

1.2.7 (2020-05-20)

Bug Fixes

  • use annotate-pure-calls instead of direct #__pure notations, mark module as side effect free (#176) (2ebe6f5)
  • use another plugin for module replacement (#175) (fc117e4)

1.2.6 (2020-04-29)

Bug Fixes

  • enable tree shaking with pure notation (#164) (9b5d0da)

1.2.5 (2020-02-19)

Bug Fixes

  • deps: update dependency react-fast-compare to v3 (#139) (1186573)

1.2.4 (2020-01-22)

Bug Fixes

  • types: allow null for preloader prop (#114) (10a1517)

1.2.3 (2020-01-15)

Bug Fixes

  • don't prefix input name and id attributes with data (#106) (ee6d2db)

1.2.2 (2020-01-09)

Bug Fixes

  • update uploadcare-widget to 3.8.2 (#99) (88d9e83)
  • types: add types for effects tab translations (#96) (28ba5b7)
  • types: fix strict-export-declare-modifiers dtslint problem (#92) (0d26981)

1.2.1 (2019-12-20)

Bug Fixes

  • types: add types for effects-tab (#66) (17e04a0)
  • types: rewrite enums to regular types (#68) (4b4cedb)

1.2.0 (2019-11-21)

Bug Fixes

  • types: fixed path to *.d.ts files(#60) (a67f5eb)

1.1.0 (2019-11-18)

Features

  • added type definitions for Typescript (#55) (99aa82c)

1.0.3 (2019-11-07)

Features

  • build locales from uploadcare-widget (#52) (284c7a9)

1.0.2 (2019-09-25)

Bug Fixes

  • hide ssr warning about useLayoutEffect (#46) (cfa48f9)

1.0.1 (2019-09-20)

Bug Fixes

  • don't reiniting widget when callbacks change (#43) (cac9960)

1.0.0 (2019-09-10)

1.0.0-2 (2019-08-28)

Features

  • provide widget api with react refs (#41) (311b6e4)

1.0.0-1 (2019-08-26)

Bug Fixes

  • validators: don’t rewrite validators with another object (#38) (fbac259)

Features

BREAKING CHANGES

    • validator → validators

Co-Authored-By: Aleksandr Grenishin nd0ut.me@gmail.com

1.0.0-0 (2019-08-22)

Features

BREAKING CHANGES

    • validator → validators

Co-Authored-By: Aleksandr Grenishin nd0ut.me@gmail.com

0.2.1 (2019-08-14)

Bug Fixes

  • panel: using function for right 'this' context in tab constructor (#30) (91a6d20)

0.2.0 (2019-08-08)

Bug Fixes

Features

Change Log