Image component lazy behavior

This commit is contained in:
tmshv committed 2020-03-03 06:49:18 +03:00
1 parent 8f54454d32
commit 11ff066e6c
4 files changed
+76 -14

No files matched your search

-3
View File
@@ -67,9 +67,6 @@ const Page: NextPage<Props> = props => {
srcSet={srcSet} srcSet={srcSet}
style={{ style={{
marginBottom: 'var(--size-l)', marginBottom: 'var(--size-l)',
}}
imgStyle={{
width: '100%',
borderRadius: 'var(--radius)', borderRadius: 'var(--radius)',
}} }}
/> />
+2 -2
View File
@@ -1,6 +1,7 @@
import './styles.css' import './styles.css'
import cx from 'classnames' import cx from 'classnames'
import Link from 'next/link' import Link from 'next/link'
import { Image } from '../Image'
export type CardLayout = 'simple' | 'featured' export type CardLayout = 'simple' | 'featured'
export type CardProps = { export type CardProps = {
@@ -19,8 +20,7 @@ export const Card: React.FC<CardProps> = props => (
<Link href={props.href}> <Link href={props.href}>
<a> <a>
<div className={'card-image'}> <div className={'card-image'}>
<img <Image
className={cx({ opa: false })}
alt={props.img.alt} alt={props.img.alt}
src={props.img.src} src={props.img.src}
srcSet={props.img.srcSet} srcSet={props.img.srcSet}
+57 -7
View File
@@ -1,23 +1,73 @@
import './styles.css' import './styles.css'
import { memo } from 'react' import { memo, useRef, useState, useEffect, useCallback } from 'react'
export type ImageProps = { export type ImageProps = {
style?: React.CSSProperties style?: React.CSSProperties
imgStyle?: React.CSSProperties
src: string src: string
alt?: string alt?: string
srcSet?: string srcSet?: string
} }
export const Image: React.FC<ImageProps> = memo(props => { export const Image: React.FC<ImageProps> = memo(props => {
const [imageSrc, setImageSrc] = useState<[string, string]>([null, null])
const ref = useRef<HTMLImageElement>()
const onLoad = useCallback((event) => {
event.target.classList.add('loaded')
event.target.classList.remove('pending')
}, [])
useEffect(() => {
if (ref.current) {
ref.current.classList.add('pending')
}
})
useEffect(() => {
let observer: IntersectionObserver
let didCancel = false
if (ref.current && imageSrc[0] !== props.src) {
if (IntersectionObserver) {
observer = new IntersectionObserver(
entries => {
entries.forEach(entry => {
if (
!didCancel &&
(entry.intersectionRatio > 0 || entry.isIntersecting)
) {
setImageSrc([props.src, props.srcSet])
observer.unobserve(ref.current)
}
})
},
{
threshold: 0.01,
rootMargin: "75%",
}
)
observer.observe(ref.current)
} else {
// Old browsers fallback
setImageSrc([props.src, props.srcSet])
}
}
return () => {
didCancel = true
if (observer && observer.unobserve) {
observer.unobserve(ref.current)
}
}
}, [props.src, props.srcSet, imageSrc, ref])
return ( return (
<picture className={'image'} style={props.style}>
<img <img
style={props.imgStyle} ref={ref}
onLoad={onLoad}
className={'image'}
style={props.style}
alt={props.alt} alt={props.alt}
src={props.src} src={imageSrc[0]}
srcSet={props.srcSet} srcSet={imageSrc[1]}
/> />
</picture>
) )
}) })
+17 -2
View File
@@ -1,7 +1,22 @@
@keyframes loaded {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
.image { .image {
width: 100%;
display: block; display: block;
} }
.image img { .image.pending {
display: block; opacity: 0;
}
.image.loaded {
animation: loaded 250ms ease-in-out;
opacity: 1;
} }