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

Navigation: Add ariaLabel block support #54418

Merged
merged 3 commits into from
Sep 18, 2023

Conversation

carolinan
Copy link
Contributor

What?

This PR enables the ariaLabel block support for the navigation block. This allows developers to manually add an aria-label to the <nav> element when they are using placeholder links and not an existing menu.

Why?

In Twenty Twenty-Four, the footer design has a 3 column menu, each column uses its own navigation block.
Each navigation block has placeholder links in the markup. To improve the accessibility, the three <nav> elements need to be differentiated and identifiable: they need an aria-label.

See WordPress/twentytwentyfour#345

How?

Enables the ariaLabel block support in block.json.
Adds a condition that populates a variable in the index.php file with the ariaLabel attribute, if it is set.

This allows developers to include the aria-label in the following format:

"ariaLabel":"Custom label here"

<!-- wp:navigation {"overlayMenu":"never","layout":{"type":"flex","orientation":"vertical"},"style":{"typography":{"fontStyle":"normal","fontWeight":"400"}},"fontSize":"small","ariaLabel":"Custom label here"} -->

<!-- wp:navigation-link {"label":"Team","url":"#"} /-->
<!-- wp:navigation-link {"label":"History","url":"#"} /-->
<!-- wp:navigation-link {"label":"Careers","url":"#"} /-->

<!-- /wp:navigation -->

And when the user updates the navigation block to use a menu the correct aria-label, the menu name, will be used because it will override the manually entered value.

Testing Instructions

Please add the example code above to your theme. Either to a block theme template,
or copy and paste the code into the code editor in a post or page.
View the navigation on the front of the website, view the HTML source, and confirm if the label is printed correctly.

Next, please test that the aria-label still works correctly for a regular navigation block with a menu assigned to it:
Add a new navigation block, create a menu, note the name of the menu in the Advanced panel, save, and view the navigation on the front. View the HTML source, and confirm if the label is printed correctly.

@carolinan carolinan added [Type] Enhancement A suggestion for improvement. [Block] Navigation Affects the Navigation Block labels Sep 13, 2023
@carolinan carolinan marked this pull request as ready for review September 13, 2023 10:52
@carolinan carolinan added [Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes). and removed [Type] Enhancement A suggestion for improvement. labels Sep 13, 2023
Copy link
Contributor

@alexstine alexstine left a comment

Choose a reason for hiding this comment

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

@carolinan This works great up until you add a standard navigation block. Once that happens, the custom aria-label goes away. Not sure why that is happening.

Copy link
Contributor

@alexstine alexstine left a comment

Choose a reason for hiding this comment

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

Sorry, I somehow managed to mess up the block code in testing. This works with a custom ariaLabel block attribute and it also works along side a navigation block with a menu pulled from Core.

Tagging @getdave so he is aware.

I think this looks good.

@alexstine alexstine requested a review from getdave September 15, 2023 20:07
Copy link
Member

@aristath aristath left a comment

Choose a reason for hiding this comment

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

This works as expected and the code is simple.
I just added a minor note, but it's not a blocker - just personal preference for code readability.

packages/block-library/src/navigation/index.php Outdated Show resolved Hide resolved
@aristath aristath enabled auto-merge (squash) September 18, 2023 12:02
@aristath aristath merged commit a854ab5 into trunk Sep 18, 2023
49 checks passed
@aristath aristath deleted the update/navigation-block-aria-label branch September 18, 2023 13:34
@github-actions github-actions bot added this to the Gutenberg 16.7 milestone Sep 18, 2023
@alexstine
Copy link
Contributor

@aristath Nice suggestion. Didn't know ?? was supported in PHP.

@aristath
Copy link
Member

@alexstine yeah, it was added in PHP 7, and since that is now the lowest version of PHP that WP supports, we can safely use it in the project 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Navigation Affects the Navigation Block [Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes).
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants