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

v1.0.1

Published

A React component for lazy loading images with Intersection Observer

Downloads

9

Readme

ImageLazy Component

The ImageLazy component is a React component designed for lazy loading images. It leverages the Intersection Observer API to load images only when they are within the viewport, optimizing the loading performance of your web application.

Installation

npm install react-lazy-img-observer

Usage

import ImageLazy from "react-lazy-img-observer";

const Example = () => {
  return (
    <ImageLazy
      src="path/to/your/image.jpg"
      alt="Description of the image"
      width={600}
      height={400}
      className="your-class-name"
      id="unique-image-id"
      title="Image Title"
      extraData={{ "data-custom-attribute": "value" }}
    />
  );
};

Props

The ImageLazy component accepts the following props:

| Prop | Type | Default | Description | | ----------- | ------------------------------------------- | ------- | --------------------------------------------------- | | src | string | - | The source URL of the image. | | alt | string | - | The alt text for the image. | | width | number | - | The width of the image. | | height | number | - | The height of the image. | | id | number \| string | - | The ID of the image element. | | className | string | - | Additional class names for the image element. | | title | string | - | The title attribute for the image element. | | extraData | React.ImgHTMLAttributes<HTMLImageElement> | - | Additional attributes to pass to the image element. |

Example

import React from 'react';
import ImageLazy from 'your-library-name';

const App = () => {
  return (
    <div>
      <h1>Lazy Loaded Images</h1>
      <ImageLazy
        src="https://example.com/image1.jpg"
        alt="Image 1"
        width={600}
        height={400}
        className="image-class"
        id="image1"
        title="First Image"
      />
      <ImageLazy
        src="https://example.com/image2.jpg"
        alt="Image 2"
        width={600}
        height={400}
        className="image-class"
        id="image2"
        title="Second Image"
      />
    </div>
  );
};

export default App;

How it works

The ImageLazy component uses the IntersectionObserver API to detect when the image enters the viewport. When the image is about to enter the viewport (50% visibility by default), the component sets the src attribute of the image element to load the actual image.

Intersection Observer Options

  • root: The element that is used as the viewport for checking visibility of the target. Defaults to null (the browser viewport).
  • rootMargin: Margin around the root. Can be used to grow or shrink the area used for intersection. Defaults to "0px".
  • threshold: A single number or an array of numbers which indicate at what percentage of the target's visibility the observer's callback should be executed. Defaults to 0.5.

Styling

The ImageLazy component applies a blur filter to the image until it is fully loaded. You can customize the transition effect and blur amount through the style prop or CSS class.

.image-class {
  filter: blur(10px);
  transition: filter 0.9s;
}

.image-class[data-src] {
  filter: none;
}

License

MIT

Contributing

Contributions are welcome! Please open an issue or submit a pull request for any bugs or feature requests.

Author

Percy Chuzon

Acknowledgments

Thanks to the React and Intersection Observer API documentation for guidance.