react-lazy-img-observer
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 β€’ Public β€’ Published

πŸ“· ImageLazy

ImageLazy is a lightweight React component for lazy loading images using the Intersection Observer API. It delays the loading of off-screen images until they appear in the viewport, helping optimize performance and reduce initial load times.

npm
license


πŸ“¦ Installation

npm install react-lazy-img-observer

πŸš€ Usage

Basic example (JSX or TSX)

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

const Example = () => (
  <ImageLazy
    src="https://example.com/image.jpg"
    alt="Description of the image"
    width={600}
    height={400}
    className="custom-class"
    id="image-1"
    title="Image title"
    extraData={{ "data-custom": "value" }}
  />
);

🧩 Props

Prop Type Required Description
src string βœ… The source URL of the image.
alt string βœ… The alt text for accessibility.
width number ❌ The width of the image.
height number ❌ The height of the image.
id string | number ❌ Optional ID for the image element.
className string ❌ CSS class for custom styling.
title string ❌ Tooltip text shown on hover.
extraData React.ImgHTMLAttributes<HTMLImageElement> ❌ Any extra HTML attributes (e.g., data-*).

πŸ“š How It Works

The component uses the IntersectionObserver API to detect when the image enters the viewport. Once at least 50% of the image is visible, the real src is assigned to the image, triggering the browser to load it.


βš™οΈ Intersection Observer Configuration

  • root: null (default viewport)
  • rootMargin: "0px"
  • threshold: 0.5 (50% visibility required)

🎨 Styling

The image starts blurred and transitions smoothly once it’s loaded. You can override or enhance this with your own styles:

.custom-class {
  filter: blur(20px);
  transition: filter 0.9s ease;
}

.custom-class.loaded {
  filter: none;
}

You may also override the style prop directly if preferred.


πŸ“„ License

ISC License


🀝 Contributing

Contributions are welcome!
Feel free to open an issue or submit a pull request.


πŸ‘€ Author

Percy Chuzon
πŸ“§ contacto@percychuzon.com
🌐 https://percychuzon.com


πŸ™ Acknowledgments

Thanks to the React community and the MDN docs for inspiration and guidance.

Package Sidebar

Install

npm i react-lazy-img-observer

Weekly Downloads

56

Version

1.1.1

License

MIT

Unpacked Size

11.6 kB

Total Files

8

Last publish

Collaborators

  • perch33