This repository has been archived by the owner on Jan 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(snackbar): Accessibility: Announce label text to screen readers (#…
- Loading branch information
Showing
20 changed files
with
330 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/** | ||
* @license | ||
* Copyright 2018 Google Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
import {numbers, strings} from './constants'; | ||
|
||
const {ARIA_LIVE_DELAY_MS} = numbers; | ||
const {ARIA_LIVE_LABEL_TEXT_ATTR} = strings; | ||
|
||
/** | ||
* @param {!HTMLElement} ariaEl | ||
* @param {!HTMLElement=} labelEl | ||
*/ | ||
function announce(ariaEl, labelEl = ariaEl) { | ||
const priority = ariaEl.getAttribute('aria-live'); | ||
const labelText = labelEl.textContent.trim(); // Ignore ` ` (see below) | ||
if (!labelText) { | ||
return; | ||
} | ||
|
||
// Temporarily disable `aria-live` to prevent JAWS+Firefox from announcing the message twice. | ||
ariaEl.setAttribute('aria-live', 'off'); | ||
|
||
// Temporarily clear `textContent` to force a DOM mutation event that will be detected by screen readers. | ||
// `aria-live` elements are only announced when the element's `textContent` *changes*, so snackbars | ||
// sent to the browser in the initial HTML response won't be read unless we clear the element's `textContent` first. | ||
// Similarly, displaying the same snackbar message twice in a row doesn't trigger a DOM mutation event, | ||
// so screen readers won't announce the second message unless we first clear `textContent`. | ||
// | ||
// We have to clear the label text two different ways to make it work in all browsers and screen readers: | ||
// | ||
// 1. `textContent = ''` is required for IE11 + JAWS | ||
// 2. `innerHTML = ' '` is required for Chrome + JAWS and NVDA | ||
// | ||
// All other browser/screen reader combinations support both methods. | ||
// | ||
// The wrapper `<span>` visually hides the space character so that it doesn't cause jank when added/removed. | ||
// N.B.: Setting `position: absolute`, `opacity: 0`, or `height: 0` prevents Chrome from detecting the DOM change. | ||
// | ||
// This technique has been tested in: | ||
// | ||
// * JAWS 2019: | ||
// - Chrome 70 | ||
// - Firefox 60 (ESR) | ||
// - IE 11 | ||
// * NVDA 2018: | ||
// - Chrome 70 | ||
// - Firefox 60 (ESR) | ||
// - IE 11 | ||
labelEl.textContent = ''; | ||
labelEl.innerHTML = '<span style="display: inline-block; width: 0; height: 1px;"> </span>'; | ||
|
||
// Prevent visual jank by temporarily displaying the label text in the ::before pseudo-element. | ||
// CSS generated content is normally announced by screen readers | ||
// (except in IE 11; see https://tink.uk/accessibility-support-for-css-generated-content/); | ||
// however, `aria-live` is turned off, so this DOM update will be ignored by screen readers. | ||
labelEl.setAttribute(ARIA_LIVE_LABEL_TEXT_ATTR, labelText); | ||
|
||
setTimeout(() => { | ||
// Allow screen readers to announce changes to the DOM again. | ||
ariaEl.setAttribute('aria-live', priority); | ||
|
||
// Remove the message from the ::before pseudo-element. | ||
labelEl.removeAttribute(ARIA_LIVE_LABEL_TEXT_ATTR); | ||
|
||
// Restore the original label text, which will be announced by screen readers. | ||
labelEl.textContent = labelText; | ||
}, ARIA_LIVE_DELAY_MS); | ||
} | ||
|
||
export {announce}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.