npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-image-loading

v0.2.2

Published

Load an image in React with loading placeholder and fallback

Downloads

314

Readme

React Image Loading

Load an image in React with loading placeholder and fallback, with maximum customizability.

Installation

npm i react-image-loading

Quick usage

This will render the out of the box implementation of the ImageLoading component, including a loading placeholder and fallback that have absolute positioning to fill the dimensions of the parent container. See below on how to implement custom rendering and behavior.

import { Img } from 'react-image-loading';

const MyComponent = props =>
    <div style={{minHeight: 150, position: 'relative'}}>
        <Img src={props.src} srcSet={props.srcSet} />
    </div>

Custom implementation

The out of the box Img provides a basic use case for loading images with a loading placeholder and fallback. To customize you can implement your own version of the Img component specific to your project needs. Below is an example that mimics the basic behavior of the provided Img component, but changes the color of the Fallback and LoadingPlaceholder:

import * as React from 'react';
import ImageLoading, { Fallback, LoadingPlaceholder } from 'react-image-loading';

const Img = (props) => (
    <ImageLoading>
        {(ref, status) => (
            <React.Fragment>
                {status === 'error' || !props.src
                    ? <Fallback style={{ backgroundColor: 'red'}} />
                    : <React.Fragment>
                        <img ref={ref} {...props} />
                        <LoadingPlaceholder
                            style={{ 
                                transition: 'opacity 0.5s', 
                                opacity: status === 'loading' ? 1 : 0, 
                                backgroundColor: 'blue' 
                            }}
                            animate={status === 'loading'}
                        />
                    </React.Fragment>
                }
            </React.Fragment>
        )}
    </ImageLoading>
);

export default Img;

You can also choose to create your own implementations of the Fallback and LoadingPlaceholder, or implement an entirely different logic altogether. Note: rendering the image tag with the provided ref is what triggers the loading of the image.

API

<ImageLoading>

Props

RenderPropsFn

Type: Function

Params

Returns

React.ReactNode

Ref

Type: Function

Params

  • imageElement HTMLImageElement | null

Returns

void

LoadState

The loading state of the image. Will always start at "loading", even before first render of the image, so it will immediately be put into loading state.

Type: 'loading' | 'complete' | 'error'

<LoadingPlaceholder>

Props

  • style React.CSSProperties?
  • animationStyle React.CSSProperties?
  • animationDuration number? Loop duration in ms
  • animate boolean?

<Fallback>

Props

  • style React.CSSProperties?