2023-02-10 10:49:59 -08:00
|
|
|
import React, { useLayoutEffect, useRef } from 'react';
|
2023-02-09 09:42:46 -08:00
|
|
|
import ReactDOM from 'react-dom';
|
|
|
|
|
|
|
|
interface IPortal {
|
|
|
|
children: React.ReactNode
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Portal
|
|
|
|
*/
|
2023-02-10 10:49:59 -08:00
|
|
|
const Portal: React.FC<IPortal> = ({ children }) => {
|
|
|
|
const isRendered = useRef<boolean>(false);
|
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
if (isRendered.current) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
isRendered.current = true;
|
|
|
|
}, [isRendered.current]);
|
|
|
|
|
|
|
|
if (!isRendered.current) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
ReactDOM.createPortal(
|
|
|
|
children,
|
|
|
|
document.getElementById('soapbox') as HTMLDivElement,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
};
|
2023-02-09 09:42:46 -08:00
|
|
|
|
|
|
|
export default Portal;
|