pleroma/app/soapbox/components/ui/portal/portal.tsx

34 lines
599 B
TypeScript
Raw Normal View History

2023-02-10 10:49:59 -08:00
import React, { useLayoutEffect, useRef } from 'react';
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,
)
);
};
export default Portal;