(e: MouseEvent)
| 43 | useEffect(() => { |
| 44 | // Not the most efficient way to do this but it works |
| 45 | const handleMouseMove = (e: MouseEvent) => { |
| 46 | const x = e.clientX; |
| 47 | const y = e.clientY; |
| 48 | const wrapperPosition = bbWrapperRef.current?.getBoundingClientRect(); |
| 49 | if ( |
| 50 | bbWrapperRef.current && |
| 51 | wrapperPosition && |
| 52 | x > wrapperPosition.left && |
| 53 | x < wrapperPosition.right && |
| 54 | y > wrapperPosition.top && |
| 55 | y < wrapperPosition.bottom |
| 56 | ) { |
| 57 | const rawBoxes = document.querySelectorAll(".bb"); |
| 58 | const positions = [...rawBoxes].map((box: Element) => { |
| 59 | const rect = (box as HTMLElement).getBoundingClientRect(); |
| 60 | return { |
| 61 | left: rect.left, |
| 62 | right: rect.right, |
| 63 | top: rect.top, |
| 64 | bottom: rect.bottom, |
| 65 | string: box.getAttribute("data-string"), |
| 66 | }; |
| 67 | }); |
| 68 | const matches = positions.filter( |
| 69 | (pos: { left: number; right: number; top: number; bottom: number }) => |
| 70 | x > pos.left && x < pos.right && y > pos.top && y < pos.bottom, |
| 71 | ); |
| 72 | // sort by distance to center |
| 73 | // @ts-ignore |
| 74 | matches.sort((a, b) => { |
| 75 | const aCenter = { |
| 76 | x: (a.left + a.right) / 2, |
| 77 | y: (a.top + a.bottom) / 2, |
| 78 | }; |
| 79 | const bCenter = { |
| 80 | x: (b.left + b.right) / 2, |
| 81 | y: (b.top + b.bottom) / 2, |
| 82 | }; |
| 83 | const aDistance = Math.sqrt( |
| 84 | (aCenter.x - x) ** 2 + (aCenter.y - y) ** 2, |
| 85 | ); |
| 86 | const bDistance = Math.sqrt( |
| 87 | (bCenter.x - x) ** 2 + (bCenter.y - y) ** 2, |
| 88 | ); |
| 89 | return aDistance - bDistance; |
| 90 | }); |
| 91 | if (matches.length > 0) { |
| 92 | const match = matches[0]; |
| 93 | setActiverHoverBox(match.string); |
| 94 | } else { |
| 95 | setActiverHoverBox(null); |
| 96 | } |
| 97 | } |
| 98 | }; |
| 99 | window.addEventListener("mousemove", handleMouseMove); |
| 100 | return () => { |
| 101 | window.removeEventListener("mousemove", handleMouseMove); |
nothing calls this directly
no outgoing calls
no test coverage detected