Adaptive image component

Downloads in past


100.0.113 years ago3 years agoMinified + gzip package size for @endiliey/react-ideal-image in KB



Adaptive image component

!Build Statusbuild-badgebuild !Code Coveragecoverage-badgecoverage !versionversion-badgepackage !downloadsdownloads-badgenpmtrends !MIT Licenselicense-badgelicense All Contributors !PRs Welcomeprs-badgeprs !Code of Conductcoc-badgecoc !Watch on GitHubgithub-watch-badgegithub-watch !Star on GitHubgithub-star-badgegithub-star !Tweettwitter-badgetwitter

The problem

I need React component to asynchronously load images, which will adapt based on network, which will allow a user to control, which image to load.

This solution

Read the introduction.

Table of Contents

- getIcon - getMessage - getUrl - height - icons - loader - placeholder - shouldAutoDownload - srcSet - theme - threshold - width


This module is distributed via npmnpm which is bundled with nodenode and should be installed as one of your project's dependencies: ``` npm install react-ideal-image --save ```
This package also depends on react, prop-types, and react-waypoint.
Please make sure you have those installed as well.


Example for create-react-app (you need v2 for macros) based project ```js import React from 'react' import lqip from 'lqip.macro' import IdealImage from 'react-ideal-image' import image from './images/doggo.jpg' const lqip = lqip('./images/doggo.jpg') const App = () => ( placeholder={{lqip}}
srcSet={[{src: image, width: 3500}]}
/> ) ```


This is the list of props that you need to pass to the component.


function(state: object) | optional, default icon is provided based on state object
This function decides what icon to show based on the current state of the component.


function(icon: string, state: object) | optional, default message is provided based on the icon and state object.
This function decides what message to show based on the icon (returned from getIcon prop) and the current state of the component.


function({}) | optional, no useful default
This function is called as soon as the component enters the viewport and is used to generate urls based on width and format if props.srcSet doesn't provide src field.


number | required
The Height of the image in px.


object | required
This provides a map of the icons. By default, the component uses icons from material design, implemented as React components with the SVG element. You can customize icons ```js const icons = { load: DownloadIcon, //... } ```


string | optional, defaults to 'xhr'
This prop takes one of the 2 options, xhr and image. Read more about it here.


object | required
This takes one of the 2 objects ```js // To add a solid color placeholder { color: '' } ``` or ```js / To add a low quality image Low Quality Image Placeholder SVG-Based Image Placeholder base64 encoded image of low quality / { lqip: '' } ``` Read more about it here.


function({}) | optional, default function is provided which decides based on the device network.
This function decides if image should be downloaded automatically. The default function returns false for a 2g network, for a 3g network it decides based on props.threshold and for a 4g network it returns true by default.


array[srcType: object] | required
This provides an array of sources of different format and size of the image. Read more about it here. The srcType has below structure ```js srcType = { width: number, // required src: string, size: number, format: string, // one of the 'jpeg' or 'webp' } ```


object | required
This provides a theme to the component. By default, the component uses inline styles, but it is also possible to use CSS modules and override all styles. ```js const theme = { placeholder: {
backgroundSize: 'cover',
backgroundRepeat: 'no-repeat',
position: 'relative',
}, // ... } ```


number | optional
Tells how much to wait in milliseconds until consider the download to be slow.


number | required
Width of the image in px.

Other Solutions


Thanks goes to these people (emoji keyemojis): |

💻 📖 🚇") ⚠️ |
Ivan Babak

📖 |
Arun Kumar

📖 |
Andrew Lisowski

💻 |
Timothy Vernon

⚠️ |

📖 |
Evgeniy Kumachev

📖 | | :---: | :---: | :---: | :---: | :---: | :---: | :---: | |
John Munn

💻 | This project follows the all-contributorsall-contributors specification. Contributions of any kind welcome!


Code - MIT Icons - Apache License 2.0