fix: require click for card before-after slider and restore horizontal showcase scroll
This commit is contained in:
@@ -18,7 +18,8 @@ export function CardBeforeAfterSlider({
|
||||
aspectRatio = 'aspect-[16/10]',
|
||||
}: CardBeforeAfterSliderProps) {
|
||||
const containerRef = useRef<HTMLDivElement>(null);
|
||||
const isHoveredRef = useRef(false);
|
||||
const isDraggingRef = useRef(false);
|
||||
const isInteractedRef = useRef(false);
|
||||
const animIdRef = useRef<number>(0);
|
||||
|
||||
const setSplit = useCallback((percent: number) => {
|
||||
@@ -29,29 +30,38 @@ export function CardBeforeAfterSlider({
|
||||
}
|
||||
}, []);
|
||||
|
||||
// Continuous auto-sliding animation when not actively hovered
|
||||
const updateSplitFromClientX = useCallback(
|
||||
(clientX: number) => {
|
||||
if (!containerRef.current) return;
|
||||
const rect = containerRef.current.getBoundingClientRect();
|
||||
const x = Math.max(0, Math.min(clientX - rect.left, rect.width));
|
||||
const percent = (x / rect.width) * 100;
|
||||
setSplit(percent);
|
||||
},
|
||||
[setSplit]
|
||||
);
|
||||
|
||||
// Gentle auto-sliding animation until the user actively clicks
|
||||
useEffect(() => {
|
||||
let startTime: number | null = null;
|
||||
let isVisible = true;
|
||||
|
||||
const animate = (time: number) => {
|
||||
if (!isVisible) return;
|
||||
if (!isHoveredRef.current) {
|
||||
if (!isInteractedRef.current) {
|
||||
if (startTime === null) startTime = time;
|
||||
const elapsed = time - startTime;
|
||||
// Smooth sine wave between 28% and 72% over a 3.4-second cycle
|
||||
const currentSplit = 50 + 22 * Math.sin((elapsed / 3400) * 2 * Math.PI);
|
||||
setSplit(currentSplit);
|
||||
} else {
|
||||
startTime = null;
|
||||
animIdRef.current = requestAnimationFrame(animate);
|
||||
}
|
||||
animIdRef.current = requestAnimationFrame(animate);
|
||||
};
|
||||
|
||||
const observer = new IntersectionObserver(
|
||||
([entry]) => {
|
||||
isVisible = entry.isIntersecting;
|
||||
if (isVisible) {
|
||||
if (isVisible && !isInteractedRef.current) {
|
||||
startTime = null;
|
||||
animIdRef.current = requestAnimationFrame(animate);
|
||||
} else {
|
||||
@@ -65,7 +75,9 @@ export function CardBeforeAfterSlider({
|
||||
observer.observe(containerRef.current);
|
||||
}
|
||||
|
||||
animIdRef.current = requestAnimationFrame(animate);
|
||||
if (!isInteractedRef.current) {
|
||||
animIdRef.current = requestAnimationFrame(animate);
|
||||
}
|
||||
|
||||
return () => {
|
||||
cancelAnimationFrame(animIdRef.current);
|
||||
@@ -73,45 +85,63 @@ export function CardBeforeAfterSlider({
|
||||
};
|
||||
}, [setSplit]);
|
||||
|
||||
// Mouse move scrubber
|
||||
const handleMouseMove = useCallback(
|
||||
// Click & Drag handlers: DOES NOT WORK ON HOVER - REQUIRES CURSOR CLICK!
|
||||
const handleMouseDown = useCallback(
|
||||
(e: React.MouseEvent<HTMLDivElement>) => {
|
||||
isHoveredRef.current = true;
|
||||
if (!containerRef.current) return;
|
||||
const rect = containerRef.current.getBoundingClientRect();
|
||||
const x = Math.max(0, Math.min(e.clientX - rect.left, rect.width));
|
||||
const percent = (x / rect.width) * 100;
|
||||
setSplit(percent);
|
||||
e.preventDefault();
|
||||
isDraggingRef.current = true;
|
||||
isInteractedRef.current = true;
|
||||
cancelAnimationFrame(animIdRef.current);
|
||||
updateSplitFromClientX(e.clientX);
|
||||
|
||||
const handleGlobalMouseMove = (moveEvent: MouseEvent) => {
|
||||
if (!isDraggingRef.current) return;
|
||||
updateSplitFromClientX(moveEvent.clientX);
|
||||
};
|
||||
|
||||
const handleGlobalMouseUp = () => {
|
||||
isDraggingRef.current = false;
|
||||
window.removeEventListener('mousemove', handleGlobalMouseMove);
|
||||
window.removeEventListener('mouseup', handleGlobalMouseUp);
|
||||
};
|
||||
|
||||
window.addEventListener('mousemove', handleGlobalMouseMove);
|
||||
window.addEventListener('mouseup', handleGlobalMouseUp);
|
||||
},
|
||||
[setSplit]
|
||||
[updateSplitFromClientX]
|
||||
);
|
||||
|
||||
// Touch handlers for mobile/tablets
|
||||
const handleTouchStart = useCallback(
|
||||
(e: React.TouchEvent<HTMLDivElement>) => {
|
||||
if (!e.touches[0]) return;
|
||||
isDraggingRef.current = true;
|
||||
isInteractedRef.current = true;
|
||||
cancelAnimationFrame(animIdRef.current);
|
||||
updateSplitFromClientX(e.touches[0].clientX);
|
||||
},
|
||||
[updateSplitFromClientX]
|
||||
);
|
||||
|
||||
// Touch move scrubber
|
||||
const handleTouchMove = useCallback(
|
||||
(e: React.TouchEvent<HTMLDivElement>) => {
|
||||
isHoveredRef.current = true;
|
||||
if (!containerRef.current || !e.touches[0]) return;
|
||||
const rect = containerRef.current.getBoundingClientRect();
|
||||
const x = Math.max(0, Math.min(e.touches[0].clientX - rect.left, rect.width));
|
||||
const percent = (x / rect.width) * 100;
|
||||
setSplit(percent);
|
||||
if (!isDraggingRef.current || !e.touches[0]) return;
|
||||
updateSplitFromClientX(e.touches[0].clientX);
|
||||
},
|
||||
[setSplit]
|
||||
[updateSplitFromClientX]
|
||||
);
|
||||
|
||||
const handlePointerLeave = useCallback(() => {
|
||||
isHoveredRef.current = false;
|
||||
const handleTouchEnd = useCallback(() => {
|
||||
isDraggingRef.current = false;
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div
|
||||
ref={containerRef}
|
||||
onMouseEnter={() => { isHoveredRef.current = true; }}
|
||||
onMouseLeave={handlePointerLeave}
|
||||
onMouseMove={handleMouseMove}
|
||||
onTouchStart={() => { isHoveredRef.current = true; }}
|
||||
onMouseDown={handleMouseDown}
|
||||
onTouchStart={handleTouchStart}
|
||||
onTouchMove={handleTouchMove}
|
||||
onTouchEnd={handlePointerLeave}
|
||||
onTouchEnd={handleTouchEnd}
|
||||
role="slider"
|
||||
aria-label="Before and after transformation slider"
|
||||
aria-valuenow={50}
|
||||
@@ -168,9 +198,9 @@ export function CardBeforeAfterSlider({
|
||||
{afterLabel}
|
||||
</div>
|
||||
|
||||
{/* Scrub Hint */}
|
||||
<div className="pointer-events-none absolute top-2.5 right-2.5 z-20 rounded-full bg-white/90 px-2 py-0.5 text-[9px] font-semibold text-slate-700 shadow-sm backdrop-blur-xs opacity-0 group-hover/slider:opacity-100 transition-opacity">
|
||||
Scrub to compare
|
||||
{/* Click / Drag Hint */}
|
||||
<div className="pointer-events-none absolute top-2.5 right-2.5 z-20 rounded-full bg-white/90 px-2.5 py-0.5 text-[9px] font-semibold text-slate-700 shadow-sm backdrop-blur-xs opacity-0 group-hover/slider:opacity-100 transition-opacity">
|
||||
Click & drag to compare
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user