Last active
January 10, 2021 06:12
-
-
Save artzub/10c13fd53a54888b0374f28e94408f82 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const checkHref = (element) => { | |
const tag = element.tagName; | |
return !(tag === 'A' || tag === 'AREA') || element.href; | |
}; | |
const onFocus = (event) => { | |
if ( | |
event?.target?.tabIndex > -1 | |
&& current !== event.target | |
) { | |
// skip anchors and areas without href and disabled items | |
if (!checkHref(event.target) || event.target.disabled) { | |
return; | |
} | |
current = event.target; | |
show(current); | |
} | |
}; | |
const onBlur = (event) => { | |
if (event?.target?.tabIndex > -1) { | |
// find parent item that can be focusable | |
current = current?.parentNode; | |
while (current && current.tabIndex < 0) { | |
current = current.parentNode; | |
} | |
if (current === document) { | |
current = null; | |
} | |
// return focus to activeElement | |
if (!current && document.activeElement && document.activeElement !== document.body) { | |
current = document.activeElement; | |
} | |
if (current) { | |
show(current); | |
} else { | |
hide(); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment