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-responsive-image

v1.6.0

Published

React responsive image with additional options.

Downloads

67

Readme

@wbe/react-responsive-image

react-responsive-image is a React component allowing to display a simple <img /> tag or a <div /> background-image who will set the appropriate thumbnail in src or url attribute, according to a defined width.

We should never choose how to show an image based on stylistic rendering. This is why this component makes the same rendering options possible, regardless of the sementic choice; Tag HTML image or background image.

This component is a wrapper DOM rendering of @wbe/use-responsive-image-data.

Installation

$ npm install -s @wbe/react-responsive-image

How to use

Basic usage return a responsive image with <img /> tag:

import ResponsiveImage from "@wbe/react-responsive-image";

const thumbs = [
  {
    url: "my/image/1.jpg",
    width: 640,
    height: 480
  },
  {
    url: "my/image/2.jpg",
    width: 1024,
    height: 800
  }
];
const App = () => {
  return <ResponsiveImage type={EImageType.TAG_IMAGE} data={thumbs} />;
};

Will returned this HTML DOM structure:

<img
  class="ResponsiveImage ResponsiveImage-tagImage"
  src="my/image/1.jpg"
  role="img"
/>

Options

Placeholder

A placeholder can be set behind the image via placeholder props. Placeholder ratio size is calc via image dimensions and allow to define an image wrapper who take image dimension.

<ResponsiveImage type={EImageType.TAG_IMAGE} data={thumbs} placeholder={true} />

A background-color can be set to this placeholder via placeholderColor props.

<ResponsiveImage
  type={EImageType.IMAGE_TAG}
  data={thumbs}
  placeholder={true}
  placeholderColor={"#EEE"}
/>

To set a placeholder, DOM structure need to returned image with additional wrapper.

<div class="ResponsiveImage ResponsiveImage-tagImage ...">
  <!-- wrapper used to set a padding bottom ratio -->
  <div class="ResponsiveImage_wrapper" style="...">
    <img class="ResponsiveImage_image" role="img" src="..." style="..." />
  </div>
</div>

Note: if lazy props is set, placeholder is automaticaly enable with default transparent placeholder color.

WARNING: object-fit: cover is used to get the same effect than background-size: cover for background-image. Check can i use compatibility.

Lazyload

ResponsiveImage as lazyload option. A small transparent base64 image will set in src util the image is visible in window.

<ResponsiveImage type={EImageType.TAG_IMAGE} data={thumbs} lazy={true} />

You can prevent image loading with lazyOffset props. example: -40 allow to preload image when it's top or bottom is to 40 pixels before or after border window.

<ResponsiveImage
  type={EImageType.TAG_IMAGE}
  data={thumbs}
  lazy={true}
  lazyOffset={-40}
/>

Before the element is visible in viewport, ResponsiveImage will returned:

<div class="ResponsiveImage ResponsiveImage-tagImage ResponsiveImage-lazyload">
  <div class="ResponsiveImage_wrapper">
    <img
      class="ResponsiveImage_image"
      src="data:image/png;base64..."
      role="img"
    />
  </div>
</div>

Ratio

Image ratio is automatically calc via image dimension, but it's possible to force a custom vertical ratio via forceVerticalRatio props.

<ResponsiveImage
  type={EImageType.TAG_IMAGE}
  data={thumbs}
  placeholder={true}
  placeholderColor={"#EEE"}
  forceVerticalRatio={1.4}
/>

Tab props

| props (* non optional) | type | description | default value | | ----------------------- | ------------- | ------------------------------------------------------------------------------- | ------------- | | classNames | string[] | class list | / | | type * | EImageType | TAG_IMAGE / BACKGROUND_IMAGE | / | | data * | IImage[] | image object array | / | | alt | string | image alt attribute | / | | children | ReactNode | add children only if type is EImageType.BACKGROUND_IMAGE | / | | lazy | boolean | active lazyloading | false | | lazyOffset | number | load image at X pixel(s) of top/bottom window | 0 | | lazyCallBack | boolean | execute function when image is loaded | / | | forceImageWidth | number | force to display the image whose size is closest to the value provided in pixel | / | | forceVerticalRatio | number | force a custom vertical ratio to the image | / | | placeholder | boolean | show a placeholder behind the image calc with image dimension | false | | placeholderColor | string | set a background color to the placeholder | transparent | | rootStyle | CSSProperties | style first child dom node | / | | imageStyle | CSSProperties | style image dom node | / | | ariaLabel | string / null | aria description of element | / | | role | string | role of element | img |