1
This commit is contained in:
+43
@@ -0,0 +1,43 @@
|
||||
import { useRef, useEffect, forwardRef, useImperativeHandle } from 'react';
|
||||
import ReactDOM from 'react-dom';
|
||||
import canUseDom from "./Dom/canUseDom";
|
||||
const Portal = /*#__PURE__*/forwardRef((props, ref) => {
|
||||
const {
|
||||
didUpdate,
|
||||
getContainer,
|
||||
children
|
||||
} = props;
|
||||
const parentRef = useRef(null);
|
||||
const containerRef = useRef(null);
|
||||
|
||||
// Ref return nothing, only for wrapper check exist
|
||||
useImperativeHandle(ref, () => ({}));
|
||||
|
||||
// Create container in client side with sync to avoid useEffect not get ref
|
||||
const initRef = useRef(false);
|
||||
if (!initRef.current && canUseDom()) {
|
||||
containerRef.current = getContainer();
|
||||
parentRef.current = containerRef.current.parentNode;
|
||||
initRef.current = true;
|
||||
}
|
||||
|
||||
// [Legacy] Used by `rc-trigger`
|
||||
useEffect(() => {
|
||||
didUpdate?.(props);
|
||||
});
|
||||
useEffect(() => {
|
||||
// Restore container to original place
|
||||
// React 18 StrictMode will unmount first and mount back for effect test:
|
||||
// https://reactjs.org/blog/2022/03/29/react-v18.html#new-strict-mode-behaviors
|
||||
if (containerRef.current.parentNode === null && parentRef.current !== null) {
|
||||
parentRef.current.appendChild(containerRef.current);
|
||||
}
|
||||
return () => {
|
||||
// [Legacy] This should not be handle by Portal but parent PortalWrapper instead.
|
||||
// Since some component use `Portal` directly, we have to keep the logic here.
|
||||
containerRef.current?.parentNode?.removeChild(containerRef.current);
|
||||
};
|
||||
}, []);
|
||||
return containerRef.current ? /*#__PURE__*/ReactDOM.createPortal(children, containerRef.current) : null;
|
||||
});
|
||||
export default Portal;
|
||||
Reference in New Issue
Block a user