diff --git a/src/widgets/PhotoGallery/PhotoGallery.style.scss b/src/widgets/PhotoGallery/PhotoGallery.style.scss index d5026cd..5467d25 100644 --- a/src/widgets/PhotoGallery/PhotoGallery.style.scss +++ b/src/widgets/PhotoGallery/PhotoGallery.style.scss @@ -16,7 +16,8 @@ margin-bottom: 16px; @include mediaMax($tablet-max-width) { - font-size: 24px; + margin-top: 20px; + font-size: 24px; } } @@ -95,7 +96,6 @@ $tablet-min-width: 768px; display: flex; flex-wrap: wrap; gap: 32px; - margin-bottom: 64px; .creativity-text-top { width: calc(50% - 16px); diff --git a/src/widgets/PhotoGallery/PhotoGallery.tsx b/src/widgets/PhotoGallery/PhotoGallery.tsx index 8db1143..fe9e520 100644 --- a/src/widgets/PhotoGallery/PhotoGallery.tsx +++ b/src/widgets/PhotoGallery/PhotoGallery.tsx @@ -1,4 +1,4 @@ -import { useState } from "react"; +import { useState, useEffect, useRef, useCallback } from "react"; import { useMedia } from "@/modules/media/hooks/useMedia"; import { SkeletonGallery } from "@/shared/Components/SkeletonGallery/SkeletonGallery"; import "./PhotoGallery.style.scss"; @@ -8,9 +8,38 @@ import HWaC2 from "@/shared/assets/images/hard_work_and_creativity/HWaC2.jpg"; const PhotoGallery = () => { const { data: mediaData, isLoading, isError } = useMedia(); const [visibleCount, setVisibleCount] = useState(10); + const [isLoadingMore, setIsLoadingMore] = useState(false); + const containerRef = useRef(null); + const isMobile = window.innerWidth <= 768; const galleryImages = mediaData?.filter((el) => el.category === "photogallery"); + const loadMore = useCallback(() => { + if (isLoadingMore || !galleryImages || visibleCount >= galleryImages.length) return; + + setIsLoadingMore(true); + setTimeout(() => { + setVisibleCount(prev => prev + 10); + setIsLoadingMore(false); + }, 800); + }, [galleryImages, isLoadingMore, visibleCount]); + + useEffect(() => { + const handleScroll = () => { + if (!containerRef.current) return; + + const { scrollTop, scrollHeight, clientHeight } = document.documentElement; + const threshold = isMobile ? 0.98 : 1; // 50% на мобиле, 98% на десктопе + + if (scrollTop + clientHeight >= scrollHeight * threshold) { + loadMore(); + } + }; + + window.addEventListener('scroll', handleScroll); + return () => window.removeEventListener('scroll', handleScroll); + }, [loadMore, isMobile]); + if (isLoading) { return ; } @@ -20,7 +49,7 @@ const PhotoGallery = () => { } return ( -
+

Трудолюбие и креативность

@@ -55,12 +84,15 @@ const PhotoGallery = () => {
{visibleCount < galleryImages.length && ( -
setVisibleCount(visibleCount + 10)}> - Загрузить ещё +
+ {isLoadingMore ? 'Загрузка...' : 'Загрузить ещё'}
)}
); }; -export default PhotoGallery; +export default PhotoGallery; \ No newline at end of file