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(16992): Prevent Page Scroll During Keyboard Navigation in MenuButton and ComboButton options #16998

Merged
merged 12 commits into from
Jul 19, 2024

Conversation

2nikhiltom
Copy link
Contributor

@2nikhiltom 2nikhiltom commented Jul 19, 2024

Closes #16992

My Initial assumption was that this issue might be related to Floating UI, which we use for positioning. However, after extensive debugging, discovered that the root cause was actually within the Menu component's keyboard navigation logic.

The scrolling issue occurred because the default browser behavior for arrow keys (scrolling the page) was not being prevented when navigating menu items. This led to both the menu focus changing and the page scrolling simultaneously.

e.preventDefault() allows to control the focus movement through menu items without triggering unwanted page scrolling.

Changelog

New
e?.preventDefault() is added to prevent scroll behavior

Changed

Updated focusItem function in Menu

function focusItem(e?: React.KeyboardEvent<HTMLUListElement>) {
    e?.preventDefault();
    const currentItem = focusableItems.findIndex((item) =>
      item.ref?.current?.contains(document.activeElement)
    );

Testing / Reviewing

Go to deploy preview
Open MenuButton and ComboButton components
Expand the menu and use the arrow keys to navigate through the items. verify no scrolls happen unexpectedly
Verify existing functionality of Menu/MenuButton/ComboButton components are intact

@2nikhiltom 2nikhiltom requested a review from a team as a code owner July 19, 2024 09:12
Copy link

netlify bot commented Jul 19, 2024

Deploy Preview for v11-carbon-react ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 699d7fe
🔍 Latest deploy log https://app.netlify.com/sites/v11-carbon-react/deploys/669a2e13937b450008560e6b
😎 Deploy Preview https://deploy-preview-16998--v11-carbon-react.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Jul 19, 2024

Deploy Preview for carbon-elements ready!

Name Link
🔨 Latest commit 699d7fe
🔍 Latest deploy log https://app.netlify.com/sites/carbon-elements/deploys/669a2e13d4e3470008c9757b
😎 Deploy Preview https://deploy-preview-16998--carbon-elements.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Contributor

@riddhybansal riddhybansal left a comment

Choose a reason for hiding this comment

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

Worked for both ComboButton and MenuButton

Copy link
Member

@tay1orjones tay1orjones left a comment

Choose a reason for hiding this comment

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

Nice fix! I tested by adding a div adjacent to the button with width: 150vh to trigger a vertical scroll. Keyboard nav works great with no impact on page scroll. 🚀

@tay1orjones tay1orjones added this pull request to the merge queue Jul 19, 2024
Merged via the queue into carbon-design-system:main with commit b4a89ef Jul 19, 2024
21 of 22 checks passed
2nikhiltom added a commit to 2nikhiltom/carbon that referenced this pull request Jul 22, 2024
…ton and ComboButton options (carbon-design-system#16998)

* fix(15337): fix release names in update automation

* fix(15337): fix release names in update automation

* fix(16992): prevent page scroll keyboard navigation
@2nikhiltom 2nikhiltom mentioned this pull request Jul 22, 2024
2nikhiltom added a commit to 2nikhiltom/carbon that referenced this pull request Jul 22, 2024
…ton and ComboButton options (carbon-design-system#16998)

* fix(15337): fix release names in update automation

* fix(15337): fix release names in update automation

* fix(16992): prevent page scroll keyboard navigation
2nikhiltom added a commit to 2nikhiltom/carbon that referenced this pull request Jul 22, 2024
…ton and ComboButton options (carbon-design-system#16998)

* fix(15337): fix release names in update automation

* fix(15337): fix release names in update automation

* fix(16992): prevent page scroll keyboard navigation
2nikhiltom added a commit to 2nikhiltom/carbon that referenced this pull request Jul 22, 2024
…ton and ComboButton options (carbon-design-system#16998)

* fix(15337): fix release names in update automation

* fix(15337): fix release names in update automation

* fix(16992): prevent page scroll keyboard navigation
riddhybansal pushed a commit to riddhybansal/carbon that referenced this pull request Jul 22, 2024
…ton and ComboButton options (carbon-design-system#16998)

* fix(15337): fix release names in update automation

* fix(15337): fix release names in update automation

* fix(16992): prevent page scroll keyboard navigation
@2nikhiltom 2nikhiltom mentioned this pull request Jul 22, 2024
tay1orjones pushed a commit to tay1orjones/carbon that referenced this pull request Jul 22, 2024
…ton and ComboButton options (carbon-design-system#16998)

* fix(15337): fix release names in update automation

* fix(15337): fix release names in update automation

* fix(16992): prevent page scroll keyboard navigation
@tay1orjones tay1orjones mentioned this pull request Jul 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: MenuButton and ComboButton keyboard navigation causing page scroll
4 participants