From 4f56edc4fc56d076cce31600666997ccd158278b Mon Sep 17 00:00:00 2001 From: Konstantinos Arvanitakis Date: Mon, 19 Oct 2026 17:32:35 +0300 Subject: [PATCH] Feat: Adding Search prefixes to homepage search --- app/Http/Controllers/SearchController.php | 42 ++++++++++- app/Support/SearchHighlight.php | 74 +++++++++++++++++++ resources/css/app.css | 19 +++++ .../js/stimulus/nav-search-controller.js | 31 +++++++- .../views/components/search-overlay.blade.php | 16 +++- resources/views/search/suggestions.blade.php | 45 +++++++++++ 6 files changed, 222 insertions(+), 5 deletions(-) create mode 100644 app/Support/SearchHighlight.php create mode 100644 resources/views/search/suggestions.blade.php diff --git a/app/Http/Controllers/SearchController.php b/app/Http/Controllers/SearchController.php index 7288c10..417a7fe 100644 --- a/app/Http/Controllers/SearchController.php +++ b/app/Http/Controllers/SearchController.php @@ -2,17 +2,34 @@ namespace App\Http\Controllers; +use App\Catalog\ProductCard; use App\Catalog\ProductListing; use App\Catalog\ProductListingPage; +use Modules\Core\Catalog\Services\ProductSearchService; class SearchController extends Controller { - public function __construct(private readonly ProductListingPage $listingPage) {} + /** Live search answers from this many characters on. */ + public const LIVE_MIN_CHARS = 3; + + /** How many products the live search dropdown lists. */ + private const LIVE_LIMIT = 6; + + public function __construct( + private readonly ProductListingPage $listingPage, + private readonly ProductSearchService $search, + ) {} public function show(string $locale) { $query = trim((string) request()->query('q', '')); + // The search overlay's live results: the same endpoint, asked for by + // its as the shopper types. + if (request()->header('Turbo-Frame') === 'search-suggestions') { + return $this->suggestions($query); + } + if ($query === '') { return redirect()->route('products'); } @@ -28,4 +45,27 @@ public function show(string $locale) return view('search.index', [...$data, 'query' => $query]); } + + /** + * The first few matches and how many there are in all — the dropdown + * under the search overlay. Empty below LIVE_MIN_CHARS characters. + */ + private function suggestions(string $query) + { + $products = collect(); + $total = 0; + + if (mb_strlen($query) >= self::LIVE_MIN_CHARS) { + $result = $this->search->search($query, perPage: self::LIVE_LIMIT); + $products = collect($result->products->items())->map(ProductCard::fromIndexed(...)); + $total = $result->products->total(); + } + + return view('search.suggestions', [ + 'query' => $query, + 'products' => $products, + 'total' => $total, + 'searched' => mb_strlen($query) >= self::LIVE_MIN_CHARS, + ]); + } } diff --git a/app/Support/SearchHighlight.php b/app/Support/SearchHighlight.php new file mode 100644 index 0000000..7598dd1 --- /dev/null +++ b/app/Support/SearchHighlight.php @@ -0,0 +1,74 @@ +contains(fn (string $term) => str_starts_with($foldedWord, $term)); + + $html .= $matched ? ''.e($word).'' : e($word); + $i = $end; + } + + return new HtmlString($html); + } + + /** + * Lower case, without accents; Greek final sigma as σ. + */ + private static function fold(string $text): string + { + if (class_exists(Normalizer::class)) { + $text = preg_replace('/\p{Mn}+/u', '', Normalizer::normalize($text, Normalizer::FORM_D) ?: $text); + } + + return strtr(mb_strtolower($text), [ + 'ά' => 'α', 'έ' => 'ε', 'ή' => 'η', 'ί' => 'ι', 'ό' => 'ο', 'ύ' => 'υ', 'ώ' => 'ω', + 'ϊ' => 'ι', 'ϋ' => 'υ', 'ΐ' => 'ι', 'ΰ' => 'υ', 'ς' => 'σ', + ]); + } + + private static function isWordChar(string $char): bool + { + return (bool) preg_match('/[\p{L}\p{N}]/u', $char); + } +} diff --git a/resources/css/app.css b/resources/css/app.css index 0cf24ad..30f59e8 100644 --- a/resources/css/app.css +++ b/resources/css/app.css @@ -241,6 +241,25 @@ @layer components { } } + /* The page behind is dimmed while searching, fading with the overlay. */ + #search-overlay::backdrop { + background-color: rgb(0 0 0 / 0); + transition: + background-color 0.2s ease, + display 0.2s allow-discrete, + overlay 0.2s allow-discrete; + } + + #search-overlay:popover-open::backdrop { + background-color: rgb(0 0 0 / 0.3); + } + + @starting-style { + #search-overlay:popover-open::backdrop { + background-color: rgb(0 0 0 / 0); + } + } + /* ── Shared underline-slide animation ────────────────────────── */ .underline-slide { background-image: linear-gradient(currentColor, currentColor); diff --git a/resources/js/stimulus/nav-search-controller.js b/resources/js/stimulus/nav-search-controller.js index 7f8afa7..5de2a86 100644 --- a/resources/js/stimulus/nav-search-controller.js +++ b/resources/js/stimulus/nav-search-controller.js @@ -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) => { diff --git a/resources/views/components/search-overlay.blade.php b/resources/views/components/search-overlay.blade.php index ccd6ae8..344fcd2 100644 --- a/resources/views/components/search-overlay.blade.php +++ b/resources/views/components/search-overlay.blade.php @@ -4,18 +4,25 @@ close come for free). The header's search button opens it via popovertarget. The `nav-search` controller measures the header on open (→ --search-overlay-h, since the header isn't a fixed height below `lg`) and focuses the field. - Fade transition lives in app.css (#search-overlay). + Fade transition and the dimmed page behind live in app.css (#search-overlay). + + Live search: from 3 characters, the `nav-search` controller points the + under the bar at the same /search + endpoint, which answers that frame with the first few matches + (search/suggestions.blade.php). Enter still goes to the full results page. --}}
@@ -38,4 +46,6 @@ class="shrink-0 text-black transition-opacity hover:opacity-70" + +
diff --git a/resources/views/search/suggestions.blade.php b/resources/views/search/suggestions.blade.php new file mode 100644 index 0000000..ecfdc97 --- /dev/null +++ b/resources/views/search/suggestions.blade.php @@ -0,0 +1,45 @@ +{{-- + Live search results under the search overlay — the answer to its + (SearchController::suggestions()). + Up to six products (thumbnail, name with the matched words underlined, + price), then a link to the full results page. Empty below 3 characters. + + Vars: $query (string), $products (Collection of ProductCard arrays), + $total (int), $searched (bool) +--}} + + @if ($searched) + + @endif +