Code Monkey home page Code Monkey logo

react-fitted-image's Introduction

Total Downloads Latest Release License

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

Property Type Description Default value Required
background bool Force the component to use the CSS bacground properties false no
className string Custom classname for the component _ no
fit string Value of the object-fit property. Can be "auto", "contain", or "cover" "auto" no
loader element Component to use as loader _ no
onLoad function Success callback for image loading _ no
onError function Error callback for image loading _ no
src string Image url to render _ yes
style object Custom styles {} no

Example

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

react-fitted-image's People

Contributors

alexjoffroy avatar cshawaus avatar fabsolute avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.