|
| 1 | +"use client" |
| 2 | + |
| 3 | +import { clsx } from "clsx" |
| 4 | +import { useMotionValue, animate, motion } from "motion/react" |
| 5 | +import { useState, useEffect } from "react" |
| 6 | +import useMeasure from "react-use-measure" |
| 7 | + |
| 8 | +export interface MarqueeProps { |
| 9 | + children: React.ReactNode |
| 10 | + gap?: number |
| 11 | + speed?: number |
| 12 | + speedOnHover?: number |
| 13 | + direction?: "horizontal" | "vertical" |
| 14 | + reverse?: boolean |
| 15 | + className?: string |
| 16 | +} |
| 17 | + |
| 18 | +export function Marquee({ |
| 19 | + children, |
| 20 | + gap = 16, |
| 21 | + speed = 100, |
| 22 | + speedOnHover, |
| 23 | + direction = "horizontal", |
| 24 | + reverse = false, |
| 25 | + className, |
| 26 | +}: MarqueeProps) { |
| 27 | + const [currentSpeed, setCurrentSpeed] = useState(speed) |
| 28 | + const [ref, { width, height }] = useMeasure() |
| 29 | + const translation = useMotionValue(0) |
| 30 | + const [isTransitioning, setIsTransitioning] = useState(false) |
| 31 | + const [key, setKey] = useState(0) |
| 32 | + |
| 33 | + useEffect(() => { |
| 34 | + let controls |
| 35 | + const size = direction === "horizontal" ? width : height |
| 36 | + const contentSize = size + gap |
| 37 | + const from = reverse ? 0 : -contentSize / 2 |
| 38 | + const to = reverse ? -contentSize / 2 : 0 |
| 39 | + |
| 40 | + const distanceToTravel = Math.abs(to - from) |
| 41 | + const duration = distanceToTravel / currentSpeed |
| 42 | + |
| 43 | + if (isTransitioning) { |
| 44 | + const remainingDistance = Math.abs(translation.get() - to) |
| 45 | + const transitionDuration = remainingDistance / currentSpeed |
| 46 | + |
| 47 | + controls = animate(translation, [translation.get(), to], { |
| 48 | + ease: "linear", |
| 49 | + duration: transitionDuration, |
| 50 | + onComplete: () => { |
| 51 | + setIsTransitioning(false) |
| 52 | + setKey(prevKey => prevKey + 1) |
| 53 | + }, |
| 54 | + }) |
| 55 | + } else { |
| 56 | + controls = animate(translation, [from, to], { |
| 57 | + ease: "linear", |
| 58 | + duration: duration, |
| 59 | + repeat: Infinity, |
| 60 | + repeatType: "loop", |
| 61 | + repeatDelay: 0, |
| 62 | + onRepeat: () => { |
| 63 | + translation.set(from) |
| 64 | + }, |
| 65 | + }) |
| 66 | + } |
| 67 | + |
| 68 | + return controls?.stop |
| 69 | + }, [ |
| 70 | + key, |
| 71 | + translation, |
| 72 | + currentSpeed, |
| 73 | + width, |
| 74 | + height, |
| 75 | + gap, |
| 76 | + isTransitioning, |
| 77 | + direction, |
| 78 | + reverse, |
| 79 | + ]) |
| 80 | + |
| 81 | + const hoverProps = |
| 82 | + speedOnHover != null |
| 83 | + ? { |
| 84 | + onHoverStart: () => { |
| 85 | + setIsTransitioning(true) |
| 86 | + setCurrentSpeed(speedOnHover) |
| 87 | + }, |
| 88 | + onHoverEnd: () => { |
| 89 | + setIsTransitioning(true) |
| 90 | + setCurrentSpeed(speed) |
| 91 | + }, |
| 92 | + } |
| 93 | + : {} |
| 94 | + |
| 95 | + return ( |
| 96 | + <div className={clsx("overflow-hidden", className)}> |
| 97 | + <motion.div |
| 98 | + className="flex w-max" |
| 99 | + drag="x" |
| 100 | + onDragStart={event => { |
| 101 | + ;(event.target as HTMLElement).style.cursor = "grabbing" |
| 102 | + }} |
| 103 | + onDragEnd={event => { |
| 104 | + ;(event.target as HTMLElement).style.cursor = "initial" |
| 105 | + }} |
| 106 | + style={{ |
| 107 | + ...(direction === "horizontal" |
| 108 | + ? { x: translation } |
| 109 | + : { y: translation }), |
| 110 | + gap: `${gap}px`, |
| 111 | + flexDirection: direction === "horizontal" ? "row" : "column", |
| 112 | + alignItems: "center", |
| 113 | + }} |
| 114 | + ref={ref} |
| 115 | + {...hoverProps} |
| 116 | + > |
| 117 | + {children} |
| 118 | + {children} |
| 119 | + </motion.div> |
| 120 | + </div> |
| 121 | + ) |
| 122 | +} |
0 commit comments