materiotheme: suggestions in the search field, ARIA combobox (F7)

Tags and thesaurus terms suggested on the first two words, from the
existing autocomplete route. Arrow keys, Enter, Escape and click; a
suggestion searches that term alone. Number of suggestions announced to
screen readers (fr/en texts in the component). Same translucent look as
the former jQuery UI list.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ShDspHaZGwHoRZCsPBJzsM
This commit is contained in:
2026-11-23 15:57:19 +01:00
co-authored by Claude Opus 5.5
parent 54e7e28046
commit f7eb6518e7
10 changed files with 177 additions and 20 deletions
File diff suppressed because one or more lines are too long
Binary file not shown.
File diff suppressed because one or more lines are too long
Binary file not shown.
File diff suppressed because one or more lines are too long
Binary file not shown.
File diff suppressed because one or more lines are too long
Binary file not shown.
@@ -668,21 +668,29 @@ header[role="banner"]{
} }
ul.ui-menu.ui-widget-content.ui-autocomplete { // Suggestions of the search field (SearchForm.vue, F7), same look as the
// former jQuery UI autocomplete.
#materio-sapi-search-form ul.search-suggestions {
position: absolute;
z-index: 100;
top: 100%;
left: 0;
right: 0;
margin: 0.2em 0 0;
list-style: none;
max-height: 60vh;
overflow-y: auto;
background-color: rgba(0, 0, 0, 0.6); background-color: rgba(0, 0, 0, 0.6);
border-radius: 5px; border-radius: 5px;
border: none; border: none;
padding: 0.3em 0.2em; padding: 0.3em 0.2em;
a.ui-menu-item-wrapper{ li{
color: #fff; color: #fff;
padding:0.1em 0.1em; padding: 0.1em 0.3em;
display: block; display: block;
&.ui-state-active{ cursor: pointer;
background: none; &.active{
border: none;
color:#fff;
background-color: rgba(0, 0, 0, 0.9); background-color: rgba(0, 0, 0, 0.9);
} }
} }
} }
@@ -1,6 +1,8 @@
<template> <template>
<!-- One field for a phrase in natural language, one button (F1). <!-- One field for a phrase in natural language, one button (F1), with
Same ids and classes as the former Drupal form, so main.scss applies. --> suggestions of tags and thesaurus terms on the first two words (F7).
Same ids and classes as the former Drupal form, so main.scss applies.
Suggestions follow the ARIA combobox pattern (list autocomplete). -->
<form <form
id="materio-sapi-search-form" id="materio-sapi-search-form"
class="materio-sapi-search-form" class="materio-sapi-search-form"
@@ -15,6 +17,11 @@
class="form-text" class="form-text"
type="search" type="search"
name="search" name="search"
role="combobox"
aria-autocomplete="list"
:aria-expanded="open ? 'true' : 'false'"
:aria-controls="listId"
:aria-activedescendant="active >= 0 ? optionId(active) : null"
:maxlength="maxLength" :maxlength="maxLength"
:placeholder="$t('default.Search')" :placeholder="$t('default.Search')"
:aria-label="$t('default.Search')" :aria-label="$t('default.Search')"
@@ -22,7 +29,30 @@
autocomplete="off" autocomplete="off"
autocapitalize="off" autocapitalize="off"
spellcheck="false" spellcheck="false"
@input="onInput"
@keydown="onKeydown"
@blur="close"
> >
<ul
v-show="open"
:id="listId"
class="search-suggestions"
role="listbox"
:aria-label="text.list"
>
<li
v-for="(item, i) in suggestions"
:id="optionId(i)"
:key="item.value"
role="option"
:aria-selected="i === active ? 'true' : 'false'"
:class="{ active: i === active }"
@mousedown.prevent="choose(item)"
@mousemove="active = i"
>{{ item.label }}</li>
</ul>
<!-- Number of suggestions, read by screen readers. -->
<div class="visually-hidden" aria-live="polite">{{ announce }}</div>
</div> </div>
<input <input
type="submit" type="submit"
@@ -36,6 +66,25 @@
<script> <script>
import router from 'vuejs/route' import router from 'vuejs/route'
import { mapState } from 'vuex' import { mapState } from 'vuex'
import MA from 'vuejs/api/ma-axios'
import qs from 'querystring-es3'
// Texts of the suggestion list, for screen readers. Kept here: the Vue
// translation files are generated outside the repository.
const TEXTS = {
fr: {
list: 'Suggestions',
none: '',
one: '1 suggestion, flèches haut et bas pour choisir',
many: n => `${n} suggestions, flèches haut et bas pour choisir`
},
en: {
list: 'Suggestions',
none: '',
one: '1 suggestion, use up and down arrows to choose',
many: n => `${n} suggestions, use up and down arrows to choose`
}
}
export default { export default {
router, router,
@@ -43,26 +92,123 @@ export default {
return { return {
typed: '', typed: '',
// Same limit as the backend (Base::MAX_PHRASE_LENGTH, H1). // Same limit as the backend (Base::MAX_PHRASE_LENGTH, H1).
maxLength: 150 maxLength: 150,
suggestions: [],
open: false,
active: -1,
announce: '',
listId: 'search-suggestions',
timer: null,
// Answers of older requests are dropped.
requestId: 0
} }
}, },
computed: { computed: {
...mapState({ ...mapState({
keys: state => state.Search.keys keys: state => state.Search.keys
}) }),
text () {
const lang = (window.drupalDecoupled && drupalDecoupled.lang_code) || 'en'
return TEXTS[lang] || TEXTS.en
}
}, },
methods: { methods: {
phraseFromKeys (keys) { phraseFromKeys (keys) {
return keys && keys.length ? keys.join(', ') : '' return keys && keys.length ? keys.join(', ') : ''
}, },
optionId (i) {
return `${this.listId}-${i}`
},
// Suggestions on the first two words only: none after the second
// space, so that they do not get in the way of a phrase (F7).
suggestible (text) {
return text.trim().length >= 2 && (text.match(/ /g) || []).length < 2
},
onInput () {
clearTimeout(this.timer)
if (!this.suggestible(this.typed)) {
this.requestId++
this.close()
return
}
this.timer = setTimeout(this.fetchSuggestions, 200)
},
fetchSuggestions () {
const id = ++this.requestId
const q = this.typed.trim()
MA.get('/materio_sapi/form/autocomplete?' + qs.stringify({ q }))
.then(({ data }) => {
if (id !== this.requestId || !this.suggestible(this.typed)) {
return
}
this.suggestions = Array.isArray(data) ? data : []
this.active = -1
this.open = this.suggestions.length > 0
const n = this.suggestions.length
this.announce = n === 0 ? this.text.none : (n === 1 ? this.text.one : this.text.many(n))
})
.catch(error => {
console.warn('Issue with search suggestions', error)
})
},
onKeydown (e) {
if (!this.open) {
return
}
const n = this.suggestions.length
switch (e.key) {
case 'ArrowDown':
e.preventDefault()
this.active = (this.active + 1) % n
break
case 'ArrowUp':
e.preventDefault()
this.active = this.active <= 0 ? n - 1 : this.active - 1
break
case 'Enter':
// With a suggestion selected, Enter chooses it; otherwise the form
// is submitted with the text typed.
if (this.active >= 0) {
e.preventDefault()
this.choose(this.suggestions[this.active])
}
break
case 'Escape':
e.preventDefault()
this.close()
break
case 'Tab':
this.close()
break
}
},
close () {
this.open = false
this.active = -1
this.suggestions = []
this.announce = ''
},
// A suggestion searches that term alone, nothing added (F7).
choose (item) {
clearTimeout(this.timer)
this.requestId++
this.close()
this.typed = item.label
this.go({ keys: item.label, terms: JSON.stringify([{ value: item.value, label: item.label }]) })
},
submit () { submit () {
clearTimeout(this.timer)
this.requestId++
this.close()
const phrase = this.typed.replace(/\s+/g, ' ').trim() const phrase = this.typed.replace(/\s+/g, ' ').trim()
// Only the phrase, no terms.
this.go(phrase ? { keys: phrase } : {})
},
go (query) {
// Hide the keyboard on mobile devices. // Hide the keyboard on mobile devices.
this.$refs.input.blur() this.$refs.input.blur()
// Base.vue reacts to the route change and runs the search. // Base.vue reacts to the route change and runs the search.
// Only the phrase: no more terms (F7) nor filters. this.$router.push({ name: 'base', query }).catch(() => {})
this.$router.push({ name: 'base', query: phrase ? { keys: phrase } : {} })
.catch(() => {})
} }
}, },
watch: { watch: {
@@ -74,6 +220,9 @@ export default {
created () { created () {
// Direct page load: the store already holds the keys of the URL. // Direct page load: the store already holds the keys of the URL.
this.typed = this.phraseFromKeys(this.keys) this.typed = this.phraseFromKeys(this.keys)
},
beforeDestroy () {
clearTimeout(this.timer)
} }
} }
</script> </script>