From 42edce1263b6890d9b0d230405c4e941474263c9 Mon Sep 17 00:00:00 2001 From: Resaki1 Date: Sun, 29 Oct 2023 18:56:35 +0100 Subject: [PATCH] fixed interval clearing and cleaned up code --- src/components/Feedback/FeedbackCard.tsx | 36 +++++++ src/components/Feedback/FeedbackCarousel.tsx | 102 ++++++------------- 2 files changed, 66 insertions(+), 72 deletions(-) create mode 100644 src/components/Feedback/FeedbackCard.tsx diff --git a/src/components/Feedback/FeedbackCard.tsx b/src/components/Feedback/FeedbackCard.tsx new file mode 100644 index 0000000..e587a70 --- /dev/null +++ b/src/components/Feedback/FeedbackCard.tsx @@ -0,0 +1,36 @@ +import type { FeedbackItem } from "./Feedback.astro"; + +const FeedbackCard = (item: FeedbackItem, posistionPercentage: number) => { + const shadowOffset = !isNaN(posistionPercentage) + ? posistionPercentage * -32 + 16 + : 0; + + return ( +
  • +

    {item.feedback}

    +
    + {item.image ? ( + {item.name} + ) : ( + {item.name} + )} +
    +

    {item.name}

    + + {item.role} + {item.role && item.company && ", "} + {item.company} + +
    +
    +
  • + ); +}; + +export default FeedbackCard; diff --git a/src/components/Feedback/FeedbackCarousel.tsx b/src/components/Feedback/FeedbackCarousel.tsx index 9ea923b..a0d6598 100644 --- a/src/components/Feedback/FeedbackCarousel.tsx +++ b/src/components/Feedback/FeedbackCarousel.tsx @@ -4,6 +4,7 @@ import type { FeedbackItem } from "./Feedback.astro"; import useElementSize from "../../hooks/useElementSize"; import "./FeedbackCarousel.scss"; import { useInterval } from "../../hooks/useInterval"; +import FeedbackCard from "./FeedbackCard"; type FeedbackCarouselProps = { items: FeedbackItem[]; @@ -20,39 +21,6 @@ type CarouselEvent = { }; }; -const FeedbackCard = (item: FeedbackItem, posistionPercentage: number) => { - const shadowOffset = !isNaN(posistionPercentage) - ? posistionPercentage * -32 + 16 - : 0; - - return ( -
  • -

    {item.feedback}

    -
    - {item.image ? ( - {item.name} - ) : ( - {item.name} - )} -
    -

    {item.name}

    - - {item.role} - {item.role && item.company && ", "} - {item.company} - -
    -
    -
  • - ); -}; - const groupArrayIntoChunks = ( inputArray: FeedbackItem[], chunkSize: number, @@ -77,51 +45,45 @@ const FeedbackCarousel = ({ items }: FeedbackCarouselProps) => { const size = useElementSize("feedback-carousel"); const availableSpace = size ? Math.floor(size.width / 400) : 1; const itemsPerSlide = availableSpace > 4 ? 4 : availableSpace; - const [intervalDelay, setIntervalDelay] = useState( - 6000 * itemsPerSlide, - ); + const [intervalDelay, setIntervalDelay] = useState(8000); + + const { carouselFragment, useListenToCustomEvent, slideToItem } = + useSpringCarousel({ + draggingSlideTreshold: 16, + items: groupedFeedback.map((group, index) => ({ + id: index, + renderItem: ( +
      + {group.map((item, index) => + FeedbackCard(item, index / (group.length - 1)), + )} +
    + ), + })), + }); useMemo(() => { setGroupedFeedback(groupArrayIntoChunks(items, itemsPerSlide || 1)); + setIntervalDelay(8000 * itemsPerSlide); // Time per slide = 8s * items per slide }, [items, itemsPerSlide]); - const { - carouselFragment, - slideToPrevItem, - slideToNextItem, - useListenToCustomEvent, - slideToItem, - } = useSpringCarousel({ - draggingSlideTreshold: 16, - items: groupedFeedback.map((group, index) => ({ - id: index, - renderItem: ( -
      - {group.map((item, index) => - FeedbackCard(item, index / (group.length - 1)), - )} -
    - ), - })), - }); - useInterval(() => { - if (activeSlide === groupedFeedback.length - 1) { - slideToItem(0); - setActiveSlide(0); - } else { - slideToItem(activeSlide + 1); - setActiveSlide(activeSlide + 1); - } + if (activeSlide === groupedFeedback.length - 1) slideTo(0); + else slideTo(activeSlide + 1); }, intervalDelay); useListenToCustomEvent((event: CarouselEvent) => { - if (event.eventName === "onSlideStartChange") { - setActiveSlide(event.nextItem.index); - setIntervalDelay(null); // Stop the interval - } + if (event.eventName === "onSlideStartChange") + return setActiveSlide(event.nextItem.index); + else if (event.eventName === "onDrag") setIntervalDelay(null); // Stop the interval }); + const slideTo = (index: number, clearInterval?: boolean) => { + if (clearInterval) setIntervalDelay(null); // Stop the interval + slideToItem(index); + setActiveSlide(index); + }; + return (