2023-02-10 11:43:41 -08:00
|
|
|
import React, { useLayoutEffect, useState } 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 }) => {
|
2023-02-10 11:43:41 -08:00
|
|
|
const [isRendered, setIsRendered] = useState<boolean>(false);
|
2023-02-10 10:49:59 -08:00
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
2023-02-10 11:43:41 -08:00
|
|
|
setIsRendered(true);
|
|
|
|
}, []);
|
2023-02-10 10:49:59 -08:00
|
|
|
|
|
|
|
|
2023-02-10 11:43:41 -08:00
|
|
|
if (!isRendered) {
|
2023-02-10 10:49:59 -08:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
ReactDOM.createPortal(
|
|
|
|
children,
|
|
|
|
document.getElementById('soapbox') as HTMLDivElement,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
};
|
2023-02-09 09:42:46 -08:00
|
|
|
|
|
|
|
export default Portal;
|