'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', fontFamily = 'var(--font-display), var(--font-inter), system-ui, sans-serif', poster, }: VideoTextProps) { const rawId = useId(); const videoRef = useRef(null); const containerRef = useRef(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 (