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:
parent
f5ca735049
commit
585f9b57cb
4 changed files with 106 additions and 26 deletions
52
src/components/common/ImageLoadingWrapper/index.tsx
Normal file
52
src/components/common/ImageLoadingWrapper/index.tsx
Normal 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 };
|
26
src/components/common/ImageLoadingWrapper/styles.module.scss
Normal file
26
src/components/common/ImageLoadingWrapper/styles.module.scss
Normal 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;
|
||||||
|
}
|
|
@ -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}>
|
||||||
|
<ImageLoadingWrapper
|
||||||
|
preview={getURL(file, imagePresets['300'])}
|
||||||
|
color={file.metadata?.dominant_color}
|
||||||
|
>
|
||||||
|
{({ loading, onLoad }) => (
|
||||||
<img
|
<img
|
||||||
style={{ backgroundColor: file.metadata?.dominant_color }}
|
|
||||||
data-srcset={getFileSrcSet(file)}
|
data-srcset={getFileSrcSet(file)}
|
||||||
width={file.metadata?.width}
|
width={file.metadata?.width}
|
||||||
height={file.metadata?.height}
|
height={file.metadata?.height}
|
||||||
onLoad={updateSwiper}
|
onLoad={onLoad}
|
||||||
onClick={() => onOpenPhotoSwipe(i)}
|
onClick={() => onOpenPhotoSwipe(index)}
|
||||||
className={classNames(styles.image, 'swiper-lazy')}
|
className={classNames(styles.image, 'swiper-lazy', {
|
||||||
|
[styles.loading]: loading,
|
||||||
|
})}
|
||||||
color={normalizeBrightColor(file?.metadata?.dominant_color)}
|
color={normalizeBrightColor(file?.metadata?.dominant_color)}
|
||||||
alt=""
|
alt=""
|
||||||
sizes="(max-width: 560px) 100vw, 50vh"
|
sizes="(max-width: 560px) 100vw, 50vh"
|
||||||
/>
|
/>
|
||||||
|
)}
|
||||||
|
</ImageLoadingWrapper>
|
||||||
</SwiperSlide>
|
</SwiperSlide>
|
||||||
))}
|
))}
|
||||||
</Swiper>
|
</Swiper>
|
||||||
|
|
|
@ -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;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue