Skip to content

Commit 288ef29

Browse files
committed
Add basic accessibility
1 parent a106717 commit 288ef29

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

horsey.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var KEY_DOWN = 40;
1212
var cache = [];
1313
var doc = document;
1414
var docElement = doc.documentElement;
15+
var c = 0;
1516

1617
function find (el) {
1718
var entry;
@@ -51,6 +52,7 @@ function horsey (el, options) {
5152
var anyInput;
5253
var ranchorleft;
5354
var ranchorright;
55+
var id = 'sey-list-' + c++;
5456

5557
if (o.autoHideOnBlur === void 0) { o.autoHideOnBlur = true; }
5658
if (o.autoHideOnClick === void 0) { o.autoHideOnClick = true; }
@@ -90,6 +92,11 @@ function horsey (el, options) {
9092
cache.push(entry);
9193
parent.appendChild(ul);
9294
el.setAttribute('autocomplete', 'off');
95+
el.setAttribute('role', 'combobox');
96+
el.setAttribute('aria-owns', id);
97+
el.setAttribute('aria-autocomplete', 'list');
98+
ul.setAttribute('id', id);
99+
ul.setAttribute('role', 'listbox');
93100

94101
if (Array.isArray(suggestions)) {
95102
loaded(suggestions);
@@ -125,12 +132,15 @@ function horsey (el, options) {
125132
}
126133
}
127134

128-
function add (suggestion) {
135+
function add (suggestion, i) {
129136
var li = tag('li', 'sey-item');
137+
var suggestionId = id + '-' + i;
130138
render(li, suggestion);
131139
crossvent.add(li, 'click', clickedSuggestion);
132140
crossvent.add(li, 'horsey-filter', filterItem);
133141
crossvent.add(li, 'horsey-hide', hideItem);
142+
li.setAttribute('role', 'option');
143+
li.setAttribute('id', suggestionId);
134144
ul.appendChild(li);
135145
api.suggestions.push(suggestion);
136146
return li;
@@ -141,6 +151,7 @@ function horsey (el, options) {
141151
hide();
142152
attachment.focus();
143153
crossvent.fabricate(attachment, 'horsey-selected', value);
154+
el.setAttribute('aria-activedescendant', suggestionId);
144155
}
145156

146157
function filterItem () {
@@ -210,13 +221,15 @@ function horsey (el, options) {
210221
if (suggestion) {
211222
selection = suggestion;
212223
selection.className += ' sey-selected';
224+
el.setAttribute('aria-activedescendant', selection.getAttribute('id'));
213225
}
214226
}
215227

216228
function unselect () {
217229
if (selection) {
218230
selection.className = selection.className.replace(/ sey-selected/g, '');
219231
selection = null;
232+
el.removeAttribute('aria-activedescendant');
220233
}
221234
}
222235

0 commit comments

Comments
 (0)