3.2.3 • Published 1 month ago

loadable-image v3.2.3

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

loadable-image

Storybook npm version npm downloads npm bundle size Stars Twitter

React Component to lazy load images.

Edit loadable-image

Installation

npm i loadable-image

Or via yarn

yarn add loadable-image

Usage examples

<AsyncImage /> accepts all props of <img /> tag.

import { AsyncImage } from 'loadable-image'
...
<AsyncImage
  src='https://picsum.photos/1900'
  style={{ width: 150, height: 150 }}
/>

Custom Loader/Error

You can pass your own loader and error components as props to AsyncImage component.

import { AsyncImage } from 'loadable-image'
...
<AsyncImage
  src='https://picsum.photos/1900'
  style={{ width: 150, height: 150 }}
  loader={<div style={{ background: '#888' }}/>}
  error={<div style={{ background: '#eee' }}/>}
/>

Modern formats (WebP, Avif) with fallback

Since under the hood <AsyncImage /> is just a picture element. You can pass an array of different Sources as a prop. And browser will pick the first one that it supports.

import { AsyncImage } from 'loadable-image'
...
<AsyncImage
  src="./image.jpg"
  sources={[ 
      { type:"image/avif",  srcSet:"./image.avif" }, 
      { type:"image/webp",  srcSet:"./image.webp" } 
  ]}
  style={{ width: 200, height: 200 }}
/>

Responsive image

To make image responsive you can use aspectRatio property in style prop. This way you can specify only width or height as 100% and the other one as auto. Note that if you support older browsers you might need to use aspectRatio padding-hack.

import { AsyncImage } from 'loadable-image'
...
<AsyncImage
  src='https://picsum.photos/1900'
  style={{ width: "100%", height: "auto", aspectRatio: 16 / 9 }}
/>

Custom Transitions

Under the hood AsyncImage uses transitions-kit library it's a collection Transition components built on top of react-transition-group its a small library maintained by React team for animating between different views. You can pass your own Transition as a prop to AsyncImage component.

Fade transition

By default AsyncImage uses Fade transition.

import { Fade } from 'transitions-kit'
import { AsyncImage } from 'loadable-image'
...
<AsyncImage
  src='https://picsum.photos/1900'
  style={{ width: 150, height: 150 }}
  loader={<div style={{ background: '#888' }}/>}
  Transition={Fade}
/>

Blur transition

Here is how you can use Blur transition and specify custom radius of the blur.

import { Blur } from 'transitions-kit'
import { AsyncImage } from 'loadable-image'
...
<AsyncImage
  src='https://picsum.photos/1900'
  style={{ width: 150, height: 150 }}
  Transition={props => <Blur radius={10} {...props}/>}
  loader={<div style={{ background: '#888' }}/>}
/>

Other transitions

There are plenty different already predefined Transition components such as Grow, Zoom, Slide, Blur, Fade etc. in transitions-kit. Feel free to try any of them.

import { Grow } from 'transitions-kit'
import { AsyncImage } from 'loadable-image'
...
<AsyncImage
  src='https://picsum.photos/1900'
  style={{ width: 150, height: 150 }}
  loader={<div style={{ background: '#888' }}/>}
  Transition={Grow}
/>

Props

<AsyncImage /> accepts all standard props for HtmlImageElement and the following:

PropertyTypeDescription
classNameStringNOTE: CSS from style object has a higher priority
styleCSSPropertiesCSSStyleDeclaration object
rootMarginstring by default: '600px 0px'Margin around the root. Specifies when to trigger an image download.
loaderReactElementReact element to display a loading state.
errorReactElementReact element to display an error state.
sourcesArray<SourceProps>An array of options for <source /> element.
TransitionComponentType<TransitionProps>Custom Transition component. Check out transitions-kit's predefined components

Requirements for loader & error props:

  • Forward the ref: The transition components require the first child element to forward its ref to the DOM node. This is usually done with React.forwardRef.
  • Single element: The transition components require only one child element (React.Fragment is not allowed).
3.2.2

1 month ago

3.2.3

1 month ago

3.2.1

1 month ago

3.2.0

1 month ago

3.1.1

4 months ago

3.1.0

4 months ago

3.0.4

8 months ago

3.0.3

8 months ago

3.0.2

8 months ago

3.0.1

8 months ago

3.0.6

7 months ago

3.0.5

8 months ago

3.0.0

8 months ago

2.3.0

11 months ago

2.3.1

10 months ago

1.2.7

1 year ago

2.2.1

12 months ago

2.1.2

1 year ago

2.0.3

1 year ago

2.1.1

1 year ago

2.0.2

1 year ago

2.1.4

1 year ago

2.0.5

1 year ago

2.2.2

12 months ago

2.1.3

1 year ago

2.0.4

1 year ago

2.0.7

1 year ago

2.1.5

12 months ago

2.0.6

1 year ago

2.0.9

1 year ago

2.0.8

1 year ago

2.1.0

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

1.2.6

1 year ago

1.2.5

1 year ago

1.1.1

2 years ago

1.1.0

2 years ago

1.1.9

2 years ago

1.1.8

2 years ago

1.1.5

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.1.3

2 years ago

1.2.1

2 years ago

1.1.2

2 years ago

1.1.10

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago