1.0.2 • Published 2 months ago

react-profile-crop v1.0.2

Weekly downloads
-
License
ISC
Repository
github
Last release
2 months ago

React Profile Cropper

A flexible, customizable React component for cropping profile pictures with real-time preview.

Features

  • 🖼️ Circular, square, or rectangular crop shapes
  • 🔄 Real-time preview
  • 📱 Responsive and mobile-friendly with touch support
  • 🎚️ Zoom and drag controls
  • 🎨 Fully customizable theming
  • 📤 Multiple output formats (PNG, JPEG, WebP)
  • ⚙️ Configurable aspect ratio
  • 🔍 Error handling for file types and size

Installation

npm install react-profile-cropper
# or
yarn add react-profile-cropper

Basic Usage

import React from "react";
import { ProfileCropper } from "react-profile-cropper";

function App() {
  return (
    <div className="app">
      <ProfileCropper
        onSave={(imageData) => {
          console.log("Cropped image:", imageData);
          // Save to server or state
        }}
      />
    </div>
  );
}

export default App;

Advanced Usage

import React, { useState } from "react";
import { ProfileCropper } from "react-profile-cropper";

function ProfileEditor() {
  const [profileImage, setProfileImage] = useState(null);
  const [showCropper, setShowCropper] = useState(false);

  const handleSave = (imageData) => {
    setProfileImage(imageData);
    setShowCropper(false);
    // Upload to server
  };

  return (
    <div className="profile-editor">
      {profileImage ? (
        <div className="current-profile">
          <img src={profileImage} alt="Profile" className="profile-image" />
          <button onClick={() => setShowCropper(true)}>Change Picture</button>
        </div>
      ) : (
        <button onClick={() => setShowCropper(true)}>
          Add Profile Picture
        </button>
      )}

      {showCropper && (
        <div className="cropper-modal">
          <ProfileCropper
            initialImage={profileImage}
            onSave={handleSave}
            onCancel={() => setShowCropper(false)}
            cropShape="circle"
            outputFormat="png"
            maxFileSizeMB={2}
            theme={{
              accentColor: "#4a90e2",
              buttonBackgroundColor: "#4a90e2",
            }}
          />
        </div>
      )}
    </div>
  );
}

export default ProfileEditor;

Custom Theming

import { ProfileCropper } from "react-profile-cropper";

const customTheme = {
  backgroundColor: "#ffffff",
  overlayColor: "rgba(0, 0, 0, 0.7)",
  borderColor: "#cccccc",
  accentColor: "#ff5722",
  textColor: "#333333",
  buttonBackgroundColor: "#ff5722",
  buttonTextColor: "white",
};

function App() {
  return (
    <ProfileCropper
      theme={customTheme}
      // other props...
    />
  );
}

Props

Core Functionality

PropTypeDefaultDescription
initialImagestringundefinedBase64 or URL of initial image to crop
onSave(imageData: string) => voidundefinedCallback when save button is clicked
onCancel() => voidundefinedCallback when cancel button is clicked
cropShape'circle' \| 'square' \| 'rectangle''circle'Shape of the crop area
aspectRationumberundefinedAspect ratio of crop area (width/height)
outputFormat'png' \| 'jpeg' \| 'webp''png'Output image format
outputQualitynumber0.9Output image quality (0-1)

Style and Appearance

PropTypeDefaultDescription
themeThemeOptionsSee belowTheme customization object
sizenumber250Size of the crop area (width in px)
classNamestring''Class name for the container element
styleReact.CSSProperties{}Inline styles for the container
containerHeightnumber350Height of the container (px)

Controls and Features

PropTypeDefaultDescription
maxZoomnumber4Maximum zoom level
minZoomnumber0Minimum zoom level
zoomStepnumber0.1Step size for zoom controls
allowZoombooleantrueEnable zoom controls
allowDragbooleantrueEnable image dragging
showPreviewbooleantrueShow preview of cropped image
previewSizenumber120Size of preview image (px)
showZoomControlsbooleantrueShow zoom slider
showChangeButtonbooleantrueShow button to change image

Text Customization

PropTypeDefaultDescription
headerTextstring'Profile Picture Cropper'Header text
previewTextstring'Preview'Text below preview image
saveButtonTextstring'Save Profile Picture'Save button text
cancelButtonTextstring'Cancel'Cancel button text
loadingTextstring'Saving...'Text shown when saving
uploadTextstring'Click to upload image'Upload area main text
uploadSubtextstring'JPG, PNG or GIF. Max 5MB.'Upload area subtext
zoomLabelstring'Zoom:'Label for zoom control
changeButtonTextstring'Change Image'Change image button text

Image Constraints

PropTypeDefaultDescription
acceptedFileTypesstring'image/*'Accepted file types for upload
maxFileSizeMBnumber5Maximum file size in MB

CSS Class Names

PropTypeDefaultDescription
containerClassNamestring''Class for the outer container
headerClassNamestring''Class for the header text
previewContainerClassNamestring''Class for preview container
buttonContainerClassNamestring''Class for button container
saveButtonClassNamestring''Class for save button
cancelButtonClassNamestring''Class for cancel button
errorContainerClassnamestring''Class for error container
errorTextClassnamestring''Class for error text

Error Handling

PropTypeDefaultDescription
onError(error: string) => voidundefinedCallback when an error occurs

Default Theme

{
  backgroundColor: "#f3f2ef",
  overlayColor: "rgba(0, 0, 0, 0.6)",
  borderColor: "#e0e0e0",
  accentColor: "#0077B5",
  textColor: "#333333",
  buttonBackgroundColor: "#0077B5",
  buttonTextColor: "white"
}

TypeScript Support

The package includes TypeScript definitions. You can import the types:

import {
  ProfileCropperProps,
  CropArea,
  ThemeOptions,
  CropShape,
  OutputImageType,
} from "react-profile-cropper";

Development

Building the package

npm run build

Running tests

npm test

License

MIT © ydixit007(https://github.com/ydixit007)