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

Keyboard nav: nav menu number shortcuts #12226

Merged
merged 4 commits into from
Mar 26, 2024
Merged

Keyboard nav: nav menu number shortcuts #12226

merged 4 commits into from
Mar 26, 2024

Conversation

wackerow
Copy link
Member

@wackerow wackerow commented Feb 18, 2024

Description

  • Adds keyboard shortcuts for numbers 1-5 to move focus to corresponding nav menu section button; enter to open
  • Guards against cases where cursor is in a textarea, input or select

Summary by CodeRabbit

  • New Features
    • Enhanced keyboard navigation in the main menu by focusing on sections when number keys are pressed.
  • Refactor
    • Improved the structure and organization of constants related to navigation and styling in the codebase.

@github-actions github-actions bot added review needed 👀 tooling 🔧 Changes related to tooling of the project translation 🌍 This is related to our Translation Program labels Feb 18, 2024
Copy link

netlify bot commented Feb 18, 2024

Deploy Preview for ethereumorg ready!

Name Link
🔨 Latest commit 6c616c7
🔍 Latest deploy log https://app.netlify.com/sites/ethereumorg/deploys/66022253a65ec20008b827c1
😎 Deploy Preview https://deploy-preview-12226--ethereumorg.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.

Base automatically changed from nav-menu-radix-ui to dev February 23, 2024 19:30
@corwintines corwintines self-assigned this Feb 26, 2024
Copy link
Contributor

coderabbitai bot commented Mar 9, 2024

Walkthrough

The recent update enhances the navigation experience in a web application by introducing keyboard navigation capabilities and refining constants related to navigation styling. It specifically adds the ability to focus on navigation sections through number keys, incorporates a new constant for the main navigation ID, and adjusts existing constants for better organization and styling consistency.

Changes

Files Change Summary
src/components/Nav/Menu/index.tsx Added MAIN_NAV_ID to imports and assigned it to the id prop of UnorderedList.
src/components/Nav/Menu/useNavMenu.ts Implemented keyboard navigation by focusing on sections based on number keys pressed.
src/lib/constants.ts Introduced MAIN_NAV_ID, reordered, and updated navigation and styling constants.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

Note: Auto-reply has been disabled for this repository by the repository owner. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 299715d and 1a9d5f0.
Files selected for processing (3)
  • src/components/Nav/Menu/index.tsx (2 hunks)
  • src/components/Nav/Menu/useNavMenu.ts (2 hunks)
  • src/lib/constants.ts (2 hunks)
Additional comments: 2
src/lib/constants.ts (1)
  • 79-83: The introduction of MAIN_NAV_ID and adjustments to other constants like NAV_BAR_PX_HEIGHT and NAV_PY are well-aligned with the PR's objectives to enhance navigation accessibility. A few considerations:
  1. Consistency: Ensure that the new and adjusted constants are consistently used across the application. This includes checking that MAIN_NAV_ID is correctly applied wherever the main navigation is referenced.

  2. Documentation: While the constants are self-explanatory to an extent, adding comments to describe their purpose, especially for the newly introduced ones, can improve maintainability and understanding for future contributors.

  3. Impact on Styling: The movement and duplication of NAV_PY might have implications on the styling consistency across the site. Verify that this change does not introduce unintended styling discrepancies.

Consider adding comments for new constants and verifying the consistent use and impact of these changes across the application.

src/components/Nav/Menu/index.tsx (1)
  • 38-43: The integration of MAIN_NAV_ID into the Menu component by assigning it to the id prop of UnorderedList is a crucial step towards enabling keyboard navigation. This change is straightforward and aligns with the PR's objectives. However, consider the following:
  1. Accessibility: Ensure that the use of id for keyboard navigation does not inadvertently affect the accessibility of the navigation menu. It's important that the navigation structure remains semantically correct and accessible to screen readers and other assistive technologies.

  2. Styling Implications: Verify that the addition of the id attribute does not conflict with existing CSS selectors or JavaScript logic that might be targeting the UnorderedList based on its attributes.

Ensure that the addition of MAIN_NAV_ID maintains the accessibility and does not introduce styling or functional conflicts.

Comment on lines +18 to +34
useEventListener("keydown", (event) => {
if (!document || !event.key.match(/[1-9]/)) return
if (event.target instanceof HTMLInputElement) return
if (event.target instanceof HTMLTextAreaElement) return
if (event.target instanceof HTMLSelectElement) return

const sectionIdx = parseInt(event.key) - 1
if (sectionIdx >= SECTION_LABELS.length) return

const button = document.querySelector(
`#${MAIN_NAV_ID} li:nth-of-type(${sectionIdx + 1}) button`
)
if (!button) return

event.preventDefault()
;(button as HTMLButtonElement).focus()
})
Copy link
Contributor

Choose a reason for hiding this comment

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

The implementation of keyboard navigation within useNavMenu is well thought out, ensuring that shortcuts do not interfere with text entry in form elements. However, there are a few areas for improvement and clarification:

  1. Performance Consideration: Every keydown event triggers checks against the document and event target, which might not be necessary after the initial setup. Consider optimizing this by setting up conditions or states that minimize the number of checks needed after the hook is initialized.

  2. Extensibility: The current implementation is tightly coupled with the assumption that there are exactly or less than SECTION_LABELS.length sections. If the navigation were to expand, this logic might need revisiting. Consider making the logic more dynamic to accommodate any number of sections without requiring adjustments.

  3. Accessibility: While the focus management is a great step towards improving accessibility, ensure that there is also proper ARIA attribute management to communicate the state and role of the navigation elements to assistive technologies.

  4. Error Handling: The current implementation silently fails (returns) if conditions are not met. While this is acceptable for control flow, consider logging unexpected states during development for easier debugging.

Consider these improvements to enhance performance, extensibility, and accessibility.

Copy link
Member

@pettinarip pettinarip left a comment

Choose a reason for hiding this comment

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

Handy shortcut, works really well 👍🏼

const sectionIdx = parseInt(event.key) - 1
if (sectionIdx >= SECTION_LABELS.length) return

const button = document.querySelector(
Copy link
Member

Choose a reason for hiding this comment

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

One observation: this is not a very react way to do this. We already know all the buttons that are in the menu and we could easily have the reference of each one. This would make all the checking you have to do with this approach a lot easier.

Not a blocker as that works fine too. Just to see what you think, maybe I'm missing something important.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I agree with the overall sentiment... I can look at this again, but I did try this is the initial approach... I forget exactly why at the moment, but I hit some issues passing the refs around and getting the correct ones to trigger correctly... I recall it ended up having a lot more code than this, since this approach just takes advantage of the nth-of-type selector. Happy to check this out again, but wanted to just drop a reply.

@wackerow wackerow merged commit d456652 into dev Mar 26, 2024
6 of 7 checks passed
@wackerow wackerow deleted the menu-number-shortcut branch March 26, 2024 01:18
This was referenced Mar 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tooling 🔧 Changes related to tooling of the project translation 🌍 This is related to our Translation Program
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants