'use client' import { useEffect, useRef, useState, useSyncExternalStore, type CSSProperties, type ReactNode, } from 'react' import { createGlyphRain, supportsHtmlInCanvas, type GlyphRainInstance, type GlyphRainOptions, } from '../core/createGlyphRain' export type GlyphRainProps = GlyphRainOptions & { children: ReactNode className?: string style?: CSSProperties } const emptySubscribe = () => () => {} export function GlyphRain({ children, className, style, ...options }: GlyphRainProps) { const sourceRef = useRef(null) const contentRef = useRef(null) const outputRef = useRef(null) const instanceRef = useRef(null) const [initialOptions] = useState(options) const [failed, setFailed] = useState(false) const supported = useSyncExternalStore( emptySubscribe, supportsHtmlInCanvas, () => false, ) const native = supported && !failed useEffect(() => { const source = sourceRef.current const content = contentRef.current const output = outputRef.current if (!source || !content || !output) return instanceRef.current = createGlyphRain( { source, content, output }, initialOptions, ) if (native && !instanceRef.current) setFailed(true) return () => { instanceRef.current?.destroy() instanceRef.current = null } }, [initialOptions, native]) useEffect(() => { instanceRef.current?.setOptions(options) }) return (
{native ? (
{children}
) : null}
{!native ? (
{children}
) : null}
) } export default GlyphRain