• Stars
    star
    3,273
  • Rank 13,164 (Top 0.3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 6 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🖼️ An Almost Ideal React Image Component

Stand With Ukraine

react-ideal-image

Adaptive image component


Build Status Code Coverage version downloads MIT License

All Contributors PRs Welcome Code of Conduct

Watch on GitHub Star on GitHub Tweet

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

Installation

This module is distributed via npm which is bundled with node 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.

Usage

Example for create-react-app (you need v2 for macros) based project

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 = () => (
  <IdealImage
    placeholder={{lqip}}
    srcSet={[{src: image, width: 3500}]}
    alt="doggo"
    width={3500}
    height={2095}
  />
)

Props

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

getIcon

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.

getMessage

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.

getUrl

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.

height

number | required

The Height of the image in px.

icons

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

const icons = {
  load: DownloadIcon,
  //...
}

loader

string | optional, defaults to 'xhr'

This prop takes one of the 2 options, xhr and image. Read more about it here.

placeholder

object | required

This takes one of the 2 objects

// To add a solid color placeholder
{
  color: ''
}

or

/**
 * To add a low quality image
 * [Low Quality Image Placeholder](https://github.com/zouhir/lqip)
 * [SVG-Based Image Placeholder](https://github.com/technopagan/sqip)
 * base64 encoded image of low quality
 */
{
  lqip: ''
}

Read more about it here.

shouldAutoDownload

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.

srcSet

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

srcType = {
  width: number, // required
  src: string,
  size: number,
  format: string, // one of the 'jpeg' or 'webp'
}

theme

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.

const theme = {
  placeholder: {
    backgroundSize: 'cover',
    backgroundRepeat: 'no-repeat',
    position: 'relative',
  },
  // ...
}

threshold

number | optional

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

width

number | required

Width of the image in px.

Other Solutions

Contributors

Thanks goes to these people (emoji key):


stereobooster

💻 📖 🚇 ⚠️

Ivan Babak

📖

Arun Kumar

📖

Andrew Lisowski

💻

Timothy Vernon

⚠️

vishalShinde

📖

Evgeniy Kumachev

📖

John Munn

💻

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

LICENSE

Code - MIT

Icons - Apache License 2.0

More Repositories

1

react-snap

👻 Zero-configuration framework-agnostic static prerendering for SPAs
JavaScript
5,025
star
2

package.json

🗃️ package.json fields explained
951
star
3

css-in-js-101

💈 CSS-in-JS 101: All you need to know
249
star
4

type-o-rama

👾 JS type systems interportability
243
star
5

programming-languages-genealogical-tree

Programming languages genealogical tree
231
star
6

typescript-monorepo

HTML
153
star
7

jekyll-press

🚨 [deprecated] Minifier plugin for jekyll. Minifies all html, js, css files. Simple just drop it in solution. No Java required
Ruby
149
star
8

react-modal-experiment

JavaScript
109
star
9

write-you-a-programming-language

List of small programming languages that you can implement in a relatively small amount of time for educational purposes.
99
star
10

guide-to-async-components

📖 Guide To JavaScript Async Components
79
star
11

diamondback-ruby

⛑ Fork of diamondback-ruby
Ruby
70
star
12

awesome-hiring-process

Collection of links and ideas about the hiring process in the IT industry
58
star
13

ruby-json-benchmark

Ruby json benchmark
Ruby
36
star
14

html_press

🚨 [deprecated] Ruby gem for compressing html
Ruby
35
star
15

pragmatic-types

Small practical guide on Flow and TypeScript for JavaScript developers
33
star
16

jshintrb

🚨 [deprecated] Ruby wrapper for JSHint
Ruby
31
star
17

mobile-safari-fullscreen

Fix for Mobile Safari fullscreen issue
JavaScript
23
star
18

ruby-memory-issues

🐲 Ruby Memory Issues and Where to Find Them
22
star
19

the-button

JavaScript
17
star
20

rb-fchange

🚨 [deprecated] Gem which uses native windows methods for watching changes of file system
Ruby
15
star
21

react-accessible-accordion

Accessible React accordion component
JavaScript
15
star
22

awesome-vscode-dev-containers

A curated list of VS Code Containers
15
star
23

d3-tube

D3 implementation of tube map
JavaScript
14
star
24

main-module-browser-test

Experiment
JavaScript
12
star
25

jekyll_oembed

🚨 [deprecated] Jekyll plugin to embed objects with the help of oEmbed. Simple liquid tag
Ruby
12
star
26

html_minifier

Ruby wrapper for kangax html-minifier
JavaScript
11
star
27

dev.wtf

HTML
10
star
28

headless-cms-comparison

JavaScript
9
star
29

the-history-of-frontend-development

Current state of frontend development is controversial. How we got here?
9
star
30

rb-notifu

🚨 [deprecated] Notification system for windows. Trying to be Growl
Ruby
8
star
31

sqip.macro

Webpack sqip-loader implemented as babel-plugin-macros
JavaScript
8
star
32

social_detector

Detect if visitors are logged into social networks
Ruby
8
star
33

webpack-comment-import.macro

JavaScript
7
star
34

css-modules-components

styled-components for CSS Modules
JavaScript
7
star
35

postgresql-experiment

JavaScript
6
star
36

css_press

🚨 [deprecated] Ruby gem for compressing CSS
Ruby
6
star
37

jBar2

jBar is a simple and lightweight jQuery notification (call to action) banner
JavaScript
4
star
38

useful-react-snippets

TypeScript
4
star
39

tree-sitter-wasm

Shell
4
star
40

react-async-issue

JavaScript
3
star
41

react-lingui-example

JavaScript
3
star
42

unstyled-components

styled-components based on React's style prop
JavaScript
3
star
43

github-issues

Github issues
3
star
44

react-fsm-example

JavaScript
3
star
45

react-simple-country-select

JavaScript
3
star
46

submodule

Small gem to simplify building process of gems with git submodules. Tended to be used for ruby gems which wrap js libraries or another assets
Ruby
2
star
47

zoos

Zoo is an attempt to classify things.
2
star
48

stereobooster.github.io

CSS
2
star
49

wisp

Mirror of https://hg.sr.ht/~arnebab/wisp
Scheme
2
star
50

readme

About me and my open-source work
2
star
51

cs-video

Computer science videos
2
star
52

parsing-with-derivalives

JavaScript
2
star
53

puma-benchmarks

Ruby
2
star
54

small-bits

Small bits of web UX
JavaScript
1
star
55

react-router-redux-example

JavaScript
1
star
56

docker-reasonml

Dockerfile
1
star
57

waypoint-bug-report

waypoint-bug-report
JavaScript
1
star
58

natural_sort-pgxn

Makefile
1
star
59

art_typograf

Ruby wrapper for typograf.artlebedev.ru webservice
Ruby
1
star
60

crystal-lisp

Crystal
1
star
61

main-module-browser

1
star
62

explain-you-mysql

JavaScript
1
star
63

loadable-components.macro

JavaScript
1
star
64

jekyll-seo-lint

Jekyll plugin to validate metadata provided in front matter and in configuration
Ruby
1
star
65

jevix

mirror of http://jevix.googlecode.com/svn/
PHP
1
star
66

jekyll_press

moved to https://github.com/stereobooster/jekyll-press
Ruby
1
star
67

react-ts-experiment

TypeScript
1
star