Discovered in #3190120: Olivero: Focused level-2 nav items should always be in viewport during keyboard navigation.

If a dropdown menu is open, and ESC is pressed, focus should return to the dropdown toggle. Instead, it is moved to body, which is not a meaningful movement of focus and violates 2.4.3: Focus Order.

Issue fork drupal-3210443

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

mherchel created an issue. See original summary.

bnjmnm’s picture

Issue summary: View changes
bnjmnm’s picture

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mherchel’s picture

Status: Active » Needs review
StatusFileSize
new2.19 KB

This should be pretty straightforward

mherchel’s picture

StatusFileSize
new2.19 KB
new693 bytes

Minor change in comment :D

gauravvvv’s picture

Now when the dropdown menu is open, and ESC is pressed, the focus should return to the dropdown toggle, not to the body.
I have added an after-patch screen recording for the same.

Patch #6, fixed the issues.

andy-blum’s picture

Status: Needs review » Reviewed & tested by the community

I can confirm the proper esc behavior occurs while signed out. +1 for RTBC.

lauriii’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

It seems like the focus isn't working properly on mobile navigation. I think it would be useful to add tests for both, mobile and desktop use cases.

mherchel’s picture

StatusFileSize
new7.01 MB

I'm still on the fence of this one.

In the summary, @bnjmnm added

If a dropdown menu is open, and ESC is pressed, focus should return to the dropdown toggle. Instead, it is moved to body, which is not a meaningful movement of focus and violates 2.4.3: Focus Order.

However, in practice this is not the case. The body does receive the focus, but when the TAB key is pressed, the focus returns to the next element within the menu. This to me is a "meaningful movement of focus" and does not violate logical focus order.

I'm attaching a video.

bnjmnm’s picture

However, in practice this is not the case. The body does receive the focus, but when the TAB key is pressed, the focus returns to the next element within the menu. This to me is a "meaningful movement of focus" and does not violate logical focus order.

The next item in the menu is not immediately preceded by body in the tabbing order, so this isn't a logical focus order.

body receiving focus is fallback behavior when the currently focused element becomes unavailable. A small change to closeAllSubNav would take care of this:

/**
   * Close all second level sub navigation menus.
   */
  function closeAllSubNav() {
    secondLevelNavMenus.forEach((el) => {
      // Example
      if (el.contains(document.activeElement)) {
        // This would need to target a js- prefixed class or data attribute, but
        // for the purposes of a working example, this presentation class works. 
        el.querySelector('.primary-nav__button-toggle').focus();
      }
      // End example
      toggleSubNav(el, false);
    });
  }
mherchel’s picture

Status: Needs work » Needs review
StatusFileSize
new3.14 KB
new1.92 KB

I like your idea to add the functionality into the closeAllSubNav better than my original implementation.

Patch attached (with tests!)

mherchel’s picture

StatusFileSize
new1.92 KB
new3.14 KB

Gah. Forgot to run prettier. New patches attached.

bnjmnm’s picture

Status: Needs review » Needs work
+++ b/core/themes/olivero/js/second-level-navigation.es6.js
@@ -124,6 +124,12 @@
+        el.querySelector(
+          '.primary-nav__button-toggle, .primary-nav__menu-link--button',
+        ).focus();

The selector needs to be a 'js-' prefixed class or data attribute to distinguish it from classes used for presentation purposes. Otherwise this won't pass the JavaScript selectors core gate. Also see #2431671: [meta] Add in js- prefixed classes for separation of JS & CSS functionality.

mherchel’s picture

Status: Needs work » Needs review

I'd honestly like to tackle that holistically in #3153234: [Meta] Olivero JavaScript should be selecting [data-drupal-selector] attributes where possible. There are a ton of selectors in that file that do not meet that criteria, and I don't think we should refactor them in this issue.

That issue is a stable blocker, so it'll get fixed, but IMO the changes are unrelated to this specific issue.

mitthukumawat’s picture

Patch #13 applied cleanly on drupal 9.3.x-dev version. Thanks for the patch.

andy-blum’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new4.47 MB
new1.74 MB

Escape key properly returns focus to the dropdown-toggling element on wide & mobile versions. Screen recordings attached, moving to RTBC.

lauriii’s picture

Status: Reviewed & tested by the community » Needs work

Let's expand the tests to include mobile menu too ✌️

mherchel’s picture

Status: Needs work » Needs review
StatusFileSize
new6.17 KB
new4.94 KB
new3.6 KB

Done. Patch attached.

mherchel’s picture

StatusFileSize
new4.67 KB
new5.9 KB

Had some invalid comments in the tests from the code that I copied over. Removing these, and re-attaching patches.

mherchel’s picture

Status: Needs review » Needs work

Tests are passing on my local, but failing on Drupal's CI.

The error that we're getting is

[0;31m ✖ oliveroMobileMenuTest[0m
 – [0;90mVerify parent <button> focus on ESC in narrow navigation[0m [1;33m(5.491s)[0m
[0;31m   Timed out while waiting for element <#home-submenu-1> to be visible for 5000 milliseconds. - expected [0;32m"visible"[0m but got: [0;31m"not visible"[0m [0;90m(5118ms)[0m[0m
[0;90m       at Object.Verify parent <button> focus on ESC in narrow navigation (/var/www/html/core/tests/Drupal/Nightwatch/Tests/oliveroMobileMenuTest.js:108:8)
mherchel’s picture

Status: Needs work » Needs review
StatusFileSize
new5.97 KB
new1000 bytes

🤞

mherchel’s picture

StatusFileSize
new5.96 KB
new1.04 KB

🤞🤞

mherchel’s picture

StatusFileSize
new4.74 KB

Yay! #23 is the winner!

Attached is a patch with the tests only, so we can see the failure.

rinku jacob 13’s picture

StatusFileSize
new651.52 KB

patch #23 successfully applied for 9.3.x-dev

andy-blum’s picture

Patch applies and the functionality works, but tabbing through a long dropdown list doesn't automatically scroll the viewport as added in #3190120.

Looking at core's commit history, it looks like that fix was only added to 9.2.x and not the 9.3.x branch. If that's not a problem, then +1 for RTBC

guilhermevp’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new284.72 KB

Patch works as intended. I believe that fix #3190120: Olivero: Focused level-2 nav items should always be in viewport during keyboard navigation. was commited to 9.3.x in 11af82a so I'm not sure if it should be considered RTBC as well.

guilhermevp’s picture

Status: Reviewed & tested by the community » Needs review

Ops, changed it by accident.

andy-blum’s picture

Status: Needs review » Reviewed & tested by the community

Disregard #26. My local was behind, patch applies and works. Moving to RTBC

bnjmnm’s picture

Status: Reviewed & tested by the community » Needs review
Issue tags: -Needs accessibility review, -Needs tests
+++ b/core/tests/Drupal/Nightwatch/Tests/oliveroMobileMenuTest.js
@@ -94,4 +97,50 @@ module.exports = {
+      .click(`[aria-controls="${linkSubMenuId}"]`)
+      .waitForElementVisible(`#${linkSubMenuId}`)
+      .keys(browser.Keys.TAB)
+      .pause(50)
+      .keys(browser.Keys.ESCAPE)
+      .pause(50)

In both tests, between the tab and escape presses, there should be confirmation that focus has moved. If keyboard navigation were somehow broken, this test would still pass as it's checking if the most recently clicked element has focus.

bnjmnm’s picture

Status: Needs review » Needs work
mherchel’s picture

Status: Needs work » Needs review
StatusFileSize
new2.73 KB
new7.28 KB
new6.05 KB

Patches attached!

bnjmnm’s picture

Status: Needs review » Reviewed & tested by the community

That takes care of #30.
The code looks good & straightforward, and this works on manual review with both toggle buttons and links.

  • lauriii committed 7d68989 on 9.3.x
    Issue #3210443 by mherchel, bnjmnm, andy-blum, guilhermevp,...

  • lauriii committed 703f752 on 9.2.x
    Issue #3210443 by mherchel, bnjmnm, andy-blum, guilhermevp,...
lauriii’s picture

Status: Reviewed & tested by the community » Fixed

Committed 7d68989 and pushed to 9.3.x and 9.2.x. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.