-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(search): add navigatable search results
- Loading branch information
Showing
8 changed files
with
106 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
package pages | ||
|
||
import "dankmuzikk/components/layouts" | ||
import "dankmuzikk/components/ui" | ||
import "dankmuzikk/components/ui/search" | ||
|
||
templ Index() { | ||
@layouts.Default(main()) | ||
} | ||
|
||
templ main() { | ||
<main class="w-full h-screen"> | ||
@ui.SearchResultSugesttions() | ||
@search.Search() | ||
</main> | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package search | ||
|
||
templ Search() { | ||
<div class=""> | ||
<form | ||
class="form-control" | ||
hx-get="/api/search-suggession" | ||
hx-swap="innerHTML" | ||
hx-target="#search-results" | ||
hx-trigger="keyup" | ||
> | ||
<input | ||
id="search-input" | ||
class="input input-bordered" | ||
type="search" | ||
name="query" | ||
autofocus | ||
autocomplete="off" | ||
/> | ||
</form> | ||
<div id="search-results"></div> | ||
</div> | ||
// | ||
<script lang="javascript"> | ||
const searchInputEl = document.getElementById("search-input"); | ||
let focusedSuggestionIndex = 0; | ||
|
||
searchInputEl.addEventListener("keydown", (e) => { | ||
if (e.key !== "ArrowDown") { | ||
return; | ||
} | ||
moveToSuggestions() | ||
}); | ||
|
||
function moveToSuggestions() { | ||
let searchSuggestionsEl = document.getElementById("search-suggestion-"+focusedSuggestionIndex.toString()); | ||
// sometimes it needs a second to initialize. | ||
if (!searchSuggestionsEl) { | ||
searchSuggestionsEl = document.getElementById("search-suggestion-"+focusedSuggestionIndex.toString()); | ||
} | ||
if (!searchSuggestionsEl) { | ||
return; | ||
} | ||
const moveToNextSuggestion = (e) => { | ||
const numSuggestions = (document.getElementById("search-suggestions").children ?? []).length | ||
if (e.key === "ArrowDown") { | ||
focusedSuggestionIndex = (focusedSuggestionIndex+1) % numSuggestions; | ||
moveToSuggestions() | ||
searchSuggestionsEl.removeEventListener("keydown", moveToNextSuggestion); | ||
} | ||
if (e.key === "ArrowUp") { | ||
focusedSuggestionIndex--; | ||
if (focusedSuggestionIndex < 0) { | ||
focusedSuggestionIndex = 0; | ||
searchInputEl.focus(); | ||
return; | ||
} | ||
moveToSuggestions() | ||
searchSuggestionsEl.removeEventListener("keydown", moveToNextSuggestion); | ||
} | ||
} | ||
searchSuggestionsEl.focus(); | ||
searchSuggestionsEl.addEventListener("keydown", moveToNextSuggestion); | ||
} | ||
</script> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package search | ||
|
||
templ SearchSuggestion(suggestion, originalQuery string) { | ||
<div> | ||
if len(suggestion) <= len(originalQuery) { | ||
<span><b>{ suggestion }</b></span> | ||
} else { | ||
<span><b>{ originalQuery }</b>{ suggestion[len(originalQuery):] }</span> | ||
} | ||
</div> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package search | ||
|
||
import "fmt" | ||
|
||
templ SearchSuggestions(suggestions []string, originalQuery string) { | ||
<ul id="search-suggestions" class="grid grid-cols-1 w-full"> | ||
for i, suggestion := range suggestions { | ||
<li class="focus-within:bg-base-300"> | ||
<a href="https://google.com" class="w-full" id={ fmt.Sprintf("search-suggestion-%d", i) }> | ||
if len(suggestion) <= len(originalQuery) { | ||
<span><b>{ suggestion }</b></span> | ||
} else { | ||
<span><b>{ originalQuery }</b>{ suggestion[len(originalQuery):] }</span> | ||
} | ||
</a> | ||
</li> | ||
} | ||
</ul> | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters