|
| 1 | +import { useEffect, useRef, useState } from "react"; |
| 2 | + |
| 3 | +type HighlightRect = { |
| 4 | + top: number; |
| 5 | + left: number; |
| 6 | + width: number; |
| 7 | + height: number; |
| 8 | +}; |
| 9 | + |
| 10 | +type BlurOverlayProps = { |
| 11 | + active: boolean; |
| 12 | + onDone: () => void; |
| 13 | +}; |
| 14 | + |
| 15 | +const OVERLAY_ROOT_ID = "cap-extension-recorder-overlay"; |
| 16 | + |
| 17 | +export function BlurOverlay({ active, onDone }: BlurOverlayProps) { |
| 18 | + const [highlightRect, setHighlightRect] = useState<HighlightRect | null>( |
| 19 | + null, |
| 20 | + ); |
| 21 | + const rafRef = useRef<number | null>(null); |
| 22 | + |
| 23 | + useEffect(() => { |
| 24 | + if (!active) { |
| 25 | + setHighlightRect(null); |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + const handlePointerMove = (event: PointerEvent) => { |
| 30 | + if (rafRef.current !== null) return; |
| 31 | + rafRef.current = window.requestAnimationFrame(() => { |
| 32 | + rafRef.current = null; |
| 33 | + const target = document.elementFromPoint( |
| 34 | + event.clientX, |
| 35 | + event.clientY, |
| 36 | + ) as HTMLElement | null; |
| 37 | + |
| 38 | + if (!target || target.closest(`#${OVERLAY_ROOT_ID}`)) { |
| 39 | + setHighlightRect(null); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + const rect = target.getBoundingClientRect(); |
| 44 | + if (rect.width <= 0 || rect.height <= 0) { |
| 45 | + setHighlightRect(null); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + setHighlightRect({ |
| 50 | + top: rect.top, |
| 51 | + left: rect.left, |
| 52 | + width: rect.width, |
| 53 | + height: rect.height, |
| 54 | + }); |
| 55 | + }); |
| 56 | + }; |
| 57 | + |
| 58 | + const handleClick = (event: MouseEvent) => { |
| 59 | + const target = document.elementFromPoint( |
| 60 | + event.clientX, |
| 61 | + event.clientY, |
| 62 | + ) as HTMLElement | null; |
| 63 | + |
| 64 | + if (!target || target.closest(`#${OVERLAY_ROOT_ID}`)) { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + event.preventDefault(); |
| 69 | + event.stopPropagation(); |
| 70 | + |
| 71 | + if (target.dataset.capBlurred === "true") { |
| 72 | + const orig = target.dataset.capOrigFilter ?? ""; |
| 73 | + if (orig) { |
| 74 | + target.style.filter = orig; |
| 75 | + } else { |
| 76 | + target.style.removeProperty("filter"); |
| 77 | + } |
| 78 | + target.style.removeProperty("user-select"); |
| 79 | + delete target.dataset.capBlurred; |
| 80 | + delete target.dataset.capOrigFilter; |
| 81 | + } else { |
| 82 | + target.dataset.capOrigFilter = target.style.filter || ""; |
| 83 | + target.style.setProperty("filter", "blur(12px)", "important"); |
| 84 | + target.style.setProperty("user-select", "none", "important"); |
| 85 | + target.dataset.capBlurred = "true"; |
| 86 | + } |
| 87 | + }; |
| 88 | + |
| 89 | + const handleKeyDown = (event: KeyboardEvent) => { |
| 90 | + if (event.key === "Escape") { |
| 91 | + event.preventDefault(); |
| 92 | + event.stopPropagation(); |
| 93 | + onDone(); |
| 94 | + } |
| 95 | + }; |
| 96 | + |
| 97 | + window.addEventListener("pointermove", handlePointerMove, { |
| 98 | + capture: true, |
| 99 | + passive: true, |
| 100 | + }); |
| 101 | + window.addEventListener("click", handleClick, { |
| 102 | + capture: true, |
| 103 | + }); |
| 104 | + window.addEventListener("keydown", handleKeyDown, { capture: true }); |
| 105 | + |
| 106 | + return () => { |
| 107 | + if (rafRef.current !== null) { |
| 108 | + window.cancelAnimationFrame(rafRef.current); |
| 109 | + rafRef.current = null; |
| 110 | + } |
| 111 | + window.removeEventListener("pointermove", handlePointerMove, { |
| 112 | + capture: true, |
| 113 | + }); |
| 114 | + window.removeEventListener("click", handleClick, { |
| 115 | + capture: true, |
| 116 | + }); |
| 117 | + window.removeEventListener("keydown", handleKeyDown, { capture: true }); |
| 118 | + }; |
| 119 | + }, [active, onDone]); |
| 120 | + |
| 121 | + if (!active || !highlightRect) return null; |
| 122 | + |
| 123 | + return ( |
| 124 | + <div |
| 125 | + className="cap-extension-blur-highlight" |
| 126 | + style={{ |
| 127 | + top: `${highlightRect.top}px`, |
| 128 | + left: `${highlightRect.left}px`, |
| 129 | + width: `${highlightRect.width}px`, |
| 130 | + height: `${highlightRect.height}px`, |
| 131 | + }} |
| 132 | + aria-hidden |
| 133 | + > |
| 134 | + <span className="cap-extension-blur-tooltip">Click to blur / unblur</span> |
| 135 | + </div> |
| 136 | + ); |
| 137 | +} |
0 commit comments