chore: simplify logic

This commit is contained in:
Cory Dransfeldt 2024-06-19 07:23:06 -07:00
parent 3cf15a323a
commit 435396f0fe
No known key found for this signature in database
3 changed files with 9 additions and 19 deletions

View file

@ -3,6 +3,7 @@ window.addEventListener('load', () => {
const menuButtonContainer = document.querySelector('.menu-button-container')
const menuItems = document.querySelectorAll('.menu-primary li[role="menu-item"]')
const isMobile = () => window.innerWidth <= 768
const updateTabIndex = () => {
const isExpanded = menuInput.checked
menuButtonContainer.setAttribute('aria-expanded', isExpanded)
@ -12,17 +13,16 @@ window.addEventListener('load', () => {
if (link) link.setAttribute('tabindex', isMobile() && !isExpanded ? '-1' : '0')
})
}
const handleMenuChange = () => updateTabIndex()
updateTabIndex()
menuInput.addEventListener('change', handleMenuChange)
menuInput.addEventListener('change', updateTabIndex)
menuButtonContainer.addEventListener('keydown', e => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault()
menuInput.checked = !menuInput.checked
handleMenuChange()
updateTabIndex()
}
})
@ -36,19 +36,9 @@ window.addEventListener('load', () => {
})
document.addEventListener('keydown', e => {
if (e.key === 'Escape') {
if (isMobile() && menuInput.checked) {
menuInput.checked = false
handleMenuChange()
}
}
})
window.addEventListener('resize', () => {
updateTabIndex()
if (!isMobile() && menuInput.checked) {
if (e.key === 'Escape' && isMobile() && menuInput.checked) {
menuInput.checked = false
handleMenuChange()
updateTabIndex()
}
})
})
})