-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialogs.js
35 lines (30 loc) · 1.18 KB
/
dialogs.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
const wrapper = document.querySelector('main')
const openModalBtn = document.getElementById('openModal')
const cancelModalBtn = document.getElementById('cancelModalBtn')
const modal = document.getElementById('modal')
const modalBackground = document.querySelector('.modalBackground')
const openModal = () => {
modal.classList.remove('hidden')
modalBackground.classList.remove('hidden')
modal.focus()
wrapper.setAttribute('inert', '')
}
const closeModal = () => {
modal.classList.add('hidden')
modalBackground.classList.add('hidden')
wrapper.removeAttribute('inert')
openModalBtn.focus()
}
openModalBtn.addEventListener('click', openModal)
cancelModalBtn.addEventListener('click', closeModal)
modalBackground.addEventListener('click', closeModal)
window.addEventListener('keyup', e => {
if (e.key === 'Escape' && !modal.classList.contains('hidden')) {
closeModal()
}
})
const openDialogBtn = document.getElementById('openDialogBtn')
const dialog = document.getElementById('dialog')
const cancelDialogBtn = document.getElementById('cancelDialogBtn')
openDialogBtn.addEventListener('click', () => dialog.showModal())
cancelDialogBtn.addEventListener('click', () => dialog.close())