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:
+1
-1
File diff suppressed because one or more lines are too long
Binary file not shown.
+1
-1
File diff suppressed because one or more lines are too long
Binary file not shown.
+2
-2
File diff suppressed because one or more lines are too long
Binary file not shown.
+1
-1
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);
|
||||
border-radius: 5px;
|
||||
border: none;
|
||||
padding: 0.3em 0.2em;
|
||||
a.ui-menu-item-wrapper{
|
||||
li{
|
||||
color: #fff;
|
||||
padding:0.1em 0.1em;
|
||||
padding: 0.1em 0.3em;
|
||||
display: block;
|
||||
&.ui-state-active{
|
||||
background: none;
|
||||
border: none;
|
||||
color:#fff;
|
||||
cursor: pointer;
|
||||
&.active{
|
||||
background-color: rgba(0, 0, 0, 0.9);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
<template>
|
||||
<!-- One field for a phrase in natural language, one button (F1).
|
||||
Same ids and classes as the former Drupal form, so main.scss applies. -->
|
||||
<!-- One field for a phrase in natural language, one button (F1), with
|
||||
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
|
||||
id="materio-sapi-search-form"
|
||||
class="materio-sapi-search-form"
|
||||
@@ -15,6 +17,11 @@
|
||||
class="form-text"
|
||||
type="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"
|
||||
:placeholder="$t('default.Search')"
|
||||
:aria-label="$t('default.Search')"
|
||||
@@ -22,7 +29,30 @@
|
||||
autocomplete="off"
|
||||
autocapitalize="off"
|
||||
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>
|
||||
<input
|
||||
type="submit"
|
||||
@@ -36,6 +66,25 @@
|
||||
<script>
|
||||
import router from 'vuejs/route'
|
||||
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 {
|
||||
router,
|
||||
@@ -43,26 +92,123 @@ export default {
|
||||
return {
|
||||
typed: '',
|
||||
// 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: {
|
||||
...mapState({
|
||||
keys: state => state.Search.keys
|
||||
})
|
||||
}),
|
||||
text () {
|
||||
const lang = (window.drupalDecoupled && drupalDecoupled.lang_code) || 'en'
|
||||
return TEXTS[lang] || TEXTS.en
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
phraseFromKeys (keys) {
|
||||
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 () {
|
||||
clearTimeout(this.timer)
|
||||
this.requestId++
|
||||
this.close()
|
||||
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.
|
||||
this.$refs.input.blur()
|
||||
// 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: phrase ? { keys: phrase } : {} })
|
||||
.catch(() => {})
|
||||
this.$router.push({ name: 'base', query }).catch(() => {})
|
||||
}
|
||||
},
|
||||
watch: {
|
||||
@@ -74,6 +220,9 @@ export default {
|
||||
created () {
|
||||
// Direct page load: the store already holds the keys of the URL.
|
||||
this.typed = this.phraseFromKeys(this.keys)
|
||||
},
|
||||
beforeDestroy () {
|
||||
clearTimeout(this.timer)
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user