This repository has been archived on 2022-10-07. You can view files and clone it, but cannot push or open issues or pull requests.
skynet-webportal/packages/dashboard-v2/src/components/Modal/ModalPortal.js

17 lines
476 B
JavaScript

import { useEffect, useRef, useState } from "react";
import { createPortal } from "react-dom";
export const MODAL_ROOT_ID = "__modal-root";
export const ModalPortal = ({ children }) => {
const ref = useRef();
const [isClientSide, setIsClientSide] = useState(false);
useEffect(() => {
ref.current = document.querySelector(MODAL_ROOT_ID) || document.body;
setIsClientSide(true);
}, []);
return isClientSide ? createPortal(children, ref.current) : null;
};