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", "name": "coryd.dev",
"version": "19.5.5", "version": "19.5.6",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "coryd.dev", "name": "coryd.dev",
"version": "19.5.5", "version": "19.5.6",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@cdransf/api-text": "^1.4.0", "@cdransf/api-text": "^1.4.0",

View file

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

View file

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