-
Notifications
You must be signed in to change notification settings - Fork 1
/
mouse.js
55 lines (47 loc) · 1.21 KB
/
mouse.js
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
import { useState, useEffect } from 'react';
import { createHook } from 'hookleton';
const useMousePosition = createHook(() => {
const [position, setPosition] = useState({ x: null, y: null });
const handleMouseMove = e => setPosition({ x: e.pageX, y: e.pageY });
useEffect(() => {
window.addEventListener('mousemove', handleMouseMove);
return () => window.removeEventListener('mousemove', handleMouseMove);
}, []);
return [position];
});
const MouseHost = () => (useMousePosition.use(), null);
const Mouse = () => {
const [mousePosition] = useMousePosition();
return (
<div style={{ borderStyle: 'solid' }}>
x: {mousePosition.x}, y: {mousePosition.y}
</div>
);
};
// put <Mouse /> in a table of 1x12 cells
const TableMouse = () => (
<table>
<tbody>
<tr>
{Array.from({ length: 12 }).map((_, idx) => (
<td key={idx}>
<Mouse />
</td>
))}
</tr>
</tbody>
</table>
);
// repeat <TableMouse /> 100 times
export default () => (
<>
<MouseHost />
<ul style={{ listStyle: 'none' }}>
{Array.from({ length: 100 }).map((_, idx) => (
<li key={idx}>
<TableMouse />
</li>
))}
</ul>
</>
);