Files
webb/src/components/sections/ShiftableToolsGridSection.tsx
T

543 lines
19 KiB
TypeScript
Raw Normal View History

'use client';
import React, { useState, useRef, useEffect, useCallback } from 'react';
import Link from 'next/link';
2026-11-06 10:53:55 +05:30
import { ArrowRight } from 'lucide-react';
import { ToolItem } from '@/types';
export interface ToolMediaItem {
id: string;
slug: string;
title: string;
mediaType: 'video' | 'photo' | 'comparison';
videoUrl?: string;
poster?: string;
imageUrl?: string;
beforeUrl?: string;
afterUrl?: string;
}
export interface ShiftableToolsGridProps {
title?: string;
subtitle?: string;
tools?: ToolItem[];
items?: ToolMediaItem[];
locale?: string;
}
export const DEFAULT_TOOL_ITEMS: ToolMediaItem[] = [
2026-11-06 10:53:55 +05:30
// ROW 1 (4 items)
{
id: 'tool-blemish',
slug: 'blemish-remover',
2026-11-06 10:53:55 +05:30
title: 'Skin & blemish retoucher',
mediaType: 'comparison',
beforeUrl: '/management/pages/home/sections/our-tools/blemish-remover/before.jpg',
afterUrl: '/management/pages/home/sections/our-tools/blemish-remover/after.jpg',
},
{
id: 'tool-bg-remover',
slug: 'background-remover',
2026-11-06 10:53:55 +05:30
title: 'Background remover (alpha matting)',
mediaType: 'video',
videoUrl: '/management/pages/home/sections/our-tools/background-remover/sample_reel.mp4',
poster: '/management/pages/home/sections/our-tools/background-remover/poster.jpg',
},
{
id: 'tool-enhancer',
slug: 'photo-enhancer',
2026-11-06 10:53:55 +05:30
title: 'Photo enhancer 4K',
mediaType: 'comparison',
beforeUrl: '/management/pages/home/sections/our-tools/photo-enhancer/before.jpg',
afterUrl: '/management/pages/home/sections/our-tools/photo-enhancer/after.jpg',
},
{
id: 'tool-restorer',
slug: 'old-photo-restorer',
2026-11-06 10:53:55 +05:30
title: 'Vintage photo restorer',
mediaType: 'comparison',
beforeUrl: '/management/pages/home/sections/our-tools/old-photo-restorer/before.jpg',
afterUrl: '/management/pages/home/sections/our-tools/old-photo-restorer/after.jpg',
},
2026-11-06 10:53:55 +05:30
// ROW 2 (4 items)
{
id: 'tool-colorization',
slug: 'colorization',
2026-11-06 10:53:55 +05:30
title: 'Photo colorizer',
mediaType: 'comparison',
beforeUrl: '/management/pages/home/sections/our-tools/colorization/before.jpg',
afterUrl: '/management/pages/home/sections/our-tools/colorization/after.jpg',
},
{
id: 'tool-teeth',
slug: 'teeth-whitener',
2026-11-06 10:53:55 +05:30
title: 'Teeth whitener & brightener',
mediaType: 'comparison',
beforeUrl: '/management/pages/home/sections/our-tools/teeth-whitener/before.jpg',
afterUrl: '/management/pages/home/sections/our-tools/teeth-whitener/after.jpg',
},
{
id: 'tool-cartoon',
slug: 'cartoon-avatar',
2026-11-06 10:53:55 +05:30
title: 'Cartoon & anime avatar',
mediaType: 'comparison',
beforeUrl: '/management/pages/home/sections/our-tools/cartoon-avatar/before.jpg',
afterUrl: '/management/pages/home/sections/our-tools/cartoon-avatar/after.jpg',
},
{
id: 'tool-polaroid',
slug: 'polaroid-maker',
2026-11-06 10:53:55 +05:30
title: 'Vintage polaroid maker',
mediaType: 'photo',
imageUrl: '/management/pages/home/sections/our-tools/polaroid-maker/image.jpg',
},
];
export function ShiftableToolsGridSection({
2026-11-06 10:53:55 +05:30
title = 'Our tools',
subtitle = 'Test any tool directly in your browser with your own photos.',
items,
}: ShiftableToolsGridProps) {
const displayItems = items && items.length >= 8 ? items : DEFAULT_TOOL_ITEMS;
const row1 = displayItems.slice(0, 4);
const row2 = displayItems.slice(4, 8);
const [isMiddleReached, setIsMiddleReached] = useState(false);
useEffect(() => {
const checkPosition = () => {
const section = document.getElementById('our-tools');
if (!section) return;
const rect = section.getBoundingClientRect();
const inMiddle = rect.top <= window.innerHeight * 0.5 && rect.bottom >= window.innerHeight * 0.15;
setIsMiddleReached(inMiddle);
};
checkPosition();
window.addEventListener('scroll', checkPosition, { passive: true });
window.addEventListener('resize', checkPosition, { passive: true });
return () => {
window.removeEventListener('scroll', checkPosition);
window.removeEventListener('resize', checkPosition);
};
}, []);
return (
2026-11-06 10:53:55 +05:30
<section id="our-tools" className="py-16 sm:py-24 px-4 sm:px-6 md:px-8 lg:px-10 bg-paper border-b border-rule relative overflow-hidden">
<div className="max-w-container mx-auto space-y-10">
{/* Section Header - Clean editorial layout without eyebrows */}
<div className="max-w-2xl space-y-2 border-b border-rule pb-5">
<h2 className="text-3xl sm:text-4xl font-display font-bold text-ink tracking-tight">
{title}
</h2>
2026-11-06 10:53:55 +05:30
<p className="text-sm sm:text-base text-ink-2 font-body text-pretty">
{subtitle}
</p>
</div>
{/* Desktop View (md and up): 2-Row Shiftable Expansion Grid */}
<div className="hidden md:block space-y-8">
<ShiftableRow items={row1} isEnabled={isMiddleReached} />
<ShiftableRow items={row2} isEnabled={isMiddleReached} />
</div>
2026-11-06 10:53:55 +05:30
{/* Mobile / Phone View (<md): Pure Vertical Stack */}
<div className="block md:hidden">
<MobileVerticalToolsList items={displayItems} />
</div>
2026-11-06 10:53:55 +05:30
{/* All tools link */}
<div className="pt-4 flex justify-between items-center text-xs font-body text-muted border-t border-rule">
<span>Eight neural models active on web trial</span>
<Link
href="/tools"
2026-11-06 10:53:55 +05:30
className="text-accent hover:underline font-medium inline-flex items-center gap-1"
>
2026-11-06 10:53:55 +05:30
<span>View complete tool directory</span>
<ArrowRight className="w-3.5 h-3.5" aria-hidden="true" />
</Link>
</div>
</div>
</section>
);
}
function MobileVerticalToolsList({ items }: { items: ToolMediaItem[] }) {
return (
<div className="flex flex-col gap-6 w-full">
{items.map((item) => (
<div key={item.id} className="w-full flex flex-col">
2026-11-06 10:53:55 +05:30
<div className="studio-sheet p-3 crop-marked">
<div className="relative w-full h-[220px] rounded-card overflow-hidden bg-paper-2">
<ToolWindowMedia item={item} isHovered={false} />
<Link
href={`/tools/${item.slug}`}
2026-11-06 10:53:55 +05:30
className="absolute top-2.5 right-2.5 z-30 inline-flex items-center gap-1 px-2.5 py-1 rounded-card text-xs font-body bg-paper-hi/90 text-ink border border-rule hover:border-ink transition-colors shadow-sm"
aria-label={`Open ${item.title}`}
>
2026-11-06 10:53:55 +05:30
<span>Try tool</span>
<ArrowRight className="w-3 h-3" aria-hidden="true" />
</Link>
</div>
<Link
href={`/tools/${item.slug}`}
2026-11-06 10:53:55 +05:30
className="mt-2.5 flex items-center justify-between text-sm font-body font-semibold text-ink hover:text-accent transition-colors"
>
2026-11-06 10:53:55 +05:30
<span>{item.title}</span>
<ArrowRight className="w-3.5 h-3.5 text-muted" aria-hidden="true" />
</Link>
2026-11-06 10:53:55 +05:30
</div>
</div>
))}
</div>
);
}
function ShiftableRow({ items, isEnabled = true }: { items: ToolMediaItem[]; isEnabled?: boolean }) {
const [hoveredId, setHoveredId] = useState<string | null>(null);
useEffect(() => {
if (!isEnabled) {
setHoveredId(null);
}
}, [isEnabled]);
return (
2026-11-06 10:53:55 +05:30
<div className="flex flex-row gap-5 w-full items-start overflow-visible">
{items.map((item) => {
const isHovered = isEnabled && hoveredId === item.id;
const hasOtherHovered = isEnabled && hoveredId !== null && !isHovered;
2026-11-06 10:53:55 +05:30
const flexGrowValue = isHovered ? 1.25 : hasOtherHovered ? 0.916 : 1;
return (
<div
key={item.id}
onMouseEnter={() => {
if (isEnabled) setHoveredId(item.id);
}}
onMouseLeave={() => setHoveredId(null)}
2026-11-06 10:53:55 +05:30
className="flex-1 transition-[flex-grow] duration-300 ease-out flex flex-col min-w-0"
style={{ flexGrow: flexGrowValue }}
>
2026-11-06 10:53:55 +05:30
<div className="studio-sheet p-3 crop-marked">
<div className="relative w-full h-[200px] lg:h-[220px] rounded-card overflow-hidden bg-paper-2">
<ToolWindowMedia item={item} isHovered={isHovered} />
<Link
href={`/tools/${item.slug}`}
2026-11-06 10:53:55 +05:30
className="absolute top-2.5 right-2.5 z-30 inline-flex items-center gap-1 px-2.5 py-1 rounded-card text-xs font-body bg-paper-hi/90 text-ink border border-rule hover:border-ink transition-colors shadow-sm"
aria-label={`Open ${item.title}`}
>
2026-11-06 10:53:55 +05:30
<span>Try tool</span>
<ArrowRight className="w-3 h-3" aria-hidden="true" />
</Link>
</div>
2026-11-06 10:53:55 +05:30
<Link
href={`/tools/${item.slug}`}
2026-11-06 10:53:55 +05:30
className="mt-2.5 flex items-center justify-between text-sm font-body font-medium text-ink hover:text-accent transition-colors"
>
2026-11-06 10:53:55 +05:30
<span className="truncate">{item.title}</span>
<ArrowRight className="w-3.5 h-3.5 text-muted ml-1 shrink-0" aria-hidden="true" />
</Link>
2026-11-06 10:53:55 +05:30
</div>
</div>
);
})}
</div>
);
}
function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered: boolean }) {
const videoRef = useRef<HTMLVideoElement>(null);
const containerRef = useRef<HTMLDivElement>(null);
const dividerRef = useRef<HTMLDivElement>(null);
const clipLayerRef = useRef<HTMLDivElement>(null);
const handleRef = useRef<HTMLDivElement>(null);
const isManualRef = useRef(false);
const isPointerDownRef = useRef(false);
const currentPercentRef = useRef(50);
const animFrameRef = useRef<number | null>(null);
const pointerRafRef = useRef<number | null>(null);
const cachedRectRef = useRef<{ left: number; width: number } | null>(null);
const setSliderDOM = useCallback((percent: number) => {
const clamped = Math.max(0, Math.min(100, percent));
currentPercentRef.current = clamped;
if (dividerRef.current) {
dividerRef.current.style.left = `${clamped}%`;
}
if (clipLayerRef.current) {
2026-11-06 10:53:55 +05:30
clipLayerRef.current.style.clipPath = `inset(0 ${100 - clamped}% 0 0)`;
(clipLayerRef.current.style as any).webkitClipPath = `inset(0 ${100 - clamped}% 0 0)`;
}
if (handleRef.current) {
handleRef.current.setAttribute('aria-valuenow', `${Math.round(clamped)}`);
}
}, []);
useEffect(() => {
if (item.mediaType !== 'video') return;
const video = videoRef.current;
if (!video) return;
video.muted = true;
video.defaultMuted = true;
video.playsInline = true;
let isIntersecting = false;
const updatePlayState = () => {
if (isIntersecting && !document.hidden) {
video.play().catch(() => {});
} else {
video.pause();
}
};
const observer = new IntersectionObserver(
([entry]) => {
isIntersecting = entry.isIntersecting;
updatePlayState();
},
{ threshold: 0.05 }
);
observer.observe(video);
const handleVisibility = () => {
updatePlayState();
};
document.addEventListener('visibilitychange', handleVisibility);
return () => {
observer.disconnect();
document.removeEventListener('visibilitychange', handleVisibility);
video.pause();
};
}, [item.mediaType]);
useEffect(() => {
if (item.mediaType !== 'comparison') return;
if (!isHovered) {
isManualRef.current = false;
if (animFrameRef.current) {
cancelAnimationFrame(animFrameRef.current);
animFrameRef.current = null;
}
setSliderDOM(50);
}
}, [isHovered, item.mediaType, setSliderDOM]);
const updateToClientX = useCallback((clientX: number) => {
if (!containerRef.current) return;
const rect = cachedRectRef.current || containerRef.current.getBoundingClientRect();
if (rect.width <= 0) return;
const x = Math.max(0, Math.min(clientX - rect.left, rect.width));
const percent = (x / rect.width) * 100;
setSliderDOM(percent);
}, [setSliderDOM]);
const handleContainerPointerEnter = (e: React.PointerEvent<HTMLDivElement> | React.MouseEvent<HTMLDivElement>) => {
if (item.mediaType !== 'comparison') return;
if (!('pointerType' in e) || e.pointerType === 'mouse') {
isManualRef.current = true;
updateToClientX(e.clientX);
}
};
const handleContainerPointerMove = (e: React.PointerEvent<HTMLDivElement> | React.MouseEvent<HTMLDivElement>) => {
if (item.mediaType !== 'comparison') return;
if (!('pointerType' in e) || e.pointerType === 'mouse') {
isManualRef.current = true;
const clientX = e.clientX;
if (pointerRafRef.current) cancelAnimationFrame(pointerRafRef.current);
pointerRafRef.current = requestAnimationFrame(() => {
updateToClientX(clientX);
});
}
};
const handleContainerPointerLeave = (e: React.PointerEvent<HTMLDivElement> | React.MouseEvent<HTMLDivElement>) => {
if (isPointerDownRef.current) return;
if (!('pointerType' in e) || e.pointerType === 'mouse') {
isManualRef.current = false;
if (pointerRafRef.current) {
cancelAnimationFrame(pointerRafRef.current);
pointerRafRef.current = null;
}
setSliderDOM(50);
}
};
const handleHandlePointerDown = (e: React.PointerEvent<HTMLDivElement>) => {
if (item.mediaType !== 'comparison') return;
if (e.button !== 0 && e.pointerType === 'mouse') return;
e.preventDefault();
e.stopPropagation();
isManualRef.current = true;
isPointerDownRef.current = true;
if (animFrameRef.current) {
cancelAnimationFrame(animFrameRef.current);
animFrameRef.current = null;
}
try {
e.currentTarget.setPointerCapture(e.pointerId);
} catch {
// Fallback
}
if (containerRef.current) {
const r = containerRef.current.getBoundingClientRect();
cachedRectRef.current = { left: r.left, width: r.width };
}
updateToClientX(e.clientX);
};
const handleHandlePointerMove = (e: React.PointerEvent<HTMLDivElement>) => {
if (!isPointerDownRef.current) return;
e.stopPropagation();
isManualRef.current = true;
const clientX = e.clientX;
if (pointerRafRef.current) {
cancelAnimationFrame(pointerRafRef.current);
}
pointerRafRef.current = requestAnimationFrame(() => {
updateToClientX(clientX);
});
};
const handleHandlePointerUp = (e: React.PointerEvent<HTMLDivElement>) => {
if (!isPointerDownRef.current) return;
e.stopPropagation();
isPointerDownRef.current = false;
cachedRectRef.current = null;
if (pointerRafRef.current) {
cancelAnimationFrame(pointerRafRef.current);
pointerRafRef.current = null;
}
try {
e.currentTarget.releasePointerCapture(e.pointerId);
} catch {
// Fallback
}
};
2026-11-06 10:53:55 +05:30
// 1. VIDEO
if (item.mediaType === 'video' && item.videoUrl) {
return (
2026-11-06 10:53:55 +05:30
<div className="relative w-full h-full overflow-hidden bg-paper-2 isolate">
<video
ref={videoRef}
src={item.videoUrl}
poster={item.poster}
autoPlay
muted
loop
playsInline
preload="auto"
2026-11-06 10:53:55 +05:30
className="absolute inset-0 w-full h-full object-cover pointer-events-none"
/>
</div>
);
}
2026-11-06 10:53:55 +05:30
// 2. COMPARISON
if (item.mediaType === 'comparison' && item.beforeUrl && item.afterUrl) {
return (
<div
ref={containerRef}
onPointerEnter={handleContainerPointerEnter}
onMouseEnter={handleContainerPointerEnter}
onPointerMove={handleContainerPointerMove}
onMouseMove={handleContainerPointerMove}
onPointerLeave={handleContainerPointerLeave}
onMouseLeave={handleContainerPointerLeave}
2026-11-06 10:53:55 +05:30
style={{ touchAction: 'pan-y' }}
className="relative w-full h-full select-none overflow-hidden bg-paper-2 isolate cursor-ew-resize"
>
2026-11-06 10:53:55 +05:30
{/* Layer 1: Enhanced Image */}
<img
src={item.afterUrl}
2026-11-06 10:53:55 +05:30
alt={`${item.title} - enhanced`}
className="absolute inset-0 w-full h-full object-cover pointer-events-none"
loading="lazy"
/>
2026-11-06 10:53:55 +05:30
{/* Layer 2: Original Image */}
<div
ref={clipLayerRef}
2026-11-06 10:53:55 +05:30
className="absolute inset-0 overflow-hidden isolate pointer-events-none"
style={{ clipPath: 'inset(0 50% 0 0)' }}
>
<img
src={item.beforeUrl}
2026-11-06 10:53:55 +05:30
alt={`${item.title} - original`}
className="absolute inset-0 w-full h-full object-cover pointer-events-none"
loading="lazy"
/>
</div>
2026-11-06 10:53:55 +05:30
{/* Slider Divider Line */}
<div
ref={dividerRef}
2026-11-06 10:53:55 +05:30
className="absolute top-0 bottom-0 w-px bg-paper-hi z-10 pointer-events-none -translate-x-1/2"
style={{ left: '50%' }}
>
2026-11-06 10:53:55 +05:30
{/* Circular Grab Handle */}
<div
ref={handleRef}
onPointerDown={handleHandlePointerDown}
onPointerMove={handleHandlePointerMove}
onPointerUp={handleHandlePointerUp}
onPointerCancel={handleHandlePointerUp}
role="slider"
aria-label="Before and after comparison slider"
aria-valuemin={0}
aria-valuemax={100}
aria-valuenow={50}
tabIndex={0}
style={{ touchAction: 'none' }}
2026-11-06 10:53:55 +05:30
className="absolute top-1/2 -translate-y-1/2 -translate-x-1/2 left-1/2 w-7 h-7 rounded-full bg-paper-hi border border-rule flex items-center justify-center text-xs text-accent pointer-events-auto cursor-ew-resize touch-none shadow-sm"
>
2026-11-06 10:53:55 +05:30
<svg className="w-3 h-3 text-accent pointer-events-none" fill="none" stroke="currentColor" viewBox="0 0 24 24" aria-hidden="true">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M8 9l-3 3 3 3m8-6l3 3-3 3" />
</svg>
</div>
</div>
2026-11-06 10:53:55 +05:30
{/* Badges in sentence case body face */}
<div className="absolute bottom-2 inset-x-2 flex justify-between pointer-events-none z-20 text-xs font-body text-ink">
<span className="bg-paper-hi/90 px-1.5 py-0.5 rounded border border-rule shadow-xs">
Original
</span>
2026-11-06 10:53:55 +05:30
<span className="bg-paper-hi/90 px-1.5 py-0.5 rounded border border-rule shadow-xs">
Enhanced
</span>
</div>
</div>
);
}
2026-11-06 10:53:55 +05:30
// 3. PHOTO
return (
2026-11-06 10:53:55 +05:30
<div className="relative w-full h-full overflow-hidden bg-paper-2 isolate">
<img
src={item.imageUrl || item.afterUrl || item.beforeUrl || '/brand/og-default.png'}
alt={item.title}
2026-11-06 10:53:55 +05:30
className="absolute inset-0 w-full h-full object-cover pointer-events-none"
loading="lazy"
/>
</div>
);
}