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

added lazy loading to lab

This commit is contained in:
Fedor Katurov 2022-12-07 13:17:47 +06:00
parent 10bb6f01b5
commit 182ff11d2b
4 changed files with 26 additions and 19 deletions

View file

@ -1,13 +1,16 @@
import React, { FC } from 'react'; import React, { FC } from 'react';
import Image from 'next/future/image';
import SwiperCore, { A11y, Navigation, Pagination } from 'swiper'; import SwiperCore, { A11y, Navigation, Pagination } from 'swiper';
import { ImagePreloader } from '~/components/media/ImagePreloader'; import { ImagePreloader } from '~/components/media/ImagePreloader';
import { Placeholder } from '~/components/placeholders/Placeholder'; import { Placeholder } from '~/components/placeholders/Placeholder';
import { INodeComponentProps } from '~/constants/node'; import { INodeComponentProps } from '~/constants/node';
import { imagePresets } from '~/constants/urls';
import { useGotoNode } from '~/hooks/node/useGotoNode'; import { useGotoNode } from '~/hooks/node/useGotoNode';
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 styles from './styles.module.scss'; import styles from './styles.module.scss';
@ -19,7 +22,7 @@ const LabImage: FC<IProps> = ({ node, isLoading }) => {
const images = useNodeImages(node); const images = useNodeImages(node);
const onClick = useGotoNode(node.id); const onClick = useGotoNode(node.id);
if (!images?.length && !isLoading) { if (!images?.length) {
return null; return null;
} }
@ -28,9 +31,12 @@ const LabImage: FC<IProps> = ({ node, isLoading }) => {
return ( return (
<Placeholder active={isLoading} width="100%" height={400}> <Placeholder active={isLoading} width="100%" height={400}>
<div className={styles.wrapper}> <div className={styles.wrapper}>
<ImagePreloader <Image
file={file} src={getURL(file, imagePresets[600])}
width={file.metadata?.width}
height={file.metadata?.height}
onClick={onClick} onClick={onClick}
alt=""
className={styles.image} className={styles.image}
color={normalizeBrightColor(file?.metadata?.dominant_color)} color={normalizeBrightColor(file?.metadata?.dominant_color)}
/> />

View file

@ -51,6 +51,8 @@
max-height: calc(100vh - 70px - 70px); max-height: calc(100vh - 70px - 70px);
max-width: 100%; max-width: 100%;
transition: box-shadow 1s; transition: box-shadow 1s;
max-inline-size: 100%;
block-size: auto;
@include tablet { @include tablet {
padding-bottom: 0; padding-bottom: 0;

View file

@ -2,28 +2,23 @@ import { FC, useCallback, useEffect, useMemo, useState } from 'react';
import classNames from 'classnames'; import classNames from 'classnames';
import { observer } from 'mobx-react-lite'; import { observer } from 'mobx-react-lite';
import Image from 'next/future/image';
import SwiperCore, { import SwiperCore, {
Keyboard, Keyboard,
Lazy,
Navigation, Navigation,
Pagination, Pagination,
SwiperOptions, SwiperOptions,
Lazy,
} from 'swiper'; } from 'swiper';
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 { ImagePreloader } from '~/components/media/ImagePreloader';
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 { IFile } from '~/types';
import { normalizeBrightColor } from '~/utils/color'; import { normalizeBrightColor } from '~/utils/color';
import { getURL } from '~/utils/dom'; import { getFileSrcSet } from '~/utils/srcset';
import { imageSrcSets, ImagePreset } from '../../../constants/urls';
import styles from './styles.module.scss'; import styles from './styles.module.scss';
@ -47,14 +42,6 @@ const lazy = {
checkInView: true, checkInView: true,
}; };
const generateSrcSet = (file: IFile) =>
Object.keys(imageSrcSets)
.map(
(preset) =>
`${getURL(file, preset as ImagePreset)} ${imageSrcSets[preset]}w`,
)
.join(', ');
const NodeImageSwiperBlock: FC<IProps> = observer(({ node }) => { const NodeImageSwiperBlock: FC<IProps> = observer(({ node }) => {
const [controlledSwiper, setControlledSwiper] = useState< const [controlledSwiper, setControlledSwiper] = useState<
SwiperClass | undefined SwiperClass | undefined
@ -154,7 +141,7 @@ const NodeImageSwiperBlock: FC<IProps> = observer(({ node }) => {
<SwiperSlide className={styles.slide} key={file.id}> <SwiperSlide className={styles.slide} key={file.id}>
<img <img
style={{ backgroundColor: file.metadata?.dominant_color }} style={{ backgroundColor: file.metadata?.dominant_color }}
data-srcset={generateSrcSet(file)} data-srcset={getFileSrcSet(file)}
width={file.metadata?.width} width={file.metadata?.width}
height={file.metadata?.height} height={file.metadata?.height}
onLoad={updateSwiper} onLoad={updateSwiper}

12
src/utils/srcset.ts Normal file
View file

@ -0,0 +1,12 @@
import { IFile } from '~/types';
import { getURL } from '~/utils/dom';
import { imageSrcSets, ImagePreset } from '../constants/urls';
export const getFileSrcSet = (file: IFile) =>
Object.keys(imageSrcSets)
.map(
(preset) =>
`${getURL(file, preset as ImagePreset)} ${imageSrcSets[preset]}w`,
)
.join(', ');