Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autocomplete accessibility features #5008

Merged
merged 2 commits into from Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var HashHandler = require("./keyboard/hash_handler").HashHandler;
var AcePopup = require("./autocomplete/popup").AcePopup;
var getAriaId = require("./autocomplete/popup").getAriaId
var util = require("./autocomplete/util");
var lang = require("./lib/lang");
var dom = require("./lib/dom");
Expand Down Expand Up @@ -54,6 +55,7 @@ var Autocomplete = function() {
this.popup.autoSelect = this.autoSelect;

this.popup.setData(this.completions.filtered, this.completions.filterText);
this.editor.textInput.setAriaOptions({activeDescendant: getAriaId(this.popup.getRow())});

editor.keyBinding.addKeyboardHandler(this.keyboardHandler);

Expand Down
23 changes: 21 additions & 2 deletions src/autocomplete/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ var event = require("../lib/event");
var lang = require("../lib/lang");
var dom = require("../lib/dom");

var getAriaId = function(index) {
return `suggest-aria-id:${index}`;
}

var $singleLineEditor = function(el) {
var renderer = new Renderer(el);

Expand Down Expand Up @@ -45,6 +49,10 @@ var AcePopup = function(parentNode) {
popup.renderer.content.style.cursor = "default";
popup.renderer.setStyle("ace_autocomplete");

// Set aria attributes for the popup
popup.renderer.container.setAttribute("role", "listbox");
popup.renderer.container.setAttribute("aria-label", "Autocomplete suggestions");

popup.setOption("displayIndentGuides", false);
popup.setOption("dragDelay", 150);

Expand Down Expand Up @@ -114,11 +122,21 @@ var AcePopup = function(parentNode) {
var row = popup.getRow();
var t = popup.renderer.$textLayer;
var selected = t.element.childNodes[row - t.config.firstRow];
if (selected !== t.selectedNode && t.selectedNode)
var el = document.activeElement; // Active element is textarea of main editor
if (selected !== t.selectedNode && t.selectedNode) {
dom.removeCssClass(t.selectedNode, "ace_selected");
el.removeAttribute("aria-activedescendant");
t.selectedNode.id = "ace_line_" + t.selectedNode.id.split(":")[1];
This conversation was marked as resolved.
Show resolved Hide resolved
}
t.selectedNode = selected;
if (selected)
if (selected) {
dom.addCssClass(selected, "ace_selected");
var ariaId = getAriaId(row);
selected.id = ariaId;
popup.renderer.container.setAttribute("aria-activedescendant", ariaId);
el.setAttribute("aria-activedescendant", ariaId);
selected.setAttribute("aria-label", popup.getData(row).value);
}
});
var hideHoverMarker = function() { setHoverMarker(-1); };
var setHoverMarker = function(row, suppressRedraw) {
Expand Down Expand Up @@ -350,3 +368,4 @@ dom.importCssString(`

exports.AcePopup = AcePopup;
exports.$singleLineEditor = $singleLineEditor;
exports.getAriaId = getAriaId;
18 changes: 17 additions & 1 deletion src/keyboard/textinput.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,23 @@ var TextInput = function(parentNode, host) {
// FOCUS
// ie9 throws error if document.activeElement is accessed too soon
try { var isFocused = document.activeElement === text; } catch(e) {}


this.setAriaOptions = function(options) {
if (options.activeDescendant) {
text.setAttribute('aria-haspopup', 'true');
This conversation was marked as resolved.
Show resolved Hide resolved
text.setAttribute('aria-autocomplete', 'list');
text.setAttribute('aria-activedescendant', options.activeDescendant);
} else {
text.setAttribute('aria-haspopup', 'false');
text.setAttribute('aria-autocomplete', 'both');
text.removeAttribute('aria-activedescendant');
}
if (options.role) {
text.setAttribute('role', options.role);
}
};
this.setAriaOptions({role: "textbox"});

event.addListener(text, "blur", function(e) {
if (ignoreFocusEvents) return;
host.onBlur(e);
Expand Down
2 changes: 2 additions & 0 deletions src/layer/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ var Text = function(parentEl) {
lineEl.className = "ace_line_group";
} else {
lineEl.className = "ace_line";
lineEl.id = "ace_line_" + row;
This conversation was marked as resolved.
Show resolved Hide resolved
lineEl.setAttribute("role", "option");
}
fragment.push(line);

Expand Down