-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSongName.tsx
78 lines (60 loc) · 1.73 KB
/
SongName.tsx
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import React, { useEffect, useRef } from 'react';
import styles from './SongName.module.css';
interface SongNameProps {
name: string;
messageText?: string;
}
function SongName(props: SongNameProps) {
let textRef = useRef<HTMLDivElement>(null);
useEffect(() => {
if (props.messageText) {
return;
}
const div = textRef.current;
if (!div) {
throw new Error('Not found div with name');
}
let stopAnimation = false;
let lastFrameTime = performance.now();
requestAnimationFrame(function animate(time) {
if (stopAnimation) {
return;
}
const width = -1 * (div.querySelector('span')?.offsetWidth || 0);
if (time - lastFrameTime > 17) {
const current = parseInt(div.style.left) || 0;
let next = current - 1;
if (next < width) {
next = 0;
const firstSpan = div.querySelector('span');
if (!firstSpan) {
throw new Error('Not found span');
}
div.removeChild(firstSpan);
div.append(firstSpan);
}
div.style.left = next + 'px';
lastFrameTime = time;
}
requestAnimationFrame(animate);
});
return () => {
stopAnimation = true;
div.style.left = '5px';
};
}, [textRef, props.messageText, props.name]);
return (
<div className={['withBorder', styles.songName].join(' ')}>
{props.messageText ? (
<div className={styles.nameText}>{props.messageText}</div>
) : (
<div ref={textRef} className={styles.nameText}>
<span>{props.name} *** </span>
<span>{props.name} *** </span>
<span>{props.name} *** </span>
</div>
)}
</div>
);
}
export default SongName;