forked from woowacourse/perf-basecamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomCursor.tsx
32 lines (26 loc) · 837 Bytes
/
CustomCursor.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
import { useEffect, useRef } from 'react';
import useMousePosition from '../../hooks/useMousePosition';
import styles from './CustomCursor.module.css';
type CustomCursorProps = {
text: string;
};
const CustomCursor = ({ text = '' }: CustomCursorProps) => {
const [...cursorTextChars] = text;
const mousePosition = useMousePosition();
const cursorRef = useRef<HTMLDivElement>(null);
useEffect(() => {
if (cursorRef.current) {
cursorRef.current.style.transform = `translate(${mousePosition.pageX}px, ${mousePosition.pageY}px)`;
}
}, [mousePosition]);
return (
<div ref={cursorRef} className={styles.cursor}>
{cursorTextChars.map((char, index) => (
<span key={index} className={styles.character}>
{char}
</span>
))}
</div>
);
};
export default CustomCursor;