mirror of
https://github.com/hydralauncher/hydra.git
synced 2025-02-03 00:33:49 +03:00
feat: correctly check if modal is top most modal
This commit is contained in:
parent
ae364f78ca
commit
bcd961b791
@ -1,4 +1,4 @@
|
|||||||
import { useEffect, useState } from "react";
|
import { useEffect, useId, useState } from "react";
|
||||||
import { createPortal } from "react-dom";
|
import { createPortal } from "react-dom";
|
||||||
import { XIcon } from "@primer/octicons-react";
|
import { XIcon } from "@primer/octicons-react";
|
||||||
|
|
||||||
@ -23,6 +23,7 @@ export function Modal({
|
|||||||
}: ModalProps) {
|
}: ModalProps) {
|
||||||
const [isClosing, setIsClosing] = useState(false);
|
const [isClosing, setIsClosing] = useState(false);
|
||||||
const dispatch = useAppDispatch();
|
const dispatch = useAppDispatch();
|
||||||
|
const componentId = useId();
|
||||||
|
|
||||||
const handleCloseClick = () => {
|
const handleCloseClick = () => {
|
||||||
setIsClosing(true);
|
setIsClosing(true);
|
||||||
@ -38,14 +39,39 @@ export function Modal({
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const isTopMostModal = () => {
|
||||||
|
const openModals = document.getElementsByClassName("modal-container");
|
||||||
|
return openModals.length && openModals[openModals.length - 1].id === componentId;
|
||||||
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const close = (e: KeyboardEvent) => {
|
const onKeyDown = (e: KeyboardEvent) => {
|
||||||
if (e.key === "Escape") {
|
if (e.key === "Escape" && isTopMostModal()) {
|
||||||
handleCloseClick();
|
handleCloseClick();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
window.addEventListener("keydown", close);
|
|
||||||
return () => window.removeEventListener("keydown", close);
|
window.addEventListener("keydown", onKeyDown, false);
|
||||||
|
return () => window.removeEventListener("keydown", onKeyDown, false);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const onMouseUp = (e: MouseEvent) => {
|
||||||
|
if (!isTopMostModal()) return;
|
||||||
|
|
||||||
|
const modalContent = document.getElementById(
|
||||||
|
"modal-content-" + componentId
|
||||||
|
);
|
||||||
|
|
||||||
|
const clickInsideContent = modalContent.contains(e.target as Node);
|
||||||
|
|
||||||
|
if (!clickInsideContent) {
|
||||||
|
handleCloseClick();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
window.addEventListener("mousedown", onMouseUp);
|
||||||
|
return () => window.removeEventListener("mousedown", onMouseUp);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -55,8 +81,14 @@ export function Modal({
|
|||||||
if (!visible) return null;
|
if (!visible) return null;
|
||||||
|
|
||||||
return createPortal(
|
return createPortal(
|
||||||
<div className={styles.backdrop({ closing: isClosing })}>
|
<div
|
||||||
<div className={styles.modal({ closing: isClosing })}>
|
className={styles.backdrop({ closing: isClosing }) + " modal-container"}
|
||||||
|
id={componentId}
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
className={styles.modal({ closing: isClosing })}
|
||||||
|
id={"modal-content-" + componentId}
|
||||||
|
>
|
||||||
<div className={styles.modalHeader}>
|
<div className={styles.modalHeader}>
|
||||||
<div style={{ display: "flex", gap: 4, flexDirection: "column" }}>
|
<div style={{ display: "flex", gap: 4, flexDirection: "column" }}>
|
||||||
<h3>{title}</h3>
|
<h3>{title}</h3>
|
||||||
|
Loading…
Reference in New Issue
Block a user