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(docs): resolve keyboard navigation issues on Actions menu #4528

Open
wants to merge 1 commit into
base: canary
Choose a base branch
from

Conversation

mohammadshahidbeigh
Copy link

@mohammadshahidbeigh mohammadshahidbeigh commented Jan 10, 2025

📝 Description

This pull request addresses accessibility issues related to keyboard navigation in the Actions menu and preview buttons. It implements functionality to close the Actions menu using the Escape key and improves focus management.

⛳️ Current behavior (updates)

  • The Actions menu does not close when pressing the Escape key.
  • Keyboard navigation is not intuitive, requiring users to press down to find focus.

🚀 New behavior

  • The Actions menu can now be closed by pressing the Escape key.
  • A close button has been added for better usability.
  • Improved focus management ensures that keyboard navigation is seamless.

💣 Is this a breaking change (Yes/No):

No

📝 Additional Information

This update enhances the accessibility of the NextUI components, making them more user-friendly for keyboard and screen reader users. It resolves issue #4459.

Summary by CodeRabbit

  • New Features

    • Added menu visibility control with keyboard and manual close options
    • Improved component accessibility with semantic attributes and close button
  • Accessibility

    • Implemented keyboard interaction for menu dismissal
    • Added ARIA attributes to enhance screen reader support

Copy link

changeset-bot bot commented Jan 10, 2025

⚠️ No Changeset found

Latest commit: 3344809

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

vercel bot commented Jan 10, 2025

@mohammadshahidbeigh is attempting to deploy a commit to the NextUI Inc Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Contributor

coderabbitai bot commented Jan 10, 2025

Walkthrough

The pull request modifies the WindowActions component in the documentation application, introducing enhanced state management and keyboard interaction capabilities. The changes focus on implementing a dynamic menu visibility feature, allowing users to control the component's display through keyboard events and a manual close button. The implementation adds state tracking, event listeners for the "Escape" key, and improves accessibility by adding semantic attributes and a close mechanism.

Changes

File Change Summary
apps/docs/components/code-window/window-actions.tsx - Added isMenuVisible state variable
- Implemented handleKeyDown function with useCallback
- Added useEffect for keyboard event listener
- Modified rendering to conditionally show/hide component
- Added accessibility attributes (aria-hidden, role)
- Introduced manual close button

Sequence Diagram

sequenceDiagram
    participant User
    participant WindowActions
    participant KeyboardEvent

    User->>WindowActions: Render component
    WindowActions-->>User: Display menu
    User->>KeyboardEvent: Press Escape key
    KeyboardEvent->>WindowActions: Trigger handleKeyDown
    WindowActions->>WindowActions: Set isMenuVisible to false
    WindowActions-->>User: Hide menu
Loading

Possibly related PRs

Suggested reviewers

  • jrgarciadev

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:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • 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 testing code 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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 using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

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.

Actionable comments posted: 3

🧹 Nitpick comments (2)
apps/docs/components/code-window/window-actions.tsx (2)

30-36: Prevent event listener interference

The global keydown listener might interfere with other modal dialogs or dropdowns. Consider:

  1. Adding a check if the menu is visible before handling the escape key
  2. Using React's onKeyDown prop instead of a global listener
-useEffect(() => {
-  window.addEventListener("keydown", handleKeyDown);
-
-  return () => {
-    window.removeEventListener("keydown", handleKeyDown);
-  };
-}, [handleKeyDown]);

And update the component's div to handle keyboard events directly:

 <div
   aria-hidden={!isMenuVisible}
   aria-labelledby="window-actions-title"
+  onKeyDown={isMenuVisible ? handleKeyDown : undefined}
   className={clsx(

38-38: Improve visibility transitions

Returning null causes abrupt DOM removal which might be jarring for users. Consider:

  1. Using CSS transitions for smooth show/hide
  2. Keeping the element in DOM but visually hidden for better accessibility
-if (!isMenuVisible) return null;
+const visibility = isMenuVisible ? "visible" : "hidden";

And update the component's className:

 className={clsx(
   "flex items-center sticky top-0 left-0 px-4 z-10 justify-between h-8 bg-code-background w-full",
+  visibility === "hidden" && "invisible opacity-0",
+  "transition-opacity duration-200",
   className,
 )}
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between eada8cb and 3344809.

📒 Files selected for processing (1)
  • apps/docs/components/code-window/window-actions.tsx (2 hunks)

Comment on lines +59 to +63
<div className="flex basis-1/3">
<button className="ml-auto text-xs text-white/50" onClick={() => setIsMenuVisible(false)}>
Close
</button>
</div>
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Improve close button accessibility and UX

The close button needs the following improvements:

  1. Add aria-label for screen readers
  2. Add keyboard shortcut hint using aria-keyshortcuts
  3. Enhance button styling for better visibility
 <div className="flex basis-1/3">
   <button
+    aria-label="Close window actions menu"
+    aria-keyshortcuts="Escape"
-    className="ml-auto text-xs text-white/50"
+    className="ml-auto text-xs text-white/50 hover:text-white/70 focus:text-white/70 focus:outline-none focus:ring-2 focus:ring-white/20 rounded px-2 py-1"
     onClick={() => setIsMenuVisible(false)}
   >
     Close
   </button>
 </div>
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
<div className="flex basis-1/3">
<button className="ml-auto text-xs text-white/50" onClick={() => setIsMenuVisible(false)}>
Close
</button>
</div>
<div className="flex basis-1/3">
<button
aria-label="Close window actions menu"
aria-keyshortcuts="Escape"
className="ml-auto text-xs text-white/50 hover:text-white/70 focus:text-white/70 focus:outline-none focus:ring-2 focus:ring-white/20 rounded px-2 py-1"
onClick={() => setIsMenuVisible(false)}
>
Close
</button>
</div>

Comment on lines +42 to +48
aria-hidden={!isMenuVisible} // Visibility state
aria-labelledby="window-actions-title"
className={clsx(
"flex items-center sticky top-0 left-0 px-4 z-10 justify-between h-8 bg-code-background w-full",
className,
)}
role="dialog" // Semantic role
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Enhance ARIA attributes for better screen reader support

Add the following ARIA attributes to improve accessibility:

  1. aria-modal="true" to indicate modal behavior
  2. aria-describedby for additional context
  3. tabindex="-1" to manage focus properly
 <div
   aria-hidden={!isMenuVisible}
   aria-labelledby="window-actions-title"
+  aria-modal="true"
+  aria-describedby="window-actions-description"
+  tabIndex={-1}
   role="dialog"
   className={clsx(

Committable suggestion skipped: line range outside the PR's diff.

Comment on lines +22 to +28
const [isMenuVisible, setIsMenuVisible] = useState(true);

const handleKeyDown = useCallback((e: KeyboardEvent) => {
if (e.key === "Escape") {
setIsMenuVisible(false); // Close the menu
}
}, []);
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Enhance focus management and initial state handling

Consider the following improvements:

  1. Initialize isMenuVisible to false by default to prevent the menu from being open on mount
  2. Implement focus management to return focus to the trigger element when menu closes
  3. Add a focus trap to keep focus within the menu while it's open
-const [isMenuVisible, setIsMenuVisible] = useState(true);
+const [isMenuVisible, setIsMenuVisible] = useState(false);
+const previousFocusRef = useRef<HTMLElement | null>(null);
+
+const handleClose = useCallback(() => {
+  setIsMenuVisible(false);
+  if (previousFocusRef.current) {
+    previousFocusRef.current.focus();
+  }
+}, []);

 const handleKeyDown = useCallback((e: KeyboardEvent) => {
   if (e.key === "Escape") {
-    setIsMenuVisible(false);
+    handleClose();
   }
 }, []);

Committable suggestion skipped: line range outside the PR's diff.

@wingkwong wingkwong changed the title fix(accessibility): resolve keyboard navigation issues on Actions men… fix(docs): resolve keyboard navigation issues on Actions menu Jan 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] - nextui.org keyboard navigation issues
1 participant