Skip to content

Commit 67cfdda

Browse files
committed
fix via text scroll speed calculation
1 parent 10596bf commit 67cfdda

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/routes/departureboard/-components/BoardViaInfo.tsx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ type BoardViaInfoProps = {
88

99
export const BoardViaInfo: FC<BoardViaInfoProps> = ({ via }) => {
1010
// code that checks if the via text is overflowing the width of the screen
11+
const measureRef = useRef<HTMLDivElement>(null);
1112
const containerRef = useRef<HTMLDivElement>(null);
1213
const [isOverflowing, setIsOverflowing] = useState(false);
1314
useEffect(() => {
1415
const container = containerRef.current;
15-
if (container) {
16-
const checkOverflow = () => setIsOverflowing(container.scrollWidth > container.clientWidth);
16+
const textMeasureDiv = measureRef.current;
17+
if (container && textMeasureDiv) {
18+
const checkOverflow = () => setIsOverflowing(textMeasureDiv.scrollWidth > container.clientWidth);
1719
checkOverflow(); // initial check
1820

1921
window.addEventListener("resize", checkOverflow);
@@ -26,18 +28,25 @@ export const BoardViaInfo: FC<BoardViaInfoProps> = ({ via }) => {
2628
const animatedDivRef = useRef<HTMLDivElement>(null);
2729
useEffect(() => {
2830
const container = containerRef.current;
31+
const textMeasureDiv = measureRef.current;
2932
const animatedDiv = animatedDivRef.current;
30-
if (container && animatedDiv && isOverflowing) {
33+
if (container && textMeasureDiv && animatedDiv && isOverflowing) {
3134
const clientWidth = container.clientWidth;
32-
const scrollWidth = container.scrollWidth;
33-
const animationDuration = (scrollWidth * 10) / clientWidth;
35+
const scrollWidth = textMeasureDiv.scrollWidth;
36+
const animationDuration = (scrollWidth + clientWidth) / 50;
3437
animatedDiv.style.animationDuration = `${animationDuration}s`;
3538
}
3639
}, [isOverflowing]);
3740

3841
const viaPoints = createViaElements(via);
3942
return (
4043
<div ref={containerRef} className={"ml-7 pt-2 overflow-hidden h-max text-start w-3/4"}>
44+
<div
45+
ref={measureRef}
46+
className="absolute tracking-tight text-2xl invisible whitespace-nowrap pointer-events-none select-none"
47+
>
48+
{...viaPoints}
49+
</div>
4150
<div
4251
ref={animatedDivRef}
4352
className={cn(

0 commit comments

Comments
 (0)