1.4.0 • Published 2 years ago

react-essentialrect v1.4.0

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

React EssentialRect

A React implementation of the essentialRect responsive display standard. EssentialRectImg is a React component that acts like an <img/> element, using the image's essential rectangle to reponsiviely display the image within the client area.

EssentialRect Library on NPM

Quick demo | Code Sandbox

Table of Contents

  1. What is essentiaRect?
  2. Installation
  3. Usage
  4. Example
  5. Props

What is essentialRect?

EssentialRect is a standard for responsive image display. Rather than cropping an image to a rectangle of a certain aspect ratio, a rectangle (its essentialRect) is defined for an image as "essential". This allows the image to be shown in a wide range of aspect ratios without cropping or leterboxing. The essentialRect will be guaranteed to be displayed, while the rest of the image will be considered "nice to have", and will be used to fill the remaining client area. The essentialRect will be as centered as possible while still avoiding letterboxing.

essentialrect example An image with an essential rect, rendered into two aspect ratios. Photo by Rodnae Productions @ Pexel

Installation

npm i react-essentialrect --save

Usage

There is no default export.

import { EssentialRectImg } from "react-essentialrect";
import "react-essentialrect/dist/essentialrect-img.css";

Example

import { EssentialRectImg } from "react-essentialrect";
import "react-essentialrect/dist/essentialrect-img.css";

const imageHeight = 300;
const aspectRatio = 1.91;

function App(imageUrl) {
  const imageStyles = {
    width: `${imageHeight * aspectRatio}px`,
    height: `${imageHeight}px`,
  };

  return (
    <div className="App">
      <EssentialRectImg
        src={imageUrl}
        essentialRect={essentialRect}
        style={imageStyles}
      />
    </div>
  );
}

Props

src (required)

The url for the image. Can be any url that <img> accepts.

essentialRect (optional)

A Rect object that defines the essential rectangle for the image. If not provided, then entire image is considered essential.

className (optional)

CSS class to add class to the EssentialRectImg.

style (optional)

CSSProperties object to add styles to the EssentialRectImg.

imageStyle (optional)

CSSProperties object to add styles to the <img> element withing EssentialRectImg.

alt (optional)

Add an alt property to the <img> element. Default is empty string.

showIcon (optional)

Show the essentialRect icon. Default false.

1.4.0

2 years ago

1.3.0

3 years ago

1.2.1

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago