mirror of
https://github.com/jeffvli/feishin.git
synced 2026-05-09 20:29:36 +02:00
persist image render after entering viewport
This commit is contained in:
@@ -6,6 +6,7 @@ import {
|
|||||||
type ImgHTMLAttributes,
|
type ImgHTMLAttributes,
|
||||||
memo,
|
memo,
|
||||||
ReactNode,
|
ReactNode,
|
||||||
|
useRef,
|
||||||
} from 'react';
|
} from 'react';
|
||||||
import { Img } from 'react-image';
|
import { Img } from 'react-image';
|
||||||
|
|
||||||
@@ -144,6 +145,22 @@ function ImageWithDebounce({
|
|||||||
const { inViewport, ref } = enableViewport ? viewport : { inViewport: true, ref: undefined };
|
const { inViewport, ref } = enableViewport ? viewport : { inViewport: true, ref: undefined };
|
||||||
const { className: containerPropsClassName, ...restContainerProps } = imageContainerProps || {};
|
const { className: containerPropsClassName, ...restContainerProps } = imageContainerProps || {};
|
||||||
|
|
||||||
|
const hasBeenInViewportRef = useRef(false);
|
||||||
|
const prevDebouncedSrcRef = useRef(debouncedSrc);
|
||||||
|
|
||||||
|
if (prevDebouncedSrcRef.current !== debouncedSrc) {
|
||||||
|
prevDebouncedSrcRef.current = debouncedSrc;
|
||||||
|
hasBeenInViewportRef.current = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (inViewport && debouncedSrc) {
|
||||||
|
hasBeenInViewportRef.current = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const shouldShowImage = enableViewport
|
||||||
|
? (inViewport || hasBeenInViewportRef.current) && debouncedSrc
|
||||||
|
: debouncedSrc;
|
||||||
|
|
||||||
if (enableViewport) {
|
if (enableViewport) {
|
||||||
return (
|
return (
|
||||||
<ImageContainer
|
<ImageContainer
|
||||||
@@ -152,7 +169,7 @@ function ImageWithDebounce({
|
|||||||
ref={ref}
|
ref={ref}
|
||||||
{...restContainerProps}
|
{...restContainerProps}
|
||||||
>
|
>
|
||||||
{inViewport && debouncedSrc ? (
|
{shouldShowImage && debouncedSrc ? (
|
||||||
<Img
|
<Img
|
||||||
className={clsx(styles.image, className, {
|
className={clsx(styles.image, className, {
|
||||||
[styles.animated]: enableAnimation,
|
[styles.animated]: enableAnimation,
|
||||||
@@ -224,6 +241,20 @@ function ImageWithViewport({
|
|||||||
const { inViewport, ref } = useInViewport();
|
const { inViewport, ref } = useInViewport();
|
||||||
const { className: containerPropsClassName, ...restContainerProps } = imageContainerProps || {};
|
const { className: containerPropsClassName, ...restContainerProps } = imageContainerProps || {};
|
||||||
|
|
||||||
|
const hasBeenInViewportRef = useRef(false);
|
||||||
|
const prevSrcRef = useRef(src);
|
||||||
|
|
||||||
|
if (prevSrcRef.current !== src) {
|
||||||
|
prevSrcRef.current = src;
|
||||||
|
hasBeenInViewportRef.current = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (inViewport && src) {
|
||||||
|
hasBeenInViewportRef.current = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const shouldShowImage = (inViewport || hasBeenInViewportRef.current) && src;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ImageContainer
|
<ImageContainer
|
||||||
className={clsx(containerClassName, containerPropsClassName)}
|
className={clsx(containerClassName, containerPropsClassName)}
|
||||||
@@ -231,7 +262,7 @@ function ImageWithViewport({
|
|||||||
ref={ref}
|
ref={ref}
|
||||||
{...restContainerProps}
|
{...restContainerProps}
|
||||||
>
|
>
|
||||||
{inViewport && src ? (
|
{shouldShowImage ? (
|
||||||
<Img
|
<Img
|
||||||
className={clsx(styles.image, className, {
|
className={clsx(styles.image, className, {
|
||||||
[styles.animated]: enableAnimation,
|
[styles.animated]: enableAnimation,
|
||||||
|
|||||||
Reference in New Issue
Block a user