Skip to content

Instantly share code, notes, and snippets.

@artzub
Last active January 10, 2021 06:12
Show Gist options
  • Save artzub/10c13fd53a54888b0374f28e94408f82 to your computer and use it in GitHub Desktop.
Save artzub/10c13fd53a54888b0374f28e94408f82 to your computer and use it in GitHub Desktop.
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