diff --git a/src/components/exams/exam-questions-panel.tsx b/src/components/exams/exam-questions-panel.tsx index 0d1c571..e6dfc9f 100644 --- a/src/components/exams/exam-questions-panel.tsx +++ b/src/components/exams/exam-questions-panel.tsx @@ -19,6 +19,7 @@ import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { ConfirmDialog } from "@/components/ui/confirm-dialog"; import { FormDialog } from "@/components/ui/form-dialog"; +import { SidePanel } from "@/components/ui/side-panel"; import { BankQuestionPicker } from "@/components/exams/bank-question-picker"; import { QuestionForm, type QuestionValues } from "@/components/exams/question-form"; import { IconArchive, IconChevronLeft, IconChevronRight, IconPlus, IconRefresh, IconTrash } from "@/components/ui/icons"; @@ -28,6 +29,11 @@ import { hasMinRole } from "@/lib/roles"; import { useAuth } from "@/stores/auth-context"; import { useT } from "@/stores/preferences-context"; +/** Close after the current pointer gesture so the tap cannot reopen the surface. */ +const closeAfterGesture = (close: () => void) => { + setTimeout(close, 0); +}; + const QUESTION_PAGE_SIZE = 1; export function ExamQuestionsPanel(props: { @@ -136,7 +142,7 @@ export function ExamQuestionsPanel(props: { const insertFromBank = async (bankQuestionId: string, subjectId: string) => { await postExamQuestionFromBank(props.examId, bankQuestionId, subjectId); - setBankOpen(false); + closeAfterGesture(() => setBankOpen(false)); setFlash(t("bank.inserted")); await refetch(); }; @@ -249,18 +255,22 @@ export function ExamQuestionsPanel(props: { - { + if (open) setBankOpen(true); + else closeAfterGesture(() => setBankOpen(false)); + }} title={t("bank.pickTemplate")} description={t("bank.title")} + size="wide" > setBankOpen(false)} + onCancel={() => closeAfterGesture(() => setBankOpen(false))} /> - + `/api/exams/${props.examId}/questions/${editing()?.id}/choices/${choiceId}/image`} loadImageBlob={editing() ? () => getExamQuestionImageBlob(props.examId, editing()!.id) : undefined} onCancel={() => { - setEditing(null); - setFormOpen(false); + // In-body Cancel bypasses FormDialog's deferred onOpenChange, so + // defer here too — otherwise the same tap can reopen the form. + closeAfterGesture(() => { + setEditing(null); + setFormOpen(false); + }); }} onSubmit={submit} /> diff --git a/src/components/ui/form-dialog.test.tsx b/src/components/ui/form-dialog.test.tsx new file mode 100644 index 0000000..bceed4b --- /dev/null +++ b/src/components/ui/form-dialog.test.tsx @@ -0,0 +1,54 @@ +import { cleanup, fireEvent, render, screen } from "@solidjs/testing-library"; +import { createSignal } from "solid-js"; +import { FormDialog } from "@/components/ui/form-dialog"; + +beforeEach(() => vi.spyOn(window, "scrollTo").mockImplementation(() => {})); +afterEach(() => { + cleanup(); + vi.restoreAllMocks(); +}); + +test("form dialog stays open when the backdrop is pressed", async () => { + function TestFormDialog() { + const [open, setOpen] = createSignal(true); + return ( + <> + {open() ? "open" : "closed"} + +

Body

+
+ + ); + } + + render(() => ); + await new Promise((resolve) => setTimeout(resolve, 0)); + fireEvent.pointerDown(document.querySelector("[data-expanded]")!); + await new Promise((resolve) => setTimeout(resolve, 0)); + + expect(screen.getByText("open")).toBeTruthy(); +}); + +test("form dialog closes from the header close button after the gesture", async () => { + function TestFormDialog() { + const [open, setOpen] = createSignal(true); + return ( + <> + {open() ? "open" : "closed"} + + +

Body

+
+ + ); + } + + render(() => ); + await new Promise((resolve) => setTimeout(resolve, 0)); + fireEvent.click(screen.getByLabelText("Close")); + expect(screen.getByText("open")).toBeTruthy(); + await new Promise((resolve) => setTimeout(resolve, 0)); + expect(screen.getByText("closed")).toBeTruthy(); +}); diff --git a/src/components/ui/form-dialog.tsx b/src/components/ui/form-dialog.tsx index 99e32ab..1378a23 100644 --- a/src/components/ui/form-dialog.tsx +++ b/src/components/ui/form-dialog.tsx @@ -8,6 +8,12 @@ import { DialogTitle, } from "@/components/ui/dialog"; +/** + * Form surface in a dialog. Closes only via the header X (or an in-body + * Cancel that calls onOpenChange(false)) — backdrop/ESC are off so a + * dismiss gesture cannot fall through onto the trigger and reopen the + * same dialog on mobile. + */ export function FormDialog(props: { open: boolean; onOpenChange: (open: boolean) => void; @@ -16,9 +22,20 @@ export function FormDialog(props: { children: JSX.Element; class?: string; }) { + const handleOpenChange = (open: boolean) => { + if (open) { + props.onOpenChange(true); + return; + } + // Defer the close until after the current pointer/click finishes. Otherwise + // removing the dialog mid-gesture lets the same tap land on the trigger + // underneath (e.g. "From bank") and the dialog pops straight back open. + setTimeout(() => props.onOpenChange(false), 0); + }; + return ( - - + + {props.title} {props.description && {props.description}}