Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search result focus improvements #556

Open
wants to merge 7 commits into
base: preview
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/static/css/site.css
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ summary .group-name {
border-bottom: 1px solid var(--dim-color);
}

.wiki-search-result:hover::before {
.wiki-search-result:hover::before, .wiki-search-result:focus::before {
leo60228 marked this conversation as resolved.
Show resolved Hide resolved
display: block;
background: var(--light-ghost-color);
}
Expand Down
47 changes: 46 additions & 1 deletion src/static/js/client/sidebar-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ export const info = {
stoppedScrollingTimeout: null,

indexDownloadStatuses: Object.create(null),

currentValue: null,
leo60228 marked this conversation as resolved.
Show resolved Hide resolved
},

session: {
Expand Down Expand Up @@ -298,18 +300,33 @@ export function mutatePageContent() {
info.searchBox.appendChild(info.endSearchLine);
}

function trackSidebarSearchInputChanged() {
const {state} = info;

const newValue = info.searchInput.value;

if (newValue === state.currentValue) {
return false;
} else {
state.currentValue = newValue;
return !!newValue;
}
}

export function addPageListeners() {
if (!info.searchInput) return;

info.searchInput.addEventListener('change', _domEvent => {
if (info.searchInput.value) {
if (trackSidebarSearchInputChanged()) {
activateSidebarSearch(info.searchInput.value);
}
});

info.searchInput.addEventListener('input', _domEvent => {
const {settings, state} = info;

trackSidebarSearchInputChanged();

if (!info.searchInput.value) {
clearSidebarSearch();
return;
Expand All @@ -325,6 +342,16 @@ export function addPageListeners() {
}, settings.stoppedTypingDelay);
});

info.searchInput.addEventListener('keydown', domEvent => {
if (domEvent.key === 'ArrowDown') {
const elem = info.results?.firstChild;
leo60228 marked this conversation as resolved.
Show resolved Hide resolved
if (elem && !elem.classList.contains('wiki-search-no-results')) {
leo60228 marked this conversation as resolved.
Show resolved Hide resolved
domEvent.preventDefault();
elem.focus({focusVisible: true});
}
}
});

info.endSearchLink.addEventListener('click', domEvent => {
domEvent.preventDefault();
clearSidebarSearch();
Expand Down Expand Up @@ -771,6 +798,24 @@ function generateSidebarSearchResultTemplate(slots) {
saveSidebarSearchResultsScrollOffset();
});

link.addEventListener('keydown', domEvent => {
if (domEvent.key === 'ArrowDown') {
const elem = link.nextElementSibling;
if (elem) {
domEvent.preventDefault();
elem.focus({focusVisible: true});
}
} else if (domEvent.key === 'ArrowUp') {
domEvent.preventDefault();
const elem = link.previousElementSibling;
if (elem) {
elem.focus({focusVisible: true});
} else {
info.searchInput.focus();
}
}
});

return link;
}

Expand Down