import { useThree } from '@react-three/fiber' import React, { forwardRef, type ReactNode, useLayoutEffect, useRef, useEffect } from 'react' // @ts-ignore import { createRoot, Root } from 'react-dom/client' interface HtmlProps { portal?: React.MutableRefObject className?: string, name?: string, children?: ReactNode } const HtmlMinimal = forwardRef(({ portal, className, children, name, ...props }, ref) => { const gl = useThree(state => state.gl) const group = useRef(null) const rootRef = useRef(null) const target = portal?.current != null ? portal.current : gl.domElement.parentNode useLayoutEffect(() => { if (!group.current || !target) return const el = document.createElement('div') const root = (rootRef.current = createRoot(el)) target.appendChild(el) return () => { root.unmount() rootRef.current = null target.removeChild(el) } }, [target]) useLayoutEffect(() => { const root = rootRef.current if (!root) return root.render(
{children}
) }) return }) export { HtmlMinimal }