Skip to content

hiddentao/react-image-holder

Repository files navigation

react-image-holder

Build Status

An <img /> component for React which can render real images as well use an offline placeholder as a fallback if need be.

All placeholders are rendered using holder.js, meaning they will work even when offline. You can even configure the component to always render a placeholder for all its instances.

View demo

Installation

$ npm install react-image-holder

## Examples

All examples assume you have require()-d the component:

var Img = require('react-image-holder');

A real image

var realImage = (
  <Img src="http://animalia-life.com/data_images/cat/cat5.jpg" />
);

Output:

<img src="http://animalia-life.com/data_images/cat/cat5.jpg" />

Additional attributes

var realImage = (
  <Img src="http://animalia-life.com/data_images/cat/cat5.jpg" 
    width="802"
    height="450"
    className="cat-photo" />
);

Output:

<img width="802" height="450" class="cat-photo" src="http://animalia-life.com/data_images/cat/cat5.jpg" />

Use a placeholder instead

var realImage = (
  <Img src="http://animalia-life.com/data_images/cat/cat5.jpg" 
    width="802"
    height="450"
    className="cat-photo" 
    usePlaceholder={true}
    />
);

Output:

<img class="cat-photo" data-src="holder.js/802x450?auto=true&theme=vine" />

Holder.js then processes the above tag and renders a placeholder.

Setting placeholder properties

var placeholderProps = {
  theme: 'blue',
  size: 50,
  auto: false,
};

var realImage = (
  <Img src="http://animalia-life.com/data_images/cat/cat5.jpg" 
    width="802"
    height="450"
    usePlaceholder={true}
    placeholder={placeholderProps}
    />
);

Output:

<img data-src="holder.js/802x450?auto=false&size=50&theme=blue" />

See holder.js property list for all available properties.

Global placeholders

Rather than passing the usePlaceholder property to each instance of the component you can also set this property's default value at the start of your app:

Img.DEFAULT_PROPS.usePlaceholder = true;

Now all subsequent instances of the component will automatically use a placeholder image. And you can still override the setting on per-instance basis!

Building

$ npm install
$ npm run build

Contributing

Contributions are welcome! Please see CONTRIBUTING.md.

License

MIT - see LICENSE.md

About

React image component with offline placeholder fallbacks

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published