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

317 lines
13 KiB
TypeScript
Raw Normal View History

'use client';
import React, { useRef, useState, useEffect, useCallback } from 'react';
import Link from 'next/link';
import { ArrowRight, Check, Smartphone, ChevronLeft, ChevronRight } from 'lucide-react';
import { DeferredComparison } from '@/components/ui/DeferredComparison';
export interface ShowcaseItem {
id: string;
index: string;
tabLabel?: string;
category: string;
model: string;
latency: string;
title: string;
tagline: string;
description: string;
variant?: 'comparison' | 'banner';
beforeUrl?: string;
afterUrl?: string;
bannerImage?: string;
bannerBadge?: string;
toolSlug: string;
appSlug: string;
appName: string;
specs: string[];
}
interface CinematicHorizontalProps {
title?: string;
badge?: string;
subtitle?: string;
items?: ShowcaseItem[];
}
export function CinematicHorizontalScrollSection({
title = 'Interactive Model Showcase',
badge = 'FLAGSHIP TRANSFORMATION MODELS',
subtitle = 'Browse the flagship tools with native cinematic pan controls.',
items = [],
}: CinematicHorizontalProps) {
const containerRef = useRef<HTMLElement>(null);
const trackRef = useRef<HTMLDivElement>(null);
const targetXRef = useRef(0);
const currentXRef = useRef(0);
const animFrameRef = useRef<number>(0);
const isLoopRunningRef = useRef(false);
const [activeIndex, setActiveIndex] = useState(0);
const [canScrollLeft, setCanScrollLeft] = useState(false);
const [canScrollRight, setCanScrollRight] = useState(true);
// High-performance, responsive LERP animation loop to smooth wheel notches into liquid momentum
const startLerpLoop = useCallback(() => {
if (isLoopRunningRef.current) return;
isLoopRunningRef.current = true;
const loop = () => {
const track = trackRef.current;
if (!track) {
isLoopRunningRef.current = false;
return;
}
const diff = targetXRef.current - currentXRef.current;
if (Math.abs(diff) > 0.4) {
// Responsive 0.28 damping factor: silky smoothing with zero perceptible lag
currentXRef.current += diff * 0.28;
track.style.transform = `translate3d(-${currentXRef.current.toFixed(2)}px, 0, 0)`;
animFrameRef.current = requestAnimationFrame(loop);
} else {
currentXRef.current = targetXRef.current;
track.style.transform = `translate3d(-${currentXRef.current.toFixed(2)}px, 0, 0)`;
isLoopRunningRef.current = false;
}
};
animFrameRef.current = requestAnimationFrame(loop);
}, []);
// Compute fluid horizontal translation directly from vertical scroll progress
const updateTranslation = useCallback(() => {
const container = containerRef.current;
const track = trackRef.current;
if (!container || !track) return;
const rect = container.getBoundingClientRect();
const totalScrollable = container.offsetHeight - window.innerHeight;
if (totalScrollable <= 0) return;
// Direct proportional progress through the sticky section [0.0, 1.0]
// 0.0: Card 0 centered, exactly as section pins
// 1.0: Final card centered, seamlessly unpins into vertical scroll
const progress = Math.max(0, Math.min(1, -rect.top / totalScrollable));
const cards = Array.from(track.querySelectorAll('article'));
if (cards.length <= 1) return;
const firstCenter = cards[0].offsetLeft + cards[0].offsetWidth / 2;
const lastCenter = cards[cards.length - 1].offsetLeft + cards[cards.length - 1].offsetWidth / 2;
const maxDistance = lastCenter - firstCenter;
targetXRef.current = progress * maxDistance;
startLerpLoop();
// Sync active card index and button states
const active = Math.min(
cards.length - 1,
Math.max(0, Math.round(progress * (cards.length - 1)))
);
setActiveIndex(active);
setCanScrollLeft(progress > 0.02);
setCanScrollRight(progress < 0.98);
}, [startLerpLoop]);
useEffect(() => {
const handleScroll = () => {
updateTranslation();
};
updateTranslation();
window.addEventListener('scroll', handleScroll, { passive: true });
window.addEventListener('resize', handleScroll);
return () => {
window.removeEventListener('scroll', handleScroll);
window.removeEventListener('resize', handleScroll);
if (animFrameRef.current) cancelAnimationFrame(animFrameRef.current);
};
}, [updateTranslation, items.length]);
// Slide smoothly between cards using side arrows
const scrollDirection = (direction: 'left' | 'right') => {
const container = containerRef.current;
if (!container) return;
const totalScrollable = container.offsetHeight - window.innerHeight;
if (totalScrollable <= 0) return;
const cardsCount = items.length;
if (cardsCount <= 1) return;
const rect = container.getBoundingClientRect();
const containerTopInDoc = window.scrollY + rect.top;
const progress = Math.max(0, Math.min(1, -rect.top / totalScrollable));
const currentCardIndex = Math.min(
cardsCount - 1,
Math.max(0, Math.round(progress * (cardsCount - 1)))
);
let targetIndex = currentCardIndex;
if (direction === 'right') {
targetIndex = Math.min(cardsCount - 1, currentCardIndex + 1);
} else if (direction === 'left') {
targetIndex = Math.max(0, currentCardIndex - 1);
}
const targetProgress = targetIndex / (cardsCount - 1);
const targetScrollY = Math.round(containerTopInDoc + targetProgress * totalScrollable);
window.scrollTo({
top: targetScrollY,
behavior: 'smooth',
});
};
if (!items.length) return null;
// Fluid vertical scroll distance: 100vh base + 85vh per additional card
const containerHeightVh = 100 + (items.length - 1) * 85;
return (
<section
id="flagship-models"
ref={containerRef}
className="relative"
style={{ height: `${containerHeightVh}vh` }}
>
<div className="sticky top-0 h-screen flex flex-col justify-center overflow-hidden border-y border-slate-900/[0.08] bg-[#FAF8F5]">
{/* Section Header */}
<div className="mx-auto max-w-7xl w-full px-4 sm:px-6 lg:px-8 mb-4 sm:mb-6 shrink-0">
<div className="flex flex-col gap-2 sm:flex-row sm:items-end sm:justify-between">
<div className="max-w-2xl">
<p className="eyebrow">{badge}</p>
<h2 className="mt-1 text-2xl font-display font-bold tracking-tight text-slate-900 sm:text-3xl lg:text-4xl">{title}</h2>
<p className="mt-1 text-xs text-slate-600 sm:text-sm">{subtitle}</p>
</div>
<p className="hidden text-xs font-medium text-slate-500 sm:block">Scroll down to explore models</p>
</div>
</div>
{/* Carousel Viewport with Pinned Rail & Floating Side Arrows */}
<div className="relative group/showcase w-full overflow-hidden">
{/* Soft edge fades for polished boundary */}
<div
aria-hidden="true"
className={`pointer-events-none absolute left-0 top-0 bottom-0 w-12 sm:w-24 bg-gradient-to-r from-[#FAF8F5] to-transparent z-10 transition-opacity duration-300 ${
canScrollLeft ? 'opacity-100' : 'opacity-0'
}`}
/>
<div
aria-hidden="true"
className={`pointer-events-none absolute right-0 top-0 bottom-0 w-12 sm:w-24 bg-gradient-to-l from-[#FAF8F5] to-transparent z-10 transition-opacity duration-300 ${
canScrollRight ? 'opacity-100' : 'opacity-0'
}`}
/>
{/* Left Side Arrow Button */}
<button
type="button"
onClick={() => scrollDirection('left')}
disabled={!canScrollLeft}
aria-label="Previous flagship model"
className={`absolute left-3 sm:left-6 lg:left-8 top-1/2 -translate-y-1/2 z-30 grid h-12 w-12 sm:h-14 sm:w-14 place-items-center rounded-full border border-slate-900/15 bg-white/95 text-slate-800 shadow-cream-md backdrop-blur-md transition-all duration-300 hover:scale-105 hover:bg-white hover:text-blue-600 hover:border-blue-400 hover:shadow-cream-lg active:scale-95 disabled:opacity-35 disabled:cursor-not-allowed disabled:hover:scale-100 disabled:hover:bg-white/95 disabled:hover:text-slate-400 disabled:hover:border-slate-900/15 disabled:shadow-none focus:outline-none focus:ring-2 focus:ring-blue-600`}
>
<ChevronLeft className="h-6 w-6 stroke-[2.5]" />
</button>
{/* Horizontal Track */}
<div
ref={trackRef}
className="cinematic-rail flex gap-6 will-change-transform"
style={{
paddingLeft: 'calc((100vw - min(780px, 88vw)) / 2)',
paddingRight: 'calc((100vw - min(780px, 88vw)) / 2)',
transform: 'translate3d(0px, 0px, 0px)',
}}
>
{items.map((item) => (
<ShowcaseCard key={item.id} item={item} />
))}
</div>
{/* Right Side Arrow Button */}
<button
type="button"
onClick={() => scrollDirection('right')}
disabled={!canScrollRight}
aria-label="Next flagship model"
className={`absolute right-3 sm:right-6 lg:right-8 top-1/2 -translate-y-1/2 z-30 grid h-12 w-12 sm:h-14 sm:w-14 place-items-center rounded-full border border-slate-900/15 bg-white/95 text-slate-800 shadow-cream-md backdrop-blur-md transition-all duration-300 hover:scale-105 hover:bg-white hover:text-blue-600 hover:border-blue-400 hover:shadow-cream-lg active:scale-95 disabled:opacity-35 disabled:cursor-not-allowed disabled:hover:scale-100 disabled:hover:bg-white/95 disabled:hover:text-slate-400 disabled:hover:border-slate-900/15 disabled:shadow-none focus:outline-none focus:ring-2 focus:ring-blue-600`}
>
<ChevronRight className="h-6 w-6 stroke-[2.5]" />
</button>
</div>
</div>
</section>
);
}
function ShowcaseCard({ item }: { item: ShowcaseItem }) {
const comparison = item.variant !== 'banner' && item.beforeUrl && item.afterUrl;
return (
<article className="w-[88vw] shrink-0 overflow-hidden rounded-3xl border border-slate-900/[0.09] bg-white shadow-cream-md sm:w-[680px] lg:w-[780px]">
<div className="grid lg:grid-cols-[1.1fr_.9fr]">
<div className="min-h-[260px] sm:min-h-[290px] bg-slate-100">
{comparison ? (
<DeferredComparison
beforeUrl={item.beforeUrl!}
afterUrl={item.afterUrl!}
beforeLabel="Original"
afterLabel="CRE result"
aspectRatio="aspect-[4/3] lg:aspect-auto lg:h-full"
/>
) : (
<div className="relative h-full min-h-[260px] sm:min-h-[290px] overflow-hidden bg-slate-950">
{/* eslint-disable-next-line @next/next/no-img-element */}
<img
src={item.bannerImage || item.beforeUrl || ''}
alt={item.title}
loading="lazy"
decoding="async"
className="h-full w-full object-cover"
/>
<div className="absolute inset-0 bg-gradient-to-t from-slate-950/70 via-transparent to-transparent" />
<span className="absolute left-4 top-4 rounded-full bg-white/92 px-3 py-1.5 text-[10px] font-semibold uppercase tracking-wide text-slate-800 shadow-sm">
{item.bannerBadge || item.model}
</span>
</div>
)}
</div>
<div className="flex flex-col p-5 sm:p-6 lg:p-7">
<div className="flex items-center justify-between gap-3">
<span className="text-xs font-bold tracking-[0.16em] text-blue-700">{item.index}</span>
<span className="rounded-full border border-emerald-200 bg-emerald-50 px-2.5 py-1 text-[10px] font-semibold text-emerald-800">
{item.latency}
</span>
</div>
<p className="mt-4 text-xs font-semibold uppercase tracking-[0.12em] text-slate-500">{item.category}</p>
<h3 className="mt-1.5 text-xl font-display font-bold leading-tight tracking-tight text-slate-900 sm:text-2xl">{item.title}</h3>
<p className="mt-2 text-xs leading-relaxed text-slate-600 sm:text-sm">{item.description}</p>
<ul className="mt-4 space-y-2 text-xs sm:text-sm text-slate-700">
{item.specs.slice(0, 3).map((spec) => (
<li key={spec} className="flex gap-2">
<Check className="mt-0.5 h-4 w-4 shrink-0 text-blue-700" />
{spec}
</li>
))}
</ul>
<div className="mt-5 flex flex-wrap gap-3">
<Link href={`/tools/${item.toolSlug}`} className="button-primary text-xs sm:text-sm py-2 px-4 sm:py-2.5 sm:px-5">
Try online <ArrowRight className="h-4 w-4" />
</Link>
<Link href={`/apps/${item.appSlug}`} className="button-secondary text-xs sm:text-sm py-2 px-4 sm:py-2.5 sm:px-5">
<Smartphone className="h-4 w-4" /> App
</Link>
</div>
</div>
</div>
</article>
);
}