forked from jordanlambrecht/tracker-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarqueeText.tsx
More file actions
62 lines (52 loc) · 1.65 KB
/
Copy pathMarqueeText.tsx
File metadata and controls
62 lines (52 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// src/components/ui/MarqueeText.tsx
"use client"
import clsx from "clsx"
import { type ReactNode, useEffect, useRef, useState } from "react"
interface MarqueeTextProps {
children: ReactNode
className?: string
speed?: number
}
function MarqueeText({ children, className = "", speed = 40 }: MarqueeTextProps) {
const containerRef = useRef<HTMLDivElement>(null)
const textRef = useRef<HTMLSpanElement>(null)
const [overflows, setOverflows] = useState(false)
const [duration, setDuration] = useState(12)
// biome-ignore lint/correctness/useExhaustiveDependencies: refs don't need to be deps
useEffect(() => {
const container = containerRef.current
const text = textRef.current
if (!container || !text) return
function check() {
if (!container || !text) return
const isOverflowing = text.scrollWidth > container.clientWidth
setOverflows(isOverflowing)
if (isOverflowing) {
setDuration(text.scrollWidth / speed)
}
}
check()
const ro = new ResizeObserver(check)
ro.observe(container)
return () => ro.disconnect()
}, [speed, children])
return (
<div ref={containerRef} className={clsx("overflow-hidden whitespace-nowrap", className)}>
<span
ref={textRef}
className={clsx("inline-block", overflows && "marquee-scroll")}
style={overflows ? { animationDuration: `${duration}s` } : undefined}
>
{children}
{overflows && (
<span className="px-8" aria-hidden="true">
·
</span>
)}
{overflows && children}
</span>
</div>
)
}
export type { MarqueeTextProps }
export { MarqueeText }