mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 21:06:42 +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;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue