🔁 default dialog dismiss on outside/ESC, add side-panel-body class to form dialog
Browse files at this commitc484f9e37b18a8333b1d8e9c5d4a4b75b044dfceParent: 8b9f154- fix
- 🔁 default dialog dismiss on outside/ESC, add side-panel-body class to form dialog
3 changed files +37 -6
- src/components/ui/dialog.test.tsx +31 -0
- src/components/ui/dialog.tsx +5 -5
- src/components/ui/form-dialog.tsx +1 -1
src/components/ui/dialog.test.tsxadded+31 -0
| @@ -0,0 +1,31 @@ | |||
| 1 | +import { cleanup, fireEvent, render, screen } from "@solidjs/testing-library"; | ||
| 2 | +import { createSignal } from "solid-js"; | ||
| 3 | +import { Dialog, DialogContent, DialogTitle } from "@/components/ui/dialog"; | ||
| 4 | + | ||
| 5 | +beforeEach(() => vi.spyOn(window, "scrollTo").mockImplementation(() => {})); | ||
| 6 | +afterEach(() => { | ||
| 7 | + cleanup(); | ||
| 8 | + vi.restoreAllMocks(); | ||
| 9 | +}); | ||
| 10 | + | ||
| 11 | +test("dialog closes when backdrop is pressed", async () => { | ||
| 12 | + function TestDialog() { | ||
| 13 | + const [open, setOpen] = createSignal(true); | ||
| 14 | + return ( | ||
| 15 | + <> | ||
| 16 | + <span>{open() ? "open" : "closed"}</span> | ||
| 17 | + <Dialog open={open()} onOpenChange={setOpen}> | ||
| 18 | + <DialogContent> | ||
| 19 | + <DialogTitle>Test dialog</DialogTitle> | ||
| 20 | + </DialogContent> | ||
| 21 | + </Dialog> | ||
| 22 | + </> | ||
| 23 | + ); | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + render(() => <TestDialog />); | ||
| 27 | + await new Promise((resolve) => setTimeout(resolve, 0)); | ||
| 28 | + fireEvent.pointerDown(document.querySelector("[data-expanded]")!); | ||
| 29 | + | ||
| 30 | + expect(screen.getByText("closed")).toBeTruthy(); | ||
| 31 | +}); | ||
src/components/ui/dialog.tsxmodified+5 -5
| @@ -26,11 +26,11 @@ export function DialogContent<T extends ValidComponent = "div">( | |||
| 26 | "pointer-events-auto relative flex max-h-[min(90vh,48rem)] w-full max-w-3xl flex-col overflow-hidden rounded-2xl border border-black/8 bg-popover/95 text-popover-foreground shadow-apple outline-hidden backdrop-blur-xl animate-fade-up dark:border-white/12 sm:rounded-3xl", | 26 | "pointer-events-auto relative flex max-h-[min(90vh,48rem)] w-full max-w-3xl flex-col overflow-hidden rounded-2xl border border-black/8 bg-popover/95 text-popover-foreground shadow-apple outline-hidden backdrop-blur-xl animate-fade-up dark:border-white/12 sm:rounded-3xl", |
| 27 | local.class, | 27 | local.class, |
| 28 | )} | 28 | )} |
| 29 | - // Default: close only via X — outside/ESC races with clickable cards underneath. | 29 | + // Standard dialogs dismiss outside/ESC; pass false only when losing |
| 30 | - // Opt into backdrop/ESC dismissal per-dialog with `dismissable`. | 30 | + // unsaved state would be unsafe. |
| 31 | - onPointerDownOutside={local.dismissable ? undefined : (e) => e.preventDefault()} | 31 | + onPointerDownOutside={local.dismissable === false ? (e) => e.preventDefault() : undefined} |
| 32 | - onInteractOutside={local.dismissable ? undefined : (e) => e.preventDefault()} | 32 | + onInteractOutside={local.dismissable === false ? (e) => e.preventDefault() : undefined} |
| 33 | - onEscapeKeyDown={local.dismissable ? undefined : (e) => e.preventDefault()} | 33 | + onEscapeKeyDown={local.dismissable === false ? (e) => e.preventDefault() : undefined} |
| 34 | {...rest} | 34 | {...rest} |
| 35 | > | 35 | > |
| 36 | <DialogPrimitive.CloseButton | 36 | <DialogPrimitive.CloseButton |
src/components/ui/form-dialog.tsxmodified+1 -1
| @@ -23,7 +23,7 @@ export function FormDialog(props: { | |||
| 23 | <DialogTitle>{props.title}</DialogTitle> | 23 | <DialogTitle>{props.title}</DialogTitle> |
| 24 | {props.description && <DialogDescription>{props.description}</DialogDescription>} | 24 | {props.description && <DialogDescription>{props.description}</DialogDescription>} |
| 25 | </DialogHeader> | 25 | </DialogHeader> |
| 26 | - <DialogBody>{props.children}</DialogBody> | 26 | + <DialogBody class="side-panel-body">{props.children}</DialogBody> |
| 27 | </DialogContent> | 27 | </DialogContent> |
| 28 | </Dialog> | 28 | </Dialog> |
| 29 | ); | 29 | ); |