forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A11y: Add Script Module (WordPress#65101)
Add a `@wordpress/a11y` WordPress Script Module. The script module has the same public API as the `wp-a11y` script. - The `domReady` package should not be necessary. Since modules are deferred, setup can be called directly when the module is evaluated. - There is no i18n script module at this time. The necessary string "Notifications" is translated on the server and passed to the package via script module data. - Most of the changes here are moving some functions around so that the script and the module form of this package rely on the same underlying implementations. --- Co-authored-by: sirreal <[email protected]> Co-authored-by: gziolo <[email protected]>
- Loading branch information
1 parent
9bdfebf
commit f88e629
Showing
16 changed files
with
151 additions
and
87 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
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,25 @@ | ||
/** | ||
* 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. | ||
*/ | ||
export const setup = makeSetupFunction( notificationsText ); | ||
|
||
setup(); |
File renamed without changes.
10 changes: 3 additions & 7 deletions
10
packages/a11y/src/add-intro-text.js → packages/a11y/src/shared/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.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import addContainer from './add-container'; | ||
import addIntroText from './add-intro-text'; | ||
import clear from './clear'; | ||
import filterMessage from './filter-message'; | ||
|
||
/** | ||
* Create the live regions. | ||
* @param {string} introTextContent The intro text content. | ||
*/ | ||
export function makeSetupFunction( introTextContent ) { | ||
return function setup() { | ||
const introText = document.getElementById( 'a11y-speak-intro-text' ); | ||
const containerAssertive = document.getElementById( | ||
'a11y-speak-assertive' | ||
); | ||
const containerPolite = document.getElementById( 'a11y-speak-polite' ); | ||
|
||
if ( introText === null ) { | ||
addIntroText( introTextContent ); | ||
} | ||
|
||
if ( containerAssertive === null ) { | ||
addContainer( 'assertive' ); | ||
} | ||
|
||
if ( containerPolite === null ) { | ||
addContainer( 'polite' ); | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Allows you to easily announce dynamic interface updates to screen readers using ARIA live regions. | ||
* This module is inspired by the `speak` function in `wp-a11y.js`. | ||
* | ||
* @param {string} message The message to be announced by assistive technologies. | ||
* @param {'polite'|'assertive'} [ariaLive] The politeness level for aria-live; default: 'polite'. | ||
* | ||
* @example | ||
* ```js | ||
* import { speak } from '@wordpress/a11y'; | ||
* | ||
* // For polite messages that shouldn't interrupt what screen readers are currently announcing. | ||
* speak( 'The message you want to send to the ARIA live region' ); | ||
* | ||
* // For assertive messages that should interrupt what screen readers are currently announcing. | ||
* speak( 'The message you want to send to the ARIA live region', 'assertive' ); | ||
* ``` | ||
*/ | ||
export function speak( message, ariaLive ) { | ||
/* | ||
* Clear previous messages to allow repeated strings being read out and hide | ||
* the explanatory text from assistive technologies. | ||
*/ | ||
clear(); | ||
|
||
message = filterMessage( message ); | ||
|
||
const introText = document.getElementById( 'a11y-speak-intro-text' ); | ||
const containerAssertive = document.getElementById( | ||
'a11y-speak-assertive' | ||
); | ||
const containerPolite = document.getElementById( 'a11y-speak-polite' ); | ||
|
||
if ( containerAssertive && ariaLive === 'assertive' ) { | ||
containerAssertive.textContent = message; | ||
} else if ( containerPolite ) { | ||
containerPolite.textContent = message; | ||
} | ||
|
||
/* | ||
* Make the explanatory text available to assistive technologies by removing | ||
* the 'hidden' HTML attribute. | ||
*/ | ||
if ( introText ) { | ||
introText.removeAttribute( 'hidden' ); | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
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
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