🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

fancy-image-loader

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fancy-image-loader

A generic React component to show a placeholder in place of an image while the image is loading and replace the placeholder with the image when the image has loaded.

0.0.7
latest
Source
npm
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

fancy-image-loader NPM version

A generic React component to show a placeholder in place of an image while the image is loading and replace the placeholder with the image when the image has loaded.

Features:

  • Placeholder can be another image or an HTML element
  • You can style the placeholder/error element
  • All attributes of tag are supported

Context

Most of the modern websites use images in some form or the other in their content. For example, e- commerce websites commonly have pages showing a list of products with the products’ images.

Whenever you navigate to a web page that has one or more images in it, the browser tries to load those images. And loading an image may take time depending upon the size of the image and the network of the user. So sometimes images can take a lot of time to load. And usually how browsers behave is that until the image has loaded, the browser would have nothing to show. This is generally considered a bad user-experience because the user does not know if something is expected in that content area but as soon as the image loads, it suddenly appears. So it is considered a good practice to show a placeholder until the image has loaded. Facebook takes this practice to another level by showing placeholder for the entire page until the content has loaded.

Getting Started

npm i fancy-image-loader 

or

npm install fancy-image-loader 

Usage - Example 1

import React from 'react';
import {render} from 'react-dom';
import FancyImageLoader from 'fancy-image-loader';

let placeholder = () => {
    return <img src="file/path/loading.gif"/>
}

let error = () => {
    return <img src="file/path/error.png"/>
}

let imageItem = "https://images.alphacoders.com/151/151291.jpg";

render(
  <FancyImageLoader 
        src={item}
        placeholder={placeholder}
        error ={error}
        class="img"
        alt = "Awesome image dude"
        style = {{borderRadius : '9px'}}
        height="200px"
        width="300px"></FancyImageLoader>
, document.getElementById('root'));

Usage - Example 2 (HTML Placeholer)

import React from 'react';
import {render} from 'react-dom';
import FancyImageLoader from 'fancy-image-loader';

let itemHtmlStyle = {
    display: "flex",
    justifyContent: "center",
    height: "100%",
    alignItems: "center",
    textAlign: "center",
    background: "azure"
}

let placeholder = function () {
    return  <img src="http://www.tbaf.org.tw/event/2016safe/imgs/loader1.gif" style={itemStyle}/>;
}

let placeholderHtml = function () {
return <div style={itemHtmlStyle}>
            Loading image please wait
        </div>  
}

let errorHtml = function () {
return <div style={itemHtmlStyle}>
            Sorry image failed
        </div>  
}

let imageItem = "https://images.alphacoders.com/151/151291.jpg";

render(
  <FancyImageLoader 
        src={item}
        placeholder={placeholderHtml}
        error ={errorHtml}
        class="fancy-image"
        alt = "Awesome image dude"
        style = {{borderRadius : '9px'}}></FancyImageLoader>
, document.getElementById('root'));

Props

NameRequiredTypeDescription
srcYesstringSource path of the image to be loaded + displayed
placeholderYesfunctionA Placeholder function which returns simple react element html/image to be dispayed before image loads/loading
errorNofunctionAn optional error function which returns simple HTML/image to be displayed when image failed to load. If error doesn't provided then placeholder will stay in place.
classNostringcss class property for the image element
widthNostringAn optioal width attribute string 100px, 200px etc
heightNostringHeight attribute string 100px, 200px etc
altNostringAlternative text content
styleNoobjectStyle object containing css stuff for the image component.
borderNostringStandered border styling - 0px solid etc.

Author

Krishcdbry [krishcdbry@gmail.com]

Demo

Click here [https://krishcdbry.github.io/fancy-image-loader/demo/]

Licence

MIT @krishcdbry

FAQs

Package last updated on 09 Feb 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts