|
4 | 4 | function openSearchPopover () {
|
5 | 5 | document.getElementById('search-background').style.display = 'block'
|
6 | 6 | document.getElementById('search').style.display = 'block'
|
| 7 | + |
7 | 8 | // focus the textbox after popover appears
|
| 9 | + focusSearchInput() |
| 10 | + // add eventlisteners after popover appears |
| 11 | + addNavigationToSearch() |
| 12 | + } |
| 13 | + |
| 14 | + function closeSearchPopover () { |
| 15 | + document.getElementById('search-background').style.display = 'none' |
| 16 | + document.getElementById('search').style.display = 'none' |
| 17 | + } |
| 18 | + |
| 19 | + function focusSearchInput () { |
8 | 20 | var searchInput = document.querySelector('.pagefind-modular-input')
|
9 | 21 | if (searchInput) {
|
10 | 22 | searchInput.focus()
|
11 | 23 | }
|
12 | 24 | }
|
13 | 25 |
|
14 |
| - function closeSearchPopover () { |
15 |
| - document.getElementById('search-background').style.display = 'none' |
16 |
| - document.getElementById('search').style.display = 'none' |
| 26 | + function addNavigationToSearch () { |
| 27 | + // focus the first search result when pressing enter in search input |
| 28 | + document.getElementById('pfmod-input-0').addEventListener('keydown', goToSearchResultsOnEnter) |
| 29 | + } |
| 30 | + |
| 31 | + function goToSearchResultsOnEnter (event) { |
| 32 | + var searchResults = document.getElementById('search-results') |
| 33 | + if (event.key === 'Enter' && searchResults.childElementCount > 0) { |
| 34 | + searchResults.firstChild.querySelector('a').focus() |
| 35 | + searchResults.addEventListener('keydown', addNavigationInSearchResults) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + function addNavigationInSearchResults (event) { |
| 40 | + if (event.key === 'ArrowDown' && document.activeElement.classList.contains('pagefind-modular-list-link')) { |
| 41 | + event.preventDefault() // prevent page scrolling |
| 42 | + var nextSibling = document.activeElement.parentElement.parentElement.parentElement.nextElementSibling |
| 43 | + if (nextSibling) { |
| 44 | + nextSibling.querySelector('a').focus() |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + if (event.key === 'ArrowUp' && document.activeElement.classList.contains('pagefind-modular-list-link')) { |
| 49 | + event.preventDefault() // prevent page scrolling |
| 50 | + var previousSibling = document.activeElement.parentElement.parentElement.parentElement.previousElementSibling |
| 51 | + if (previousSibling) { |
| 52 | + previousSibling.querySelector('a').focus() |
| 53 | + } |
| 54 | + } |
17 | 55 | }
|
18 | 56 |
|
19 | 57 | // open the popover when clicking the magnifying glass search icon
|
|
28 | 66 | // close functionality when clicking the background
|
29 | 67 | var searchBackground = document.getElementById('search-background')
|
30 | 68 | if (searchBackground) {
|
31 |
| - searchBackground.addEventListener('click', function (e) { |
32 |
| - e.stopPropagation() // trap event |
| 69 | + searchBackground.addEventListener('click', function (event) { |
| 70 | + event.stopPropagation() // trap event |
33 | 71 | closeSearchPopover()
|
34 | 72 | })
|
35 | 73 | }
|
36 | 74 |
|
37 |
| - // open/close with keyboard |
38 | 75 | document.addEventListener('keydown', function (event) {
|
| 76 | + // open search with keyboard |
39 | 77 | if (event.ctrlKey && event.key === 'k') {
|
40 | 78 | event.preventDefault() // prevent focussing the URL bar in the browser
|
41 | 79 | openSearchPopover()
|
42 | 80 | }
|
| 81 | + // close search with keyboard |
43 | 82 | if (event.key === 'Escape') {
|
44 | 83 | closeSearchPopover()
|
45 | 84 | }
|
| 85 | + // focus the search input when pressing / while search popover is open |
| 86 | + if (event.key === '/') { |
| 87 | + event.preventDefault() // prevent opening the browser search dialog |
| 88 | + focusSearchInput() |
| 89 | + } |
46 | 90 | })
|
47 | 91 | })()
|
0 commit comments