Feat: Adding Search prefixes to homepage search

This commit is contained in:
arvanitakis committed 2026-10-19 17:32:35 +03:00
1 parent 623851bd70
commit 4f56edc4fc
6 files changed
+222 -5

No files matched your search

+30 -1
View File
@@ -4,8 +4,17 @@ import { Controller } from '@hotwired/stimulus'
// header has no fixed height below `lg`, so on open we copy its current height
// onto --search-overlay-h and move focus into the field. Escape / click-away
// close come from the Popover API; the fade is CSS (#search-overlay).
//
// Live search: as the shopper types (from minChars characters, after a short
// pause), the results frame under the bar loads the same /search endpoint the
// form submits to; SearchController answers that frame with the first few
// matches. Turbo cancels a frame's previous request when its src changes, so
// only the latest query's results land.
export default class extends Controller {
static targets = ['input']
static targets = ['input', 'form', 'results']
static values = { minChars: { type: Number, default: 3 } }
#timer = null
connect() {
this.element.addEventListener('toggle', this.#onToggle)
@@ -13,6 +22,26 @@ export default class extends Controller {
disconnect() {
this.element.removeEventListener('toggle', this.#onToggle)
clearTimeout(this.#timer)
}
suggest() {
clearTimeout(this.#timer)
this.#timer = setTimeout(() => this.#load(), 250)
}
#load() {
const query = this.inputTarget.value.trim()
if (query.length < this.minCharsValue) {
this.resultsTarget.removeAttribute('src')
this.resultsTarget.innerHTML = ''
return
}
const url = new URL(this.formTarget.action, window.location.href)
url.searchParams.set('q', query)
this.resultsTarget.src = url.toString()
}
#onToggle = (event) => {