Skip to content

fix: clear search navbar input only when focused on esc key event#39464

Open
Anushka203-del wants to merge 1 commit intoRocketChat:developfrom
Anushka203-del:fix/clear-search-navbar-only-when-focused-on-escape
Open

fix: clear search navbar input only when focused on esc key event#39464
Anushka203-del wants to merge 1 commit intoRocketChat:developfrom
Anushka203-del:fix/clear-search-navbar-only-when-focused-on-escape

Conversation

@Anushka203-del
Copy link

@Anushka203-del Anushka203-del commented Mar 9, 2026

fix: Prevent global Escape key press from unexpectedly clearing search

Proposed changes (including videos or screenshots)

The Problem:
Currently, the Escape key listener within NavBarSearch.tsx is attached globally to the window object using tinykeys. This causes an issue where pressing Escape anywhere in the application triggers the handleEscSearch() function, clearing and resetting the search even when the search input is not focused or the search dropdown list is completely closed.
The Fix:
This PR modifies the event handler to verify whether the search component is currently active before taking action. It verifies that the search input is focused (document.activeElement === triggerRef.current) or the search dropdown is visibly open (state.isOpen). The event.preventDefault() and handleEscSearch() functions are now appropriately restricted to execute only when the user is actively interacting with the search.
https://github.com/user-attachments/assets/b46069ec-7b9a-4749-9f39-24926a1340e1

Issue(s)

Closes #39462

Steps to test or reproduce

  1. Open the application and click away from the Navbar search input (ensure it is not focused).
  2. Press the Escape key.
  3. Observe that the search state is no longer unintentionally cleared or modified.
  4. Now, click on the search input to focus it, or type something to open the search dropdown.
  5. Press the Escape key.
  6. Verify that the search input correctly resets and the dropdown closes as intended.

Further comments

This change preserves the intended accessibility shortcuts of NavBarSearch without unintentionally interfering with other components that may rely on Escape key functionality globally.

Summary by CodeRabbit

  • Bug Fixes
    • Improved Escape key handling in search navigation — the key now only closes search when the overlay is open or focus is on the search trigger, preventing unintended closures.

@Anushka203-del Anushka203-del requested a review from a team as a code owner March 9, 2026 07:44
@dionisio-bot
Copy link
Contributor

dionisio-bot bot commented Mar 9, 2026

Looks like this PR is not ready to merge, because of the following issues:

  • This PR is missing the 'stat: QA assured' label
  • This PR is missing the required milestone or project

Please fix the issues and try again

If you have any trouble, please check the PR guidelines

@changeset-bot
Copy link

changeset-bot bot commented Mar 9, 2026

⚠️ No Changeset found

Latest commit: 0a86264

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

@CLAassistant
Copy link

CLAassistant commented Mar 9, 2026

CLA assistant check
All committers have signed the CLA.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 9, 2026

Walkthrough

The Escape key handling in the NavBarSearch component has been refined to only trigger the search close logic when the overlay is open or focus is on the trigger element, replacing the previous behavior of always preventing default and invoking close logic.

Changes

Cohort / File(s) Summary
NavBar Search Esc Key Handling
apps/meteor/client/navbar/NavBarSearch/NavBarSearch.tsx
Modified Escape key event handler to conditionally trigger close logic based on overlay state and focus, improving targeted keyboard event behavior.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~5 minutes

Suggested labels

type: bug

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The PR changes to NavBarSearch.tsx are unrelated to the linked issue #39461, which requires adding deprecation warnings for anonymous read/write settings. The PR modifies search navbar Escape key behavior, but the linked issue requires deprecation warnings for anonymous features. Either relink to the correct issue or ensure changes align with #39461.
Out of Scope Changes check ⚠️ Warning The PR modifies NavBarSearch.tsx to improve Escape key handling, but this is unrelated to the stated objective of adding deprecation warnings for anonymous read/write settings in #39461. The search navbar changes appear to be out-of-scope for the linked deprecation warning issue. Verify the correct issue is linked or confirm these changes are intentional.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The PR title accurately describes the main change: improving Escape key handling in the search navbar to only clear input when focused, matching the code modification summary.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

No issues found across 1 file


Since this is your first cubic review, here's how it works:

  • cubic automatically reviews your code and comments on bugs and improvements
  • Teach cubic by replying to its comments. cubic learns from your replies and gets better over time
  • Add one-off context when rerunning by tagging @cubic-dev-ai with guidance or docs links (including llms.txt)
  • Ask questions if you need clarification on any suggestion

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.

🧹 Nitpick comments (1)
apps/meteor/client/navbar/NavBarSearch/NavBarSearch.tsx (1)

64-68: Logic is sound, but consider adding state to the dependency array for clarity.

The condition correctly restricts Escape handling to when the overlay is open or the input is focused. The dependency on state is implicitly maintained via handleEscSearch, but adding state directly to line 75's dependency array would make this relationship explicit and avoid subtle stale-closure risks if handleEscSearch is refactored.

♻️ Suggested improvement
-	}, [focusManager, handleEscSearch, setFocus]);
+	}, [focusManager, handleEscSearch, setFocus, state]);
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@apps/meteor/client/navbar/NavBarSearch/NavBarSearch.tsx` around lines 64 -
68, The Escape key handler references `state` (and `triggerRef`) but the effect
that registers this handler only lists `handleEscSearch` in its dependency
array; update the effect that adds the 'Escape' keyboard binding in NavBarSearch
to include `state` in its dependency array so the closure sees the latest
`state` values (this is the same effect that uses the 'Escape' mapping and calls
`handleEscSearch`), keeping `handleEscSearch` and `triggerRef` as-is to avoid
stale-closure bugs if `handleEscSearch` is refactored.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@apps/meteor/client/navbar/NavBarSearch/NavBarSearch.tsx`:
- Around line 64-68: The Escape key handler references `state` (and
`triggerRef`) but the effect that registers this handler only lists
`handleEscSearch` in its dependency array; update the effect that adds the
'Escape' keyboard binding in NavBarSearch to include `state` in its dependency
array so the closure sees the latest `state` values (this is the same effect
that uses the 'Escape' mapping and calls `handleEscSearch`), keeping
`handleEscSearch` and `triggerRef` as-is to avoid stale-closure bugs if
`handleEscSearch` is refactored.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 1c0d7e5c-96b5-46de-a292-8a5b9e64be5b

📥 Commits

Reviewing files that changed from the base of the PR and between 4f43a85 and 0a86264.

📒 Files selected for processing (1)
  • apps/meteor/client/navbar/NavBarSearch/NavBarSearch.tsx
📜 Review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: cubic · AI code reviewer
🧰 Additional context used
📓 Path-based instructions (1)
**/*.{ts,tsx,js}

📄 CodeRabbit inference engine (.cursor/rules/playwright.mdc)

**/*.{ts,tsx,js}: Write concise, technical TypeScript/JavaScript with accurate typing in Playwright tests
Avoid code comments in the implementation

Files:

  • apps/meteor/client/navbar/NavBarSearch/NavBarSearch.tsx
🧠 Learnings (1)
📓 Common learnings
Learnt from: ggazzo
Repo: RocketChat/Rocket.Chat PR: 35995
File: apps/meteor/app/api/server/v1/rooms.ts:1107-1112
Timestamp: 2026-02-23T17:53:18.785Z
Learning: In Rocket.Chat PR reviews, maintain strict scope boundaries—when a PR is focused on a specific endpoint (e.g., rooms.favorite), avoid reviewing or suggesting changes to other endpoints that were incidentally refactored (e.g., rooms.invite) unless explicitly requested by maintainers.
🔇 Additional comments (1)
apps/meteor/client/navbar/NavBarSearch/NavBarSearch.tsx (1)

64-68: PR objectives mismatch: linked issue #39461 appears unrelated.

The linked issue discusses deprecation warnings for anonymous read/write settings (Accounts_AllowAnonymousRead, Accounts_AllowAnonymousWrite), while this PR modifies Escape key handling in the search navbar. Please verify the correct issue is linked.

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.

Global Escape key resets Navbar search even when search input is not active

2 participants