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

laziest-image

v1.0.2

Published

The image loader to give you full control over image load.

Downloads

23

Readme

Laziest image

The image loader to give you full control over image loading.

:heart: ...and reduce all annoying work, of course.

Benefits

Technical sweets

  • Uses native lazy loading when possible. And no, if not.
  • IntersectionObserver instead of scroll listener.
  • Renders a new src from background srcSet on resize only if the image is visible.
  • By default: loading="lazy", decoding="async" and content-visibility="auto".

Install

NPM:

npm i laziest-image

Yarn:

yarn add laziest-image

Getting started

LazyImage instead of img

It uses native lazy loading if the browser supports it. If not, custom lazy loading will be used.

import { LazyImage } from 'laziest-image'

export function MyComponent() {
  return (
    <LazyImage
      src="/cats.jpg"
      width="1000"
      height="500"
    />
  )
}

To force custom loading, just add the flag:

import { LazyImage } from 'laziest-image'

export function MyComponent() {
  return (
    <LazyImage
      src="/cats.jpg"
      width="1000"
      height="500"
      customLoading // <-- enables custom loading
    />
  )
}

LazyBackground

Backgrounds are loaded with custom loading by default.

import { LazyBackground } from 'laziest-image'

export function MyComponent() {
  return (
    <LazyBackground src="/cats.jpg">
      Your content
    </LazyBackground>
  )
}

srcSet and sizes

Just use it like inside <img>.

I'm serious, just use it. This will work natively and be controlled by the browser under the hood. The library simply updates backgroundImage when the browser changes current src due to srcSet and sizes.

import { LazyBackground } from 'laziest-image'

export function MyComponent() {
  return (
    <LazyBackground
      src="/cats.jpg"
      srcSet="/cats-300.jpg 300w, /cats-1000.jpg 1000w"
      sizes="(max-width: 800px) 300px, 1000px"
    >
      Your content
    </LazyBackground>
  )
}

Properties

You can use it to change the loading behavior.

  • customLoading. Enable the custom loading.

    • Default: false for LazyImage. true for LazyBackground.
  • afterPageLoad. Don't start loading until the entire page has loaded.

    • Example: if it's true and customLoading is also true, then the whole customLoading mechanism will only run after the page is loaded.
  • withoutBlank. Disable the transparent blank image when it's not already loaded and browser can show gray borders of the empty src.

    • Default: true for LazyBackground.
  • onLoad. Provide a callback function that will be called each time the image URL is loaded.

    • Example. The image has a srcSet. The function called. You then resize the browser window to the next breakpoint of sizes. The image changes url and the function called again.
  • onFirstLoad. Provide a callback function that will only be called the first time the image URL is loaded.

  • onSrcChange. Provide a callback function that will be called each time the image URL is loaded, except for the first load.

  • width and height. Specify them to prevent layout shift.

  • priority. Set it to the LCP image to load the image immediately.

...and all <img> tag properties for <LazyImage> and all <div> properties for <LazyBackground>.

The Custom Loading properties:

  • yOffset. Y-axis offset to start loading. % or px only. % is the percentage of the current viewport.

    • Example: yOffset="0px" to start loading only when the image has just "touched" the top or bottom of the screen.
    • Default: 200%.
  • xOffset. X-axis offset to start loading. Same as yOffset but horizontally.

    • Default: 50%.
  • withoutWatchingSrcChange. If false, the custom loaded image will change its src when the image URL changed due to srcSet and sizes.

    • Default: true for LazyImage, because the browser has native mechanism for changing src.
  • disabledPreload. If disabled, the image will not be preloaded before being set to src (or backgroundImage) and withoutWatchingSrcChange will be true.

    • Default: true for LazyImage.

Hook useLazyImage

This hook is used by the components under the hood. You can use it directly.

The components are like presets of properties.

All properties are false by default for the useLazyImage hook.

Hook useSrc

This is a great hook to help you subscribe and unsubscribe src updates from the useLazyImage hook.

const { src, loaded } = useSrc(useLazyImage(ref, props))

// Until the image is loaded,
// "loaded" will be false and
// "src" will be an empty string or with a blank (empty) image,
// depending on your properties.