mirror of
https://github.com/antebudimir/feishin.git
synced 2026-01-01 10:23:33 +00:00
Use lazy loading (react-intersection-observer) for image loading
This commit is contained in:
parent
e00aeb2b67
commit
49bb42a298
3 changed files with 54 additions and 26 deletions
|
|
@ -2,6 +2,7 @@ import clsx from 'clsx';
|
|||
import { motion, MotionConfigProps } from 'motion/react';
|
||||
import { type ImgHTMLAttributes } from 'react';
|
||||
import { Img } from 'react-image';
|
||||
import { InView } from 'react-intersection-observer';
|
||||
|
||||
import styles from './image.module.css';
|
||||
|
||||
|
|
@ -44,33 +45,41 @@ export function Image({
|
|||
}: ImageProps) {
|
||||
if (src) {
|
||||
return (
|
||||
<Img
|
||||
className={clsx(styles.image, className)}
|
||||
container={(children) => (
|
||||
<ImageContainer
|
||||
className={containerClassName}
|
||||
enableAnimation={enableAnimation}
|
||||
{...imageContainerProps}
|
||||
>
|
||||
{children}
|
||||
</ImageContainer>
|
||||
<InView>
|
||||
{({ inView, ref }) => (
|
||||
<div ref={ref}>
|
||||
{inView && (
|
||||
<Img
|
||||
className={clsx(styles.image, className)}
|
||||
container={(children) => (
|
||||
<ImageContainer
|
||||
className={containerClassName}
|
||||
enableAnimation={enableAnimation}
|
||||
{...imageContainerProps}
|
||||
>
|
||||
{children}
|
||||
</ImageContainer>
|
||||
)}
|
||||
loader={
|
||||
includeLoader ? (
|
||||
<ImageContainer className={containerClassName}>
|
||||
<ImageLoader className={className} />
|
||||
</ImageContainer>
|
||||
) : null
|
||||
}
|
||||
src={src}
|
||||
unloader={
|
||||
includeUnloader ? (
|
||||
<ImageContainer className={containerClassName}>
|
||||
<ImageUnloader className={className} />
|
||||
</ImageContainer>
|
||||
) : null
|
||||
}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
loader={
|
||||
includeLoader ? (
|
||||
<ImageContainer className={containerClassName}>
|
||||
<ImageLoader className={className} />
|
||||
</ImageContainer>
|
||||
) : null
|
||||
}
|
||||
src={src}
|
||||
unloader={
|
||||
includeUnloader ? (
|
||||
<ImageContainer className={containerClassName}>
|
||||
<ImageUnloader className={className} />
|
||||
</ImageContainer>
|
||||
) : null
|
||||
}
|
||||
/>
|
||||
</InView>
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue