-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathPortal.tsx
26 lines (22 loc) · 890 Bytes
/
Portal.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import React, { /*memo, */ useRef, useEffect } from 'react';
import { createPortal } from 'react-dom';
const { memo } = React;
/**
* This component detaches its contents and re-attaches them to document.body (using ReactDOM.createPortal).
* Use it when you need to circumvent DOM z-stacking (for dialogs, popovers, etc.).
*/
export const Portal = memo(({ children }) => {
const targetElement: React.MutableRefObject<HTMLDivElement | null> = useRef(null);
if (targetElement.current === null) {
targetElement.current = document.body.appendChild(document.createElement('div'));
}
useEffect(() => {
return () => {
if (targetElement.current !== null) {
document.body.removeChild(targetElement.current);
targetElement.current = null;
}
};
}, []);
return createPortal(children !== undefined ? children : null, targetElement.current);
});