diff --git a/src/components/magicui/infinite-moving-cards.tsx b/src/components/magicui/infinite-moving-cards.tsx new file mode 100644 index 0000000..8fbee83 --- /dev/null +++ b/src/components/magicui/infinite-moving-cards.tsx @@ -0,0 +1,105 @@ +"use client"; + +import { cn } from "@/lib/utils"; +import React, { useCallback, useEffect, useState } from "react"; + +export const InfiniteMovingCards = ({ + items, + direction = "left", + speed = "fast", + pauseOnHover = true, + className, +}: { + items: React.ReactNode[]; + direction?: "left" | "right"; + speed?: "fast" | "normal" | "slow"; + pauseOnHover?: boolean; + className?: string; +}): JSX.Element => { + const containerRef = React.useRef(null); + const scrollerRef = React.useRef(null); + const [start, setStart] = useState(false); + + const getDirection = useCallback(() => { + if (containerRef.current) { + if (direction === "left") { + containerRef.current.style.setProperty("--animation-direction", "forwards"); + } else { + containerRef.current.style.setProperty("--animation-direction", "reverse"); + } + } + }, [direction]); + + const getSpeed = useCallback(() => { + if (containerRef.current) { + if (speed === "fast") { + containerRef.current.style.setProperty("--animation-duration", "20s"); + } else if (speed === "normal") { + containerRef.current.style.setProperty("--animation-duration", "40s"); + } else { + containerRef.current.style.setProperty("--animation-duration", "80s"); + } + } + }, [speed]); + + const addAnimation = useCallback(() => { + if (containerRef.current && scrollerRef.current) { + const scrollerContent = Array.from(scrollerRef.current.children); + + scrollerContent.forEach((item) => { + const duplicatedItem = item.cloneNode(true); + if (scrollerRef.current) { + scrollerRef.current.appendChild(duplicatedItem); + } + }); + + getDirection(); + getSpeed(); + setStart(true); + } + }, [getDirection, getSpeed]); + + useEffect(() => { + addAnimation(); + }, [addAnimation]); + + return ( +
+
    + {items.map((item, idx) => ( +
  • +
    + + + {item} + +
    +
  • + ))} +
+
+ ); +};