Coder Social home page Coder Social logo

yosbelms / react-progressive-loader Goto Github PK

View Code? Open in Web Editor NEW
217.0 5.0 12.0 184 KB

Utility to load images and React components progressively, and get code splitting for free

TypeScript 100.00%
react reactjs img lazy-loading lazyload progressive medium loadable

react-progressive-loader's Introduction

React Progressive Loader

Defer the load of non-critical images and components if they are off-screen.

This library makes possible to progressively load images, just like Medium does, and React components only when the user is ready to consume the content. Additionaly, take component based code splitting for free. Two at the price of one.

Lazy Loading Images: https://developers.google.com/web/fundamentals/performance/lazy-loading-guidance/images-and-video/

Installation

// with yarn
yarn add react-progressive-loader

// with npm
npm install react-progressive-loader

Usage

// ES2015+ and TS
import { Defer, Img } from 'react-progressive-loader'

Components

Defer

Defers the loading of a React component

Props:

  • render: The content to render
  • renderPlaceholder: The content to render while the content is loading
  • loadOnScreen: Load the content only when the area it is going to be rendered is visible for the user

If case the React component is default-exported in ./comp module

<Defer
  render={() => import('./comp')}
  renderPlaceholder={() => <div>Loading...</div>}
/>

If the component is not default-exported

// './comp.jsx'
export const MyComp = () => 'Loaded!'

// './app.jsx'
<Defer
  render={() => import('./comp').then(({MyComp}) => <MyComp />)}
/>

The render prop can also be a React element

<Defer
  render={() => <img src='my-img.png'></img>}
  renderPlaceholder={() => <div>Loading...</div>}
/>

Load the content only when it is on-screen

<Defer
  render={() => <img src='my-img.png'></img>}
  renderPlaceholder={() => <div>Loading...</div>}
  loadOnScreen
/>

Img

Progressively load images. This component makes a smooth animated transition in the following order:

[Background]->[Placeholder]->[Content]

Props:

Any other prop (not listed here) passed to this components will be passed down to the wrapper div

Basic usage

<Img
  src='image.jpeg'
  placeholderSrc='image-placeholder.jpeg'
/>

Transitioning only between background and content. Sometimes you may want to transit only from background to content by finding the dominant color of the image and assigning it to bgColor. This strategy is used by Google image search.

<Img
  bgColor='#FA8054'
  src='image.jpeg'
/>

Load the content only when it is on-screen

<Img
  src='image.jpeg'
  placeholderSrc='image-placeholder.jpeg'
  loadOnScreen
/>

This library uses IntersectionObserver API, for wide browser compatibility consider to add a polyfill

Published under MIT Licence

(c) Yosbel Marin 2018

react-progressive-loader's People

Contributors

felixmosh avatar yosbelms avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

react-progressive-loader's Issues

do we really need to use react-deco?

First of all, thank you for this awesome lib!

This lib can be ~50% smaller by removing the dep on react-deco.

image
If you agree I will create a PR for that.

Support download button in slow networks

Detect slow networks and show a download button. eg: downloadImageOn='demand|3g|slow-network'.

We could detect network speed while loading placeholders. Research which browser supports slow network detection.

React does not recognize the `aspectRatio` prop on a DOM element

Hi I got following error once used with React "16.4.1".
Is that something that you noticed as well?

Warning: React does not recognize the `aspectRatio` prop on a DOM element. 
If you intentionally want it to appear in the DOM as a custom attribute,
 spell it as lowercase `aspectratio` instead. 
If you accidentally passed it from a parent component, 
remove it from the DOM element.
    in div (created by Bare)
    in Bare (created by Img)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.