-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
a11y: Print script module live regions in page HTML (#65380)
Fix issues where @wordpress/a11y script module `speak` may not be detected and announced by accessibility software, in particular when dynamically loading the module. Accessibility software needs to detect the live regions in order to announce them. The `@wordpress/a11y` script module was adding the live regions to the DOM correctly when the module was loaded, but when loaded dynamically (via `import( '@wordpress/a11y' ).then( /*…*/ )` ) the live region changes would happen before the live region was detected by accessibility software and the announcements would not happen. The `@wordpress/a11y` script module is updated so that it does not inject the live regions into the DOM but expects them to be present on the page. A hook is added to inject the necessary HTML for the live regions when the @wordpress/a11y script module may be present (enqueued or in the import map). This has a few additional advantages: - Accessibility software correctly detects the live regions. - The DOM does not need to be modified immediately on page load. - The (gzipped) size of the script module is reduced by 416B (~46%). The `wp-a11y` script shares the same DOM elements. It includes protection to prevent injecting duplicate elements. --- Co-authored-by: sirreal <[email protected]> Co-authored-by: gziolo <[email protected]> Co-authored-by: afercia <[email protected]>
- Loading branch information
1 parent
be8f9b6
commit 05660fe
Showing
7 changed files
with
75 additions
and
59 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,11 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { makeSetupFunction } from '../shared/index'; | ||
export { speak } from '../shared/index'; | ||
|
||
// Without an i18n Script Module, "Notifications" (the only localized text used in this module) | ||
// will be translated on the server and provided as script-module data. | ||
let notificationsText = 'Notifications'; | ||
try { | ||
const textContent = document.getElementById( | ||
'wp-script-module-data-@wordpress/a11y' | ||
)?.textContent; | ||
if ( textContent ) { | ||
const parsed = JSON.parse( textContent ); | ||
notificationsText = parsed?.i18n?.Notifications ?? notificationsText; | ||
} | ||
} catch {} | ||
|
||
/** | ||
* Create the live regions. | ||
* This no-op function is exported to provide compatibility with the `wp-a11y` Script. | ||
* | ||
* Filters should inject the relevant HTML on page load instead of requiring setup. | ||
*/ | ||
export const setup = makeSetupFunction( notificationsText ); | ||
|
||
setup(); | ||
export const setup = () => {}; |
File renamed without changes.
10 changes: 7 additions & 3 deletions
10
packages/a11y/src/shared/add-intro-text.ts → packages/a11y/src/script/add-intro-text.ts
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
File renamed without changes.
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