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 | |
| parent | df85a62383d529e3b75a2eab83a34fb6199c0b97 (diff) | |
| download | mitjafelicijan.com-b43c8546760cc05af867b2c4c067cb29c7c911fe.tar.gz | |
Added search with navigational keyboard arrow
Diffstat (limited to 'themes/simple/layouts')
| -rw-r--r-- | themes/simple/layouts/partials/navigation.html | 12 | ||||
| -rw-r--r-- | themes/simple/layouts/partials/search.html | 61 |
2 files changed, 67 insertions, 6 deletions
diff --git a/themes/simple/layouts/partials/navigation.html b/themes/simple/layouts/partials/navigation.html index b65fe35..f6e5a66 100644 --- a/themes/simple/layouts/partials/navigation.html +++ b/themes/simple/layouts/partials/navigation.html | |||
| @@ -8,11 +8,13 @@ | |||
| 8 | <meta itemprop="name" content="Main Menu"> | 8 | <meta itemprop="name" content="Main Menu"> |
| 9 | 9 | ||
| 10 | <!-- Search button --> | 10 | <!-- Search button --> |
| 11 | <span class="search-button flex gap-1 items-center text-gray-500 bg-gray-100 hover:bg-gray-200 rounded px-2 py-1 text-xs cursor-pointer mr-2 hidden" onclick="showSearchModal()"> | 11 | <span class="hidden lg:block"> |
| 12 | <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="3" stroke="currentColor" class="w-4 h-4"> | 12 | <span class="search-button flex gap-1 items-center text-gray-500 bg-gray-100 hover:bg-gray-200 rounded px-2 py-1 text-xs cursor-pointer mr-2 hidden" onclick="showSearchModal()"> |
| 13 | <path stroke-linecap="round" stroke-linejoin="round" d="M21 21l-5.197-5.197m0 0A7.5 7.5 0 105.196 5.196a7.5 7.5 0 0010.607 10.607z" /> | 13 | <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="3" stroke="currentColor" class="w-4 h-4"> |
| 14 | </svg> | 14 | <path stroke-linecap="round" stroke-linejoin="round" d="M21 21l-5.197-5.197m0 0A7.5 7.5 0 105.196 5.196a7.5 7.5 0 0010.607 10.607z" /> |
| 15 | <span class="search-button-text uppercase font-bold"></span> | 15 | </svg> |
| 16 | <span class="search-button-text uppercase font-bold"></span> | ||
| 17 | </span> | ||
| 16 | </span> | 18 | </span> |
| 17 | 19 | ||
| 18 | <a href="/notes.html" class="font-medium px-2 hover:bg-yellow-100">Notes</a> | 20 | <a href="/notes.html" class="font-medium px-2 hover:bg-yellow-100">Notes</a> |
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'); |
