1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-24 20:36:40 +07:00

added image preloader to swiper

This commit is contained in:
Fedor Katurov 2022-12-14 17:21:56 +06:00
parent f5ca735049
commit 585f9b57cb
4 changed files with 106 additions and 26 deletions

View file

@ -0,0 +1,52 @@
import React, {
CSSProperties,
FC,
useCallback,
useMemo,
useReducer,
useState,
} from 'react';
import classNames from 'classnames';
import { LoaderCircle } from '~/components/input/LoaderCircle';
import { DivProps } from '~/utils/types';
import styles from './styles.module.scss';
interface ImageLoadingWrapperProps extends Omit<DivProps, 'children'> {
children: (props: { loading: boolean; onLoad: () => void }) => void;
preview?: string;
}
const ImageLoadingWrapper: FC<ImageLoadingWrapperProps> = ({
className,
children,
preview,
color,
...props
}) => {
const [loading, onLoad] = useReducer((v) => false, true);
const style = useMemo<CSSProperties>(
() => ({
backgroundImage: `url('${preview}')`,
backgroundColor: color || 'var(--color-primary)',
}),
[preview, color],
);
return (
<div className={classNames(styles.wrapper, className)} {...props}>
{!!loading && !!preview && (
<div className={styles.preview}>
<div className={styles.thumbnail} style={style} />
<LoaderCircle size={32} />
</div>
)}
{children({ loading, onLoad })}
</div>
);
};
export { ImageLoadingWrapper };

View file

@ -0,0 +1,26 @@
@import 'src/styles/variables';
.wrapper {
position: relative;
}
.preview {
position: absolute;
inset: 0;
pointer-events: none;
touch-action: none;
display: flex;
align-items: flex-end;
justify-content: flex-end;
padding: $gap;
}
.thumbnail {
filter: blur(10px);
position: absolute;
inset: 0;
border-radius: $radius;
background-size: cover;
background-position: 50% 50%;
box-sizing: border-box;
}

View file

@ -12,12 +12,14 @@ import SwiperCore, {
import { Swiper, SwiperSlide } from 'swiper/react'; import { Swiper, SwiperSlide } from 'swiper/react';
import SwiperClass from 'swiper/types/swiper-class'; import SwiperClass from 'swiper/types/swiper-class';
import { ImagePreloader } from '~/components/media/ImagePreloader'; import { ImageLoadingWrapper } from '~/components/common/ImageLoadingWrapper/index';
import { INodeComponentProps } from '~/constants/node'; import { INodeComponentProps } from '~/constants/node';
import { imagePresets } from '~/constants/urls';
import { useModal } from '~/hooks/modal/useModal'; import { useModal } from '~/hooks/modal/useModal';
import { useImageModal } from '~/hooks/navigation/useImageModal'; import { useImageModal } from '~/hooks/navigation/useImageModal';
import { useNodeImages } from '~/hooks/node/useNodeImages'; import { useNodeImages } from '~/hooks/node/useNodeImages';
import { normalizeBrightColor } from '~/utils/color'; import { normalizeBrightColor } from '~/utils/color';
import { getURL } from '~/utils/dom';
import { getFileSrcSet } from '~/utils/srcset'; import { getFileSrcSet } from '~/utils/srcset';
import styles from './styles.module.scss'; import styles from './styles.module.scss';
@ -101,18 +103,6 @@ const NodeImageSwiperBlock: FC<IProps> = observer(({ node }) => {
return null; return null;
} }
if (images.length === 1) {
return (
<div className={styles.single}>
<ImagePreloader
file={images[0]}
onClick={() => onOpenPhotoSwipe(0)}
className={styles.image}
/>
</div>
);
}
return ( return (
<div className={styles.wrapper}> <div className={styles.wrapper}>
<Swiper <Swiper
@ -137,20 +127,28 @@ const NodeImageSwiperBlock: FC<IProps> = observer(({ node }) => {
watchSlidesProgress watchSlidesProgress
lazy={lazy} lazy={lazy}
> >
{images.map((file, i) => ( {images.map((file, index) => (
<SwiperSlide className={styles.slide} key={file.id}> <SwiperSlide className={styles.slide} key={file.id}>
<img <ImageLoadingWrapper
style={{ backgroundColor: file.metadata?.dominant_color }} preview={getURL(file, imagePresets['300'])}
data-srcset={getFileSrcSet(file)} color={file.metadata?.dominant_color}
width={file.metadata?.width} >
height={file.metadata?.height} {({ loading, onLoad }) => (
onLoad={updateSwiper} <img
onClick={() => onOpenPhotoSwipe(i)} data-srcset={getFileSrcSet(file)}
className={classNames(styles.image, 'swiper-lazy')} width={file.metadata?.width}
color={normalizeBrightColor(file?.metadata?.dominant_color)} height={file.metadata?.height}
alt="" onLoad={onLoad}
sizes="(max-width: 560px) 100vw, 50vh" onClick={() => onOpenPhotoSwipe(index)}
/> className={classNames(styles.image, 'swiper-lazy', {
[styles.loading]: loading,
})}
color={normalizeBrightColor(file?.metadata?.dominant_color)}
alt=""
sizes="(max-width: 560px) 100vw, 50vh"
/>
)}
</ImageLoadingWrapper>
</SwiperSlide> </SwiperSlide>
))} ))}
</Swiper> </Swiper>

View file

@ -124,6 +124,10 @@
transition: box-shadow 1s; transition: box-shadow 1s;
box-shadow: transparentize(black, 0.7) 0 3px 5px; box-shadow: transparentize(black, 0.7) 0 3px 5px;
&.loading {
opacity: 0;
}
:global(.swiper-slide-active) & { :global(.swiper-slide-active) & {
box-shadow: transparentize(black, 0.9) 0 10px 5px 4px, box-shadow: transparentize(black, 0.9) 0 10px 5px 4px,
transparentize(black, 0.7) 0 5px 5px, $gray_90 0 -1px 2px, $gray_90 0 -1px; transparentize(black, 0.7) 0 5px 5px, $gray_90 0 -1px 2px, $gray_90 0 -1px;