0.7.2 ā€¢ Published 2 years ago

@jesobreira/react-avatar-edit v0.7.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

react-avatar

šŸ‘¤ Load, crop and preview avatar with ReactJS component

npm version

Demo

npm.io

Demo website

Install

npm i react-avatar-edit

Usage

import React from 'react'
import ReactDOM from 'react-dom'
import Avatar from '@jesobreira/react-avatar-edit'

class App extends React.Component {

  constructor(props) {
    super(props)
    const src = './example/einshtein.jpg'
    this.state = {
      preview: null,
      src
    }
    this.onCrop = this.onCrop.bind(this)
    this.onClose = this.onClose.bind(this)
    this.onBeforeFileLoad = this.onBeforeFileLoad.bind(this)
  }
  
  onClose(status) {
    // status = true for OK button, false for Cancel button
    this.setState({preview: null})
  }
  
  onCrop(preview) {
    this.setState({preview})
  }

  onBeforeFileLoad(elem) {
    if(elem.target.files[0].size > 71680){
      alert("File is too big!");
      elem.target.value = "";
    };
  }
  
  render () {
    return (
      <div>
        <Avatar
          width={390}
          height={295}
          onCrop={this.onCrop}
          onClose={this.onClose}
          onBeforeFileLoad={this.onBeforeFileLoad}
          src={this.state.src}
        />
        <img src={this.state.preview} alt="Preview" />
      </div>
    )
  }
}

ReactDOM.render(<App /> , document.getElementById('root'))

Component properties

PropTypeDescription
imgImageThe Image object to display
srcString/Base64The url to base64 string to load (use urls from your domain to prevent security errors)
widthNumberThe width of the editor
heightNumberThe height of the editor (image will fit to this height if neither imageHeight, nor imageWidth is set)
imageWidthNumberThe desired width of the image, can not be used together with imageHeight
imageHeightNumberThe desired height of the image, can not be used together with imageWidth
cropRadiusNumberThe crop area radius in px (default: calculated as min image with/height / 3)
cropColorStringThe crop border color (default: white)
lineWidthNumberThe crop border width (default: 4)
minCropRadiusNumberThe min crop area radius in px (default: 30)
backgroundColorStingThe color of the image background (default: white)
closeIconColorStringThe close button color (default: white)
shadingColorStringThe shading color (default: grey)
shadingOpacityNumberThe shading area opacity (default: 0.6)
mimeTypesStringThe mime types used to filter loaded files (default: image/jpeg,image/png)
labelStringLabel text (default: Choose a file)
labelStyleObjectThe style object for preview label (use camel case for css properties fore example: fontSize)
borderStyleObjectThe style for object border preview (use camel case for css properties fore example: fontSize)
onImageLoad(image)FunctionInvoked when image based on src prop finish loading
onCrop(image)FunctionInvoked when user drag&drop event stop and return croped image in base64 sting
onBeforeFileLoad(file)FunctionInvoked when user before upload file with internal file loader (etc. check file size)
onFileLoad(file)FunctionInvoked when user upload file with internal file loader
onClose(status)FunctionInvoked when user clicks on close editor button

Contributing

  • To start developer server please use npm run start
  • To build production bundle use npm run build