-
Notifications
You must be signed in to change notification settings - Fork 4k
/
index.js
66 lines (58 loc) · 1.95 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* WordPress dependencies
*/
import domReady from '@wordpress/dom-ready';
/**
* Internal dependencies
*/
import addContainer from './addContainer';
import clear from './clear';
import filterMessage from './filterMessage';
/**
* Create the live regions.
*/
export const setup = function() {
const containerPolite = document.getElementById( 'a11y-speak-polite' );
const containerAssertive = document.getElementById( 'a11y-speak-assertive' );
if ( containerPolite === null ) {
addContainer( 'polite' );
}
if ( containerAssertive === null ) {
addContainer( 'assertive' );
}
};
/**
* Run setup on domReady.
*/
domReady( setup );
/**
* 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 {string} ariaLive Optional. The politeness level for aria-live. Possible values:
* polite or assertive. 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 const speak = function( message, ariaLive ) {
// Clear previous messages to allow repeated strings being read out.
clear();
message = filterMessage( message );
const containerPolite = document.getElementById( 'a11y-speak-polite' );
const containerAssertive = document.getElementById( 'a11y-speak-assertive' );
if ( containerAssertive && 'assertive' === ariaLive ) {
containerAssertive.textContent = message;
} else if ( containerPolite ) {
containerPolite.textContent = message;
}
};