From a69482ee612f1ff689d4fbc5a259b5b6d1f77c9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20Boduro=C4=9Flu?= <80620802+burakboduroglu@users.noreply.github.com> Date: Fri, 18 Sep 2026 12:00:11 +0300 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=9B=9F=20side=20panels=20ask=20be?= =?UTF-8?q?fore=20throwing=20input=20away=20-=20SidePanel=20takes=20dirty?= =?UTF-8?q?=20(explicit)=20or=20guardUnsaved=20(any=20input=20or=20change?= =?UTF-8?q?=20=20=20inside=20counts);=20Escape,=20an=20outside=20click=20o?= =?UTF-8?q?r=20the=20close=20button=20then=20open=20a=20=20=20discard=20co?= =?UTF-8?q?nfirm,=20while=20a=20submit=20or=20explicit=20Cancel=20still=20?= =?UTF-8?q?closes=20directly=20-=20pilot:=20class=20create,=20meal=20publi?= =?UTF-8?q?sh=20and=20the=20exam=20create=20details=20step=20-=20fix:=20a?= =?UTF-8?q?=20press=20inside=20the=20confirm=20read=20as=20an=20outside=20?= =?UTF-8?q?click=20to=20the=20=20=20non-modal=20panel=20and=20reopened=20t?= =?UTF-8?q?he=20confirm=20-=20fix:=20Kobalte=20hands=20plain=20dialogs=20t?= =?UTF-8?q?he=20alertdialog=20role=20whenever=20an=20=20=20AlertDialog=20i?= =?UTF-8?q?s=20in=20the=20tree;=20SidePanel=20and=20Dialog=20set=20role=3D?= =?UTF-8?q?dialog=20-=20fix:=20ConfirmDialog's=20cancel=20button=20was=20a?= =?UTF-8?q?nnounced=20as=20an=20English=20=20=20"Dismiss";=20it=20now=20ca?= =?UTF-8?q?rries=20its=20visible=20label=20-=20SidePanel=20close=20button?= =?UTF-8?q?=20label=20moves=20into=20i18n=20-=20tests=20for=20the=20guard,?= =?UTF-8?q?=20the=20role=20and=20guardUnsaved?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/ui/confirm-dialog.tsx | 3 ++ src/components/ui/dialog.tsx | 3 ++ src/components/ui/side-panel.test.tsx | 71 +++++++++++++++++++++++++++ src/components/ui/side-panel.tsx | 59 ++++++++++++++++++++-- src/i18n/messages.ts | 18 +++++++ src/pages/classes-page.tsx | 2 +- src/pages/exams-page.tsx | 3 ++ src/pages/meals-page.tsx | 2 +- 8 files changed, 155 insertions(+), 6 deletions(-) create mode 100644 src/components/ui/side-panel.test.tsx diff --git a/src/components/ui/confirm-dialog.tsx b/src/components/ui/confirm-dialog.tsx index 3f9e5c1..3f4dccb 100644 --- a/src/components/ui/confirm-dialog.tsx +++ b/src/components/ui/confirm-dialog.tsx @@ -134,6 +134,9 @@ export function ConfirmDialog(props: ConfirmDialogProps) { "hover:bg-accent hover:text-accent-foreground disabled:opacity-50", )} disabled={pending()} + // Kobalte's close button defaults its accessible name to an English + // "Dismiss", which overrides the visible text. + aria-label={props.cancelLabel ?? t("common.cancel")} > {props.cancelLabel ?? t("common.cancel")} diff --git a/src/components/ui/dialog.tsx b/src/components/ui/dialog.tsx index 544f1e8..04d7b48 100644 --- a/src/components/ui/dialog.tsx +++ b/src/components/ui/dialog.tsx @@ -25,6 +25,9 @@ export function DialogContent( never clipped by the status bar or the gesture bar. */}
vi.spyOn(window, "scrollTo").mockImplementation(() => {})); +afterEach(() => { + cleanup(); + vi.restoreAllMocks(); +}); + +function mount(dirty: boolean) { + const [open, setOpen] = createSignal(true); + render(() => ( + + {open() ? "open" : "closed"} + + + + + )); +} + +const confirmTitle = /Discard your changes\?|Değişiklikler silinsin mi\?/; +const closeButton = () => screen.getAllByRole("button", { name: /^(Close|Kapat)$/ })[0]; + +test("a clean panel closes straight away", async () => { + mount(false); + fireEvent.click(closeButton()); + await waitFor(() => expect(screen.getByTestId("state").textContent).toBe("closed")); +}); + +test("a dirty panel asks before discarding, and stays open on Keep editing", async () => { + mount(true); + fireEvent.click(closeButton()); + + expect(await screen.findByText(confirmTitle)).toBeTruthy(); + expect(screen.getByTestId("state").textContent).toBe("open"); + + fireEvent.click(screen.getByRole("button", { name: /Keep editing|Düzenlemeye devam et/ })); + await waitFor(() => expect(screen.queryByText(confirmTitle)).toBeNull()); + expect(screen.getByTestId("state").textContent).toBe("open"); + + fireEvent.click(closeButton()); + fireEvent.click(await screen.findByRole("button", { name: /^(Discard|Sil ve kapat)$/ })); + await waitFor(() => expect(screen.getByTestId("state").textContent).toBe("closed")); +}); + +test("the panel is announced as a dialog, not an alert dialog", () => { + mount(false); + expect(screen.getByRole("dialog")).toBeTruthy(); + expect(screen.queryByRole("alertdialog")).toBeNull(); +}); + +test("guardUnsaved asks only once something was typed", async () => { + const [open, setOpen] = createSignal(true); + render(() => ( + + {open() ? "open" : "closed"} + + + + + )); + + fireEvent.input(screen.getByLabelText("Slot"), { target: { value: "Lunch" } }); + fireEvent.click(closeButton()); + expect(await screen.findByText(confirmTitle)).toBeTruthy(); + expect(screen.getByTestId("state").textContent).toBe("open"); +}); diff --git a/src/components/ui/side-panel.tsx b/src/components/ui/side-panel.tsx index b266ba6..bdb3514 100644 --- a/src/components/ui/side-panel.tsx +++ b/src/components/ui/side-panel.tsx @@ -1,7 +1,9 @@ import { Dialog as DialogPrimitive } from "@kobalte/core/dialog"; -import { createEffect, onCleanup, type ParentProps } from "solid-js"; +import { createEffect, createSignal, onCleanup, type ParentProps } from "solid-js"; +import { ConfirmDialog } from "@/components/ui/confirm-dialog"; import { IconX } from "@/components/ui/icons"; import { cn } from "@/lib/cn"; +import { useT } from "@/stores/preferences-context"; let openPanelCount = 0; @@ -15,8 +17,38 @@ export function SidePanel( /** default ~34rem, wide ~42rem, xl ~48rem — for report tables */ size?: "default" | "wide" | "xl"; bodyClass?: string; + /** + * The form inside holds unsaved input. Escape, an outside click or the + * close button then ask before discarding it; a submit or an explicit + * Cancel that sets `open` directly is not asked. + */ + dirty?: boolean; + /** + * Treat any input or change inside the panel as unsaved work, for forms + * that keep their state to themselves. Ignored when `dirty` is given. + * Leave it off for panels whose controls save as you go. + */ + guardUnsaved?: boolean; }>, ) { + const t = useT(); + const [confirmDiscard, setConfirmDiscard] = createSignal(false); + const [touched, setTouched] = createSignal(false); + createEffect(() => { + if (props.open) setTouched(false); + }); + const isDirty = () => props.dirty ?? (!!props.guardUnsaved && touched()); + const markTouched = () => { + if (props.guardUnsaved) setTouched(true); + }; + const requestOpenChange = (open: boolean) => { + // A press inside the confirm dialog reads as an outside interaction to + // this non-modal panel; while the dialog is up it owns the decision. + if (!open && confirmDiscard()) return; + if (!open && isDirty()) return setConfirmDiscard(true); + props.onOpenChange(open); + }; + // Kobalte's modal Dialog hides the app with aria-hidden before its focus // scope can move focus into this portalled panel. Chromium correctly blocks // that transition when the opener still owns focus. `inert` makes the app @@ -40,11 +72,15 @@ export function SidePanel( }; return ( - + <> + {/* z-[70]: above the mobile nav sheet (z-60) and tab bar (z-40). */}
-
+
{props.children}
+ props.onOpenChange(false)} + /> + ); } diff --git a/src/i18n/messages.ts b/src/i18n/messages.ts index 1b807ac..da2da1c 100644 --- a/src/i18n/messages.ts +++ b/src/i18n/messages.ts @@ -616,6 +616,12 @@ export type MessageKey = | "common.deleted" | "common.saved" | "common.loadAll" + | "common.close" + | "common.dismiss" + | "sidePanel.discardTitle" + | "sidePanel.discardSummary" + | "sidePanel.discard" + | "sidePanel.keepEditing" | "dashboard.partialError" | "common.breadcrumb" | "common.truncatedNotice" @@ -2751,6 +2757,12 @@ const en: Dict = { "common.deleted": "Deleted.", "common.saved": "Saved successfully.", "common.loadAll": "Load all", + "common.close": "Close", + "common.dismiss": "Dismiss", + "sidePanel.discardTitle": "Discard your changes?", + "sidePanel.discardSummary": "What you entered in this form has not been saved and will be lost.", + "sidePanel.discard": "Discard", + "sidePanel.keepEditing": "Keep editing", "dashboard.partialError": "Some panels could not load and show no data.", "common.breadcrumb": "Breadcrumb", "common.truncatedNotice": "Showing the first {shown} of {total} records.", @@ -4888,6 +4900,12 @@ const tr: Dict = { "common.deleted": "Silindi.", "common.saved": "Başarıyla kaydedildi.", "common.loadAll": "Tümünü yükle", + "common.close": "Kapat", + "common.dismiss": "Gizle", + "sidePanel.discardTitle": "Değişiklikler silinsin mi?", + "sidePanel.discardSummary": "Bu forma girdikleriniz kaydedilmedi ve kaybolacak.", + "sidePanel.discard": "Sil ve kapat", + "sidePanel.keepEditing": "Düzenlemeye devam et", "dashboard.partialError": "Bazı bölümler yüklenemedi ve veri göstermiyor.", "common.breadcrumb": "Sayfa konumu", "common.truncatedNotice": "{total} kayıttan ilk {shown} tanesi gösteriliyor.", diff --git a/src/pages/classes-page.tsx b/src/pages/classes-page.tsx index 5a0a36e..03401b8 100644 --- a/src/pages/classes-page.tsx +++ b/src/pages/classes-page.tsx @@ -173,7 +173,7 @@ function ClassesContent() { return (
- +
setName(e.currentTarget.value)} />
diff --git a/src/pages/exams-page.tsx b/src/pages/exams-page.tsx index 73c0bdb..690989b 100644 --- a/src/pages/exams-page.tsx +++ b/src/pages/exams-page.tsx @@ -388,6 +388,9 @@ function ExamsContent() { { setCreateOpen(open); if (!open) { diff --git a/src/pages/meals-page.tsx b/src/pages/meals-page.tsx index e3e7d75..0dbc048 100644 --- a/src/pages/meals-page.tsx +++ b/src/pages/meals-page.tsx @@ -91,7 +91,7 @@ function MealsContent() { return (
- + void refetchLimits()} />
setDate(inputDateToIso(value))} />