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

531 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';
import { KineticText } from '@/components/magicui/kinetic-text';
import { AnimatedShinyText } from '@/components/magicui/animated-shiny-text';
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',
title: 'AI Blemish Remover',
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',
title: 'AI Background Remover',
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',
title: 'Ultra HD AI Photo Enhancer',
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',
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',
title: 'Black & White 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',
title: 'AI Teeth Whitener & Smile Glow',
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',
title: 'CartoonGAN & 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',
title: 'Vintage Polaroid & Film Filter',
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);
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">
{/* Studio Lighting Ambient Glow */}
<div
className="pointer-events-none absolute top-10 left-10 w-[550px] h-[350px] [background:radial-gradient(circle_at_center,color-mix(in_oklab,var(--color-accent)_10%,transparent)_0%,transparent_70%)]"
aria-hidden="true"
/>
<div
className="pointer-events-none absolute bottom-10 right-10 w-[550px] h-[350px] [background:radial-gradient(circle_at_center,color-mix(in_oklab,var(--color-ink)_6%,transparent)_0%,transparent_70%)]"
aria-hidden="true"
/>
<div
className="pointer-events-none absolute inset-0 opacity-20 studio-proof-grid"
aria-hidden="true"
/>
<div className="max-w-container mx-auto space-y-10 relative z-10">
{/* Section Header - Clean editorial layout with kinetic typography */}
2026-11-06 10:53:55 +05:30
<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">
<KineticText>{title}</KineticText>
</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} />
<ShiftableRow items={row2} />
</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-8 w-full">
{items.map((item) => (
<div key={item.id} className="w-full flex flex-col">
<div className="relative w-full h-[230px] rounded-2xl overflow-hidden bg-paper-2 border border-rule/80 hover:border-accent transition-[border-color,box-shadow]">
<ToolWindowMedia item={item} isHovered={false} />
<Link
href={`/tools/${item.slug}`}
className="absolute top-3 right-3 z-30 inline-flex items-center gap-1.5 px-3.5 py-1.5 rounded-full text-xs font-body font-bold bg-paper-hi text-ink border border-rule hover:border-accent hover:text-accent transition-colors duration-150 shadow-sm active:scale-95"
aria-label={`Open ${item.title}`}
>
<span>Try tool</span>
<ArrowRight className="w-3 h-3 text-accent" aria-hidden="true" />
</Link>
2026-11-06 10:53:55 +05:30
</div>
<Link
href={`/tools/${item.slug}`}
className="mt-2.5 flex items-center justify-between text-base font-body font-semibold text-ink hover:text-accent transition-colors px-1"
>
<span>{item.title}</span>
<ArrowRight className="w-4 h-4 text-accent" aria-hidden="true" />
</Link>
</div>
))}
</div>
);
}
function ShiftableRow({ items }: { items: ToolMediaItem[] }) {
const [hoveredId, setHoveredId] = useState<string | null>(null);
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 = hoveredId === item.id;
const hasOtherHovered = hoveredId !== null && !isHovered;
// Subtle and gentle sideways expansion (~25-30% wider on hover, no drastic jumping or zoom)
const flexGrowValue = isHovered ? 1.25 : hasOtherHovered ? 0.915 : 1;
return (
<div
key={item.id}
onMouseEnter={() => setHoveredId(item.id)}
onMouseLeave={() => setHoveredId(null)}
className="flex-1 transition-[flex-grow] duration-500 ease-[cubic-bezier(0.16,1,0.3,1)] flex flex-col min-w-0"
2026-11-06 10:53:55 +05:30
style={{ flexGrow: flexGrowValue }}
>
<div className={`relative w-full h-[220px] lg:h-[240px] rounded-2xl overflow-hidden bg-paper-2 border transition-[border-color,box-shadow] duration-500 ease-[cubic-bezier(0.16,1,0.3,1)] ${
isHovered ? 'border-accent shadow-glow' : 'border-rule/80 hover:border-accent/60'
}`}>
<ToolWindowMedia item={item} isHovered={isHovered} />
2026-11-06 10:53:55 +05:30
<Link
href={`/tools/${item.slug}`}
className="absolute top-3 right-3 z-30 inline-flex items-center gap-1.5 px-3.5 py-1.5 rounded-full text-xs font-body font-bold bg-paper-hi text-ink border border-rule hover:border-accent hover:text-accent transition-colors duration-150 shadow-sm active:scale-95"
aria-label={`Open ${item.title}`}
>
<span>Try tool</span>
<ArrowRight className="w-3 h-3 text-accent" aria-hidden="true" />
</Link>
2026-11-06 10:53:55 +05:30
</div>
<Link
href={`/tools/${item.slug}`}
className="mt-2.5 flex items-center justify-between text-sm font-body font-semibold text-ink hover:text-accent transition-colors px-1"
>
<span className="font-semibold text-ink line-clamp-1">
<AnimatedShinyText>{item.title}</AnimatedShinyText>
</span>
<ArrowRight className="w-3.5 h-3.5 text-accent ml-1 shrink-0" aria-hidden="true" />
</Link>
</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}
muted
loop
playsInline
preload="none"
className="absolute top-0 bottom-0 left-1/2 -translate-x-1/2 h-full w-[440px] min-w-full max-w-none 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 top-0 bottom-0 left-1/2 -translate-x-1/2 h-full w-[440px] min-w-full max-w-none 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 top-0 bottom-0 left-1/2 -translate-x-1/2 h-full w-[440px] min-w-full max-w-none 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>
{/* Badges in colored pills */}
<div className="absolute bottom-2.5 inset-x-2.5 flex justify-between pointer-events-none z-20 font-body">
<span className="bg-ink/80 backdrop-blur-md text-paper-hi text-[10px] font-semibold px-2.5 py-0.5 rounded-full border border-white/20 shadow-xs">
2026-11-06 10:53:55 +05:30
Original
</span>
<span className="bg-accent backdrop-blur-md text-paper-hi text-[10px] font-bold px-2.5 py-0.5 rounded-full border border-white/25 shadow-xs">
2026-11-06 10:53:55 +05:30
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}
className="absolute top-0 bottom-0 left-1/2 -translate-x-1/2 h-full w-[440px] min-w-full max-w-none object-cover pointer-events-none"
loading="lazy"
/>
</div>
);
}