diff options
| author | Mitja Felicijan <m@mitjafelicijan.com> | 2023-06-29 21:33:31 +0200 |
|---|---|---|
| committer | Mitja Felicijan <m@mitjafelicijan.com> | 2023-06-29 21:33:31 +0200 |
| commit | b43c8546760cc05af867b2c4c067cb29c7c911fe (patch) | |
| tree | 2dc8869daf61b25c0e7087a57d476c5dbfcc0697 /themes/simple/layouts/partials/search.html | |
| parent | df85a62383d529e3b75a2eab83a34fb6199c0b97 (diff) | |
| download | mitjafelicijan.com-b43c8546760cc05af867b2c4c067cb29c7c911fe.tar.gz | |
Added search with navigational keyboard arrow
Diffstat (limited to 'themes/simple/layouts/partials/search.html')
| -rw-r--r-- | themes/simple/layouts/partials/search.html | 61 |
1 files changed, 60 insertions, 1 deletions
diff --git a/themes/simple/layouts/partials/search.html b/themes/simple/layouts/partials/search.html index 3e31753..5a76384 100644 --- a/themes/simple/layouts/partials/search.html +++ b/themes/simple/layouts/partials/search.html | |||
| @@ -50,6 +50,7 @@ | |||
| 50 | } | 50 | } |
| 51 | 51 | ||
| 52 | // On keyboard shortcut shows search modal. | 52 | // On keyboard shortcut shows search modal. |
| 53 | let currentSearchResultsSectionIndex = -1; | ||
| 53 | document.addEventListener('keydown', function(event) { | 54 | document.addEventListener('keydown', function(event) { |
| 54 | // Handles macOS CMD+k. | 55 | // Handles macOS CMD+k. |
| 55 | if ((event.ctrlKey || event.metaKey) && event.key === 'k') { | 56 | if ((event.ctrlKey || event.metaKey) && event.key === 'k') { |
| @@ -62,6 +63,62 @@ | |||
| 62 | event.preventDefault(); | 63 | event.preventDefault(); |
| 63 | showSearchModal(); | 64 | showSearchModal(); |
| 64 | } | 65 | } |
| 66 | |||
| 67 | // If ESC is pressed when the input is empty close the search modal. | ||
| 68 | if (event.key === 'Escape' || event.key === 'Esc') { | ||
| 69 | if (searchInput.value.length == 0) { | ||
| 70 | if (!searchModal.classList.contains('hidden')) { | ||
| 71 | cachedSearchTerm = null; | ||
| 72 | searchModal.classList.add('hidden'); | ||
| 73 | searchResults.innerText = ''; | ||
| 74 | if (!searchResults.classList.contains('hidden')) { | ||
| 75 | searchResults.classList.add('hidden'); | ||
| 76 | } | ||
| 77 | } | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | // Arrow UP/DOWN movement through search results. | ||
| 82 | const list = document.querySelector('.ulist'); | ||
| 83 | const itemHoverClass = 'bg-gray-100'; | ||
| 84 | if (event.key === 'ArrowUp') { | ||
| 85 | if (!searchResults.classList.contains('hidden')) { | ||
| 86 | event.preventDefault(); | ||
| 87 | |||
| 88 | const listItems = searchResults.querySelectorAll('a'); | ||
| 89 | listItems.forEach(el => el.classList.remove(itemHoverClass)); | ||
| 90 | currentSearchResultsSectionIndex--; | ||
| 91 | |||
| 92 | if (!listItems[currentSearchResultsSectionIndex]) { | ||
| 93 | currentSearchResultsSectionIndex = -1; | ||
| 94 | searchInput.focus(); | ||
| 95 | } | ||
| 96 | |||
| 97 | if (currentSearchResultsSectionIndex != -1) { | ||
| 98 | listItems[currentSearchResultsSectionIndex].classList.add(itemHoverClass); | ||
| 99 | listItems[currentSearchResultsSectionIndex].focus(); | ||
| 100 | } | ||
| 101 | } | ||
| 102 | } | ||
| 103 | if (event.key === 'ArrowDown') { | ||
| 104 | if (!searchResults.classList.contains('hidden')) { | ||
| 105 | event.preventDefault(); | ||
| 106 | |||
| 107 | const listItems = searchResults.querySelectorAll('a'); | ||
| 108 | listItems.forEach(el => el.classList.remove(itemHoverClass)); | ||
| 109 | currentSearchResultsSectionIndex++; | ||
| 110 | |||
| 111 | if (!listItems[currentSearchResultsSectionIndex]) { | ||
| 112 | currentSearchResultsSectionIndex = 0; | ||
| 113 | } | ||
| 114 | |||
| 115 | if (currentSearchResultsSectionIndex != -1) { | ||
| 116 | listItems[currentSearchResultsSectionIndex].classList.add(itemHoverClass); | ||
| 117 | listItems[currentSearchResultsSectionIndex].focus(); | ||
| 118 | } | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 65 | }); | 122 | }); |
| 66 | 123 | ||
| 67 | // Debounce magic. | 124 | // Debounce magic. |
| @@ -82,7 +139,9 @@ | |||
| 82 | const results = searchIndex.search(query); | 139 | const results = searchIndex.search(query); |
| 83 | 140 | ||
| 84 | if (results.length == 0) { | 141 | if (results.length == 0) { |
| 85 | searchResults.classList.add('hidden'); | 142 | if (!searchResults.classList.contains('hidden')) { |
| 143 | searchResults.classList.add('hidden'); | ||
| 144 | } | ||
| 86 | } else { | 145 | } else { |
| 87 | searchResults.innerText = ''; | 146 | searchResults.innerText = ''; |
| 88 | searchResults.classList.remove('hidden'); | 147 | searchResults.classList.remove('hidden'); |
