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

Improve accessibility of the Warning component in the block editor #67433

Open
wants to merge 4 commits into
base: trunk
Choose a base branch
from

Conversation

sarthaknagoshe2002
Copy link
Contributor

@sarthaknagoshe2002 sarthaknagoshe2002 commented Nov 29, 2024

Accessibility Fix: #62737

What?

This PR improves the Warning component in the block editor by enhancing its accessibility.

Why?

Currently, the Warning component only provides a visual cue without notifying screen readers, which limits accessibility. These changes ensure that the component is accessible to all users.

How?

  • Added ARIA role="alert" and aria-live="assertive" to the Warning component for screen reader announcements.
  • Added aria-describedby="block-editor-warning-message" to the Warning actions.
  • Added tabIndex="0", so that the Warning can be accessed using tabs. This helps to navigate inside the warning.

Testing Instructions

  1. Add a More block to a post and copy it using the keyboard shortcut or the Options menu.
  2. Paste the copied block elsewhere in the post.
  3. Observe the Warning message appears with improved design and triggers a screen reader announcement.

Copy link

github-actions bot commented Nov 29, 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.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: sarthaknagoshe2002 <[email protected]>
Co-authored-by: afercia <[email protected]>

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

@t-hamano t-hamano added [Type] Bug An existing feature does not function as intended [Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes). [Package] Blocks /packages/blocks [Package] Block editor /packages/block-editor and removed [Package] Blocks /packages/blocks labels Nov 30, 2024
@afercia
Copy link
Contributor

afercia commented Dec 17, 2024

@sarthaknagoshe2002 thanks for your PR.

Good catch for adding the tabindex="0" which makes possible to navigate into the Warning with the keyboard.

Unfortunately, testing with Safari and VoiceOver the Warning isn't announced. I guess because the editor sets focus on the newly inserted block. See in the screenshot below: focus is on the doubled 'More' block:

Screenshot 2024-12-17 at 09 07 55

I think a potential way to fix this would be setting focus on the Warning when it renders by using useEffect and useRef.

I'll leave a few comments in the review as well.
Worth noting there's a merge conflict because it appears this branch doesn't incorporate recent changes to the Warning component introduced in #67675

<div
className={ clsx( className, 'block-editor-warning' ) }
role="alert"
aria-live="assertive"
Copy link
Contributor

Choose a reason for hiding this comment

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

role alert inherently works as an assertive live region so that aria-live="assertive" isn't necessary. aria-live doesn;t work well for elements that appear in the DOM anyways.

<div className="block-editor-warning__actions">
<div
className="block-editor-warning__actions"
aria-describedby="block-editor-warning-message"
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure this aria-describedby is necessary. It doesn't point to any ID anyways, please remove it.

Copy link
Contributor

@afercia afercia left a comment

Choose a reason for hiding this comment

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

Left some comments

@sarthaknagoshe2002
Copy link
Contributor Author

I think a potential way to fix this would be setting focus on the Warning when it renders by using useEffect and useRef.

It seems to work fine in Chrome on macOS using the default screen reader. I also tested it on Safari, and it’s a bit buggy—I noticed it works intermittently. I’m currently investigating the issue and exploring possible solutions.

@sarthaknagoshe2002
Copy link
Contributor Author

sarthaknagoshe2002 commented Dec 17, 2024

@afercia I have now fixed things to make it work in safari.
Also, I have implemented the changes suggested by you.

Chrome Safari
Screenshot 2024-12-17 at 11 04 03 PM Screenshot 2024-12-17 at 11 42 21 PM

Let me know this this requires any more changes. If not then I'll update the tests too. 😇

@sarthaknagoshe2002
Copy link
Contributor Author

Also, the git hook seems to have modified the linting of some tsconfig.json file.
let me know if we should keep those changes too.

if ( null !== alertRef.current ) {
alertRef.current.focus();
}
}, [] );
Copy link
Contributor

Choose a reason for hiding this comment

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

Question: can this be simplified? I'm not sure there's the need to set the initial ref to null and then checking for it. To my understanding the optional chaining operator ?. would make this much more concise. See an example here:

const dropdownPopoverRef = useRef();
useEffect( () => {
dropdownPopoverRef.current?.focus();
}, [ trackBeingEdited ] );

@@ -8,13 +8,13 @@
},
"include": [ "src/**/*" ],
"references": [
{ "path": "../api-fetch" },
Copy link
Contributor

Choose a reason for hiding this comment

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

changes to this file should be reverted, probably a result of merge conflict or something

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The pre-commit Git hook is enforcing linting even after reverting changes. Are there alternative solutions?

Copy link
Contributor

Choose a reason for hiding this comment

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

I submitted a quick PR #68083 to fix it separately.
Once that is merged, please rebase this PR.

<p className="block-editor-warning__message">
<p
className="block-editor-warning__message"
ref={ alertRef }
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the reg should be set on the container with tabIndex="0", otherwise moving focus will fail?

@afercia
Copy link
Contributor

afercia commented Dec 18, 2024

let me know if we should keep those changes too.

@sarthaknagoshe2002 yes I think those changes should be reverted.
Thanks for working on this, I left some new comments.

As a last step, when everything is done, I guess the snapshot of the failing test should be updated.

npm run test:unit -- --updateSnapshot --testPathPattern packages/block-editor/src/components/warning/test/index.js

@afercia
Copy link
Contributor

afercia commented Dec 18, 2024

It's worth noting that all Warnings will receive focus when they render, for example the 'invalid content' Warning and other ones. There may be edge cases but I think it's OK as default behavior.

Screenshot 2024-12-18 at 09 19 18

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes). [Package] Block editor /packages/block-editor [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve the Block editor warning accessibility and design
3 participants