Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wethegit/react-marquee",
"version": "2.2.0",
"version": "2.3.0",
"description": "",
"files": [
"dist"
Expand Down
4 changes: 2 additions & 2 deletions src/lib/marquee.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

@keyframes scroll {
0% {
transform: translateX(0%);
transform: translateX(calc(var(--padding) * -1px));
}

100% {
transform: translateX(calc(var(--marquee-width) * -1px));
transform: translateX(calc((var(--padding) + var(--marquee-width)) * -1px));
}
}
}
Expand Down
20 changes: 13 additions & 7 deletions src/lib/marquee.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ export interface MarqueeProps extends React.ComponentPropsWithoutRef<"div"> {
* Whether to animate from left to right
*/
reverse?: boolean
/**
* Extra spacing in pixels that the slide should travel beyond its width.
* This ensures the slide is completely offscreen before the loop restarts.
* Default value is 10.
*/
padding?: number
}

type Timer = ReturnType<typeof setTimeout>
Expand All @@ -40,11 +46,13 @@ export function Marquee({
reducedMotionSpeed = 20,
prefersReducedMotion = false,
playing = true,
padding = 10,
reverse,
children,
className,
...props
}: MarqueeProps) {
// const [containerWidth, setContainerWidth] = useState(0)
const [marqueeWidth, setMarqueeWidth] = useState(0)
const [duration, setDuration] = useState(0)
const [neededAmount, setNeededAmount] = useState(1)
Expand Down Expand Up @@ -77,12 +85,9 @@ export function Marquee({
setMarqueeWidth(marqueeWidth)

// Set duration speed, which is used below in the --duration inline style.
if (marqueeWidth < containerWidth) {
setDuration(containerWidth / speedAmount)
} else {
setDuration(marqueeWidth / speedAmount)
}
}, [prefersReducedMotion, reducedMotionSpeed, speed])
// Include padding in the total distance
setDuration((marqueeWidth + padding) / speedAmount)
}, [prefersReducedMotion, reducedMotionSpeed, speed, padding])

useEffect(() => {
updateState()
Expand Down Expand Up @@ -113,9 +118,10 @@ export function Marquee({
])}
style={
{
"--marquee-width": marqueeWidth,
"--marquee-width": `${Math.floor(marqueeWidth)}`,
"--duration": duration + `s`,
"--animation-state": playing ? "running" : "paused",
"--padding": padding,
} as React.CSSProperties
}
>
Expand Down