Skip to content

Commit

Permalink
Close dialog by clicking outside
Browse files Browse the repository at this point in the history
  • Loading branch information
graphemecluster committed Nov 20, 2024
1 parent 8575052 commit 1a3a713
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/Components/CreateSchemaDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import ExplorerFolder from "./ExplorerFolder";
import Spinner from "./Spinner";
import { invalidCharsRegex, newFileTemplate, tshetUinhExamplesURLPrefix } from "../consts";
import samples from "../samples";
import { fetchFile, normalizeFileName } from "../utils";
import { fetchFile, normalizeFileName, stopPropagation } from "../utils";

import type { Folder, Sample, SchemaState } from "../consts";
import type { ChangeEventHandler, FormEvent, RefObject } from "react";
Expand Down Expand Up @@ -267,8 +267,8 @@ const CreateSchemaDialog = forwardRef<HTMLDialogElement, CreateSchemaDialogProps
);

return createPortal(
<Container ref={ref} onClose={resetDialog}>
<Popup>
<Container ref={ref} onClick={closeDialog} onClose={resetDialog}>
<Popup onClick={stopPropagation}>
<Title>新增方案</Title>
<Explorer>
<ul>
Expand Down
12 changes: 8 additions & 4 deletions src/Components/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import evaluate from "../evaluate";
import { listenArticle } from "../options";
import initialState, { stateStorageLocation } from "../state";
import TooltipLabel from "./TooltipLabel";
import { stopPropagation } from "../utils";

import type { MainState, Option, ReactNode } from "../consts";
import type { MutableRefObject } from "react";
Expand Down Expand Up @@ -161,6 +162,9 @@ export default function Main({ evaluateHandlerRef }: { evaluateHandlerRef: Mutab
const [operation, increaseOperation] = useReducer((operation: number) => operation + 1, 0);

const dialogRef = useRef<HTMLDialogElement>(null);
const closeDialog = useCallback(() => {
dialogRef.current?.close();
}, []);
evaluateHandlerRef.current = useCallback(async () => {
evaluationResult = [];
dialogRef.current?.showModal();
Expand All @@ -169,11 +173,11 @@ export default function Main({ evaluateHandlerRef }: { evaluateHandlerRef: Mutab
evaluationResult = await evaluate(state);
increaseOperation();
} catch {
dialogRef.current?.close();
closeDialog();
} finally {
setLoading(false);
}
}, [state]);
}, [state, closeDialog]);

const [loading, setLoading] = useState(false);

Expand Down Expand Up @@ -292,8 +296,8 @@ export default function Main({ evaluateHandlerRef }: { evaluateHandlerRef: Mutab
evaluateHandlerRef={evaluateHandlerRef}
/>
{createPortal(
<OutputContainer ref={dialogRef}>
<OutputPopup>
<OutputContainer onClick={closeDialog} ref={dialogRef}>
<OutputPopup onClick={stopPropagation}>
<Title>
<span>推導結果</span>
{!loading && (
Expand Down
3 changes: 3 additions & 0 deletions src/Components/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { createRoot } from "react-dom/client";

import { css as stylesheet } from "@emotion/css";

import { stopPropagation } from "../utils";

import type { ReactElement, SyntheticEvent } from "react";

function getPageWidth() {
Expand Down Expand Up @@ -34,6 +36,7 @@ const fixedWidthStyle = stylesheet`
`;

const div = document.getElementById("tooltip") ?? document.createElement("div");
div.addEventListener("click", stopPropagation);
div.id = "tooltip";
div.style.visibility = "hidden";
div.className = tooltipStyle;
Expand Down
5 changes: 5 additions & 0 deletions src/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import styled from "@emotion/styled";
import Swal from "./Classes/SwalReact";
import Spinner from "./Components/Spinner";

import type { SyntheticEvent } from "react";
import type { SweetAlertOptions } from "sweetalert2";

const LoadModal = styled.div`
Expand Down Expand Up @@ -119,3 +120,7 @@ export function displaySchemaLoadingErrors(errors: unknown[], nSchemas: number)
notifyError(nSchemas === 1 ? "無法載入方案" : "1 個方案無法載入", errors[0]);
}
}

export function stopPropagation(event: Event | SyntheticEvent) {
event.stopPropagation();
}

0 comments on commit 1a3a713

Please sign in to comment.