File size: 5,896 Bytes
f56a29b | 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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
import { useRef, useState, useLayoutEffect, useCallback } from 'react';
import { motion, AnimatePresence } from 'motion/react';
import { useSceneSelector } from '@/lib/contexts/scene-context';
import { useCanvasStore } from '@/lib/store/canvas';
import type { SlideContent } from '@/lib/types/stage';
import type { PPTElement } from '@/lib/types/slides';
interface SpotlightRect {
x: number;
y: number;
w: number;
h: number;
}
/**
* Spotlight overlay component
*
* Uses DOM measurement (getBoundingClientRect) to compute spotlight position,
* avoiding alignment offsets from percentage coordinate conversion.
*/
export function SpotlightOverlay() {
const spotlightElementId = useCanvasStore.use.spotlightElementId();
const spotlightOptions = useCanvasStore.use.spotlightOptions();
const containerRef = useRef<HTMLDivElement>(null);
const [rect, setRect] = useState<SpotlightRect | null>(null);
const elements = useSceneSelector<SlideContent, PPTElement[]>(
(content) => content.canvas.elements,
);
// Compute target element position in SVG coordinate system via DOM measurement
const measure = useCallback(() => {
if (!spotlightElementId || !containerRef.current) {
setRect(null);
return;
}
const domElement = document.getElementById(`screen-element-${spotlightElementId}`);
if (!domElement) {
setRect(null);
return;
}
// Prefer measuring .element-content (the actual rendered area for auto-height)
const contentEl = domElement.querySelector('.element-content');
const targetEl = contentEl ?? domElement;
const containerRect = containerRef.current.getBoundingClientRect();
const targetRect = targetEl.getBoundingClientRect();
if (containerRect.width === 0 || containerRect.height === 0) {
setRect(null);
return;
}
// Convert to SVG viewBox 0-100 coordinates
setRect({
x: ((targetRect.left - containerRect.left) / containerRect.width) * 100,
y: ((targetRect.top - containerRect.top) / containerRect.height) * 100,
w: (targetRect.width / containerRect.width) * 100,
h: (targetRect.height / containerRect.height) * 100,
});
}, [spotlightElementId]);
useLayoutEffect(() => {
// eslint-disable-next-line react-hooks/set-state-in-effect -- DOM measurement requires effect
measure();
}, [measure, elements]);
const active = !!spotlightElementId && !!spotlightOptions && !!rect;
const dimness = spotlightOptions?.dimness ?? 0.7;
return (
<div
ref={containerRef}
className="absolute inset-0 z-[100] pointer-events-none overflow-hidden"
>
<AnimatePresence mode="wait">
{active && rect && (
<motion.div
key={`spotlight-${spotlightElementId}`}
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
className="absolute inset-0"
>
<svg
width="100%"
height="100%"
viewBox="0 0 100 100"
preserveAspectRatio="none"
className="absolute inset-0"
>
<defs>
<mask id={`mask-${spotlightElementId}`}>
{/* White background = show mask layer (dimmed) */}
<rect x="0" y="0" width="100" height="100" fill="white" />
{/* Black rectangle = hide mask layer (highlighted area / cutout) */}
<motion.rect
fill="black"
initial={{
x: rect.x - 8,
y: rect.y - 8,
width: rect.w + 16,
height: rect.h + 16,
rx: 4,
}}
animate={{
x: rect.x - 0.4,
y: rect.y - 0.6,
width: rect.w + 0.8,
height: rect.h + 1.2,
rx: 1,
}}
transition={{
duration: 0.6,
ease: [0.16, 1, 0.3, 1],
}}
/>
</mask>
</defs>
{/* Dimmed Background. No backdrop-filter: combined with SVG <mask>
it breaks compositing (backdrop bypasses the mask cutout) in some
browsers, leaving the focused area dimmed despite the cutout.
Tailwind 3 silently dropped `backdrop-blur-[1.5px]` on SVG via
--tw-* variables; Tailwind 4 emits the property directly and
surfaced the bug. */}
<rect
width="100"
height="100"
fill={`rgba(0,0,0,${dimness})`}
mask={`url(#mask-${spotlightElementId})`}
/>
{/* THE ONE BORDER - white border */}
<motion.rect
initial={{
x: rect.x - 4,
y: rect.y - 4,
width: rect.w + 8,
height: rect.h + 8,
opacity: 0,
rx: 2,
}}
animate={{
x: rect.x - 0.4,
y: rect.y - 0.6,
width: rect.w + 0.8,
height: rect.h + 1.2,
opacity: 1,
rx: 1,
}}
fill="none"
stroke="rgba(255,255,255,0.7)"
strokeWidth="1.2"
style={{ vectorEffect: 'non-scaling-stroke' } as React.CSSProperties}
transition={{
duration: 0.5,
delay: 0.05,
ease: [0.16, 1, 0.3, 1],
}}
/>
</svg>
</motion.div>
)}
</AnimatePresence>
</div>
);
}
|