function Portal()

in packages/bui-core/src/Portal/PortalCore.tsx [15:57]


  function Portal(props, ref) {
    const {
      children,
      container,
      disablePortal = false,
      rootElement,
      onRootElementMouted,
    } = props;
    const [mountNode, setMountNode] = React.useState(null);
    const handleRef = useForkRef(
      // @ts-expect-error will upstream fix
      React.isValidElement(children) ? children.ref : null,
      ref,
    );

    useLayoutEffect(() => {
      if (!disablePortal) {
        setMountNode(getContainer(container) || rootElement);
      }
    }, [container, disablePortal, rootElement]);

    useLayoutEffect(() => {
      if (mountNode && !disablePortal) {
        setRef(ref, mountNode);
        onRootElementMouted?.(mountNode);
        return () => {
          setRef(ref, null);
        };
      }

      return undefined;
    }, [ref, mountNode, disablePortal]);
    if (disablePortal) {
      if (React.isValidElement(children)) {
        const newProps = {
          ref: handleRef,
        };
        return React.cloneElement(children, newProps);
      }
      return children;
    }
    return mountNode ? createPortal(children, mountNode) : mountNode;
  },