Feat: Live search accessibility, no-results message and View all styling
This commit is contained in:
1 parent
d1d27a9ebd
commit
9217960de0
4 files changed
+118
-33
No files matched your search
@@ -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
|
||||
|
||||
|
||||
Reference in new issue
Block a user