npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-simple-crop

v1.0.2

Published

A simple and light-weight library of React components for cropping images

Downloads

51

Readme

React Simple Crop

A simple, easy-to-use and dependecy-free library for cropping images in React.

Features

  • Responsive and touch enabled
  • Crop supports mimimum and maximum sizes
  • Crop can conform to an aspect ratio
  • Crop can be refined with arrow keys
  • Dependency-free (only 8kb minified)
  • Compatible with >IE11
  • Typescript support
  • Accessible

Installation

npm i react-simple-crop

Demo

A simple example can be found here on CodeSandbox.

There is also a full suite of Storybook stories, which can be run locally:

npm run storybook

Basic Usage

import React from "react";
import { Crop, Preview } from "react-simple-crop";

export const App = () => {
  const imageRef = React.createRef();

  const [value, setValue] = React.useState({
    x: 0,
    y: 0,
    width: 0,
    height: 0
  });

  return (
    <>
      <Crop
        onChange={crop => setValue({ ...value, ...crop })}
        ref={imageRef}
        src={"..."}
        value={value}
      />
      {filePreview && <Preview ref={imageRef} value={value} />}
    </>
  );
};

Components

<Crop />

Displays an image with a crop area overlay.

This crop area can be drawn, moved or resized, using mouse click, touch, or keyboard arrow events.

The onChange callback will fire any time the coordinates or size of the crop area change.

You must then update the local parent component state with the new value to see these changes reflected in the DOM.

How to use

import { Crop } from "react-simple-crop";

/* ... */

const [value, setValue] = React.useState({
  x: 0,
  y: 0,
  width: 0,
  height: 0
});

<Crop
  onChange={crop => setValue({ ...value, ...crop })}
  src="..."
  value={value}
/>;

Props

| Prop | Required | Type | Description | | --------------- | :------: | -------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- | | onChange | ✓ | ({ x: number; y: number; width: number; height: number; }) => void | Callback which is fired any time the coordinates or size of the crop area change. | | src | ✓ | string | Source attribute which is passed to the image element. | | value | ✓ | { x: number; y: number; width: number; height: number; } | Percentage coordinates and size of the crop area. You can also use this prop to initialize the size of the crop on the first render. | | aspectRatio | | [number, number] | Restricts the ability to draw or resize a crop area to specific dimensions. | | alt | | string | Alt attribute which is passed to the image element. | | className | | string | Class name attribute passed to the containing element of the image and crop area. | | crossOrigin | | "anonymous" | "use-credentials" | Cross origin attribute which is passed to the image element. | | maxHeight | | number | Restricts the height of the crop area to a maximum percentage of the image element. | | maxWidth | | number | Restricts the width of the crop area to a maximum percentage of the image element. | | minHeight | | number | Restricts the height of the crop area to a minimum percentage of the image element. | | minWidth | | number | Restricts the width of the crop area to a minimum percentage of the image element. | | onComplete | | () => void | Callback which is fired any time the crop area finishes being drawn, moved by mouse or keyboard, or resized. | | onStart | | () => void | Callback which is fired any time the crop area starts being drawn, moved by mouse or keyboard, or resized. | | ref | | { current: HTMLImageElement } | React ref object which is passed to the image element, and also needs to be passed to the <Preview /> component. This is used to draw the preview image. |

<Preview />

Displays a new preview image of the currently selected crop area. This image can then be saved as its own file.

How to use

import { Crop, Preview } from "react-simple-crop";

/* ... */

const imageRef = React.createRef();

const [value, setValue] = React.useState({
  x: 0,
  y: 0,
  width: 0,
  height: 0
});

<Crop
  onChange={crop => setValue({ ...value, ...crop })}
  ref={imageRef}
  src="..."
  value={value}
/>
<Preview
  ref={imageRef}
  value={value}
/>

Props

| Prop | Required | Type | Description | | ------------ | :------: | ---------------------------------------------------------- | --------------------------------------------------------------------------------------------------- | | ref | ✓ | { current: HTMLImageElement } | React ref object of the <Crop /> component image element. This is used to draw the preview image. | | value | ✓ | { x: number; y: number; width: number; height: number; } | Percentage coordinates and size of the crop area. | | fileType | | string | File type of the preview image (i.e. image/jpeg, image/png, or image/gif). |

Also accepts any HTML image attributes as props (i.e. alt, crossOrigin or style), apart from src, which is used internally by this component.

Testing

There are no unit tests. Instead, we use end-to-end tests.

We require an actual DOM, versus an emulated DOM like JSDOM, which is often used with unit testing libraries, to be able to easily compute image sizes and positions.

To run the Cypress E2E tests locally, first ensure the Storybook server is running:

npm run storybook

Then, in another terminal:

npm run cypress:open