Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interactivity API: Use a11y Script Module in Gutenberg #65123

Merged
merged 16 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions lib/interactivity-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,41 @@ function gutenberg_reregister_interactivity_script_modules() {
wp_register_script_module(
'@wordpress/interactivity-router',
gutenberg_url( '/build-module/interactivity-router/index.min.js' ),
array( '@wordpress/interactivity' ),
array(
array(
'id' => '@wordpress/a11y',
'import' => 'dynamic',
),
'@wordpress/interactivity',
),
$default_version
);
}

add_action( 'init', 'gutenberg_reregister_interactivity_script_modules' );

/**
* Adds script data to the interactivity-router script module.
*
* This filter is registered conditionally anticipating a WordPress Core change to add the script module data.
* The filter runs on 'after_setup_theme' (when Core registers Interactivity and Script Modules hooks)
* to ensure that the conditional registration happens after Core and correctly determine whether
* the filter should be added.
*
* @see https://github.com/WordPress/wordpress-develop/pull/7304
*/
function gutenberg_register_interactivity_script_module_data_hooks() {
gziolo marked this conversation as resolved.
Show resolved Hide resolved
gziolo marked this conversation as resolved.
Show resolved Hide resolved
if ( ! has_filter( 'script_module_data_@wordpress/interactivity-router', array( wp_interactivity(), 'filter_script_module_interactivity_router_data' ) ) ) {
gziolo marked this conversation as resolved.
Show resolved Hide resolved
add_filter(
'script_module_data_@wordpress/interactivity-router',
function ( $data ) {
if ( ! isset( $data['i18n'] ) ) {
$data['i18n'] = array();
}
$data['i18n']['loading'] = __( 'Loading page, please wait.', 'default' );
$data['i18n']['loaded'] = __( 'Page Loaded.', 'default' );
return $data;
}
);
}
}
add_action( 'after_setup_theme', 'gutenberg_register_interactivity_script_module_data_hooks', 20 );
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the latest changes to use the localized fallback strings (passed via Interactivity API), if I disable this filter it correctly uses the those strings as a fallback.

#65129 proposes adding this filter to Core.

2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/interactivity-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"types": "build-types",
"wpScriptModuleExports": "./build-module/index.js",
"dependencies": {
"@wordpress/a11y": "file:../a11y",
"@wordpress/interactivity": "file:../interactivity"
},
"publishConfig": {
Expand Down
65 changes: 52 additions & 13 deletions packages/interactivity-router/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,16 +209,18 @@ const isValidEvent = ( event: MouseEvent ) =>
// Variable to store the current navigation.
let navigatingTo = '';

let hasLoadedNavigationTextsData = false;
const navigationTexts = {
loading: 'Loading page, please wait.',
loaded: 'Page Loaded.',
michalczaplinski marked this conversation as resolved.
Show resolved Hide resolved
};

export const { state, actions } = store( 'core/router', {
state: {
url: window.location.href,
navigation: {
hasStarted: false,
hasFinished: false,
texts: {
loading: '',
loaded: '',
},
Comment on lines -218 to -221
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would overwrite state from the server, it was breaking the server-provided localized strings previously.

message: '',
},
},
Expand Down Expand Up @@ -275,7 +277,7 @@ export const { state, actions } = store( 'core/router', {
navigation.hasFinished = false;
}
if ( screenReaderAnnouncement ) {
navigation.message = navigation.texts.loading;
a11ySpeak( 'loading' );
}
}, 400 );

Expand Down Expand Up @@ -315,14 +317,7 @@ export const { state, actions } = store( 'core/router', {
}

if ( screenReaderAnnouncement ) {
// Announce that the page has been loaded. If the message is the
// same, we use a no-break space similar to the @wordpress/a11y
// package: https://github.com/WordPress/gutenberg/blob/c395242b8e6ee20f8b06c199e4fc2920d7018af1/packages/a11y/src/filter-message.js#L20-L26
navigation.message =
navigation.texts.loaded +
( navigation.message === navigation.texts.loaded
? '\u00A0'
: '' );
a11ySpeak( 'loaded' );
}

// Scroll to the anchor if exits in the link.
Expand Down Expand Up @@ -363,6 +358,50 @@ export const { state, actions } = store( 'core/router', {
},
} );

/**
* Announces a message to screen readers.
*
* This is a wrapper around the `@wordpress/a11y` package's `speak` function. It handles importing
* the package on demand and should be used instead of calling `ally.speak` direacly.
*
* @param messageKey The message to be announced by assistive technologies.
*/
function a11ySpeak( messageKey: keyof typeof navigationTexts ) {
if ( ! hasLoadedNavigationTextsData ) {
hasLoadedNavigationTextsData = true;
const content = document.getElementById(
'wp-script-module-data-@wordpress/interactivity-router'
)?.textContent;
if ( content ) {
try {
const parsed = JSON.parse( content );
if ( typeof parsed?.i18n?.loading === 'string' ) {
navigationTexts.loading = parsed.i18n.loading;
}
if ( typeof parsed?.i18n?.loaded === 'string' ) {
navigationTexts.loaded = parsed.i18n.loaded;
}
} catch {}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The localized strings weren't working correctly before because the server-provided state was being overwritten with empty strings.

This form will work in Gutenberg but requires #65129 for it to work in Core.

There should be fallback code here to use the localized strings from Interactivity API state.

}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code eventually could get replaced with dynamically loaded @wordpress/i18n.


const message = navigationTexts[ messageKey ];

if ( globalThis.IS_GUTENBERG_PLUGIN ) {
import( '@wordpress/a11y' ).then(
( { speak } ) => speak( message ),
// Ignore failures to load the a11y module.
() => {}
);
} else {
Comment on lines +417 to +423
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's behind the flag so the new approach will be only used in the Gutenberg plugin in case we decide against shipping it in the WordPress 6.7.

state.navigation.message =
// Announce that the page has been loaded. If the message is the
// same, we use a no-break space similar to the @wordpress/a11y
// package: https://github.com/WordPress/gutenberg/blob/c395242b8e6ee20f8b06c199e4fc2920d7018af1/packages/a11y/src/filter-message.js#L20-L26
message + ( state.navigation.message === message ? '\u00A0' : '' );
}
}

// Add click and prefetch to all links.
if ( globalThis.IS_GUTENBERG_PLUGIN ) {
if ( navigationMode === 'fullPage' ) {
Expand Down
2 changes: 1 addition & 1 deletion packages/interactivity-router/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"checkJs": false,
"strict": false
},
"references": [ { "path": "../interactivity" } ],
"references": [ { "path": "../a11y" }, { "path": "../interactivity" } ],
"include": [ "src/**/*" ]
}
Loading