chore: consolidate broadly used js + cleanup
This commit is contained in:
parent
ee0832dd0b
commit
10dd21f6e3
10 changed files with 78 additions and 85 deletions
43
src/assets/scripts/index.js
Normal file
43
src/assets/scripts/index.js
Normal file
|
@ -0,0 +1,43 @@
|
|||
window.addEventListener('load', () => {
|
||||
// menu keyboard controls
|
||||
;(() => {
|
||||
const menuInput = document.getElementById('menu-toggle')
|
||||
const menuButtonContainer = document.querySelector('.menu-button-container')
|
||||
const menuItems = document.querySelectorAll('.menu-primary li')
|
||||
|
||||
menuButtonContainer.addEventListener('keydown', (e) => {
|
||||
if (e.key === 'Enter' || e.key === ' ') {
|
||||
e.preventDefault()
|
||||
menuInput.checked = !menuInput.checked
|
||||
}
|
||||
});
|
||||
|
||||
menuItems.forEach((item) => {
|
||||
item.addEventListener('keydown', (e) => {
|
||||
if (e.key === 'Enter' || e.key === ' ') {
|
||||
e.preventDefault()
|
||||
item.querySelector('a').click()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
document.addEventListener('keydown', (e) => {
|
||||
if (e.key === 'Escape' && menuInput.checked) menuInput.checked = false
|
||||
})
|
||||
})()
|
||||
|
||||
// modal keyboard controls
|
||||
;(() => {
|
||||
const modalInputs = document.querySelectorAll('.modal-input')
|
||||
|
||||
if (!modalInputs) return // early return if dom nodes are missing
|
||||
|
||||
document.addEventListener('keydown', (e) => {
|
||||
if (e.key === 'Escape') {
|
||||
modalInputs.forEach((modalInput) => {
|
||||
if (modalInput.checked) modalInput.checked = false
|
||||
})
|
||||
}
|
||||
})
|
||||
})()
|
||||
})
|
Reference in a new issue