1.0.1 • Published 5 years ago

react-fitted-image v1.0.1

Weekly downloads
465
License
MIT
Repository
github
Last release
5 years ago

react-fitted-image

Pictures are always difficult to handle in web page, especially in a responsive way. A well-known workaround is to style the img tag with something like this:

img {
  max-width: 100%;
  height: auto;
}

This method works well, but there is a lot of cases where it's not sufficient. Fortunately CSS introduced some times ago the "object-fit" property, improving the way we can handle image displaying. The current component provides an easy react binding to display images with object-fit property.

In addition, you can provide a loader which will be displayed during the image processing by the browser.

Object-fit is well supported by browsers but not all of them for now ( see Caniuse ). To work on stuffs like IE, a basic test is done with CSS.supports to provide a fallback based on "background" CSS property.

Demo

http://alexjoffroy.github.io/react-fitted-image/example/

Usage

Install

npm install --save react-fitted-image

Properties

PropertyTypeDescriptionDefault valueRequired
backgroundboolForce the component to use the CSS bacground propertiesfalseno
classNamestringCustom classname for the component_no
fitstringValue of the object-fit property. Can be "auto", "contain", or "cover""auto"no
loaderelementComponent to use as loader_no
onLoadfunctionSuccess callback for image loading_no
onErrorfunctionError callback for image loading_no
srcstringImage url to render_yes
styleobjectCustom styles{}no

Example

<FittedImage
  fit="contain"
  loader={<div>Loading</div>}
  onLoad={(...args) => console.log(...args)}
  onError={(...args) => console.log(...args)}
  src="public/img.jpg"
/>