|
1 | 1 | <script lang="ts"> |
2 | | - import { onDestroy } from "svelte"; |
3 | 2 | import type { DialogProps, ParamsType } from "$lib"; |
4 | 3 | import { trapFocus } from "$lib/utils/actions"; |
5 | | - import { dialog } from "./theme"; |
6 | 4 | import CloseButton from "$lib/utils/CloseButton.svelte"; |
7 | 5 | import { createDismissableContext } from "$lib/utils/dismissable"; |
8 | 6 | import clsx from "clsx"; |
9 | 7 | import { sineIn } from "svelte/easing"; |
10 | 8 | import { fade } from "svelte/transition"; |
| 9 | + import { dialog } from "./theme"; |
11 | 10 |
|
12 | 11 | let { children, onaction = () => true, oncancel, onsubmit, ontoggle, form = false, modal = true, autoclose = false, focustrap = false, open = $bindable(false), permanent = false, dismissable = true, outsideclose = true, class: className, classes, transition = fade, transitionParams, count, ...restProps }: DialogProps = $props(); |
13 | 12 |
|
|
56 | 55 | } |
57 | 56 |
|
58 | 57 | function _onsubmit(ev: SubmitEvent & { currentTarget: HTMLDialogElement }) { |
59 | | - // When dialog contains the <form method="dialog"> and when child with type="submit" was pressed |
60 | | -
|
61 | 58 | onsubmit?.(ev); |
62 | 59 | if (ev.defaultPrevented) return; |
63 | 60 |
|
| 61 | + // When dialog contains the <form method="dialog"> and when child with type="submit" was pressed |
| 62 | + if (!(ev.target instanceof HTMLFormElement) || ev.target.method !== "dialog") { |
| 63 | + return; |
| 64 | + } |
| 65 | +
|
64 | 66 | ev.preventDefault(); // stop dialog.close() |
65 | 67 |
|
66 | 68 | const dlg: HTMLDialogElement = ev.currentTarget; |
67 | 69 |
|
68 | | - if (ev.submitter instanceof HTMLButtonElement || ev.submitter instanceof HTMLInputElement) { |
69 | | - dlg.returnValue = ev.submitter.value; |
| 70 | + if (ev.submitter && "value" in ev.submitter) { |
| 71 | + // this is done by the system but after the submit event |
| 72 | + dlg.returnValue = String(ev.submitter.value ?? ""); |
70 | 73 | } |
71 | 74 |
|
72 | 75 | if (!dlg.returnValue) { |
73 | 76 | return cancel(dlg); // if no action - treat that as cancel |
74 | 77 | } |
75 | 78 |
|
76 | | - // explicit false from onaction blocks the form closing |
77 | | - if (typeof onaction === "function" && onaction({ action: dlg.returnValue, data: new FormData(ev.target as HTMLFormElement) }) === false) return; |
| 79 | + if (typeof onaction === "function") { |
| 80 | + const result = onaction({ action: dlg.returnValue, data: new FormData(ev.target) }); |
| 81 | + // explicit false from onaction blocks the form closing |
| 82 | + if (result === false) return; |
| 83 | + } |
78 | 84 |
|
79 | 85 | close(dlg); |
80 | 86 | } |
|
116 | 122 | } |
117 | 123 |
|
118 | 124 | createDismissableContext(close_handler); |
119 | | -
|
120 | | - let escHandler: ((e: KeyboardEvent) => void) | null = null; |
121 | | -
|
122 | | - $effect(() => { |
123 | | - if (escHandler) { |
124 | | - window.removeEventListener("keydown", escHandler); |
125 | | - escHandler = null; |
126 | | - } |
127 | | -
|
128 | | - if (open && typeof count === "number" && count > 0) { |
129 | | - escHandler = (e: KeyboardEvent) => { |
130 | | - if (e.key === "Escape") { |
131 | | - e.preventDefault(); |
132 | | - e.stopPropagation(); |
133 | | - } |
134 | | - }; |
135 | | - window.addEventListener("keydown", escHandler); |
136 | | - } |
137 | | - }); |
138 | | -
|
139 | | - onDestroy(() => { |
140 | | - if (escHandler) { |
141 | | - window.removeEventListener("keydown", escHandler); |
142 | | - } |
143 | | - }); |
144 | 125 | </script> |
145 | 126 |
|
146 | 127 | {#snippet content()} |
|
0 commit comments