Giter VIP home page Giter VIP logo

synapsestudios / react-drop-n-crop Goto Github PK

View Code? Open in Web Editor NEW
19.0 30.0 7.0 440 KB

An opinionated implementation of react-dropzone and react-cropper

Home Page: https://synapsestudios.github.io/react-drop-n-crop/

License: MIT License

JavaScript 57.99% CSS 42.01%
react javascript drop-n-crop react-drop-n-crop dropzone react-dropzone cropper react-cropper drag-and-drop crop-image image-cropper crop image

react-drop-n-crop's Introduction

A combined implementation of react-dropzone and react-cropper (Cropper.js) for front-end image upload/validation/cropping.

npm version react-drop-n-crop dependencies react-drop-n-crop peer dependencies

Demo

A demo is available at https://synapsestudios.github.io/react-drop-n-crop/

Usage

Installing via CLI

// yarn
yarn add @synapsestudios/react-drop-n-crop

// npm
npm install --save @synapsestudios/react-drop-n-crop

Importing JS

import DropNCrop from '@synapsestudios/react-drop-n-crop';

Importing CSS

// Minified, autoprefixed css
import '@synapsestudios/react-drop-n-crop/lib/react-drop-n-crop.min.css';

// Not-minified, not-autoprefixed css
import '@synapsestudios/react-drop-n-crop/lib/react-drop-n-crop.css';

Using Stylus

If you are using Stylus you can import the .styl file into your build:

@import '@synapsestudios/react-drop-n-crop/lib/react-drop-n-crop.styl';

! See the Stylus Variables section below for variables/details.

Using with an ES6 Class and React Component State

import React, { Component } from 'react';
import DropNCrop from '@synapsestudios/react-drop-n-crop';
import '@synapsestudios/react-drop-n-crop/lib/react-drop-n-crop.min.css';

class SetStateExample extends Component {
  state = {
    result: null,
    filename: null,
    filetype: null,
    src: null,
    error: null,
  };

  onChange = value => {
    this.setState(value);
  };

  render() {
    return <DropNCrop onChange={this.onChange} value={this.state} />;
  }
}

export default SetStateExample;

API

Required Props

DropNCrop is built as a controlled component. The following props must be supplied to the component:

onChange: (required)

onChange is the callback function invoked when an image is dropped or cropped. onChange returns an object (in the shape of value below).

onChange: PropTypes.func.isRequired,

value: (required)

value is the object passed back from the onChange function. It must be in the following shape:

value: PropTypes.shape({
  result: PropTypes.string, // Resulting DataURL from Cropper.js crop box
  filename: PropTypes.string, // Original filename from uploaded file
  filetype: PropTypes.string, // Original MIME type from uploaded file
  src: PropTypes.string, // Original DataURL from the FileReader.result
  error: PropTypes.string, // Error returned from fileSize/fileType validators
}).isRequired,

Optional Props

canvasHeight:

canvasHeight is a string for the container inline style height property.

canvasHeight: PropTypes.string, // default: '360px'

canvasWidth:

canvasWidth is a string for the container inline style width property.

canvasWidth: PropTypes.string, // default: '100%'

className:

className is a string for the outermost container class name.

className: PropTypes.string, // default: ''

cropperOptions:

cropperOptions is an object for customizing the cropper component. Lists of available options can be found here: https://github.com/roadmanfong/react-cropper

cropperOptions: PropTypes.object, // default: {guides: true, viewMode: 0, autoCropArea: 1}

maxFileSize:

maxFileSize is a maximum number (in bytes) for file upload validation.

maxFileSize: PropTypes.object, // default: 3145728

allowedFileTypes:

allowedFileTypes is an array (of strings) of valid MIME types for file upload validation.

allowedFileTypes: PropTypes.array, // default: ['image/jpeg', 'image/jpg', 'image/png']

Stylus Variables

react-drop-n-crop comes with a set of stylus variables that can be overridden to add your own project-specific theming, as shown below:

/* Theming by overriding default stylus variables with your projects colors */

$drop-n-crop--primary-color = $your-project-primary-color;
$drop-n-crop--error-color = $your-project-error-color;

$drop-n-crop--body-color = $your-project-body-color;
$drop-n-crop--heading-color = $your-project-heading-color;

$drop-n-crop--input-background-color = $your-project-background-color;
$drop-n-crop--input-border-color = $your-project-border-color;

@import '@synapsestudios.com/react-drop-n-crop/css/react-drop-n-crop.styl';

Contributing

To run the project on your own computer:

  • Clone this repository
  • yarn install or npm install
  • yarn run storybook or npm run storybook
  • Visit http://localhost:5000/
  • Changes made to files in the src directory should immediately compile and be visible in your browser.

react-drop-n-crop's People

Contributors

chrisheninger avatar spruce-bruce avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-drop-n-crop's Issues

Cropper does not crop on initialization

If you set the autoCropArea to any value < 1,, drop an image, and then immediately crop the image without moving the crop area, the value will be the entire image, instead of the cropped area.

Is there a way to set up the cropper so that even when I just dropped an image, the crop result will match the crop area ?

Having issues when using with typescript.

Having issues when using with Typescript.

Could not find a declaration file for module '@synapsestudios/react-drop-n-crop'. '.../node_modules/@synapsestudios/react-drop-n-crop/lib/index.js' implicitly has an 'any' type.
Try npm install @types/synapsestudios__react-drop-n-crop if it exists or add a new declaration (.d.ts) file containing declare module '@synapsestudios/react-drop-n-crop';

using the const = require form cuses problems when I render - with the message that render expects a string but got an object.

[Documentation Suggestion] Converting dataURL to File object

I came across this amazing library yet I have a feeling that documentation is not enough.

In case anyone struggle with handling the uploaded files, hope this help :)

If you follow the instructions at ReadMe.md, then you should have a state variable with 5 attributes (result, filename, filetype, src, err).
The "result" attribute contains a dataURL. Although it is nice of this library to do the conversion for us, sometimes we may need to use a File object. In my case, I need to upload the image to my Firebase Cloud Storage, and the SDK requires a File object.

https://stackoverflow.com/questions/6850276/how-to-convert-dataurl-to-file-object-in-javascript

The above page at stack overflow illustrates exactly what needs to be done.
Good luck with programming!

Required prop warning when used React.cloneElement

When used with redux-form, props are passed in with cloneElement and react throws a warning:
Failed prop type: The prop 'onChange' is marked as required in 'DropNCrop', but its value is 'undefined'.

This is apparently intentional.

facebook/react#4494

Conclusion: Props shouldn't be required. onChange shouldn't be required and should default to a no-op.

value or initialValue prop

It'd be nice to figure out how to implement a value prop so that this can be turned into a controlled component. Obviously that would require more information than just the cropped File object that is output by onSave, so this might not be a good idea.

In order to implement a value prop onChange needs to be updated to return an object shaped something like this:

{
  originalImage: File object,
  croppedImage: File object,
  croppedDimensions: {x: int, y: int, width: int, height: int}
}

These values could be useful in other scenarios anyway (especially if we decide to do image processing on the server), but changes fundamentally the simplicity of this component.

At the very least we'll want to do an initialValue prop that takes a File object for use cases like re-cropping an image that's already been uploaded.

Repo settings updates

I don't have permissions, need @bobeagan.

  • Add description An opinionated implementation of react-dropzone and react-cropper
  • Add Tags react javascript drop-n-crop react-drop-n-crop dropzone react-dropzone cropper react-cropper drag-and-drop crop-image image-cropper crop image

Rename onSave prop to onChange

Because this component is intended to be used as a form input it should conform to the established form input api.

In the future, we can look at supporting things like onBlur, etc, but for now just changing onSave to onChange will help.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.