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

fix(left-nav): fix focus trapping #11842

Merged

Conversation

andy-blum
Copy link
Member

@andy-blum andy-blum commented May 31, 2024

Related Ticket(s)

https://jsw.ibm.com/browse/ADCMS-5191

Description

Fixes the focus trapping on the left-nav component

Changelog

Changed

  • Fixes the focus trapping on the left-nav component

Testing Instructions

  • Open the masthead story
  • Reduce viewport width to get the mobile version of the nav
  • Open the left-nav with the keyboard
  • Ensure focus moves to the first item in the left-nav on open
  • Ensure focus is trapped in the left-nav, cycling between the close button & last link/button element in both directions.

@andy-blum andy-blum requested a review from a team as a code owner May 31, 2024 13:39
@andy-blum andy-blum requested review from IgnacioBecerra and sangeethababu9223 and removed request for a team May 31, 2024 13:39
) as HTMLElement
)?.focus();
}
}
Copy link
Member Author

Choose a reason for hiding this comment

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

While we change the value of this.expanded here, we then immediately wait for the resolution of this.update from the component's update/render cycle.

this.update is already resolved from the last run of the component lifecycle and our current function hasn't been off the main thread to let the component start a new update.

Since we're awaiting a resolved promise, we immediate move on to try to focus on elements that are not yet in the DOM.

@ibmdotcom-bot
Copy link
Contributor

ibmdotcom-bot commented May 31, 2024

this.querySelector(
(this.constructor as typeof CDSSideNav).selectorNavItems
) as HTMLElement
)?.focus();
Copy link
Member Author

Choose a reason for hiding this comment

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

Moving the logic out of the event listener and into the update lifecycle ensures we focus after the seletorNavItems are added to the DOM.

Comment on lines 226 to 233
await Promise.all([
import('./left-nav-name'),
import('./left-nav-menu'),
import('./left-nav-menu-section'),
import('./left-nav-menu-item'),
import('./left-nav-menu-category-heading'),
import('./left-nav-overlay')
]);
Copy link
Member Author

Choose a reason for hiding this comment

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

We don't want to await each of these in series, we should see a slight perf boost by awaiting in parallel.

import('./left-nav-menu-category-heading');
import('./left-nav-overlay');
this._importedSideNav = true;
}
Copy link
Member Author

Choose a reason for hiding this comment

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

super.updated(), called at the top of this function, is where we try to set focus. We have to make sure all our components are loaded before that, so this section is moved to willUpdate() to prioritize loading in the components we need.

@andy-blum andy-blum requested review from m4olivei and jkaeser May 31, 2024 13:46
@andy-blum andy-blum changed the title Fix/masthead focus trap v2 fix(left-nav): fix focus trapping May 31, 2024
Copy link
Member

@jkaeser jkaeser left a comment

Choose a reason for hiding this comment

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

Code changes are looking good, but CI and deploy previews are failing.

@ibmdotcom-bot
Copy link
Contributor

ibmdotcom-bot commented May 31, 2024

@ibmdotcom-bot
Copy link
Contributor

ibmdotcom-bot commented May 31, 2024

@ibmdotcom-bot
Copy link
Contributor

ibmdotcom-bot commented May 31, 2024

Copy link
Member

@jkaeser jkaeser left a comment

Choose a reason for hiding this comment

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

Works beautifully! +1 from me

@andy-blum
Copy link
Member Author

This is on hold pending a successful test of #11839's deploy preview CDN on ibm.com

@andy-blum andy-blum added the test: CDN preview used for generating @carbon/ibmdotcom-web-components CDN for testing label Jun 6, 2024
@ibmdotcom-bot
Copy link
Contributor

ibmdotcom-bot commented Jun 6, 2024

Deploy preview created for package IBM.com Web Components Deploy Preview CDN:
https://ibmdotcom-cdn.s3.us-east.cloud-object-storage.appdomain.cloud/deploy-previews/11842/index.html

Built with commit: ab93ccadbc7c75ccf5ec43e87736da1ddc012400

@andy-blum
Copy link
Member Author

This is ready now. I tested successfully by doing a chrome devtools override and replacing dds-masthead-container with c4d-masthead-container and using the CDN deploy preview.

@kennylam kennylam added the Ready to merge Label for the pull requests that are ready to merge label Jun 10, 2024
@kodiakhq kodiakhq bot merged commit 5f824ec into carbon-design-system:main Jun 10, 2024
18 of 21 checks passed
Copy link
Contributor

Hey there! This issue/pull request was referenced in recently released v2.11.0.

1 similar comment
Copy link
Contributor

Hey there! This issue/pull request was referenced in recently released v2.11.0.

annawen1 pushed a commit to annawen1/carbon-for-ibm-dotcom that referenced this pull request Jul 24, 2024
https://jsw.ibm.com/browse/ADCMS-5191

Fixes the focus trapping on the left-nav component

**Changed**

- Fixes the focus trapping on the left-nav component

- Open the masthead story
- Reduce viewport width to get the mobile version of the nav
- Open the left-nav with the keyboard
- Ensure focus moves to the first item in the left-nav on open
- Ensure focus is trapped in the left-nav, cycling between the close button & last link/button element in both directions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ready to merge Label for the pull requests that are ready to merge test: CDN preview used for generating @carbon/ibmdotcom-web-components CDN for testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants