Skip to content

Commit f66fe07

Browse files
authored
Fix location combobox invalid aria property (#186)
unset `aria-activedescendant` property of location combobox if no suggestion is focused
1 parent 19f2d2c commit f66fe07

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vahrplan",
3-
"version": "4.1.2",
3+
"version": "4.1.3",
44
"scripts": {
55
"dev": "vite dev",
66
"build": "vite build",

src/routes/[lang]/[profile]/StationInput.svelte

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
2424
let inputText = $state(selectedLocation?.name ?? "");
2525
let inputElement: HTMLInputElement;
26-
let isExpanded = $state(false);
27-
let focused = $state(-1);
26+
let isFocused = $state(false);
27+
let focusedId = $state(-1);
2828
let isInputBlurredBySelection = $state(true);
2929
let isSuggestionsHidden = $state(false);
3030
@@ -51,7 +51,7 @@
5151
* selects the first suggested location if the user leaves the input without a selection
5252
*/
5353
function handleInputBlur(): void {
54-
isExpanded = false;
54+
isFocused = false;
5555
isSuggestionsHidden = false;
5656
isInputBlurredBySelection = false; // this is reset to `true` in `handleSuggestionClick()`, otherwise it remains `false`
5757
setTimeout(() => {
@@ -68,7 +68,7 @@
6868
void suggestions.then((suggestions) => {
6969
selectedLocation = suggestions[0];
7070
inputText = suggestions[0]?.name ?? "";
71-
focused = -1;
71+
focusedId = -1;
7272
});
7373
}
7474
}, 500);
@@ -98,7 +98,7 @@
9898
inputElement.blur();
9999
selectedLocation = suggestion;
100100
inputText = suggestion.name;
101-
focused = -1;
101+
focusedId = -1;
102102
isInputBlurredBySelection = true;
103103
isSuggestionsHidden = true;
104104
}
@@ -107,33 +107,33 @@
107107
void suggestions.then((suggestions) => {
108108
switch (ev.key) {
109109
case "ArrowDown":
110-
focused = (focused + 1) % suggestions.length;
110+
focusedId = (focusedId + 1) % suggestions.length;
111111
ev.preventDefault();
112112
break;
113113
case "ArrowUp":
114-
focused = focused <= 0 ? suggestions.length - 1 : focused - 1;
114+
focusedId = focusedId <= 0 ? suggestions.length - 1 : focusedId - 1;
115115
ev.preventDefault();
116116
break;
117117
case "Enter":
118-
if (focused >= 0) {
118+
if (focusedId >= 0) {
119119
isSuggestionsHidden = true;
120120
}
121121
ev.preventDefault();
122122
// fallthrough
123123
case "Tab":
124-
if (focused >= 0) {
124+
if (focusedId >= 0) {
125125
// something is focused
126-
selectedLocation = suggestions[focused]; // do not use `handleSuggestionClick`, since the input does not need to be blurred
127-
inputText = suggestions[focused].name;
126+
selectedLocation = suggestions[focusedId]; // do not use `handleSuggestionClick`, since the input does not need to be blurred
127+
inputText = suggestions[focusedId].name;
128128
}
129-
focused = -1;
129+
focusedId = -1;
130130
break;
131131
case "ArrowRight":
132132
case "ArrowLeft":
133133
break;
134134
default:
135135
isSuggestionsHidden = false;
136-
focused = -1;
136+
focusedId = -1;
137137
}
138138
});
139139
}
@@ -163,14 +163,16 @@
163163
onblur={handleInputBlur}
164164
onfocus={(): void => {
165165
isInputBlurredBySelection = false;
166-
isExpanded = true;
166+
isFocused = true;
167167
isSuggestionsHidden = false;
168168
}}
169169
role="combobox"
170170
aria-label="Station {stationInputId + 1}"
171171
aria-autocomplete="list"
172-
aria-activedescendant="search-input__{stationInputId}--suggestions__{focused}"
173-
aria-expanded={isExpanded && !isSuggestionsHidden}
172+
aria-activedescendant={focusedId !== -1
173+
? `search-input__${stationInputId}--suggestions__${focusedId}`
174+
: ""}
175+
aria-expanded={isFocused && !isSuggestionsHidden}
174176
aria-controls="search-input__{stationInputId}--suggestions"
175177
/>
176178
{#if selectedLocation !== undefined && selectedLocation.name !== "Standort"}
@@ -223,7 +225,7 @@
223225
<button
224226
type="button"
225227
class="flex-row padded-top-bottom suggestion__button"
226-
class:focused={focused === i}
228+
class:focused={focusedId === i}
227229
tabindex="-1"
228230
onclick={() => void handleSuggestionClick(suggestion)}
229231
>

0 commit comments

Comments
 (0)