create-expo-cljs-app
Version:
Create a react native application with Expo and Shadow-CLJS!
49 lines (40 loc) • 1.16 kB
JavaScript
/**
* Copyright (c) Nicolas Gallagher.
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import * as React from 'react';
import { canUseDOM } from 'fbjs/lib/ExecutionEnvironment';
import ReactDOM from 'react-dom';
export type ModalPortalProps = {|
children: any
|};
function ModalPortal(props: ModalPortalProps): React.Node {
const { children } = props;
const elementRef = React.useRef(null);
if (canUseDOM && !elementRef.current) {
const element = document.createElement('div');
if (element && document.body) {
document.body.appendChild(element);
elementRef.current = element;
}
}
React.useEffect(() => {
if (canUseDOM) {
return () => {
if (document.body && elementRef.current) {
document.body.removeChild(elementRef.current);
elementRef.current = null;
}
};
}
}, []);
return elementRef.current && canUseDOM
? ReactDOM.createPortal(children, elementRef.current)
: null;
}
export default ModalPortal;