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 🙏

© 2025 – Pkg Stats / Ryan Hefner

smart-lazy-loader

v1.3.0

Published

Framework-agnostic smart lazy loading for images, components, and scripts.

Readme

smart-lazy-loader

A simple and flexible lazy load utility that works in various event-driven scenarios, such as visibility, idle, click, and mousemove. Optimized for performance and can be easily integrated into any JavaScript or TypeScript project.

npm version license bundle size types coverage semantic release stars

✨ Features

  • Framework agnostic: Works with any JavaScript framework, or vanilla JavaScript.
  • 🧠 Event-based loading: Load components or execute code based on multiple events:
    • visible (IntersectionObserver)
    • idle (using requestIdleCallback or setTimeout)
    • delay (with customizable delay)
    • element-event (triggered by existing or custom events on any DOM element)
  • 📦 Batch support: Load multiple modules in parallel with a single or multiple triggers.
  • 🛠️ Full control: Exposes a controller with trigger, cancel, and hasLoaded for more flexibility.
  • 🎯 Tiny, tree-shakable, no dependencies
  • 💡 Written in TypeScript with full type support

📦 Installation

To install the library, run the following command:

# via yarn
yarn add smart-lazy-loader

# or npm
npm install smart-lazy-loader

🔧 Usage

To use the lazyLoad function, simply pass a dynamic import function (or a function returning a promise) and the appropriate options.

Example: Lazy loading a component when it is visible

import { lazyLoad } from 'smart-lazy-loader';

const MyComponent = () => import('./MyComponent');

const target = document.getElementById('component-target') as HTMLElement;

const controller = lazyLoad(MyComponent, {
  on: 'visible',
  target,
  rootMargin: '100px',
  threshold: 0.5,
});

// Trigger manually
controller.trigger();

// Check if the component has already loaded
console.log(controller.hasLoaded); // false (initially)

// Cancel lazy loading (if it hasn't already triggered)
controller.cancel();

Example: Lazy loading when a custom event is triggered on an element

import { lazyLoad } from 'smart-lazy-loader';

const MyComponent = () => import('./MyComponent');

const target = document.getElementById('custom-event-target') as HTMLElement;

const controller = lazyLoad(MyComponent, {
  on: 'element-event', // Trigger lazy load on custom event
  eventName: 'customEvent', // Custom event name
  target,
});

// Dispatch the custom event
const event = new CustomEvent('customEvent');
target.dispatchEvent(event);

Example: Lazy loading multiple modules (batch)

import { lazyLoad } from 'smart-lazy-loader';

const loadA = () => import('./ComponentA');
const loadB = () => import('./ComponentB');

const target = document.getElementById('batch-target') as HTMLElement;

lazyLoad([loadA, loadB], {
  on: 'idle',
  target,
});

Example: Lazy loading with multiple triggers

import { lazyLoad } from 'smart-lazy-loader';

const MyComponent = () => import('./MyComponent');

const target = document.getElementById('batch-target') as HTMLElement;

lazyLoad(MyComponent, [
  {
    on: 'element-event',
    eventName: 'click',
    target,
    eventOptions: { once: true },
  },
  {
    on: 'element-event',
    eventName: 'mousemove',
    target,
    eventOptions: { once: true },
  },
]);

🛠 Configuration Options

  • on (Required)

    • visible - Use IntersectionObserver to load when the element is visible in the viewport.
    • idle - Load when the browser is idle (either via requestIdleCallback or setTimeout fallback).
    • delay - Load after a specified delay in milliseconds.
    • element-event - Load when a custom event is triggered on a target element (e.g., click, customEvent, etc.).
  • target (Required)

    • The DOM element that you want to observe or attach the event to.
  • delay (Optional)

    • The delay in milliseconds before loading the component (used when on is set to 'delay').
  • rootMargin (Optional)

    • Used with 'visible' event type. The margin around the root element to trigger the lazy load earlier or later. Default is '0'.
  • threshold (Optional)

    • The percentage of the target element that must be visible before it’s considered in view. A value between 0 and 1, with 0.5 representing 50% visibility.
  • eventName (Required for element-event)

    • The name of the custom event to trigger lazy loading (e.g., click, customEvent, etc.).
  • eventOptions (Optional for element-event)

🤝 Contributing

Contributions are welcome! If you have suggestions or improvements, please create an issue or submit a pull request.