Skip to content

Improve search bar navigation #1827

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

Merged
merged 1 commit into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 assets/css/autocomplete.css
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
top: 9px;
left: 10%;
transform: translateX(-50%);
z-index: 250;
z-index: 100;
background-color: transparent;
}

Expand Down
4 changes: 4 additions & 0 deletions assets/js/autocomplete/autocomplete-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { getSuggestions } from './suggestions'
import { isBlank, qs } from '../helpers'

export const AUTOCOMPLETE_CONTAINER_SELECTOR = '.autocomplete'
export const AUTOCOMPLETE_SUGGESTION_LIST_SELECTOR = '.autocomplete-suggestions'
export const AUTOCOMPLETE_SUGGESTION_SELECTOR = '.autocomplete-suggestion'

const state = {
Expand Down Expand Up @@ -88,6 +89,9 @@ export function moveAutocompleteSelection (offset) {

if (elementToSelect) {
elementToSelect.classList.add('selected')
elementToSelect.scrollIntoView({ block: 'nearest' })
} else {
qs(AUTOCOMPLETE_SUGGESTION_LIST_SELECTOR).scrollTop = 0
}
}

Expand Down
10 changes: 8 additions & 2 deletions assets/js/search-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,17 @@ function addEventListeners () {
const searchInput = qs(SEARCH_INPUT_SELECTOR)

searchInput.addEventListener('keydown', event => {
const macOS = isMacOS()

if (event.key === 'Escape') {
clearSearch()
searchInput.blur()
} else if (event.key === 'Enter') {
handleAutocompleteFormSubmission(event)
} else if (event.key === 'ArrowUp') {
} else if (event.key === 'ArrowUp' || (macOS && event.ctrlKey && event.key === 'p')) {
moveAutocompleteSelection(-1)
event.preventDefault()
} else if (event.key === 'ArrowDown') {
} else if (event.key === 'ArrowDown' || (macOS && event.ctrlKey && event.key === 'n')) {
moveAutocompleteSelection(1)
event.preventDefault()
}
Expand Down Expand Up @@ -135,3 +137,7 @@ function hideAutocomplete () {
document.body.classList.remove('search-focused')
hideAutocompleteList()
}

function isMacOS () {
return /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform)
}
2 changes: 0 additions & 2 deletions lib/ex_doc/formatter/html/templates/sidebar_template.eex
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,4 @@
</div>
</div>
<div class="autocomplete">
<div class="autocomplete-results">
</div>
</div>