3.5.0 • Published 2 years ago

lazy-react v3.5.0

Weekly downloads
15
License
ISC
Repository
github
Last release
2 years ago

lazy-react

npm version Issue Count styled with prettier

Utility components to lazy load images, images-as-background and iframes using only requestAnimationFrame to handle scroll (both vertical and orizzontal) and window resize.

Install

npm install --save lazy-react

Also available as umd on unpkg

<script src="https://unpkg.com/lazy-react"></script>

Demo

You can see a demo of those packages in my personal site, cloning the repo and open the index.html in the demo folder or in this codepen.

Usage

The package exports 4 components:

  • LazyBackgroundImage
  • LazyImage
  • LazyFrame
  • LazyComponent

Every component accepts offeset as a prop, with 100px as fallback.

LazyBackgroundImage

Props

NameTypeDescriptionRequiredDefault
linkStringthe url of the resource✔️
classNameStringhtml class attributeempty string
styleObjecthtml style attribute{}

LazyComponent

Props

NameTypeDescriptionRequiredDefault
classNameStringhtml class attributeempty string
styleObjecthtml style attribute{}

This component is used to have a div placeholder before loading the component.

Usage:

<LazyComponent>
  <ComponentToLoadWhenInViewport>
</LazyComponent>

LazyImage

Props

NameTypeDescriptionRequiredDefault
linkStringthe url of the resource✔️
altStringsame as html image alt attributeempty string
styleObjecthtml style attribute{}
classNameStringhtml class attributeempty string
preserveAspectBooleanIf false it try to calculate width and height, it can break the layouttrue

If no style.height has been provided, it will use 300px as fallback to calculate position. 'preserveAspect' was added in 2.0.1 to prevent the component to apply style to the elements that doesn't have a class but are styled with nested selectors.

LazyFrame

Props

NameTypeDescriptionRequiredDefault
linkStringthe url of the resource✔️
heightStringsame as html image alt attribute500px
scrollingStringsame as html'no'
frameBorderStringsame as html'no'
allowTransparencyStringsame as html'true'
allowFullScreenStringsame as html'true'
styleObjecthtml style attribute{width:'100%'}

Example code

//with es6
import { LazyBackgroundImage, LazyImage, LazyFrame, LazyComponent } from 'lazy-react'
//with es5
var LazyBackgroundImage = require('lazy-react').LazyBackgroundImage
var LazyImage = require('lazy-react').LazyImage
var LazyFrame = require('lazy-react').LazyFrame
var LazyComponent = require('lazy-react').LazyComponent

class Example extends React.Component {
  constructor(props) {
    super(props)
  }
  render() {
    return <div>
      <LazyBackgroundImage
        link={'https://images.unsplash.com/photo-1462834026679-7c03bf571a67?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&s=6e160dc1e65511df7bf1c461f8a93c82'}
        className="fill"
      />
      <LazyImage
        link={'https://images.unsplash.com/photo-1462834026679-7c03bf571a67?ixlib=rb-0.3.5&q=80&fm=jpg&crop=entropy&cs=tinysrgb&s=6e160dc1e65511df7bf1c461f8a93c82'}
        offset={100}
      />
      <LazyFrame
        link={'http://jonathanobino.xyz'}
        scrolling={true}
      />
      <LazyComponent>
        <ComponentToLoadWhenInViewport>
      </LazyComponent>
    </div>
  }
}

Hook

Since version 3.x there is an hook available named 'useIsInViewport' that exposes 3 elements:

  • setRef: used to set the ref of the dom that has to be in the viewport
  • link: the passed link. It's equal to an empty string until the element is in the specified viewport
  • isVisible: it's false until the element is in the specified viewport

Usage

import useIsInViewport from 'lazy-react'

function Example({link, offset}) {
  const [setRef, link, isVisible] = useIsInViewport({link, offset})

  if(!isVisible){
    return <Placeholder />
  }

  return <div ref={(node)=>{
    setRef(node)
  }}>
    <Content/>
  </div>

}

The required props that have to be passed to the hooks are:

  • link: string
  • offset: number

Contributing

Pull requests for bug fixes, new features, and improvements are welcomed.

3.5.0

2 years ago

3.4.1

2 years ago

3.4.0

2 years ago

3.3.0

2 years ago

3.2.0

2 years ago

3.1.1

2 years ago

3.0.2

2 years ago

3.1.0

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

2.2.0

4 years ago

2.1.0

6 years ago

2.0.5

6 years ago

2.0.4

7 years ago

2.0.3

7 years ago

2.0.2

7 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.3.2

7 years ago

1.3.1

7 years ago

1.3.0

7 years ago

1.2.0

7 years ago

1.1.0

8 years ago

1.0.12

8 years ago

1.0.11

8 years ago

1.0.10

8 years ago

1.0.9

8 years ago

1.0.8

8 years ago

1.0.7

8 years ago

1.0.6

8 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago