-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDialog.js
More file actions
48 lines (43 loc) · 1.35 KB
/
Dialog.js
File metadata and controls
48 lines (43 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* @template {HTMLElement} T
* @param {T} dialogParent
*/
export function open(dialogParent, {dismissable = false} = {}) {
document.body.appendChild(dialogParent)
dialogParent.classList.remove('custom-element') // TODO: 'display: contents' breaks dialogs
let dialog = dialogParent.querySelector('dialog')
dialog.showModal()
dialog.addEventListener('close', () => {
dialogParent.remove()
})
if (dismissable) {
// 'closedby' attribute is not supported in target browsers
dialog.addEventListener('click', e => {
let rect = dialog.getBoundingClientRect()
if (
dialog.open && e.detail != 0 &&
!(rect.left <= e.clientX && e.clientX <= rect.right
&& rect.top <= e.clientY && e.clientY <= rect.bottom)
) {
cancel(dialogParent)
}
})
}
return dialogParent
}
/**
* @param {HTMLElement} dialogParent
*/
export function close(dialogParent) {
dialogParent.querySelector('dialog').close()
}
/**
* @param {HTMLElement} dialogParent
*/
export function cancel(dialogParent) {
// 'requestClose()' function is not supported in target browsers
let dialog = dialogParent.querySelector('dialog')
if (dialog.dispatchEvent(new Event('cancel', {cancelable: true}))) {
dialog.close()
}
}