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

Radio Example: Add scroll-to feature using aria-activedescendant #2056

Merged
merged 4 commits into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 18 additions & 0 deletions examples/radio/js/radio-activedescendant.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ class RadioGroupActiveDescendant {
this.groupNode.tabIndex = 0;
}

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

setChecked(currentItem) {
for (var i = 0; i < this.radioButtons.length; i++) {
var rb = this.radioButtons[i];
Expand All @@ -50,6 +62,9 @@ class RadioGroupActiveDescendant {
currentItem.setAttribute('aria-checked', 'true');
currentItem.classList.add('focus');
this.groupNode.setAttribute('aria-activedescendant', currentItem.id);
if (!this.isRadioInView(currentItem)) {
currentItem.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
}
this.groupNode.focus();
}

Expand Down Expand Up @@ -142,6 +157,9 @@ class RadioGroupActiveDescendant {

handleFocus() {
var currentItem = this.getCurrentRadioButton();
if (!this.isRadioInView(currentItem)) {
currentItem.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
}
currentItem.classList.add('focus');
}

Expand Down
5 changes: 4 additions & 1 deletion examples/radio/radio-activedescendant.html
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ <h3 id="group_label_2">Pizza Delivery</h3>
<section>
<h2>Accessibility Features</h2>
<ul>
<li>Uses CSS attribute selectors for synchronizing <code>aria-checked</code> state with the visual state indicator.</li>
<li>
The radio button referenced by <code>aria-activedescendant</code> is scrolled into view when it is not visible in the graphical rendering. This can occur under many conditions, but is most common when people with visual impairments use a browser's zoom feature to increase the size of content.
</li>
<li>Uses CSS attribute selectors for synchronizing <code>aria-checked</code> state with the visual state indicator.</li>
<li>Uses CSS <code>:hover</code> and <code>:focus</code> pseudo-classes for styling visual keyboard focus and hover.
<ul>
<li>The focus indicator encompasses both the radio button and label, making it easier to perceive which option is being chosen.</li>
Expand Down