Gathering detailed insights and metrics for @t7/image
Gathering detailed insights and metrics for @t7/image
Gathering detailed insights and metrics for @t7/image
Gathering detailed insights and metrics for @t7/image
npm install @t7/image
Typescript
Module System
Node Version
NPM Version
70.1
Supply Chain
96.9
Quality
81.3
Maintenance
100
Vulnerability
100
License
Total Downloads
2,393
Last Day
2
Last Week
10
Last Month
40
Last Year
297
Minified
Minified + Gzipped
Latest Version
0.0.10
Package Id
@t7/image@0.0.10
Unpacked Size
47.17 kB
Size
12.01 kB
File Count
7
NPM Version
lerna/3.10.7/node@v11.10.0+x64 (darwin)
Node Version
11.10.0
Cumulative downloads
Total Downloads
Last Day
100%
2
Compared to previous day
Last Week
233.3%
10
Compared to previous week
Last Month
-45.9%
40
Compared to previous month
Last Year
-2.6%
297
Compared to previous year
This is a React component that loads an image source, only after it is partially or fully visible in the viewport.
1# Install via NPM. 2npm install @t7/image --save
For complete browser support, please include a window.IntersectionObserver
polyfill:
1# Install via NPM. 2npm install intersection-observer --save
View on NPM:
https://www.npmjs.com/package/@t7/image
The <Image>
component uses width
and height
props to force an immediate layout, as if the image source had already been loaded. This prevents unnecessary recalculations by the browser and allows the image to be resized via CSS with proportional aspect ratios.
It causes an encoded, inline SVG to be generated, which allows for a solid color to be displayed while the image is still processing. This prevents unintended rendering of alt
text or the display of a browser's default "broken image" icon.
Passing these props yields the following markup output.
1<Image 2 width="800" 3 height="450" 4 style={{ 5 background: '#393', 6 width: '50%', 7 height: 'auto' 8 }} 9/>
1<img 2 width="800" 3 height="450" 4 style=" 5 background: #393; 6 width: 50%; 7 height: auto; 8 " 9 src="data:image/svg+xml,%3Csvg 10 xmlns='http://www.w3.org/2000/svg' 11 width='800' 12 height='450' 13 %3E%3C/svg%3E" 14/>
The <Image>
component uses the rootMargin
and thresholds
props, along with the Intersection Observer API, to load its source only as the image comes into view.
For instance, if you wanted the image to be loaded only after it passed the 50%
mark, you could set the thresholds
prop to 0.5
.
Once visible, <Image>
loads the placeholder
and src
images simultaneously. If the placeholder
image is loaded before the src
, then the placeholder
is made visible as a background image while src
continues to load.
Once the src
is loaded, then it is immediately displayed and the background image is removed. If the src
cannot be loaded, then the fallback
image is displayed instead.
1<Image 2 // For accessibility. 3 alt="Test image: 400x225." 4 5 // Sources. 6 src="https://placehold.it/400x225/393/fff?text=TEST+IMAGE" 7 placeholder="https://placehold.it/400x225/393/fff?text=LOADING" 8 fallback="https://placehold.it/400x225/f60/fff?text=FALLBACK" 9 10 // Dimensions. 11 width="800" 12 height="450" 13 14 // Intersection observer. 15 rootMargin="50px 0px" 16 thresholds="0.01" 17/>
See the Intersection Observer options for best usage of rootMargin
and thresholds
.
The <Image>
component allows you to hook into various events during the loading process.
1<Image 2 alt="A test image: 400x225." 3 src="https://placehold.it/400x225" 4 5 // Events. 6 onIntersection={(event) => { 7 console.log('Interseciton detected. Image loading started.') 8 }} 9 10 onLoad={(event) => { 11 console.log('Image loading complete.') 12 }} 13 14 onFallback={(event) => { 15 console.log('Fallback loading complete.') 16 }} 17/>
onIntersection
The onIntersection
listener is dispatched when all/part of the image is visible for the first time.
onLoad
The onLoad
listener is dispatched when the src
file has loaded.
onFallback
The onFallback
listener is dispatched when the fallback
file has loaded.
The following examples use the Sloow.me proxy site to purposefully delay image loading.
It can be helpful testing with a delayed response, to ensure that images are not silently loading "below the fold" before being scrolled into view.
Example: After (at least) 5 seconds, image load completes.
1<Image 2 alt="Test image: 400x225." 3 src="https://sloow.me/5000/placehold.it/400x225/393/fff?text=TEST+IMAGE" 4 placeholder="https://placehold.it/400x225?text=PLACEHOLDER" 5 fallback="https://placehold.it/400x225/f60/fff?text=FALLBACK" 6 width="400" 7 height="225" 8/>
Example: After (at least) 3 seconds, image load fails.
1<Image 2 alt="Test image: 400x225." 3 src="https://sloow.me/3000+404/placehold.it/400x225/393/fff?text=TEST+IMAGE" 4 placeholder="https://placehold.it/400x225/393/fff?text=PLACEHOLDER" 5 fallback="https://placehold.it/400x225/f60/fff?text=FALLBACK" 6 width="400" 7 height="225" 8/>
No vulnerabilities found.
No security vulnerabilities found.