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

Add @wordpress/a11y script module #7405

Closed
wants to merge 8 commits into from

Conversation

sirreal
Copy link
Member

@sirreal sirreal commented Sep 20, 2024

Add a new @wordpress/a11y Script Module. The Script Module has the same API as the wp-a11y WP Script.

This implements Gutenberg PRs WordPress/gutenberg#65101 and WordPress/gutenberg#65380 to add the @wordpress/a11y Script Module in Core.

One difference with the wp-a11y WP Script is that the necessary HTML is injected into the initial page's HTML. The Script injects some elements into the DOM when loaded. This is problematic with accessibility live regions and modules that may have deferred loading. The background and justification for this change was discussed in WordPress/gutenberg#65380.

The script and module do not conflict. If the script is loaded, it will not inject elements into the DOM but will re-use the HTML rendered for the module. There is some potential for conflict if the live regions are used simultaneously by the script and module, but this has always been the case if different consumers use the script simultaneously.

#7304 and WordPress/gutenberg#65539 leverage the new @wordpress/a11y Script Module in the @wordpress/interactivity-router script module.

Testing

This can be tested by adding a dependency on @wordpress/a11y from another script module. The exposed speak function should work when the module is imported dynamically or statically.

If the wp-a11y script is also enqueued, no duplicate live regions should be injected into the DOM. Both the script's and the module's speak function should work correctly.

It can be helpful to observe a something like this in the browser console:

[...document.querySelectorAll('[aria-live]')].map(e => `${e.ariaLive}: ${JSON.stringify(e.textContent)}`)

For example:

(await import('@wordpress/a11y')).speak('Hello, ', 'assertive');
// ['assertive: "Hello, "', 'polite: ""']
wp.a11y.speak('world!');
// ['assertive: ""', 'polite: "world!"']

Trac ticket: https://core.trac.wordpress.org/ticket/60647


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

Copy link

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@sirreal sirreal force-pushed the add/a11y-script-module branch from 8f38434 to d6201e3 Compare September 24, 2024 10:08
@sirreal sirreal marked this pull request as ready for review September 24, 2024 13:11
Copy link

github-actions bot commented Sep 24, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props jonsurrell, gziolo, czapla.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Comment on lines 33 to 42
/**
* Tracks wehther the @wordpress/a11y script module is available.
*
* Some additional HTML is required on the page for the module to work. Track
* whether it's available to print at the appropriate time.
*
* @since 6.7.0
* @var bool
*/
private $a11y_available = false;
Copy link
Member Author

Choose a reason for hiding this comment

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

I don't love this, but as a private property I'm not too worried. I wanted to avoid iterating over the enqueued modules and the dependency graph repeatedly and this property allowed for pre-existing iteration to be used.

Perhaps when https://core.trac.wordpress.org/ticket/60597 is addressed a method like ::is_available( '@wordpress/a11y' ) could be implemented to know whether the module will be "available" (enqueued or in the import map).

@sirreal
Copy link
Member Author

sirreal commented Sep 24, 2024

@gziolo @DAreRodz @michalczaplinski this is ready for review.

It can't be tested with #7304 at this time unless interactivity-router the package is modified in node_modules to modify this check.

sirreal added a commit to WordPress/gutenberg that referenced this pull request Sep 24, 2024
This code is needed for compatibility with WordPress < 6.7.
The filter should only be registered if the Core filter will not run,
otherwise redundant elements are printed.

See:

WordPress/wordpress-develop#7405
@michalczaplinski michalczaplinski self-requested a review September 25, 2024 13:31
Copy link
Contributor

@michalczaplinski michalczaplinski left a comment

Choose a reason for hiding this comment

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

This looks good to me! 👍

I've also tested it with a simple custom block using the @wordpress/a11y module:

view.js of my custom block
import { store, getContext } from "@wordpress/interactivity";

store("create-block", {
  actions: {
    toggle: () => {
      const context = getContext();
      context.isOpen = !context.isOpen;
    },
  },
  callbacks: {
    logIsOpen: function* () {
      const { isOpen } = getContext();

      const { speak } = yield import("@wordpress/a11y");

      console.log("isOpen", isOpen);

      if (isOpen) {
        speak("toggle open", "assertive");
      } else {
        speak("toggle closed", "assertive");
      }
    },
  },
});
output_2278ef.mp4

@michalczaplinski
Copy link
Contributor

michalczaplinski commented Sep 25, 2024

@sirreal sirreal deleted the add/a11y-script-module branch September 25, 2024 18:19
sirreal added a commit to WordPress/gutenberg that referenced this pull request Sep 25, 2024
This code is needed for compatibility with WordPress < 6.7.
The filter should only be registered if the Core filter will not run,
otherwise redundant elements are printed.

See:

WordPress/wordpress-develop#7405
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants