Skip to content

Commit 34c861b

Browse files
Script Loader: Add @wordpress/a11y as a Script Module.
The Script Module has the same API as the `wp-a11y` WP Script. Key changes: - Add `@wordpress/a11y` to the list of Script and Module dual packages. - Update `script-modules-packages.min.php` to include the a11y module. - Modify `WP_Script_Modules` class to track and handle a11y module availability. - Add method to print required HTML markup for a11y `speak()` functionality. See #60647. Props jonsurrell, gziolo, czapla. git-svn-id: https://develop.svn.wordpress.org/trunk@59089 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 9fc6fa1 commit 34c861b

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('interactivity/index.min.js' => array('dependencies' => array(), 'version' => '2d6d1fdbcb3fda39c768', 'type' => 'module'), 'interactivity/debug.min.js' => array('dependencies' => array(), 'version' => '1ccc67b05c275e51a8f8', 'type' => 'module'), 'interactivity-router/index.min.js' => array('dependencies' => array('@wordpress/interactivity'), 'version' => '64645ef3cd2d32860d7d', 'type' => 'module'), 'block-library/file/view.min.js' => array('dependencies' => array('@wordpress/interactivity'), 'version' => 'fdc2f6842e015af83140', 'type' => 'module'), 'block-library/image/view.min.js' => array('dependencies' => array('@wordpress/interactivity'), 'version' => 'acfec7b3c0be4a859b31', 'type' => 'module'), 'block-library/navigation/view.min.js' => array('dependencies' => array('@wordpress/interactivity'), 'version' => '8ff192874fc8910a284c', 'type' => 'module'), 'block-library/query/view.min.js' => array('dependencies' => array('@wordpress/interactivity', array('id' => '@wordpress/interactivity-router', 'import' => 'dynamic')), 'version' => 'f4c91c89fa5271f3dad9', 'type' => 'module'), 'block-library/search/view.min.js' => array('dependencies' => array('@wordpress/interactivity'), 'version' => '2a73400a693958f604de', 'type' => 'module'));
1+
<?php return array('interactivity/index.min.js' => array('dependencies' => array(), 'version' => '2d6d1fdbcb3fda39c768', 'type' => 'module'), 'interactivity/debug.min.js' => array('dependencies' => array(), 'version' => '1ccc67b05c275e51a8f8', 'type' => 'module'), 'interactivity-router/index.min.js' => array('dependencies' => array('@wordpress/interactivity'), 'version' => '64645ef3cd2d32860d7d', 'type' => 'module'), 'a11y/index.min.js' => array('dependencies' => array(), 'version' => 'b7d06936b8bc23cff2ad', 'type' => 'module'), 'block-library/file/view.min.js' => array('dependencies' => array('@wordpress/interactivity'), 'version' => 'fdc2f6842e015af83140', 'type' => 'module'), 'block-library/image/view.min.js' => array('dependencies' => array('@wordpress/interactivity'), 'version' => 'acfec7b3c0be4a859b31', 'type' => 'module'), 'block-library/navigation/view.min.js' => array('dependencies' => array('@wordpress/interactivity'), 'version' => '8ff192874fc8910a284c', 'type' => 'module'), 'block-library/query/view.min.js' => array('dependencies' => array('@wordpress/interactivity', array('id' => '@wordpress/interactivity-router', 'import' => 'dynamic')), 'version' => 'f4c91c89fa5271f3dad9', 'type' => 'module'), 'block-library/search/view.min.js' => array('dependencies' => array('@wordpress/interactivity'), 'version' => '2a73400a693958f604de', 'type' => 'module'));

src/wp-includes/class-wp-script-modules.php

+35
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ class WP_Script_Modules {
3030
*/
3131
private $enqueued_before_registered = array();
3232

33+
/**
34+
* Tracks whether the @wordpress/a11y script module is available.
35+
*
36+
* Some additional HTML is required on the page for the module to work. Track
37+
* whether it's available to print at the appropriate time.
38+
*
39+
* @since 6.7.0
40+
* @var bool
41+
*/
42+
private $a11y_available = false;
43+
3344
/**
3445
* Registers the script module if no script module with that script module
3546
* identifier has already been registered.
@@ -185,6 +196,8 @@ public function add_hooks() {
185196

186197
add_action( 'wp_footer', array( $this, 'print_script_module_data' ) );
187198
add_action( 'admin_print_footer_scripts', array( $this, 'print_script_module_data' ) );
199+
add_action( 'wp_footer', array( $this, 'print_a11y_script_module_html' ), 20 );
200+
add_action( 'admin_print_footer_scripts', array( $this, 'print_a11y_script_module_html' ), 20 );
188201
}
189202

190203
/**
@@ -367,9 +380,15 @@ private function get_src( string $id ): string {
367380
public function print_script_module_data(): void {
368381
$modules = array();
369382
foreach ( array_keys( $this->get_marked_for_enqueue() ) as $id ) {
383+
if ( '@wordpress/a11y' === $id ) {
384+
$this->a11y_available = true;
385+
}
370386
$modules[ $id ] = true;
371387
}
372388
foreach ( array_keys( $this->get_import_map()['imports'] ) as $id ) {
389+
if ( '@wordpress/a11y' === $id ) {
390+
$this->a11y_available = true;
391+
}
373392
$modules[ $id ] = true;
374393
}
375394

@@ -465,4 +484,20 @@ public function print_script_module_data(): void {
465484
}
466485
}
467486
}
487+
488+
/**
489+
* @access private This is only intended to be called by the registered actions.
490+
*
491+
* @since 6.7.0
492+
*/
493+
public function print_a11y_script_module_html() {
494+
if ( ! $this->a11y_available ) {
495+
return;
496+
}
497+
echo '<div style="position:absolute;margin:-1px;padding:0;height:1px;width:1px;overflow:hidden;clip-path:inset(50%);border:0;word-wrap:normal !important;">'
498+
. '<p id="a11y-speak-intro-text" class="a11y-speak-intro-text" hidden>' . esc_html__( 'Notifications' ) . '</p>'
499+
. '<div id="a11y-speak-assertive" class="a11y-speak-region" aria-live="assertive" aria-relevant="additions text" aria-atomic="true"></div>'
500+
. '<div id="a11y-speak-polite" class="a11y-speak-region" aria-live="polite" aria-relevant="additions text" aria-atomic="true"></div>'
501+
. '</div>';
502+
}
468503
}

tools/webpack/shared.js

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ const MODULES = [
103103
'@wordpress/interactivity-router',
104104
];
105105
const SCRIPT_AND_MODULE_DUAL_PACKAGES = [
106+
'@wordpress/a11y',
106107
'@wordpress/block-library',
107108
];
108109
const WORDPRESS_NAMESPACE = '@wordpress/';

0 commit comments

Comments
 (0)