2026-11-06 18:38:45 +05:30
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
import React, { ElementType, ReactNode, useId, useRef, useEffect } from 'react';
|
|
|
|
|
import { cn } from '@/lib/utils';
|
|
|
|
|
|
|
|
|
|
export interface VideoTextProps {
|
|
|
|
|
src: string;
|
|
|
|
|
children: ReactNode;
|
|
|
|
|
as?: ElementType;
|
|
|
|
|
className?: string;
|
|
|
|
|
autoPlay?: boolean;
|
|
|
|
|
muted?: boolean;
|
|
|
|
|
loop?: boolean;
|
|
|
|
|
preload?: 'auto' | 'metadata' | 'none';
|
|
|
|
|
fontSize?: string | number;
|
|
|
|
|
fontWeight?: string | number;
|
|
|
|
|
textAnchor?: 'start' | 'middle' | 'end' | 'inherit';
|
|
|
|
|
dominantBaseline?: 'auto' | 'middle' | 'central' | 'hanging' | 'alphabetic' | 'inherit';
|
|
|
|
|
fontFamily?: string;
|
|
|
|
|
poster?: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Magic UI - Video Text
|
|
|
|
|
* Dynamic typography component with looping video playing masked inside the text.
|
|
|
|
|
* Optimized with IntersectionObserver to completely halt video decoding when scrolled offscreen.
|
|
|
|
|
* Reference: https://magicui.design/docs/components/video-text
|
|
|
|
|
*/
|
|
|
|
|
export function VideoText({
|
|
|
|
|
src,
|
|
|
|
|
children,
|
|
|
|
|
as: Component = 'div',
|
|
|
|
|
className,
|
|
|
|
|
autoPlay = true,
|
|
|
|
|
muted = true,
|
|
|
|
|
loop = true,
|
|
|
|
|
preload = 'auto',
|
|
|
|
|
fontSize = 106,
|
|
|
|
|
fontWeight = 900,
|
|
|
|
|
textAnchor = 'start',
|
|
|
|
|
dominantBaseline = 'central',
|
2026-11-16 18:29:09 +05:30
|
|
|
fontFamily = 'var(--font-display), var(--font-inter), system-ui, sans-serif',
|
2026-11-06 18:38:45 +05:30
|
|
|
poster,
|
|
|
|
|
}: VideoTextProps) {
|
|
|
|
|
const rawId = useId();
|
|
|
|
|
const videoRef = useRef<HTMLVideoElement>(null);
|
|
|
|
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
const clipId = `video-text-clip-${rawId.replace(/:/g, '')}`;
|
|
|
|
|
|
|
|
|
|
// Pause video decoding when offscreen to completely eliminate CPU/GPU drain
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const video = videoRef.current;
|
|
|
|
|
const container = containerRef.current;
|
|
|
|
|
if (!video || !container) return;
|
|
|
|
|
|
|
|
|
|
const observer = new IntersectionObserver(
|
|
|
|
|
([entry]) => {
|
|
|
|
|
if (entry.isIntersecting && !document.hidden) {
|
|
|
|
|
video.play().catch(() => {});
|
|
|
|
|
} else {
|
|
|
|
|
video.pause();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{ threshold: 0.05 }
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
observer.observe(container);
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
observer.disconnect();
|
|
|
|
|
video.pause();
|
|
|
|
|
};
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
// Parse lines if children is a multiline string or array
|
|
|
|
|
const textLines = React.useMemo(() => {
|
|
|
|
|
if (typeof children === 'string') {
|
|
|
|
|
return children.split('\n').map((l) => l.trim()).filter(Boolean);
|
|
|
|
|
}
|
|
|
|
|
if (Array.isArray(children)) {
|
|
|
|
|
return children.filter(Boolean);
|
|
|
|
|
}
|
|
|
|
|
return [children];
|
|
|
|
|
}, [children]);
|
|
|
|
|
|
|
|
|
|
const isMultiLine = textLines.length > 1;
|
|
|
|
|
const singleLineStr = typeof children === 'string' ? children : textLines[0] ? String(textLines[0]) : '';
|
|
|
|
|
const charCount = Math.max(singleLineStr.length, 6);
|
|
|
|
|
// Wide optical letter geometry so the water video has rich surface visibility
|
|
|
|
|
const autoWidth = Math.round(charCount * 68) + 24;
|
|
|
|
|
const viewBoxWidth = isMultiLine ? 1000 : autoWidth;
|
|
|
|
|
const viewBoxHeight = isMultiLine ? 140 * textLines.length : 126;
|
|
|
|
|
const posX = textAnchor === 'start' ? '0.5%' : textAnchor === 'end' ? '99.5%' : '50%';
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Component
|
|
|
|
|
ref={containerRef as any}
|
|
|
|
|
className={cn('relative w-full overflow-hidden flex items-center select-none', className)}
|
|
|
|
|
>
|
|
|
|
|
<svg
|
|
|
|
|
className="w-full h-full pointer-events-none"
|
|
|
|
|
viewBox={`0 0 ${viewBoxWidth} ${viewBoxHeight}`}
|
|
|
|
|
preserveAspectRatio="xMidYMid meet"
|
|
|
|
|
aria-hidden="true"
|
|
|
|
|
>
|
|
|
|
|
<defs>
|
|
|
|
|
<clipPath id={clipId}>
|
|
|
|
|
{isMultiLine ? (
|
|
|
|
|
<text
|
|
|
|
|
x={posX}
|
|
|
|
|
y={`${Math.round(viewBoxHeight / (textLines.length + 1))}%`}
|
|
|
|
|
textAnchor={textAnchor}
|
|
|
|
|
dominantBaseline={dominantBaseline}
|
|
|
|
|
fontSize={fontSize}
|
|
|
|
|
fontWeight={fontWeight}
|
|
|
|
|
fontFamily={fontFamily}
|
|
|
|
|
letterSpacing="-0.02em"
|
|
|
|
|
>
|
|
|
|
|
{textLines.map((line, idx) => (
|
|
|
|
|
<tspan
|
|
|
|
|
key={idx}
|
|
|
|
|
x={posX}
|
|
|
|
|
dy={idx === 0 ? 0 : '1.15em'}
|
|
|
|
|
textAnchor={textAnchor}
|
|
|
|
|
dominantBaseline={dominantBaseline}
|
|
|
|
|
>
|
|
|
|
|
{line}
|
|
|
|
|
</tspan>
|
|
|
|
|
))}
|
|
|
|
|
</text>
|
|
|
|
|
) : (
|
|
|
|
|
<text
|
|
|
|
|
x={posX}
|
|
|
|
|
y="52%"
|
|
|
|
|
textAnchor={textAnchor}
|
|
|
|
|
dominantBaseline={dominantBaseline}
|
|
|
|
|
fontSize={fontSize}
|
|
|
|
|
fontWeight={fontWeight}
|
|
|
|
|
fontFamily={fontFamily}
|
|
|
|
|
letterSpacing="-0.02em"
|
|
|
|
|
>
|
|
|
|
|
{children}
|
|
|
|
|
</text>
|
|
|
|
|
)}
|
|
|
|
|
</clipPath>
|
|
|
|
|
</defs>
|
|
|
|
|
|
|
|
|
|
<foreignObject x="0" y="0" width="100%" height="100%" clipPath={`url(#${clipId})`}>
|
|
|
|
|
<video
|
|
|
|
|
ref={videoRef}
|
|
|
|
|
src={src}
|
|
|
|
|
poster={poster}
|
|
|
|
|
autoPlay={autoPlay}
|
|
|
|
|
muted={muted}
|
|
|
|
|
loop={loop}
|
|
|
|
|
playsInline
|
|
|
|
|
preload={preload}
|
|
|
|
|
className="w-full h-full object-cover"
|
|
|
|
|
/>
|
|
|
|
|
</foreignObject>
|
|
|
|
|
</svg>
|
|
|
|
|
{/* Accessible fallback text for screen readers */}
|
|
|
|
|
<span className="sr-only">{singleLineStr || 'Imagine Transform'}</span>
|
|
|
|
|
</Component>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default VideoText;
|