perf(animations): refactor ShiftableToolsGrid, cursor, hero & sections to locked 60-120fps with zero layout thrashing
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
'use client';
|
||||
|
||||
import React, { useState, useRef, useEffect } from 'react';
|
||||
import React, { useState, useRef, useEffect, useCallback } from 'react';
|
||||
import Link from 'next/link';
|
||||
import { ArrowRight, Sparkles, Video, Sliders, Image as ImageIcon } from 'lucide-react';
|
||||
import { TextAnimate } from '@/components/magicui/text-animate';
|
||||
@@ -18,7 +18,7 @@ export interface ToolMediaItem {
|
||||
afterUrl?: string;
|
||||
}
|
||||
|
||||
interface ShiftableToolsGridProps {
|
||||
export interface ShiftableToolsGridProps {
|
||||
title?: string;
|
||||
subtitle?: string;
|
||||
tools?: ToolItem[];
|
||||
@@ -26,7 +26,7 @@ interface ShiftableToolsGridProps {
|
||||
locale?: string;
|
||||
}
|
||||
|
||||
const DEFAULT_TOOL_ITEMS: ToolMediaItem[] = [
|
||||
export const DEFAULT_TOOL_ITEMS: ToolMediaItem[] = [
|
||||
// ROW 1 (4 items) - Landscape 16:9 assets
|
||||
{
|
||||
id: 'tool-blemish',
|
||||
@@ -134,7 +134,7 @@ export function ShiftableToolsGridSection({
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* 2-Row Shiftable Grid Container (Pure Horizontal Expansion, Fixed Height, No Vertical Shifts) */}
|
||||
{/* 2-Row Shiftable Grid Container (Slow, Calm, Pure Horizontal Expansion with Locked Height) */}
|
||||
<div className="space-y-6 sm:space-y-8">
|
||||
<ShiftableRow items={row1} />
|
||||
<ShiftableRow items={row2} />
|
||||
@@ -144,10 +144,10 @@ export function ShiftableToolsGridSection({
|
||||
<div className="pt-6 text-center">
|
||||
<Link
|
||||
href="/tools"
|
||||
className="group inline-flex items-center gap-2.5 px-8 py-4 rounded-full font-bold text-xs sm:text-sm text-white bg-gradient-to-r from-slate-950 via-slate-900 to-indigo-950 hover:from-blue-600 hover:to-indigo-600 border border-white/10 shadow-lg shadow-slate-900/15 transition-all duration-300 hover:scale-105 active:scale-95"
|
||||
className="group inline-flex items-center gap-2.5 px-8 py-4 rounded-full font-bold text-xs sm:text-sm text-white bg-gradient-to-r from-slate-950 via-slate-900 to-indigo-950 hover:from-blue-600 hover:to-indigo-600 border border-white/10 shadow-lg shadow-slate-900/15 transition-[transform,opacity] duration-300 ease-[cubic-bezier(0.16,1,0.3,1)] hover:scale-105 active:scale-95"
|
||||
>
|
||||
<span>See More Tools</span>
|
||||
<ArrowRight className="w-4 h-4 transition-transform group-hover:translate-x-1" />
|
||||
<ArrowRight className="w-4 h-4 transition-transform duration-300 ease-[cubic-bezier(0.16,1,0.3,1)] group-hover:translate-x-1" />
|
||||
</Link>
|
||||
</div>
|
||||
</div>
|
||||
@@ -164,31 +164,44 @@ function ShiftableRow({ items }: { items: ToolMediaItem[] }) {
|
||||
const isHovered = hoveredId === item.id;
|
||||
const hasOtherHovered = hoveredId !== null && !isHovered;
|
||||
|
||||
// Luxurious, slow, and smooth horizontal expansion (flex weighting)
|
||||
// No vertical scaling, no height changes, purely horizontal shift
|
||||
const flexClass = isHovered
|
||||
? 'md:flex-[1.75]'
|
||||
: hasOtherHovered
|
||||
? 'md:flex-[0.75]'
|
||||
: 'md:flex-1';
|
||||
// Slow, calm, luxurious horizontal expansion:
|
||||
// When hovered: 1.85 flex-grow (~46% width)
|
||||
// When other is hovered: 0.716 flex-grow (~18% width)
|
||||
// At rest: 1.0 flex-grow (25% width each)
|
||||
// Sum is always mathematically conserved at exactly 4.0 (1.85 + 3 * 0.716 = 4.00)
|
||||
// Fixed flex-basis: 0 eliminates conflicting layout calculations
|
||||
const flexGrowValue = isHovered ? 1.85 : hasOtherHovered ? 0.716 : 1;
|
||||
|
||||
return (
|
||||
<div
|
||||
key={item.id}
|
||||
onMouseEnter={() => setHoveredId(item.id)}
|
||||
onMouseLeave={() => setHoveredId(null)}
|
||||
className={`w-full ${flexClass} transition-[flex-grow,flex-basis] duration-700 ease-[cubic-bezier(0.25,1,0.5,1)] flex flex-col flex-shrink-0 min-w-0`}
|
||||
style={{
|
||||
flexGrow: flexGrowValue,
|
||||
flexShrink: 1,
|
||||
flexBasis: '0%',
|
||||
}}
|
||||
className="w-full transition-[flex-grow] duration-1000 ease-[cubic-bezier(0.16,1,0.3,1)] will-change-[flex-grow] flex flex-col min-w-0"
|
||||
>
|
||||
{/* Window Container with strictly locked pixel height & landscape aspect */}
|
||||
<Link
|
||||
href={`/tools/${item.slug}`}
|
||||
className="group block relative w-full h-[220px] sm:h-[240px] lg:h-[260px] rounded-2xl sm:rounded-3xl overflow-hidden bg-slate-950 border border-slate-900/[0.08] shadow-cream-sm hover:shadow-cream-lg transition-shadow duration-500"
|
||||
className="group block relative w-full h-[220px] sm:h-[240px] lg:h-[260px] rounded-2xl sm:rounded-3xl overflow-hidden bg-slate-950 border border-slate-900/[0.08] shadow-cream-sm"
|
||||
>
|
||||
{/* GPU-Composited Shadow Elevation Layer (Opacity-only transition, zero paint thrash) */}
|
||||
<div
|
||||
className="absolute -inset-px rounded-2xl sm:rounded-3xl shadow-cream-lg opacity-0 group-hover:opacity-100 transition-opacity duration-700 ease-[cubic-bezier(0.16,1,0.3,1)] pointer-events-none z-30"
|
||||
aria-hidden="true"
|
||||
/>
|
||||
|
||||
{/* Media Renderer */}
|
||||
<ToolWindowMedia item={item} isHovered={isHovered} />
|
||||
|
||||
{/* Hover Media Type Pill */}
|
||||
<div className="absolute top-3 left-3 z-20 pointer-events-none opacity-0 group-hover:opacity-100 transition-opacity duration-500">
|
||||
{/* Hover Media Type Pill (Smooth GPU Opacity Transition) */}
|
||||
<div
|
||||
className="absolute top-3 left-3 z-20 pointer-events-none opacity-0 group-hover:opacity-100 transition-opacity duration-500 ease-[cubic-bezier(0.16,1,0.3,1)]"
|
||||
>
|
||||
<span className="inline-flex items-center gap-1.5 px-2.5 py-1 rounded-full text-[10px] font-mono font-bold tracking-wider uppercase bg-slate-950/85 backdrop-blur-md text-white border border-white/20">
|
||||
{item.mediaType === 'video' && <Video className="w-3 h-3 text-cyan-400" />}
|
||||
{item.mediaType === 'comparison' && <Sliders className="w-3 h-3 text-amber-400" />}
|
||||
@@ -201,10 +214,10 @@ function ShiftableRow({ items }: { items: ToolMediaItem[] }) {
|
||||
{/* Little Title Below Window (Fixed height, no vertical jumping) */}
|
||||
<Link
|
||||
href={`/tools/${item.slug}`}
|
||||
className="mt-2.5 h-6 px-1 flex items-center justify-between text-xs sm:text-sm font-display font-semibold text-slate-800 hover:text-blue-600 transition-colors group"
|
||||
className="mt-2.5 h-6 px-1 flex items-center justify-between text-xs sm:text-sm font-display font-semibold text-slate-800 hover:text-blue-600 transition-colors duration-300 ease-[cubic-bezier(0.16,1,0.3,1)] group"
|
||||
>
|
||||
<span className="truncate">{item.title}</span>
|
||||
<ArrowRight className="w-3.5 h-3.5 text-slate-400 group-hover:text-blue-600 group-hover:translate-x-0.5 transition-all flex-shrink-0 ml-1.5" />
|
||||
<ArrowRight className="w-3.5 h-3.5 text-slate-400 group-hover:text-blue-600 group-hover:translate-x-0.5 transition-transform duration-300 ease-[cubic-bezier(0.16,1,0.3,1)] flex-shrink-0 ml-1.5" />
|
||||
</Link>
|
||||
</div>
|
||||
);
|
||||
@@ -215,9 +228,26 @@ function ShiftableRow({ items }: { items: ToolMediaItem[] }) {
|
||||
|
||||
function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered: boolean }) {
|
||||
const videoRef = useRef<HTMLVideoElement>(null);
|
||||
const [sliderPos, setSliderPos] = useState<number>(50);
|
||||
const [isManualHover, setIsManualHover] = useState(false);
|
||||
const containerRef = useRef<HTMLDivElement>(null);
|
||||
const dividerRef = useRef<HTMLDivElement>(null);
|
||||
const afterImgRef = useRef<HTMLImageElement>(null);
|
||||
|
||||
// High performance refs - completely eliminates per-frame React re-renders!
|
||||
const isManualRef = useRef(false);
|
||||
const animFrameRef = useRef<number | null>(null);
|
||||
const pointerRafRef = useRef<number | null>(null);
|
||||
const cachedRectRef = useRef<{ left: number; width: number } | null>(null);
|
||||
|
||||
// Directly mutate DOM styles on the GPU layer without triggering React reconciliation
|
||||
const setSliderDOM = useCallback((percent: number) => {
|
||||
const clamped = Math.max(8, Math.min(92, percent));
|
||||
if (dividerRef.current) {
|
||||
dividerRef.current.style.left = `${clamped}%`;
|
||||
}
|
||||
if (afterImgRef.current) {
|
||||
afterImgRef.current.style.clipPath = `inset(0 ${100 - clamped}% 0 0)`;
|
||||
}
|
||||
}, []);
|
||||
|
||||
// Video Autoplay Muted Loop only on Hover (No zoom, no scale)
|
||||
useEffect(() => {
|
||||
@@ -231,23 +261,28 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
|
||||
}
|
||||
}, [isHovered, item.mediaType]);
|
||||
|
||||
// Before/After Smooth Automatic Oscillating Scan when hovered
|
||||
// Smooth Automatic Oscillating Scan when hovered:
|
||||
// Runs at 60-120fps directly mutating DOM refs (0 React re-renders!)
|
||||
useEffect(() => {
|
||||
if (item.mediaType !== 'comparison') return;
|
||||
|
||||
if (!isHovered) {
|
||||
setSliderPos(50);
|
||||
setIsManualHover(false);
|
||||
if (animFrameRef.current) cancelAnimationFrame(animFrameRef.current);
|
||||
isManualRef.current = false;
|
||||
if (animFrameRef.current) {
|
||||
cancelAnimationFrame(animFrameRef.current);
|
||||
animFrameRef.current = null;
|
||||
}
|
||||
setSliderDOM(50);
|
||||
return;
|
||||
}
|
||||
|
||||
let startTime = Date.now();
|
||||
const sweep = () => {
|
||||
if (!isManualHover) {
|
||||
const elapsed = (Date.now() - startTime) / 1000;
|
||||
const pos = 50 + Math.sin(elapsed * 2.2) * 24;
|
||||
setSliderPos(pos);
|
||||
const startTime = performance.now();
|
||||
const sweep = (now: number) => {
|
||||
if (!isManualRef.current) {
|
||||
const elapsed = (now - startTime) / 1000;
|
||||
// Calm, breathing oscillation: 1.4 rad/s sine wave with 22% sweep amplitude
|
||||
const pos = 50 + Math.sin(elapsed * 1.4) * 22;
|
||||
setSliderDOM(pos);
|
||||
}
|
||||
animFrameRef.current = requestAnimationFrame(sweep);
|
||||
};
|
||||
@@ -255,22 +290,53 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
|
||||
animFrameRef.current = requestAnimationFrame(sweep);
|
||||
|
||||
return () => {
|
||||
if (animFrameRef.current) cancelAnimationFrame(animFrameRef.current);
|
||||
if (animFrameRef.current) {
|
||||
cancelAnimationFrame(animFrameRef.current);
|
||||
animFrameRef.current = null;
|
||||
}
|
||||
};
|
||||
}, [isHovered, isManualHover, item.mediaType]);
|
||||
}, [isHovered, item.mediaType, setSliderDOM]);
|
||||
|
||||
// Pointer position tracking when mouse moves over the comparison window
|
||||
const handleMouseMove = (e: React.MouseEvent<HTMLDivElement>) => {
|
||||
// Pointer position tracking:
|
||||
// Caches container bounding rect on enter to avoid layout thrashing in pointermove.
|
||||
// Updates slider DOM in requestAnimationFrame with zero React state calls.
|
||||
const handlePointerEnter = (e: React.PointerEvent<HTMLDivElement>) => {
|
||||
if (item.mediaType !== 'comparison') return;
|
||||
setIsManualHover(true);
|
||||
const rect = e.currentTarget.getBoundingClientRect();
|
||||
const x = Math.max(0, Math.min(e.clientX - rect.left, rect.width));
|
||||
const percent = Math.max(10, Math.min(90, (x / rect.width) * 100));
|
||||
setSliderPos(percent);
|
||||
cachedRectRef.current = { left: rect.left, width: rect.width };
|
||||
};
|
||||
|
||||
const handleMouseLeave = () => {
|
||||
setIsManualHover(false);
|
||||
const handlePointerMove = (e: React.PointerEvent<HTMLDivElement>) => {
|
||||
if (item.mediaType !== 'comparison') return;
|
||||
isManualRef.current = true;
|
||||
|
||||
const clientX = e.clientX;
|
||||
if (pointerRafRef.current) {
|
||||
cancelAnimationFrame(pointerRafRef.current);
|
||||
}
|
||||
|
||||
pointerRafRef.current = requestAnimationFrame(() => {
|
||||
let rect = cachedRectRef.current;
|
||||
if (!rect && containerRef.current) {
|
||||
const r = containerRef.current.getBoundingClientRect();
|
||||
rect = { left: r.left, width: r.width };
|
||||
cachedRectRef.current = rect;
|
||||
}
|
||||
if (!rect || rect.width <= 0) return;
|
||||
|
||||
const x = Math.max(0, Math.min(clientX - rect.left, rect.width));
|
||||
const percent = (x / rect.width) * 100;
|
||||
setSliderDOM(percent);
|
||||
});
|
||||
};
|
||||
|
||||
const handlePointerLeave = () => {
|
||||
isManualRef.current = false;
|
||||
cachedRectRef.current = null;
|
||||
if (pointerRafRef.current) {
|
||||
cancelAnimationFrame(pointerRafRef.current);
|
||||
pointerRafRef.current = null;
|
||||
}
|
||||
};
|
||||
|
||||
// 1. VIDEO (Strictly locked to fixed 680px centered width - ZERO ZOOM)
|
||||
@@ -284,9 +350,10 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
|
||||
muted
|
||||
loop
|
||||
playsInline
|
||||
className="absolute inset-y-0 left-1/2 -translate-x-1/2 h-full w-[680px] max-w-none object-cover pointer-events-none"
|
||||
className="absolute inset-y-0 left-1/2 -translate-x-1/2 h-full w-[680px] max-w-none object-cover pointer-events-none transform-gpu will-change-transform [backface-visibility:hidden]"
|
||||
/>
|
||||
<div className="absolute inset-0 bg-slate-950/10 group-hover:bg-transparent transition-colors duration-500" />
|
||||
{/* GPU-composited overlay (opacity only) */}
|
||||
<div className="absolute inset-0 bg-slate-950/10 opacity-100 group-hover:opacity-0 transition-opacity duration-700 ease-[cubic-bezier(0.16,1,0.3,1)] pointer-events-none" />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -295,51 +362,57 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
|
||||
if (item.mediaType === 'comparison' && item.beforeUrl && item.afterUrl) {
|
||||
return (
|
||||
<div
|
||||
onMouseMove={handleMouseMove}
|
||||
onMouseLeave={handleMouseLeave}
|
||||
className="relative w-full h-full select-none cursor-ew-resize overflow-hidden bg-slate-950"
|
||||
ref={containerRef}
|
||||
onPointerEnter={handlePointerEnter}
|
||||
onPointerMove={handlePointerMove}
|
||||
onPointerLeave={handlePointerLeave}
|
||||
className="relative w-full h-full select-none cursor-ew-resize overflow-hidden bg-slate-950 transform-gpu [backface-visibility:hidden]"
|
||||
>
|
||||
{/* BEFORE Image (Base layer, fixed scale & position, 0% zoom) */}
|
||||
<img
|
||||
src={item.beforeUrl}
|
||||
alt="Before"
|
||||
className="absolute inset-y-0 left-1/2 -translate-x-1/2 h-full w-[680px] max-w-none object-cover pointer-events-none"
|
||||
className="absolute inset-y-0 left-1/2 -translate-x-1/2 h-full w-[680px] max-w-none object-cover pointer-events-none transform-gpu will-change-transform [backface-visibility:hidden]"
|
||||
loading="lazy"
|
||||
/>
|
||||
|
||||
{/* AFTER Image (Top layer, exact same position & scale, clipped via clip-path) */}
|
||||
<img
|
||||
ref={afterImgRef}
|
||||
src={item.afterUrl}
|
||||
alt="After"
|
||||
className="absolute inset-y-0 left-1/2 -translate-x-1/2 h-full w-[680px] max-w-none object-cover pointer-events-none"
|
||||
className="absolute inset-y-0 left-1/2 -translate-x-1/2 h-full w-[680px] max-w-none object-cover pointer-events-none transform-gpu will-change-transform [backface-visibility:hidden]"
|
||||
style={{
|
||||
clipPath: `inset(0 ${100 - sliderPos}% 0 0)`,
|
||||
clipPath: 'inset(0 50% 0 0)',
|
||||
}}
|
||||
loading="lazy"
|
||||
/>
|
||||
|
||||
{/* Glowing Slider Divider Line */}
|
||||
{/* Glowing Slider Divider Line (Direct GPU transform & position) */}
|
||||
<div
|
||||
className="absolute top-0 bottom-0 w-[2px] bg-white shadow-[0_0_12px_rgba(255,255,255,1)] pointer-events-none z-10"
|
||||
style={{ left: `${sliderPos}%` }}
|
||||
ref={dividerRef}
|
||||
className="absolute top-0 bottom-0 w-[2px] bg-white shadow-[0_0_12px_rgba(255,255,255,1)] pointer-events-none z-10 will-change-transform transform-gpu -translate-x-1/2"
|
||||
style={{ left: '50%' }}
|
||||
>
|
||||
{/* Circular Grab Handle with Before/After Arrow Cue */}
|
||||
<div className="absolute top-1/2 -translate-y-1/2 -translate-x-1/2 w-7 h-7 rounded-full bg-white shadow-lg border border-slate-200/90 flex items-center justify-center text-[10px] font-bold text-slate-900 pointer-events-none">
|
||||
<div className="absolute top-1/2 -translate-y-1/2 -translate-x-1/2 left-1/2 w-7 h-7 rounded-full bg-white shadow-lg border border-slate-200/90 flex items-center justify-center text-[10px] font-bold text-slate-900 pointer-events-none">
|
||||
↔
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Clean Badges Visible on Hover */}
|
||||
{isHovered && (
|
||||
<div className="absolute bottom-3 inset-x-3 flex justify-between pointer-events-none z-20 text-[9px] font-mono font-bold uppercase tracking-wider text-white">
|
||||
<span className="bg-slate-950/80 px-2 py-0.5 rounded backdrop-blur border border-white/10">
|
||||
AFTER
|
||||
</span>
|
||||
<span className="bg-slate-950/80 px-2 py-0.5 rounded backdrop-blur border border-white/10">
|
||||
BEFORE
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
{/* Badges with Smooth GPU Opacity Transition (No DOM mounting/unmounting) */}
|
||||
<div
|
||||
className={`absolute bottom-3 inset-x-3 flex justify-between pointer-events-none z-20 text-[9px] font-mono font-bold uppercase tracking-wider text-white transition-opacity duration-300 ease-[cubic-bezier(0.16,1,0.3,1)] ${
|
||||
isHovered ? 'opacity-100' : 'opacity-0'
|
||||
}`}
|
||||
>
|
||||
<span className="bg-slate-950/80 px-2 py-0.5 rounded backdrop-blur border border-white/10">
|
||||
AFTER
|
||||
</span>
|
||||
<span className="bg-slate-950/80 px-2 py-0.5 rounded backdrop-blur border border-white/10">
|
||||
BEFORE
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -350,10 +423,11 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered:
|
||||
<img
|
||||
src={item.imageUrl || item.afterUrl || item.beforeUrl || '/brand/og-default.png'}
|
||||
alt={item.title}
|
||||
className="absolute inset-y-0 left-1/2 -translate-x-1/2 h-full w-[680px] max-w-none object-cover pointer-events-none"
|
||||
className="absolute inset-y-0 left-1/2 -translate-x-1/2 h-full w-[680px] max-w-none object-cover pointer-events-none transform-gpu will-change-transform [backface-visibility:hidden]"
|
||||
loading="lazy"
|
||||
/>
|
||||
<div className="absolute inset-0 bg-slate-950/10 group-hover:bg-transparent transition-colors duration-500" />
|
||||
{/* GPU-composited overlay (opacity only) */}
|
||||
<div className="absolute inset-0 bg-slate-950/10 opacity-100 group-hover:opacity-0 transition-opacity duration-700 ease-[cubic-bezier(0.16,1,0.3,1)] pointer-events-none" />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user