From c93c901ad309838c28fe01543e0458b3c9231c0d Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Sat, 8 Nov 2025 13:55:55 +1300 Subject: [PATCH] Load first suggestion when pressing enter --- static/search-suggestions.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/static/search-suggestions.js b/static/search-suggestions.js index ddd2927..ae8be36 100644 --- a/static/search-suggestions.js +++ b/static/search-suggestions.js @@ -1,4 +1,4 @@ -import {h, htm, render, signal, computed, effect} from "preact" +import {h, htm, render, signal, computed, effect, useSignalEffect} from "preact" const html = htm.bind(h) const classNames = classArr => classArr.filter(el => el).join(" ") @@ -13,6 +13,7 @@ const query = signal("") const focus = signal(false) const st = signal("ready") const suggestions = signal([]) +const enterWasLastKey = signal(false) // processing functions @@ -49,6 +50,12 @@ function acceptSuggestion(hit) { // suggestion list view function Suggestion(hit) { + useSignalEffect(() => { + if (enterWasLastKey.value && st.value === "ready") { + enterWasLastKey.value.preventDefault() + acceptSuggestion(hit) + } + }) return html`
  • ` } @@ -79,9 +86,15 @@ window.addEventListener("pageshow", () => { st.value = "ready" // unlock results from changing after returning to page }) +effect(() => { + if (enterWasLastKey.value && st.value === "loading") { + enterWasLastKey.value.preventDefault() // wait for results before going + } +}) + function SuggestionInput() { return html` - query.value = e.target.value} value=${query.value} class=${classNames(["bw-ss__input", `bw-ss__input--${st.value}`])} />` + query.value = e.target.value} onKeyDown=${e => enterWasLastKey.value = e.key === "Enter" && e} value=${query.value} class=${classNames(["bw-ss__input", `bw-ss__input--${st.value}`])} />` } render(html`<${SuggestionInput} />`, eInput)