feat(homepage+tools): tools page card refinement and homepage fixes
This commit is contained in:
@@ -0,0 +1,225 @@
|
||||
'use client';
|
||||
|
||||
import React, { useCallback, useEffect, useRef, useState } from 'react';
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
interface MorphingTextProps {
|
||||
className?: string;
|
||||
texts: string[];
|
||||
morphTime?: number;
|
||||
cooldownTime?: number;
|
||||
}
|
||||
|
||||
const SvgFilters: React.FC = () => (
|
||||
<svg
|
||||
id="magicui-morphing-filters"
|
||||
className="fixed h-0 w-0 pointer-events-none"
|
||||
aria-hidden="true"
|
||||
preserveAspectRatio="xMidYMid slice"
|
||||
>
|
||||
<defs>
|
||||
<filter id="magicui-threshold">
|
||||
<feColorMatrix
|
||||
in="SourceGraphic"
|
||||
type="matrix"
|
||||
values="1 0 0 0 0
|
||||
0 1 0 0 0
|
||||
0 0 1 0 0
|
||||
0 0 0 255 -140"
|
||||
/>
|
||||
</filter>
|
||||
</defs>
|
||||
</svg>
|
||||
);
|
||||
|
||||
/**
|
||||
* Magic UI - Morphing Text (Thermal-Optimized)
|
||||
* Fluid text morphing using blur & threshold filter with zero offscreen CPU consumption.
|
||||
* Pauses RAF when resting and when scrolled out of view.
|
||||
* Reference: https://magicui.design/docs/components/morphing-text
|
||||
*/
|
||||
export function MorphingText({
|
||||
texts,
|
||||
className,
|
||||
morphTime = 1.2,
|
||||
cooldownTime = 2.2,
|
||||
}: MorphingTextProps) {
|
||||
const containerRef = useRef<HTMLDivElement>(null);
|
||||
const text1Ref = useRef<HTMLSpanElement>(null);
|
||||
const text2Ref = useRef<HTMLSpanElement>(null);
|
||||
|
||||
const textIndexRef = useRef(0);
|
||||
const morphRef = useRef(0);
|
||||
const cooldownRef = useRef(0);
|
||||
const timeRef = useRef(new Date());
|
||||
const [isInView, setIsInView] = useState(false);
|
||||
|
||||
// Set text blur & opacity styles during the morphing phase
|
||||
const setStyles = useCallback(
|
||||
(fraction: number) => {
|
||||
const current1 = text1Ref.current;
|
||||
const current2 = text2Ref.current;
|
||||
if (!current1 || !current2) return;
|
||||
|
||||
// Blur calculation: clamped so small fonts retain high legibility
|
||||
const blurVal = Math.min(8 / fraction - 8, 24);
|
||||
current2.style.filter = `blur(${blurVal}px)`;
|
||||
current2.style.opacity = `${Math.pow(fraction, 0.4) * 100}%`;
|
||||
|
||||
const invertedFraction = 1 - fraction;
|
||||
const blurInv = Math.min(8 / invertedFraction - 8, 24);
|
||||
current1.style.filter = `blur(${blurInv}px)`;
|
||||
current1.style.opacity = `${Math.pow(invertedFraction, 0.4) * 100}%`;
|
||||
|
||||
current1.textContent = texts[textIndexRef.current % texts.length];
|
||||
current2.textContent = texts[(textIndexRef.current + 1) % texts.length];
|
||||
},
|
||||
[texts]
|
||||
);
|
||||
|
||||
const doMorph = useCallback(() => {
|
||||
morphRef.current -= cooldownRef.current;
|
||||
cooldownRef.current = 0;
|
||||
|
||||
let fraction = morphRef.current / morphTime;
|
||||
|
||||
if (fraction > 1) {
|
||||
cooldownRef.current = cooldownTime;
|
||||
fraction = 1;
|
||||
}
|
||||
|
||||
setStyles(fraction);
|
||||
|
||||
if (fraction === 1) {
|
||||
textIndexRef.current++;
|
||||
}
|
||||
}, [setStyles, morphTime, cooldownTime]);
|
||||
|
||||
const doCooldown = useCallback(() => {
|
||||
morphRef.current = 0;
|
||||
const current1 = text1Ref.current;
|
||||
const current2 = text2Ref.current;
|
||||
if (current1 && current2) {
|
||||
current2.style.filter = 'none';
|
||||
current2.style.opacity = '100%';
|
||||
current1.style.filter = 'none';
|
||||
current1.style.opacity = '0%';
|
||||
}
|
||||
}, []);
|
||||
|
||||
// IntersectionObserver: 0% CPU consumption when scrolled out of view
|
||||
useEffect(() => {
|
||||
const el = containerRef.current;
|
||||
if (!el || typeof IntersectionObserver === 'undefined') {
|
||||
setIsInView(true);
|
||||
return;
|
||||
}
|
||||
|
||||
const observer = new IntersectionObserver(
|
||||
([entry]) => {
|
||||
setIsInView(entry.isIntersecting);
|
||||
},
|
||||
{ threshold: 0.1 }
|
||||
);
|
||||
|
||||
observer.observe(el);
|
||||
return () => observer.disconnect();
|
||||
}, []);
|
||||
|
||||
// Main animation driver: runs only while in view and page visible
|
||||
useEffect(() => {
|
||||
if (!isInView || !texts || texts.length === 0) return;
|
||||
|
||||
let animationFrameId: number | null = null;
|
||||
let cooldownTimeoutId: NodeJS.Timeout | null = null;
|
||||
let isRunning = true;
|
||||
|
||||
timeRef.current = new Date();
|
||||
|
||||
const step = () => {
|
||||
if (!isRunning || document.hidden) return;
|
||||
|
||||
const newTime = new Date();
|
||||
const dt = (newTime.getTime() - timeRef.current.getTime()) / 1000;
|
||||
timeRef.current = newTime;
|
||||
|
||||
cooldownRef.current -= dt;
|
||||
|
||||
if (cooldownRef.current <= 0) {
|
||||
// Active morph phase: run RAF smoothly
|
||||
doMorph();
|
||||
animationFrameId = requestAnimationFrame(step);
|
||||
} else {
|
||||
// Resting phase: apply clean cooldown state and pause RAF!
|
||||
doCooldown();
|
||||
// Rather than spinning 120 RAF loops per second doing nothing, schedule timer
|
||||
const waitMs = Math.max(50, cooldownRef.current * 1000);
|
||||
cooldownTimeoutId = setTimeout(() => {
|
||||
cooldownRef.current = 0;
|
||||
timeRef.current = new Date();
|
||||
animationFrameId = requestAnimationFrame(step);
|
||||
}, waitMs);
|
||||
}
|
||||
};
|
||||
|
||||
animationFrameId = requestAnimationFrame(step);
|
||||
|
||||
const handleVisibilityChange = () => {
|
||||
if (document.hidden) {
|
||||
if (animationFrameId) cancelAnimationFrame(animationFrameId);
|
||||
if (cooldownTimeoutId) clearTimeout(cooldownTimeoutId);
|
||||
} else {
|
||||
timeRef.current = new Date();
|
||||
animationFrameId = requestAnimationFrame(step);
|
||||
}
|
||||
};
|
||||
|
||||
document.addEventListener('visibilitychange', handleVisibilityChange);
|
||||
|
||||
return () => {
|
||||
isRunning = false;
|
||||
if (animationFrameId) cancelAnimationFrame(animationFrameId);
|
||||
if (cooldownTimeoutId) clearTimeout(cooldownTimeoutId);
|
||||
document.removeEventListener('visibilitychange', handleVisibilityChange);
|
||||
};
|
||||
}, [isInView, texts, doMorph, doCooldown]);
|
||||
|
||||
const [useSvgThreshold, setUseSvgThreshold] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
// Only apply SVG filter on desktop non-touch browsers where SVG filters on HTML are fully supported
|
||||
if (typeof window !== 'undefined') {
|
||||
const isMobileOrTouch =
|
||||
window.innerWidth < 1024 ||
|
||||
navigator.maxTouchPoints > 0 ||
|
||||
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
|
||||
setUseSvgThreshold(!isMobileOrTouch);
|
||||
}
|
||||
}, []);
|
||||
|
||||
if (!texts || texts.length === 0) return null;
|
||||
|
||||
return (
|
||||
<div
|
||||
ref={containerRef}
|
||||
style={useSvgThreshold ? { filter: 'url(#magicui-threshold) blur(0.3px)' } : undefined}
|
||||
className={cn(
|
||||
'relative inline-block overflow-hidden align-baseline select-none whitespace-nowrap',
|
||||
className
|
||||
)}
|
||||
aria-label={texts[textIndexRef.current % texts.length]}
|
||||
>
|
||||
<span
|
||||
ref={text1Ref}
|
||||
className="inline-block transition-transform duration-75 whitespace-nowrap"
|
||||
>
|
||||
{texts[0]}
|
||||
</span>
|
||||
<span
|
||||
ref={text2Ref}
|
||||
className="absolute inset-0 m-auto inline-block text-left opacity-0 whitespace-nowrap"
|
||||
/>
|
||||
{useSvgThreshold && <SvgFilters />}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user