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-lazy-props

v1.0.2

Published

Lazyload component props with IntersectionObserver API

Downloads

35

Readme

react-lazy-props  

LazyLoad media elements with react-lazy-props and IntersectionObserver

Installation

npm i react-lazy-props

Usage

Wrap the elements you want to lazyload with LazyProps component
It will unload media before components were mounted and will load them only when they are observed.

Example:

import LazyProps from "react-lazy-props";
...
return (
 <LazyProps>
  <div>
   <h2 style={{backgroundImage: `url("${backgroundImage}")`}}>Title</h2>
   <p>Some Images</p>
   <img src="example.jpg" srcSet={"example2x.jpg 2x", "example3x.jpg 3x"} />
   <video src="https://www.example.com/video.ogg" />
  </div>
 </LazyProps>
);

Unloading process:

LazyProps renders its child components with props and styles replaced according to the map:

  • src to data-src
  • srcSet to data-srcset
  • backgroundImage to data-bg-src

HTML Output (Unloaded):

<div>
 <h2 data-bg-src="url('background-image.png')">Title</h2>
 <p>Some Images</p>
 <img data-src="example.jpg" data-srcset="example2x.jpg 2x, example3x.jpg 3x" />
 <video data-src="https://www.example.com/video.ogg" />
</div>

Loading process:

LazyProps component observes NodeList with Unloaded Prop Keys using IntersectionObserverAPI.
As soon as at least 1/10 of Node element is intersected, element props will gain thier initial key.

HTML Output (Loaded):

<div>
 <h2 style="background-image: url('background-image.png')">Title</h2>
 <p>Some Images</p>
 <img src="example.jpg" srcset="example2x.jpg 2x, example3x.jpg 3x" />
 <video src="https://www.example.com/video.ogg" />
</div>

Component Props

children (undefined)

NOTE! Always wrap child components as LazyProps component returns its children unwrapped.

onElementLoad (function)

Arguments:

  • element - Node Object

Function will be invoked when one of the child elements is intersected.

import LazyProps from "react-lazy-props";
...
 doSomething(element){
   element.classList.add("intersected");
 }
}
...
render {
 return (
  <LazyProps onElementLoad={element => this.doSomething(element)}>
...

onUnloadProps (function)

Arguments:

  • props - Object
  • componentType - String

Function will be invoked after component props were unloaded, this way you will be able to customize unloaded props of the element, for example setup a placeholder src to images:

import LazyProps from "react-lazy-props";
...
 setPlaceholder(props, componentType){
  if(componentType === "img"){
   props.src = this.state.placeholder;
  }
 }
 return props;
}
...
render {
 return (
  <LazyProps onUnloadProps={(props, componentType)=> this.setPlaceholder(props, componentType)}>
...

NOTE! The function must always return props object.

unloaded (boolean)

Default: false

If set to true elements wont be unloaded, but observer still will be there, this approach will be more efficient because we will not have to loop through child elements and components unloading their props.
The differecne is that you will have to set lazy props yourself:

import LazyProps from "react-lazy-props";
...
return (
 <LazyProps unloaded={true}>
  <div>
   <h2 data-bg-src={backgroundImage}>Title</h2>
   <p>Some Images</p>
   <img data-src="example.jpg" />
   <video data-src="https://www.example.com/video.ogg" />
  </div>
 </LazyProps>
);

IntersectionObserver polyfill

Since this package is observing elements with IntersectionObserver, you may want old browsers to support it with IntersectionObserver polyfill

Credits