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

@wbe/react-image

v2.5.0

Published

react image component

Downloads

5

Readme

@wbe/react-image

React image component with lazyloading management.

Installation

$ npm install -s @wbe/react-image

<Image /> component

import { Image } from "@wbe/react-image";

src props

Be sure you set appropriated css image width and height.

<Image src={`https://image-url`} alt={"src image"} />

Will return this HTML before lazyload:

<img
  class="Image lazyload"
  alt="src image"
  src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=="
  data-src="https://image-url"
/>

and after lazyload:

<img
  class="Image lazyloaded"
  alt="src image"
  src="https://image-url"
  data-src="https://image-url"
/>

srcset props

<Image
  srcset={`https://image-url 360w, https://image-url-2 768w`}
  alt={"srcset image"}
/>

data props

Will generate an appropriated srcset string.

const data = [
  {
    url: "https://url",
    width: 600,
    height: 400,
  },
  {
    url: "https://url-2",
    width: 1000,
    height: 800,
  },
];

<Image data={data} alt={"srcset image"} />;

srcPlaceholder props

Allow to display specific low quality image before lazyloading src image.

<Image
  srcPlaceholder={`https://low-quality-image-url`}
  src={`https://image-url`}
  alt={""}
/>

Props list

interface IProps {
  // image to display before lazyload
  // default is lightest base64 transparent image
  srcPlaceholder?: string;

  // src URL to lazyload
  src?: string;

  // srcset URL to lazyload
  srcset?: string;

  // list of images with dimension used to build srcset attr
  data?: TImageData[];

  // callback when lazyload state change (lazyload | lazyloading | lazyloaded)
  lazyCallback?: (lazyState: TLazy) => void;

  // intersection observer options
  observerOptions?: IntersectionObserverInit;

  // alt attr and aria html
  alt: string;
  ariaLabel?: string;

  // class name added on root element
  className?: string;

  // style attrs
  style?: CSSProperties;
  width?: number | string;
  height?: number | string;
}

<BackgroundImage />

Works this the same API than <Image /> component.

Before lazyload:

<div
  class="BackgroundImage lazyload"
  style="background-image: url('data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==')"
  data-background-srcset="img-1.jpg 360w, img-2.jpg 768w"
/>

After lazyload:

<div
  class="BackgroundImage lazyloaded"
  style="background-image: url('img-1.jpg')"
  data-background-srcset="img-1.jpg 360w, img-2.jpg 768w"
/>

<Placeholder /> wrapper component

Image component comes with <Placeholder /> parent component used to display a placeholder behind Image. This process is particulary useful when image is lazyloading. It allows to not preload all 1px height images in pages on page load but really waiting the image was shown in viewport to lazyload it.

Example with <Image />:

<Placeholder>
    <Image
      src={`https://image-url`}
      width={600}
      height={400}
      alt={"with placeholder"}
    />
<Placeholder/>

Example with <BackgroundImage />:

<Placeholder>
    <BackgroundImage
      src={`https://image-url`}
      // or srcset={`https://image-url 360w, https://image-url 768w`}
      width={600}
      height={400}
      alt={"with placeholder"}
    />
<Placeholder/>

<Placeholder /> get children Image ratio to calculate its placeholder height. It's possible to specify an arbitrary ratio who will override image dimension

<Placeholder ratio={3/4}>
    <Image
      src={`https://image-url`}
      alt={"with placeholder"}
    />
<Placeholder />

Props list

interface IProps {
  // Image component
  children: ReactNode;

  // by default, ratio is calc from children image data dimension.
  // set ratio override native image ratio
  ratio?: number;

  // shortcut to style wrapper background color
  backgroundColor?: string;

  // add style to each dom element
  style?: {
    root?: CSSProperties;
    wrapper?: CSSProperties;
    img?: CSSProperties;
  };

  // class name added on root element
  className?: string;
}

Returns