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-lazily-img

v1.0.71

Published

**React Lazily IMG** is a React Wrapper Component to lazily load images. The goal is to use the convenient and known standard HTML tags and just have them lazily loaded.

Downloads

5

Readme

React Lazily IMG

React Lazily IMG is a React Wrapper Component to lazily load images. The goal is to use the convenient and known standard HTML tags and just have them lazily loaded.

Features

  • Picture tag and IMG srcset support
  • Webp detection
  • Placeholder
  • HTML && CSS image support
  • First render when image is downloaded
  • Customize percentage of visibility
  • Custom Callback when image is rendered

Installation

npm install react-lazily-img --save

Demo

Coming soon.

How does it work ?

React Lazily IMG is powered by the performant Intersection Observer API. No need of annoying unperformant scroll event listeners and elem.getBoundingClientRect() to check if an element is in the viewport.

MDN:

The Intersection Observer API provides a way to asynchronously observe changes in the intersection of a target element with an ancestor element or with a top-level document's viewport.

Documentation

Code examples

Picture tag

import React from 'react';

// import React Lazily IMG
import Lazy from 'react-lazily-img';

// import images
import Image1 from './images/1.jpg';
import Image1WebP from './images/1.webp';

import Image2 from './images/2.jpg';
import Image2WebP from './images/2.webp';

import Image3 from './images/3.jpg';
import Image3WebP from './images/3.webp';

import Placeholder from './images/placeholder.jpg';
import PlaceholderWebp from './images/placeholder.webp';

// You dont need to set webp to true when working with a picture tag as its has a webp detection on its own. 

const options = {
  waitComplete: false, 
  webp: false
};

const App (props) => (
  <div className="App">
    <Lazy {...options}>
      <picture>
        <source type="image/webp" media="(min-width:600px)" data-srcset={Image1WebP} />
        <source type="image/webp" media="(min-width:500px)" data-srcset={Image2WebP} />
        <source media="(min-width:600px)" data-srcset={Image1} />
        <source media="(min-width:500px)" data-srcset={Image2} />
        <img data-src={Image3} data-webpsrc={Image3WebP} alt="butterfly" />
      </picture>
    </Lazy>
  </div>
);

IMG srcset

<Lazy {...options}>
  <img 
    data-srcset={`
      ${Image1} 320w,
      ${Image3} 480w,
      ${Image2} 800w`}
    data-webpsrcset={`
      ${Image1WebP} 320w,
      ${Image3WebP} 480w,
      ${Image2WebP} 800w`}
    sizes="
      (max-width: 320px) 280px,
      (max-width: 480px) 440px,
      800px"
    alt="butterfly"
    data-src={Image1}
    data-webpsrc={Image1WebP}
  /> 
</Lazy>

IMG tag

<Lazy {...options}>
  // data-webpsrc and the optional data-webpplaceholder are only needed 
  // if you enable webp detection in the options

  // placeholder are optional. Image that is shown until the image is in the viewport

  <img data-placeholder={Placeholder} data-webpplaceholder={PlaceholderWebp} data-src={Image1} data-webpsrc={Image1WebP} alt="butterfly" />
</Lazy>

DIV tag - CSS background

<Lazy {...options}>
  <div data-src={Image1} data-webpsrc={Image1WebP} />
</Lazy>

Placeholder

A placeholder is an image that is shown until the image is in the viewport. data-webpsrc and the optional data-webpplaceholder are only needed if you enable webp detection in the options.

<Lazy {...options}>
  <img data-placeholder={Placeholder} data-webpplaceholder={PlaceholderWebp} data-src={Image1} data-webpsrc={Image1WebP} alt="butterfly" />
</Lazy>

Multiple images in one wrapper

You can also place multiple images in one wrapper

<Lazy {...options}>
  <img data-src={Image1} alt="butterfly" />
  <div data-src={Image1} />
  <picture>
    <source media="(min-width:600px)" data-srcset={Image1} />
    <source media="(min-width:500px)" data-srcset={Image2} />
    <img data-src={Image3} alt="butterfly" />
  </picture>
</Lazy>

options

|Option|Description|value|default| |:-----|:-----|:-----|:-----| |waitComplete|Wait until the image is fully downloaded before rendering. Doesn't yet support the picture tag.|bool|true| |webp|Ship a webp version if Browser supports it. No need to enable it when working with picture tag as it has its own detection (type="image/webp" - see code examples.). You need to pass the webp version of your picture in data-webpsrc={Image}.|bool|false| |hideTillRender|Hides the image until its rendered as you would otherwise see the alt tag.|bool|true| |clearAttribute|Clear the data-attributes you used to pass the image after its rendered.|bool|true| |callback|Takes a custom callback thats executed after the image is rendered.|func|null| |root|The element that is used as the viewport to check the visiblity of a target.|elem|browser viewport| |rootMargin|Similair to the CSS 'margin' property. It manipulates the elements bounding box. Same syntax as in CSS with either an absolute length or a percentage.|px || %|0px 0px 0px 0px| |threshold|Indicate at what percentage of the target's visibility the observer's callback should be executed. i.e at an visibility above 50%: 0.5|num (0 -> 1)|0|

Example

const options = {
  waitComplete: true, 
  webp: true
  hideTillRender: true,
  clearAttributes: true,
  callback: () => console.log("Rendered!"),
  root: document.querySelector('#scrollArea'),
  rootMargin: '50px 0px',
  threshold: 0.75
};

<Lazy {...options}>
  // image
</Lazy>

Polyfill

For the full support IntersectionObserver polyfill is used.