-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialog.js
37 lines (34 loc) · 1.21 KB
/
dialog.js
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
const dialog = (msg, buttons) => {
const modal = document.createElement('dialog')
const close = document.createElement('button')
const form = document.createElement('form')
modal.classList.add('dde-dialog');
modal.innerHTML += '<div class="body">'+msg+'</div>'
close.classList.add('close');
close.textContent = 'X'
form.setAttribute('method', 'dialog')
modal.prepend(close)
close.addEventListener('click', (ev) => {modal.close(); console.log('CLOSE?');})
let lastButton;
for(name in buttons) {
let b = document.createElement('button')
b.textContent = name
b.addEventListener('click', (ev) => {buttons[name].call(b, ev); modal.close();})
form.append(b)
lastButton = b
}
/*
modal.addEventListener('click', function(event) { // from https://stackoverflow.com/questions/25864259/how-to-close-the-new-html-dialog-tag-by-clicking-on-its-backdrop
var rect = modal.getBoundingClientRect();
var isInDialog = (rect.top <= event.clientY && event.clientY <= rect.top + rect.height &&
rect.left <= event.clientX && event.clientX <= rect.left + rect.width);
if (!isInDialog) {
modal.close();
}
});
*/
modal.append(form)
document.body.append(modal)
modal.showModal();
lastButton.focus(); // last button
}