}
searchDialog.show();
searchInput.focus();
-
+
const clickListener = e => {
- if(!e.target.closest('dialog')) {
+ if (!e.target.closest('dialog')) {
closeListener();
}
};
const escListener = e => {
if (e.key === 'Escape') {
- closeListener();
+ closeListener();
}
};
+ const arrowListener = e => {
+ if (e.key !== 'ArrowDown' && e.key !== 'ArrowUp') {
+ return;
+ }
+ e.preventDefault();
+
+ const links = Array.from(searchDialog.querySelectorAll('a'));
+ const focusables = [searchInput, ...links];
+ if (focusables.length < 2) { // Only search input
+ return;
+ }
+
+ const active = document.activeElement;
+ let currentIndex = focusables.indexOf(active);
+
+ if (e.key === 'ArrowDown') {
+ currentIndex = (currentIndex + 1) % focusables.length;
+ } else { // ArrowUp
+ currentIndex = (currentIndex - 1 + focusables.length) % focusables.length;
+ }
+
+ focusables[currentIndex].focus();
+ };
+
const mouseLeaveListener = e => {
closeListener();
- }
+ };
const closeListener = () => {
searchDialog.close();
document.removeEventListener('click', clickListener);
document.removeEventListener('keydown', escListener);
searchForm.removeEventListener('mouseleave', mouseLeaveListener);
+ searchForm.removeEventListener('keydown', arrowListener);
};
document.addEventListener('click', clickListener);
document.addEventListener('keydown', escListener);
searchForm.addEventListener('mouseleave', mouseLeaveListener);
+ searchForm.addEventListener('keydown', arrowListener);
}
function showSearchLoading() {