refactor: add typescript definition and fix some type error

This commit is contained in:
HD
2022-06-23 22:58:13 +08:00
parent b66919114d
commit e0bbbf4f30
39 changed files with 640 additions and 324 deletions
+16 -6
View File
@@ -1,11 +1,19 @@
import { useEffect, useState } from "react";
import { FC, ReactNode, useEffect, useState } from "react";
import { createPortal } from "react-dom";
export default function Modal({ id = "root-modal", mask = true, children }) {
const [wrapper, setWrapper] = useState(null);
// let eleRef = useRef(null);
interface Props {
id?: string;
mask?: boolean;
children?: ReactNode;
}
// todo: check memory leak
const Modal: FC<Props> = ({ id = "root-modal", mask = true, children }) => {
const [wrapper, setWrapper] = useState<HTMLDivElement | null>(null);
useEffect(() => {
const modalRoot = document.getElementById(id);
if (!modalRoot) return;
if (mask) {
modalRoot.classList.add("mask");
}
@@ -17,7 +25,9 @@ export default function Modal({ id = "root-modal", mask = true, children }) {
modalRoot.removeChild(wrapper);
};
}, [id, mask]);
if (!wrapper) return null;
console.log("create portal");
return createPortal(children, wrapper);
}
};
export default Modal;