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

4
package-lock.json generated
View file

@ -1,12 +1,12 @@
{
"name": "coryd.dev",
"version": "19.5.5",
"version": "19.5.6",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "coryd.dev",
"version": "19.5.5",
"version": "19.5.6",
"license": "MIT",
"dependencies": {
"@cdransf/api-text": "^1.4.0",

View file

@ -1,6 +1,6 @@
{
"name": "coryd.dev",
"version": "19.5.5",
"version": "19.5.6",
"description": "The source for my personal site. Built using 11ty.",
"type": "module",
"scripts": {

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()
}
})
})