Feat: Live search accessibility, no-results message and View all styling

This commit is contained in:
elvira committed 2026-10-23 20:59:23 +03:00
1 parent d1d27a9ebd
commit 9217960de0
4 files changed
+118 -33

No files matched your search

+50 -1
View File
@@ -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
@@ -9,7 +9,11 @@
Live search: from 3 characters, the `nav-search` controller points the
<turbo-frame id="search-suggestions"> 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.
--}}
<div
id="search-overlay"
@@ -24,15 +28,19 @@ class="fixed w-full inset-x-0 top-0 bottom-auto m-0 border-0 bg-transparent p-0"
data-nav-search-target="form"
class="flex h-[var(--search-overlay-h)] items-center gap-6 bg-brand px-10"
>
<label for="search-overlay-input" class="sr-only">{{ __('storefront.shop.search_label') }}</label>
<label id="search-overlay-label" for="search-overlay-input" class="sr-only">{{ __('storefront.shop.search_label') }}</label>
<input
id="search-overlay-input"
type="search"
name="q"
required
autocomplete="off"
role="combobox"
aria-autocomplete="list"
aria-expanded="false"
aria-controls="search-suggestions-list"
data-nav-search-target="input"
data-action="input->nav-search#suggest"
data-action="input->nav-search#suggest keydown->nav-search#navigate"
placeholder="{{ __('storefront.search.placeholder') }}"
class="min-w-0 flex-1 appearance-none border-0 border-b border-black bg-transparent pb-2 placeholder:text-black/60 focus:outline-none [&::-webkit-search-cancel-button]:appearance-none"
>
@@ -47,5 +55,7 @@ class="shrink-0 text-black transition-opacity hover:opacity-70"
</button>
</form>
<p role="status" class="sr-only" data-nav-search-target="status"></p>
<turbo-frame id="search-suggestions" data-nav-search-target="results"></turbo-frame>
</div>
@@ -7,7 +7,7 @@
--}}
@if ($products->isEmpty())
<p class="text-neutral-500">{{ __('storefront.shop.no_products') }}</p>
<p class="text-neutral-500">{{ __('storefront.search.no_results', ['query' => $query]) }}</p>
@else
<div class="flex items-center justify-between gap-6 flex-wrap mb-7">
<x-shop.result-count :paginator="$products" />
+54 -28
View File
@@ -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)
--}}
<turbo-frame id="search-suggestions">
@if ($searched)
<div class="max-h-[calc(100dvh-var(--search-overlay-h))] overflow-y-auto bg-white border-b border-black">
@forelse ($products as $product)
<a
href="{{ $product['href'] }}"
data-turbo-frame="_top"
class="flex items-center gap-5 border-b border-neutral-300 px-10 py-4 transition-colors hover:bg-neutral-100 focus-visible:bg-neutral-100 focus-visible:outline-none"
>
<span class="flex aspect-[251/335] w-16 shrink-0 items-center justify-center overflow-hidden border border-black bg-white">
@if ($product['image'])
<img src="{{ $product['image'] }}" alt="" loading="lazy" class="block h-auto w-full">
@endif
</span>
<span class="min-w-0 flex-1">{{ \App\Support\SearchHighlight::name((string) $product['name'], $query) }}</span>
@if ($product['price'] !== null)
<span class="shrink-0"><x-ui.price :amount="$product['price']" /></span>
@endif
</a>
@empty
<p class="px-10 py-6 text-neutral-500">{{ __('storefront.shop.no_products') }}</p>
@endforelse
<p hidden data-nav-search-target="summary">{{ trans_choice('storefront.search.results_count', $total, ['count' => $total]) }}</p>
@if ($total > 0)
<a
href="{{ route('search', ['q' => $query]) }}"
data-turbo-frame="_top"
class="flex items-center justify-between px-10 py-5 font-medium uppercase tracking-wide transition-colors hover:bg-neutral-100 focus-visible:bg-neutral-100 focus-visible:outline-none"
<div class="max-h-[calc(100dvh-var(--search-overlay-h))] overflow-y-auto bg-white border-b border-black">
@if ($products->isNotEmpty())
<div
id="search-suggestions-list"
role="listbox"
aria-labelledby="search-overlay-label"
data-nav-search-target="listbox"
>
{{ trans_choice('storefront.search.view_all', $total, ['count' => $total]) }}
<x-ui.icon name="arrow-right" :size="20" />
</a>
@foreach ($products as $product)
<a
id="search-option-{{ $loop->index }}"
href="{{ $product['href'] }}"
role="option"
aria-selected="false"
tabindex="-1"
data-turbo-frame="_top"
data-nav-search-target="option"
class="flex items-center gap-5 border-b border-neutral-300 px-10 py-2 transition-colors hover:bg-neutral-100 aria-selected:bg-neutral-100"
>
<span class="flex aspect-[251/335] w-14 shrink-0 items-center justify-center overflow-hidden border border-black bg-white">
@if ($product['image'])
<img src="{{ $product['image'] }}" alt="" loading="lazy" class="block h-auto w-full">
@endif
</span>
<span class="min-w-0 flex-1">{{ \App\Support\SearchHighlight::name((string) $product['name'], $query) }}</span>
@if ($product['price'] !== null)
<span class="shrink-0"><x-ui.price :amount="$product['price']" /></span>
@endif
</a>
@endforeach
<a
id="search-option-all"
href="{{ route('search', ['q' => $query]) }}"
role="option"
aria-selected="false"
tabindex="-1"
data-turbo-frame="_top"
data-nav-search-target="option"
class="flex items-center gap-3 bg-black px-10 py-4 text-[19px] font-bold italic uppercase text-white transition-colors hover:bg-neutral-800 aria-selected:bg-neutral-800"
>
{{ trans_choice('storefront.search.view_all', $total, ['count' => $total]) }}
<x-ui.icon name="arrow-right" :size="20" />
</a>
</div>
@else
<p class="px-10 py-6 text-neutral-500">{{ __('storefront.search.no_results', ['query' => $query]) }}</p>
@endif
</div>
@endif