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

@codewithkyle/lazy-loader

v1.1.0

Published

A lightweight Web Component based lazy loading library.

Downloads

3

Readme

Lazy Loader

A lightweight (~2kb) Web Component based lazy loading library.

Install

Install via NPM:

npm i -S @codewithkyle/lazy-loader

Or via CDN:

import { configure, update, mount, css } from "https://cdn.jsdelivr.net/npm/@codewithkyle/lazy-loader@1/lazy-loader.min.mjs";
<script src="https://cdn.jsdelivr.net/npm/@codewithkyle/lazy-loader@1/lazy-loader.min.js">

Usage

ES Module

import { configure, update, mount, css } from "https://cdn.jsdelivr.net/npm/@codewithkyle/lazy-loader@1/lazy-loader.min.mjs";

// Start the lazy loading process by configuring the default locations for your JS and CSS files
configure({
    jsDir: "/js",
    cssDir: "/css",
    default: "lazy", // optional
    lazierCSS: false, // optional
});

// Alternatively if the default settings (seen above) are okay you could simply call the update function instead
// You can call the update function at any time
update();

// Manually mount new components
import { MyCustomElement } from "./my-custom-element.js";
mount("my-custom-element", MyCustomElement); // returns a promise

// Alternatively if the components file name matches the tag name the library can dynamically import the script from the JS directory (see configure function)
mount("my-custom-element");

// Manually lazy load CSS files
css("exmaple.css"); // returns a promise

// Alternatively you can load multiple files at once
css(["example-one", "examle-two.css", "https://cdn.example.com/example-two.css", "../../relative-path-example.css"]);

Common JS

LazyLoader.configure({
    jsDir: "/",
    cssDir: "/",
    default: "lazy", // optional
    lazierCSS: false, // optional
});
LazyLoader.update();
LazyLoader.mount("my-custom-element")

Interfaces

type Loading = "eager" | "lazy";

interface LazyLoaderSettings {
    cssDir?: string;
    jsDir?: string;
    default?: Loading;
    lazierCSS: boolean;
};

declare const configure: (settings?:Partial<LazyLoaderSettings>) => void;
declare const update: () => void;
declare const mount: (tagName:string, constructor?: CustomElementConstructor) => Promise<void>;
declare const css: (files:string|string[]) => Promise<void>;

HTML Attributes

<!-- Lazy load Web Components by tagging custom elements with the web-component attribute. -->
<!-- In this example the custom-element.js file will be imported from the configured jsDir directory. -->
<custom-element web-component></custom-element>

<!-- You can override the default import behavior by providing a custom file name, relative path, or a URL. -->
<custom-element web-component="custom-file-name.js"></custom-element>

<!-- By default components are loaded and mounted when they enter the viewport. -->
<!-- You can bypass the lazy loader by using the loading attribute. -->
<custom-element web-component loading="eager"></custom-element>

<!-- You can lazy load CSS by attaching the css attribute to any element within the documents body. -->
<!-- You can load multiple files using a whitespace separator. Note that the .css file extenstion is optional. -->
<div class="my-awesome-class" css="awesome-transitions awesome.css"></div>