feat: real-time auto-sliding and hover-scrubbing cards in tool stack, and interactive 2-column Try It transformation studio on tool pages
This commit is contained in:
@@ -0,0 +1,177 @@
|
||||
'use client';
|
||||
|
||||
import React, { useRef, useEffect, useCallback } from 'react';
|
||||
|
||||
interface CardBeforeAfterSliderProps {
|
||||
beforeUrl: string;
|
||||
afterUrl: string;
|
||||
beforeLabel?: string;
|
||||
afterLabel?: string;
|
||||
aspectRatio?: string;
|
||||
}
|
||||
|
||||
export function CardBeforeAfterSlider({
|
||||
beforeUrl,
|
||||
afterUrl,
|
||||
beforeLabel = 'BEFORE',
|
||||
afterLabel = 'AFTER',
|
||||
aspectRatio = 'aspect-[16/10]',
|
||||
}: CardBeforeAfterSliderProps) {
|
||||
const containerRef = useRef<HTMLDivElement>(null);
|
||||
const isHoveredRef = useRef(false);
|
||||
const animIdRef = useRef<number>(0);
|
||||
|
||||
const setSplit = useCallback((percent: number) => {
|
||||
const clamped = Math.max(3, Math.min(97, percent));
|
||||
if (containerRef.current) {
|
||||
containerRef.current.style.setProperty('--split', `${clamped}%`);
|
||||
containerRef.current.setAttribute('aria-valuenow', `${Math.round(clamped)}`);
|
||||
}
|
||||
}, []);
|
||||
|
||||
// Continuous auto-sliding animation when not actively hovered
|
||||
useEffect(() => {
|
||||
let startTime: number | null = null;
|
||||
let isVisible = true;
|
||||
|
||||
const animate = (time: number) => {
|
||||
if (!isVisible) return;
|
||||
if (!isHoveredRef.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);
|
||||
};
|
||||
|
||||
const observer = new IntersectionObserver(
|
||||
([entry]) => {
|
||||
isVisible = entry.isIntersecting;
|
||||
if (isVisible) {
|
||||
startTime = null;
|
||||
animIdRef.current = requestAnimationFrame(animate);
|
||||
} else {
|
||||
cancelAnimationFrame(animIdRef.current);
|
||||
}
|
||||
},
|
||||
{ threshold: 0.05 }
|
||||
);
|
||||
|
||||
if (containerRef.current) {
|
||||
observer.observe(containerRef.current);
|
||||
}
|
||||
|
||||
animIdRef.current = requestAnimationFrame(animate);
|
||||
|
||||
return () => {
|
||||
cancelAnimationFrame(animIdRef.current);
|
||||
observer.disconnect();
|
||||
};
|
||||
}, [setSplit]);
|
||||
|
||||
// Mouse move scrubber
|
||||
const handleMouseMove = 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);
|
||||
},
|
||||
[setSplit]
|
||||
);
|
||||
|
||||
// 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);
|
||||
},
|
||||
[setSplit]
|
||||
);
|
||||
|
||||
const handlePointerLeave = useCallback(() => {
|
||||
isHoveredRef.current = false;
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div
|
||||
ref={containerRef}
|
||||
onMouseEnter={() => { isHoveredRef.current = true; }}
|
||||
onMouseLeave={handlePointerLeave}
|
||||
onMouseMove={handleMouseMove}
|
||||
onTouchStart={() => { isHoveredRef.current = true; }}
|
||||
onTouchMove={handleTouchMove}
|
||||
onTouchEnd={handlePointerLeave}
|
||||
role="slider"
|
||||
aria-label="Before and after transformation slider"
|
||||
aria-valuenow={50}
|
||||
tabIndex={0}
|
||||
style={{ '--split': '50%' } as React.CSSProperties}
|
||||
className={`relative w-full ${aspectRatio} overflow-hidden cursor-ew-resize select-none bg-slate-900 group/slider`}
|
||||
>
|
||||
{/* Layer 1: After Image (Transformed Neural Result) */}
|
||||
{/* eslint-disable-next-line @next/next/no-img-element */}
|
||||
<img
|
||||
src={afterUrl}
|
||||
alt="AI enhanced result"
|
||||
loading="lazy"
|
||||
decoding="async"
|
||||
className="absolute inset-0 h-full w-full object-cover pointer-events-none"
|
||||
/>
|
||||
|
||||
{/* Layer 2: Before Image (Clipped Left by CSS variable --split) */}
|
||||
<div
|
||||
className="absolute inset-0 overflow-hidden pointer-events-none"
|
||||
style={{ clipPath: 'polygon(0 0, var(--split) 0, var(--split) 100%, 0 100%)' }}
|
||||
>
|
||||
{/* eslint-disable-next-line @next/next/no-img-element */}
|
||||
<img
|
||||
src={beforeUrl}
|
||||
alt="Original input"
|
||||
loading="lazy"
|
||||
decoding="async"
|
||||
className="absolute inset-0 h-full w-full object-cover"
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Layer 3: Vertical Split Divider Line */}
|
||||
<div
|
||||
className="absolute top-0 bottom-0 w-[2.5px] bg-white z-20 pointer-events-none shadow-[0_0_10px_rgba(0,0,0,0.8)]"
|
||||
style={{ left: 'var(--split)' }}
|
||||
>
|
||||
{/* Glowing Aura */}
|
||||
<div className="absolute -top-4 -bottom-4 -left-1.5 w-3.5 bg-gradient-to-r from-blue-500/30 via-white/70 to-amber-400/30 blur-[2px]" />
|
||||
|
||||
{/* Center Pill Handle with Left/Right Arrows */}
|
||||
<div className="absolute top-1/2 -translate-y-1/2 -translate-x-1/2 w-8 h-8 sm:w-9 sm:h-9 rounded-full bg-white text-slate-800 shadow-xl border border-slate-900/15 flex items-center justify-center transition-transform group-hover/slider:scale-110">
|
||||
<svg className="w-3.5 h-3.5 text-blue-700" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2.5} d="M8 9l-3 3 3 3m8-6l3 3-3 3" />
|
||||
</svg>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Floating Corner Badges */}
|
||||
<div className="pointer-events-none absolute bottom-2.5 left-2.5 z-20 rounded-md bg-black/70 px-2 py-0.5 text-[9px] font-mono font-bold tracking-wider text-white backdrop-blur-sm border border-white/10 shadow-sm">
|
||||
{beforeLabel}
|
||||
</div>
|
||||
<div className="pointer-events-none absolute bottom-2.5 right-2.5 z-20 rounded-md bg-blue-600/90 px-2 py-0.5 text-[9px] font-mono font-bold tracking-wider text-white backdrop-blur-sm shadow-sm border border-white/20">
|
||||
{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
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user