|
| 1 | +import { createAttachmentKey } from 'svelte/attachments'; |
| 2 | + |
| 3 | +/** |
| 4 | + * @template Resolved |
| 5 | + * @typedef EnhanceDialogOptions |
| 6 | + * @property {Resolved} [defaultReturnValue] default return value when dialog is closed, helpful to capture method="dialog" form / inputs without the need for additional JavaScript |
| 7 | + * @property {'animationend'} [delayResolution] delay resolution, helpful for exit animation and such |
| 8 | + */ |
| 9 | + |
| 10 | +/** |
| 11 | + * @typedef EnhanceDialogAttributes |
| 12 | + * @property {(e: CustomEvent<void>) => void} [onclickbackdrop] fired when clicked on backdrop |
| 13 | + */ |
| 14 | + |
| 15 | +/** |
| 16 | + * enhance an `HTMLDialogElement` when used as component for `StackItem`. This will: |
| 17 | + * 1. call `showModal()` on the dialog is mounted, |
| 18 | + * 2. capture `form.returnValue` when integrated with method="dialog" form / inputs, |
| 19 | + * 3. close the dialog when backdrop is clicked. |
| 20 | + * @template Resolved |
| 21 | + * @param {import('../stack-item.svelte').StackItem<import('svelte').Component<import('../types.public').StackItemProps<Resolved>>, Resolved>} item |
| 22 | + * @param {EnhanceDialogOptions<Resolved>} [options] |
| 23 | + * @returns {import('svelte/elements').HTMLDialogAttributes & EnhanceDialogAttributes} |
| 24 | + */ |
| 25 | +export function enhanceDialog(item, options) { |
| 26 | + /** @type {undefined | (() => void)} */ |
| 27 | + let resumeResolution = undefined; |
| 28 | + if (options?.delayResolution) { |
| 29 | + item.onResolve(() => new Promise((resolve) => (resumeResolution = resolve))); |
| 30 | + } |
| 31 | + |
| 32 | + return { |
| 33 | + /** @param {Event} event */ |
| 34 | + onanimationend(event) { |
| 35 | + const dialog = /** @type {HTMLDialogElement} */ (event.target); |
| 36 | + if (dialog.open) return; |
| 37 | + if (options?.delayResolution === 'animationend') { |
| 38 | + resumeResolution?.(); |
| 39 | + } |
| 40 | + }, |
| 41 | + |
| 42 | + /** @param {Event} event */ |
| 43 | + onclose: function (event) { |
| 44 | + const dialog = /** @type {HTMLDialogElement} */ (event.target); |
| 45 | + // if dialog is setup with "method=dialog" form / inputs |
| 46 | + // this will help capture without the need for JavaScript |
| 47 | + item.resolve( |
| 48 | + /** @type {Resolved} */ (dialog.returnValue) || options?.defaultReturnValue || undefined, |
| 49 | + ); |
| 50 | + }, |
| 51 | + |
| 52 | + /** |
| 53 | + * @param {HTMLDialogElement} element |
| 54 | + */ |
| 55 | + [createAttachmentKey()]: function (element) { |
| 56 | + const dialog = /** @type {HTMLDialogElement} */ (element); |
| 57 | + dialog.showModal(); |
| 58 | + }, |
| 59 | + |
| 60 | + onclickbackdrop, |
| 61 | + onclick, |
| 62 | + }; |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * @param {MouseEvent} event |
| 67 | + */ |
| 68 | +function onclick(event) { |
| 69 | + const dialog = /** @type {HTMLDialogElement} */ (event.currentTarget); |
| 70 | + const rect = /** @type {HTMLDialogElement} */ (event.target).getBoundingClientRect(); |
| 71 | + if (!event.clientX || !event.clientY) return; // not a mouse event (probably triggered by keyboard) |
| 72 | + if ( |
| 73 | + rect.left > event.clientX || |
| 74 | + rect.right < event.clientX || |
| 75 | + rect.top > event.clientY || |
| 76 | + rect.bottom < event.clientY |
| 77 | + ) { |
| 78 | + dialog.dispatchEvent(new CustomEvent('clickbackdrop')); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * @param {CustomEvent} event |
| 84 | + */ |
| 85 | +function onclickbackdrop(event) { |
| 86 | + const dialog = /** @type {HTMLDialogElement} */ (event.currentTarget); |
| 87 | + dialog.close(); |
| 88 | +} |
0 commit comments