From 9217960de072aa57737be5828b4f022f43568734 Mon Sep 17 00:00:00 2001 From: elvira Date: Fri, 23 Oct 2026 20:59:23 +0300 Subject: [PATCH] Feat: Live search accessibility, no-results message and View all styling --- .../js/stimulus/nav-search-controller.js | 51 +++++++++++- .../views/components/search-overlay.blade.php | 16 +++- .../views/search/partials/results.blade.php | 2 +- resources/views/search/suggestions.blade.php | 82 ++++++++++++------- 4 files changed, 118 insertions(+), 33 deletions(-) diff --git a/resources/js/stimulus/nav-search-controller.js b/resources/js/stimulus/nav-search-controller.js index 5de2a86..bd508a2 100644 --- a/resources/js/stimulus/nav-search-controller.js +++ b/resources/js/stimulus/nav-search-controller.js @@ -10,11 +10,18 @@ import { Controller } from '@hotwired/stimulus' // 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. +// +// ARIA combobox: the input keeps focus while ArrowUp/ArrowDown move +// aria-selected over the options (aria-activedescendant points at the current +// one) and Enter follows it; with nothing highlighted, Enter submits the form +// as usual. Each load's hidden summary is copied into the persistent +// role="status" region so screen readers announce the result count. export default class extends Controller { - static targets = ['input', 'form', 'results'] + static targets = ['input', 'form', 'results', 'status', 'summary', 'listbox', 'option'] static values = { minChars: { type: Number, default: 3 } } #timer = null + #active = -1 connect() { this.element.addEventListener('toggle', this.#onToggle) @@ -36,6 +43,7 @@ export default class extends Controller { if (query.length < this.minCharsValue) { this.resultsTarget.removeAttribute('src') this.resultsTarget.innerHTML = '' + this.statusTarget.textContent = '' return } @@ -44,6 +52,47 @@ export default class extends Controller { this.resultsTarget.src = url.toString() } + navigate(event) { + const options = this.optionTargets + if (options.length === 0) return + + if (event.key === 'ArrowDown') { + event.preventDefault() + this.#highlight((this.#active + 1) % options.length) + } else if (event.key === 'ArrowUp') { + event.preventDefault() + this.#highlight(this.#active <= 0 ? options.length - 1 : this.#active - 1) + } else if (event.key === 'Enter' && this.#active >= 0) { + event.preventDefault() + options[this.#active].click() + } + } + + summaryTargetConnected(summary) { + this.statusTarget.textContent = summary.textContent + } + + listboxTargetConnected() { + this.#active = -1 + this.inputTarget.setAttribute('aria-expanded', 'true') + this.inputTarget.removeAttribute('aria-activedescendant') + } + + listboxTargetDisconnected() { + this.#active = -1 + this.inputTarget.setAttribute('aria-expanded', 'false') + this.inputTarget.removeAttribute('aria-activedescendant') + } + + #highlight(index) { + this.#active = index + this.optionTargets.forEach((option, i) => option.setAttribute('aria-selected', String(i === index))) + + const option = this.optionTargets[index] + this.inputTarget.setAttribute('aria-activedescendant', option.id) + option.scrollIntoView({ block: 'nearest' }) + } + #onToggle = (event) => { if (event.newState !== 'open') return diff --git a/resources/views/components/search-overlay.blade.php b/resources/views/components/search-overlay.blade.php index 344fcd2..85a1cf8 100644 --- a/resources/views/components/search-overlay.blade.php +++ b/resources/views/components/search-overlay.blade.php @@ -9,7 +9,11 @@ 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. + (search/suggestions.blade.php). Enter still goes to the full results page, + unless a suggestion is highlighted with the arrow keys. + + ARIA: the input is a combobox for the suggestions listbox; the role="status" + region below announces the result count after each load. --}}
- + @@ -47,5 +55,7 @@ class="shrink-0 text-black transition-opacity hover:opacity-70" +

+
diff --git a/resources/views/search/partials/results.blade.php b/resources/views/search/partials/results.blade.php index 0d6443a..6669877 100644 --- a/resources/views/search/partials/results.blade.php +++ b/resources/views/search/partials/results.blade.php @@ -7,7 +7,7 @@ --}} @if ($products->isEmpty()) -

{{ __('storefront.shop.no_products') }}

+

{{ __('storefront.search.no_results', ['query' => $query]) }}

@else
diff --git a/resources/views/search/suggestions.blade.php b/resources/views/search/suggestions.blade.php index ecfdc97..9e72bab 100644 --- a/resources/views/search/suggestions.blade.php +++ b/resources/views/search/suggestions.blade.php @@ -4,41 +4,67 @@ Up to six products (thumbnail, name with the matched words underlined, price), then a link to the full results page. Empty below 3 characters. + ARIA: the overlay's input is a combobox controlling the listbox below. The + options stay out of the tab order (focus remains in the input); the + `nav-search` controller moves aria-selected / aria-activedescendant with + the arrow keys. The hidden summary is copied into the overlay's persistent + role="status" region, since a live region swapped in with the frame isn't + reliably announced. + Vars: $query (string), $products (Collection of ProductCard arrays), $total (int), $searched (bool) --}} @if ($searched) - @endif