22
33import { clsx } from "clsx"
44import { useMotionValue , animate , motion } from "motion/react"
5- import { useState , useEffect , Fragment } from "react"
5+ import { useState , useEffect , Fragment , useRef } from "react"
66import useMeasure from "react-use-measure"
77
88export interface MarqueeProps {
@@ -102,19 +102,27 @@ export function Marquee({
102102 }
103103 : { }
104104
105- const multiples = drag ? 12 : 2
106105 const dragProps = drag
107106 ? {
108- drag : " x" as const ,
107+ drag : direction === "horizontal" ? ( " x" as const ) : ( "y" as const ) ,
109108 onDragStart : ( ) => {
110109 document . documentElement . style . cursor = "grabbing"
111110 } ,
112111 onDragEnd : ( ) => {
113112 document . documentElement . style . cursor = "initial"
114113 } ,
114+ dragConstraints :
115+ direction === "horizontal"
116+ ? {
117+ right : 0 ,
118+ // window.innerWidth won't be stale because resizing the window triggers useMeasure
119+ left : window . innerWidth - width ,
120+ }
121+ : { } ,
115122 }
116123 : { }
117124
125+ const multiples = 2
118126 return (
119127 < div className = { clsx ( "overflow-hidden" , className ) } >
120128 < motion . div
@@ -131,10 +139,10 @@ export function Marquee({
131139 { ...dragProps }
132140 { ...hoverProps }
133141 >
134- { Array . from ( { length : multiples } ) . map ( ( _ , i ) => (
142+ { Array . from ( { length : 2 } ) . map ( ( _ , i ) => (
135143 < Fragment key = { i } >
136144 { children }
137- { separator }
145+ { i < multiples - 1 && separator }
138146 </ Fragment >
139147 ) ) }
140148 </ motion . div >
0 commit comments