Skip to content

Fix: Side Panel composer steals focus back after opening a new chat - #101217

Merged
stitesExpensify merged 5 commits into
Expensify:mainfrom
mukhrr:fix/100601
Sep 21, 2026
Merged

stitesExpensify merged 5 commits into
Expensify:mainfrom
mukhrr:fix/100601

Conversation

@mukhrr

@mukhrr mukhrr commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

Explanation of Change

The Side Panel renders a second composer alongside the main report composer. When a modal closes, both composers run a "refocus after modal close" effect. The Side Panel composer's guard for that effect only checks whether it previously held focus, read at the moment the effect runs, not when the delayed .focus() actually lands 150ms later. If the newly opened chat's composer takes focus in that window, the Side Panel's stale timer still fires afterward and steals it back.

focusComposerWithDelay now accepts an optional predicate, re-evaluated inside the timer right before focusing. Only the Side Panel composer supplies one, re-checking its own focus claim at fire time instead of trusting the value from when focus was scheduled. The main composer's own modal-close refocus is also guarded to yield when the Side Panel still holds that claim, and the claim is cleared when the Side Panel composer unmounts, so it can never outlive the panel that set it.

Fixed Issues

$ #100601
PROPOSAL: #100601 (comment)

Tests

Preconditions: Side Panel (Concierge anywhere) enabled, window wider than 1300px.

  1. Open the Side Panel and click into its composer so it has focus.
  2. Open FAB > Start chat.
  3. Enter a new user's email and select them.
  4. Verify the newly opened chat's composer has focus and typing lands there, not in the Side Panel.
  5. Click into the Side Panel composer again, then press Cmd/Ctrl+K to open search and press Esc to close it.
  6. Verify focus returns to the Side Panel composer.
  7. With the Side Panel composer focused, switch to a different chat from the LHN.
  8. Verify focus lands on that chat's main composer.
  • Verify that no errors appear in the JS console

Offline tests

This is a client-side focus race with no network calls, offline state cannot affect it. The Tests steps above are unaffected by connectivity.

QA Steps

Same as tests.

  • Verify that no errors appear in the JS console

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android: Native
    • Android: mWeb Chrome
    • iOS: Native
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))
  • If new assets were added or existing ones were modified, I verified that:
    • The assets are optimized and compressed (for SVG files, run npm run compress-svg)
    • The assets load correctly across all supported platforms.
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • I added unit tests for any new feature or bug fix in this PR to help automatically prevent regressions in this user flow.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.

Screenshots/Videos

Android: Native web only bug
Android: mWeb Chrome web only bug
iOS: Native web only bug
iOS: mWeb Safari web only bug
MacOS: Chrome / Safari
web.mp4

…cus fires

The Side Panel renders a second ComposerWithSuggestions instance that
runs the same modal-close refocus effect as the main composer. That
effect reads the Side Panel's focus claim (sidePanelComposerRef) when
scheduling a delayed focus, but the actual .focus() call lands 150ms
later against whatever composer holds focus by then. If the main
composer took focus in the meantime, the Side Panel's stale timer
still fires and steals it back.

focusComposerWithDelay now accepts an optional predicate re-evaluated
inside the setTimeout, right before focusing. Only the Side Panel
composer supplies one, so every other caller is unaffected. The main
composer's own modal-close refocus is also guarded to yield when the
Side Panel still holds the claim, and the claim is cleared when the
Side Panel composer unmounts, so the guard cannot outlive the panel
that set it.
@mukhrr
mukhrr requested review from a team as code owners September 15, 2026 14:11
@melvin-bot
melvin-bot Bot requested review from dylanexpensify and ikevin127 and removed request for a team September 15, 2026 14:11
@melvin-bot

melvin-bot Bot commented Sep 15, 2026

Copy link
Copy Markdown

@ikevin127 Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

@codecov

codecov Bot commented Sep 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ Changes either increased or maintained existing code coverage, great job!

Files with missing lines Coverage Δ
src/libs/focusComposerWithDelay/index.ts 73.07% <100.00%> (+7.85%) ⬆️
src/pages/NewChatPage/index.tsx 83.21% <ø> (ø)
...tionCompose/shouldYieldFocusToSidePanelComposer.ts 100.00% <100.00%> (ø)
...rt/ReportActionCompose/ComposerWithSuggestions.tsx 63.54% <76.92%> (+0.26%) ⬆️
... and 6 files with indirect coverage changes

Comment thread tests/unit/focusComposerWithDelayTest.ts Outdated
Explain why the unsafe cast is needed, per review feedback.
@ikevin127

ikevin127 commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

Reviewer Checklist

  • I have verified the author checklist is complete (all boxes are checked off).
  • I verified the correct issue is linked in the ### Fixed Issues section above
  • I verified testing steps are clear and they cover the changes made in this PR
    • I verified the steps for local testing are in the Tests section
    • I verified the steps for Staging and/or Production testing are in the QA steps section
    • I verified the steps cover any possible failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
  • I checked that screenshots or videos are included for tests on all platforms
  • I included screenshots or videos for tests on all platforms
  • I verified that the composer does not automatically focus or open the keyboard on mobile unless explicitly intended. This includes checking that returning the app from the background does not unexpectedly open the keyboard.
  • I verified tests pass on all platforms & I tested again on:
    • Android: HybridApp
    • Android: mWeb Chrome
    • iOS: HybridApp
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • If there are any errors in the console that are unrelated to this PR, I either fixed them (preferred) or linked to where I reported them in Slack
  • I verified proper code patterns were followed (see Reviewing the code)
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I verified that this PR follows the guidelines as stated in the Review Guidelines
  • I verified other components that can be impacted by these changes have been tested, and I retested again (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar have been tested & I retested again)
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG)
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • For any bug fix or new feature in this PR, I verified that sufficient unit tests are included to prevent regressions in this flow.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR reviewer checklist, including those that don't apply to this PR.

Screenshots/Videos

100601.mov

Comment thread src/pages/inbox/report/ReportActionCompose/ComposerWithSuggestions.tsx Outdated
Comment thread src/pages/inbox/report/ReportActionCompose/ComposerWithSuggestions.tsx Outdated
Comment thread tests/unit/focusComposerWithDelayTest.ts Outdated
Comment thread tests/unit/focusComposerWithDelayTest.ts Outdated
Comment thread tests/unit/focusComposerWithDelayTest.ts Outdated
Comment thread tests/unit/focusComposerWithDelayTest.ts
@ikevin127

ikevin127 commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

⚠️ Found one more case while testing, and I want to be clear up front that I do not think this PR caused it, but it seems to be in scope and if not addressed it might be reported as regression by QA.

Screen.Recording.2026-09-15.at.16.13.58.mov

Steps, continuing from a state where the Side Panel composer holds focus:

  1. Side Panel open and focused.
  2. Start chat with X. X's composer gains focus. ✅
  3. Click back into the Concierge composer.
  4. Cmd+K, then click outside. Focus returns to Concierge. ✅ (this is #86658 test 2, working as intended)
  5. Start chat with the same X, whose chat is already open in the central pane.
  6. Focus stays in Concierge. ❌

The reason is that step 5 never mounts anything. navigateToAndOpenReport finds the existing chat and calls navigateToReport(chat.reportID, ...) (Report/index.ts:2673), and since that report is already current there is no new ReportScreen. That removes the only thing that releases the claim in the working flow.

sidePanelComposerRef has two release sites: handleSidePanelFocus (reached from handleFocus, so only when a non-panel composer actually receives DOM focus) and the new unmount effect. Neither fires here. So the claim is still set when the modal-close effect runs, the new guard makes the main composer yield, and the panel's fire-time re-check passes because nothing cleared the claim.

This is why it works when you pick a different person and not when you pick the one already open.

On main this flow already ends in Concierge, for a different reason: both composers schedule a 150ms timer and the panel's is queued last, so it wins on ordering. Same end state, so not a regression, but the guard turns "wins by accident" into "wins by design" and the fix does not cover this variant of the reported bug. Given the issue's expected result is "chat focus will be on the newly opened chat", I read selecting an already-open chat as in scope.

The re-check answers "another composer took focus during the delay". It has no answer for "the user explicitly navigated but nothing mounted to take focus". Clearing the claim in navigateToReport before Navigation.dismissModal() covers the second case and composes cleanly with what's here:

// Selecting a chat destination hands composer-focus ownership to the main pane, so release the Side Panel's
// claim before the dismiss. Without this, picking a chat that is already open never mounts a composer to
// release it, and the Side Panel's refocus wins by default.
ReportActionComposeFocusManager.sidePanelComposerRef.current = null;
Navigation.dismissModal();

#86658's scenarios are unaffected: Cmd+K and Esc never call navigateToReport, and switching chats from the LHN mounts a new composer that clears the claim anyway.

Yield to the Side Panel composer only while the panel is actually on
screen. The claim outlives a closing panel by the length of its exit
animation, so on narrow screens, where navigating closes the panel and
dismisses the RHP at the same moment, the guard could block the main
composer's refocus and reproduce the original symptom at a different
width.

Extract the guard into shouldYieldFocusToSidePanelComposer so it is
covered directly, and keep focus on an empty dependency list by reading
isInSidePanel through a ref synced in an effect. Assigning the ref
during render would exceed this file's react-hooks/refs seatbelt
budget.

The test now uses a real textarea instead of a cast, and the composer
focus delay constant instead of a hardcoded 150. jest/setupAfterEnv.ts
switches every suite back to real timers, so the explicit
useFakeTimers() call stays.
@mukhrr
mukhrr requested a review from ikevin127 September 16, 2026 05:50
dylanexpensify
dylanexpensify previously approved these changes Sep 16, 2026

@dylanexpensify dylanexpensify left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good from a product perspective

@ikevin127

Copy link
Copy Markdown
Contributor

Status of my previous comments

# Comment Status
1 🟠 Mirror guard missing the Side Panel visibility check ✅ Done
2 🟡 focus picked up a reactive dependency ✅ Done, and my snippet was wrong
3 🟡 Unmount effect undocumented, guard comment should link #86658 ⚠️ Half done
4 🟡 Test double cast and its eslint-disable avoidable ✅ Done
5 🟡 150 hardcoded instead of the constant ✅ Done
6 🟡 jest.useFakeTimers() redundant ✅ Correctly rejected, I was wrong
7 🟠 Mirror guard and unmount clear untested ⚠️ Mostly done
G ⚠️ Focus stays in Concierge when the chat is already open in the central pane ❌ Not addressed, no reply

Comment on lines +5 to +6
/** Stays false for the length of the Side Panel's exit animation, while its composer is still mounted */
shouldHideSidePanel: boolean;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 shouldHideSidePanel's param doc is inverted, and it inverts the meaning of the fix.

It is the opposite. SidePanelActions.dismissSidePanel does Onyx.merge(ONYXKEYS.NVP_SIDE_PANEL, {open: false, openNarrowScreen: false, ...}) (src/libs/actions/SidePanel.ts:31), so isSidePanelVisible flips false immediately and shouldHideSidePanel becomes true the moment the panel starts closing.

The thing that stays false for the length of the exit animation is isSidePanelTransitionEnded, which is why SidePanel/index.tsx keeps the composer mounted until both are satisfied.

That distinction is the entire reason this parameter exists. The panel goes logically hidden while its composer is still mounted holding the claim, and shouldHideSidePanel going true is what stops the main composer from yielding to it.

The code and the test (shouldHideSidePanel: true asserts no yield) are both correct, so this is only the doc, but it reads as a contradiction of the behaviour it documents and it is exactly the kind of comment that gets someone to "correct" the logic later:

/** True as soon as the Side Panel starts closing, while its composer is still mounted for the exit animation */
shouldHideSidePanel: boolean;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

oh right, thanks. updated

@ikevin127 ikevin127 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟠 The already-open-chat case has no reply and no change

This is the one I actually need an answer on before approving.

To be clear, I am not asking you to prove it is unrelated, because I already established that: on main the same flow ends in Concierge too, since both composers schedule a 150ms timer and the panel's is queued last. It is a pre-existing gap, not something this PR introduced.

The risk is QA: The issue's repro is "FAB > Start chat > enter email > select the user" with the expected result "chat focus will be on the newly opened chat". Nothing in that wording says the chat must be new, so a tester picking someone whose chat is already open in the central pane will see focus stay in Concierge and file it against this PR.

Either resolution works for me:

  • Fix it here. Release the claim in navigateToReport before Navigation.dismissModal(). That covers "the user explicitly navigated but nothing mounted to take focus", which the fire-time re-check structurally cannot see. #86658 is unaffected, since Cmd+K and Esc never reach navigateToReport and an LHN switch mounts a composer that clears the claim anyway.
  • Or scope it out explicitly. Say so in a comment, and add a line to the PR description noting the already-open-chat variant is pre-existing and tracked separately, so QA has something to read before filing.

What I do not want is silence on it, because then it surfaces after deploy with no written history.

Selecting a chat that is already open in the central pane mounts no
composer, so nothing released the Side Panel's claim and the panel won
the refocus that follows the dismiss. Releasing it in NewChatPage
covers that alongside the new-chat and self-DM branches, all of which
dismiss the modal themselves without going through navigateToReport.

Also correct the shouldHideSidePanel doc, which had the flag inverted:
closing the panel merges open: false immediately, so the flag goes true
at the start of the close while isSidePanelTransitionEnded is what
lags for the exit animation. Document why the main composer has to
yield at all, since the Side Panel only wins that race by effect-flush
order.
@mukhrr

mukhrr commented Sep 17, 2026

Copy link
Copy Markdown
Contributor Author

🟠 The already-open-chat case has no reply and no change

ah sorry, I kinda confused it. it should be fixed now

@ikevin127 ikevin127 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 LGTM - Thanks for addressing all comments

@stitesExpensify

Copy link
Copy Markdown
Contributor

Conflict :(

@ikevin127

Copy link
Copy Markdown
Contributor

cc @mukhrr for conflict resolution - so we can merge this 🙌

@mukhrr

mukhrr commented Sep 19, 2026

Copy link
Copy Markdown
Contributor Author

@stitesExpensify resolved

@stitesExpensify
stitesExpensify merged commit 2cf1c98 into Expensify:main Sep 21, 2026
35 of 36 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

🚧 stitesExpensify has triggered a test Expensify/App build. You can view the workflow run here.

@OSBotify

Copy link
Copy Markdown
Contributor

✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release.

@OSBotify

Copy link
Copy Markdown
Contributor

🚀 Deployed to staging by https://github.com/stitesExpensify in version: 9.4.91-0 🚀

platform result
🕸 web 🕸 success ✅
🤖 android 🤖 success ✅
🍎 iOS 🍎 success ✅

@MelvinBot

Copy link
Copy Markdown
Contributor

🤖 No help site changes are required for this PR.

This is an internal focus-race fix. It changes no feature, setting, label, button, or user-facing workflow — it only makes the Side Panel composer stop stealing focus back after a modal closes. Focus behavior between the Side Panel composer and the main composer is not documented anywhere in App/docs/articles, so there is nothing to update and no draft PR to open.

What I checked

Changed files — all implementation and tests, no copy or src/languages strings:

Help site search — I searched App/docs/articles for "side panel", "composer", and "focus". The only relevant hit is docs/articles/new-expensify/concierge-ai/Concierge-Basics.md:28, which says Concierge opens "in the side panel on web" when you use the Help icon. That statement stays accurate — the panel still opens the same way, so the line needs no edit.

The remaining "focus" hits are unrelated uses of the word (SmartScan troubleshooting, playbooks, analytics).

Per the naming conventions and authoring guidelines, help articles document what a user does and sees, not internal timing behavior. A bug fix that restores the already-expected behavior — typing lands in the chat you just opened — doesn't warrant a new or amended article.

@mukhrr, I didn't open a help site PR because none is needed, so there's nothing to mark Ready for review. If you disagree and want a specific article updated anyway, reply with which one and what it should say, and I'll open the draft PR, label it HelpDot, and assign it to you.

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.

6 participants