Skip to content

Commit

Permalink
added interval that automatically slides the feedback cards
Browse files Browse the repository at this point in the history
  • Loading branch information
Resaki1 committed Oct 19, 2023
1 parent 652adf0 commit 37699f5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
3 changes: 0 additions & 3 deletions src/components/Feedback/FeedbackCarousel.scss
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,6 @@
}
}

@media screen and (max-width: 992px) {
}

@media screen and (max-width: 375px) {
#feedback-carousel {
height: 358px;
Expand Down
16 changes: 16 additions & 0 deletions src/components/Feedback/FeedbackCarousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useSpringCarousel } from "react-spring-carousel";
import type { FeedbackItem } from "./Feedback.astro";
import useElementSize from "../../hooks/useElementSize";
import "./FeedbackCarousel.scss";
import { useInterval } from "../../hooks/useInterval";

type FeedbackCarouselProps = {
items: FeedbackItem[];
Expand Down Expand Up @@ -76,6 +77,9 @@ 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<number | null>(
6000 * itemsPerSlide,
);

useMemo(() => {
setGroupedFeedback(groupArrayIntoChunks(items, itemsPerSlide || 1));
Expand All @@ -101,9 +105,20 @@ const FeedbackCarousel = ({ items }: FeedbackCarouselProps) => {
})),
});

useInterval(() => {
if (activeSlide === groupedFeedback.length - 1) {
slideToItem(0);
setActiveSlide(0);
} else {
slideToItem(activeSlide + 1);
setActiveSlide(activeSlide + 1);
}
}, intervalDelay);

useListenToCustomEvent((event: CarouselEvent) => {
if (event.eventName === "onSlideStartChange") {
setActiveSlide(event.nextItem.index);
setIntervalDelay(null); // Stop the interval
}
});

Expand All @@ -130,6 +145,7 @@ const FeedbackCarousel = ({ items }: FeedbackCarouselProps) => {
onClick={() => {
slideToItem(index);
setActiveSlide(index);
setIntervalDelay(null); // Stop the interval
}}
>
<div className="feedback-carousel__dot" />
Expand Down
25 changes: 25 additions & 0 deletions src/hooks/useInterval.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// shamelessly stolen from usehooks-ts: https://usehooks-ts.com/react-hook/use-interval

import { useEffect, useLayoutEffect, useRef } from "react";

export function useInterval(callback: () => void, delay: number | null) {
const savedCallback = useRef(callback);

// Remember the latest callback if it changes.
useLayoutEffect(() => {
savedCallback.current = callback;
}, [callback]);

// Set up the interval.
useEffect(() => {
// Don't schedule if no delay is specified.
// Note: 0 is a valid value for delay.
if (!delay && delay !== 0) {
return;
}

const id = setInterval(() => savedCallback.current(), delay);

return () => clearInterval(id);
}, [delay]);
}

0 comments on commit 37699f5

Please sign in to comment.