Files
webb/src/components/ui/Kinetic3DText.tsx
T

71 lines
2.5 KiB
TypeScript
Raw Normal View History

'use client';
import React, { useRef, useCallback } from 'react';
interface Kinetic3DTextProps {
primaryText: string;
highlightText?: string;
subtitle?: string;
className?: string;
}
export function Kinetic3DText({
primaryText,
highlightText,
subtitle,
className = '',
}: Kinetic3DTextProps) {
const containerRef = useRef<HTMLDivElement>(null);
const textRef = useRef<HTMLDivElement>(null);
const handleMouseMove = useCallback((e: React.MouseEvent<HTMLDivElement>) => {
if (!containerRef.current) return;
const rect = containerRef.current.getBoundingClientRect();
const nx = (e.clientX - rect.left) / rect.width - 0.5;
const ny = (e.clientY - rect.top) / rect.height - 0.5;
if (!textRef.current || window.matchMedia('(prefers-reduced-motion: reduce)').matches) return;
const rx = Math.max(Math.min(-ny * 3, 2.5), -2.5);
const ry = Math.max(Math.min(nx * 4, 3), -3);
textRef.current.style.transform = `perspective(1000px) rotateX(${rx}deg) rotateY(${ry}deg)`;
}, []);
const handleMouseLeave = useCallback(() => {
if (textRef.current) textRef.current.style.transform = 'perspective(1000px) rotateX(0deg) rotateY(0deg)';
}, []);
return (
<div
ref={containerRef}
onMouseMove={handleMouseMove}
onMouseLeave={handleMouseLeave}
className={`relative select-none ${className}`}
>
<div
ref={textRef}
className="transition-transform duration-300 ease-out will-change-transform"
style={{ transform: 'perspective(1000px) rotateX(0deg) rotateY(0deg)', transformStyle: 'preserve-3d' }}
>
{/* Main Floating Headline (100% Visible, Crisp, Zero Disappearance) */}
<h1 className="text-4xl sm:text-5xl lg:text-6xl xl:text-7xl font-display font-black text-slate-900 tracking-tight leading-[1.08] relative z-10">
<span className="inline-block text-slate-900">{primaryText}</span>{' '}
<br className="hidden sm:inline" />
{highlightText && (
<span className="relative inline-block mt-1 sm:mt-0">
<span className="text-transparent bg-clip-text bg-gradient-to-r from-blue-700 via-indigo-600 to-amber-600 inline-block font-black">
{highlightText}
</span>
</span>
)}
</h1>
{/* Subtitle (100% Visible & Crisp) */}
{subtitle && (
<p className="mt-6 text-base sm:text-lg lg:text-xl text-slate-600 leading-relaxed max-w-2xl mx-auto font-normal">
{subtitle}
</p>
)}
</div>
</div>
);
}