Skip to content

Commit

Permalink
fix(placeholder): follow HTML5 select screenreader workflow #111
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanjones243 committed Oct 14, 2022
1 parent 81b67a1 commit 3abffea
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/auro-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ class AuroSelect extends LitElement {
this.dropdown.hide();
}
});

this.labelForSr();
}

/**
Expand Down Expand Up @@ -321,6 +323,34 @@ class AuroSelect extends LitElement {
}
}

/**
* Handles reading of auro-select by screenreaders.
* @private
* @returns {void}
*/
labelForSr() {
const placeholderLabel = document.createElement("div");
const textId = "label";

placeholderLabel.setAttribute("id", textId);
placeholderLabel.setAttribute("aria-live", "polite");
placeholderLabel.classList.add("util_displayHiddenVisually");

this.addEventListener('focus', () => {
document.body.appendChild(placeholderLabel);

if (!this.optionSelected) {
document.getElementById(textId).innerHTML = this.placeholder;
} else {
document.getElementById(textId).innerHTML = `${this.optionSelected.innerText}, ${this.placeholder}`;
}
});

this.addEventListener('blur', () => {
document.body.removeChild(document.getElementById(textId));
});
}

// When using auroElement, use the following attribute and function when hiding content from screen readers.
// aria-hidden="${this.hideAudible(this.hiddenAudible)}"

Expand Down

0 comments on commit 3abffea

Please sign in to comment.