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

Grid Combobox Example: Add scroll into view feature #2075

Merged
merged 8 commits into from
Nov 2, 2021
6 changes: 6 additions & 0 deletions examples/combobox/grid-combo.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ <h2 id="ex_label">Example</h2>
<div role="separator" id="ex_end_sep" aria-labelledby="ex_end_sep ex_label" aria-label="End of"></div>
</section>

<section>
<h2>Accessibility Features</h2>
<p>Browsers do not manage visibility of elements referenced by <code>aria-activedescendant</code> like they do for elements with focus. When a keyboard event changes the active option in the listbox, the JavaScript scrolls the option referenced by <code>aria-activedescendant</code> into view. Managing <code>aria-activedescendant</code> visibility is essential to accessibility for people who use a browser's zoom feature to increase the size of content.
</p>
</section>

<section>
<h2 id="kbd_label">Keyboard Support</h2>
<p>
Expand Down
23 changes: 23 additions & 0 deletions examples/combobox/js/grid-combo.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,19 @@ aria.GridCombobox.prototype.handleGridClick = function (evt) {
this.selectItem(selectItem);
};

aria.GridCombobox.prototype.isElementInView = function (element) {
var bounding = element.getBoundingClientRect();

return (
bounding.top >= 0 &&
bounding.left >= 0 &&
bounding.bottom <=
(window.innerHeight || document.documentElement.clientHeight) &&
bounding.right <=
(window.innerWidth || document.documentElement.clientWidth)
);
};

aria.GridCombobox.prototype.updateResults = function () {
var searchString = this.input.value;
var results = this.searchFn(searchString);
Expand Down Expand Up @@ -278,6 +291,11 @@ aria.GridCombobox.prototype.hideResults = function () {
this.rowsCount = 0;
this.colsCount = 0;
this.input.setAttribute('aria-activedescendant', '');

// ensure the input is in view
if (!this.isElementInView(this.input)) {
this.input.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
}
};

aria.GridCombobox.prototype.removeFocusCell = function (rowIndex, colIndex) {
Expand All @@ -292,4 +310,9 @@ aria.GridCombobox.prototype.focusCell = function (rowIndex, colIndex) {
aria.Utils.addClass(row, 'focused');
var cell = this.getItemAt(rowIndex, colIndex);
aria.Utils.addClass(cell, 'focused-cell');

// ensure the cell is in view
if (!this.isElementInView(cell)) {
cell.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
}
};