Skip to content

fix: Screen Reader: Global: Focus is lost when returning to the previous screen#91752

Open
TaduJR wants to merge 114 commits into
Expensify:mainfrom
TaduJR:fix-Screen-Reader-Global-Focus-is-lost-when-returning-to-the-previous-screen
Open

fix: Screen Reader: Global: Focus is lost when returning to the previous screen#91752
TaduJR wants to merge 114 commits into
Expensify:mainfrom
TaduJR:fix-Screen-Reader-Global-Focus-is-lost-when-returning-to-the-previous-screen

Conversation

@TaduJR

@TaduJR TaduJR commented May 26, 2026

Copy link
Copy Markdown
Contributor

Explanation of Change

When a screen-reader user navigates back, focus now returns to the row they tapped to go forward (WCAG 2.4.3). Also fixes WCAG 2.4.7 (Focus Visible) on touch initial-focus and modal close.

Approach. On press, we record the element + its identifier against the screen the user is leaving. On back navigation, after the transition settles, we re-fire accessibility focus on that element. If react-native-screens detached the original view, a per-screen registry resolves the live re-mounted one by identifier. Bounded retry budget covers slow re-attach. Sighted users pay nothing the pipeline short-circuits when the screen reader is known-off.

  • Native rescue (new) capture on press, restore after transition, registry fallback for detached views, retry budget for slow re-attach.
  • Web rescue (refactor) migrated off the deprecated InteractionManager onto TransitionTracker. Also fixes a latent ~1s back-nav stall.
  • Focus priority all focus claimers go through one arbiter so the next screen's autofocus can't trample the restore.
  • Stable row identity 6 pages re-keyed off value-derived React keys so the captured row doesn't remount on every edit.
  • Save-and-close submit-driven goBack skips restore so a re-focused row can't hijack the destination form's Save button.

Fixed Issues

$ #77411
PROPOSAL:

Tests

1. Going back returns focus to the item you opened

  1. Turn the screen reader ON.
  2. Go to Account (Settings) → Profile → Display name.
  3. On the Display name screen, activate the Back button.
  4. Expected: focus goes back to the Display name row and the screen reader reads it out. Focus is not lost or silent.
  5. Repeat from a few other screens (e.g. Profile → Contact methods, then back) and confirm focus always returns to the row or button you opened.

2. Same behavior inside Workspace settings

  1. Screen reader ON.
  2. Go to Settings → Workspaces → [a workspace] and open any settings sub-page (e.g. More features, Members, Categories).
  3. Open a row from there, then activate Back.
  4. Expected: focus returns to the exact row you opened — not the Back button and not the top of the screen.

3. Opening a screen announces it

  1. On Android Chrome or iOS Safari, screen reader ON.
  2. Navigate into a settings screen (e.g. Account → Profile → Display name).
  3. Expected: once the screen finishes opening, the screen reader automatically announces the screen (focus lands on the Back button). Before, nothing was announced on open.
  4. Confirm the page does not jump or scroll when the screen opens.
  • Verify that no errors appear in the JS console

Offline tests

Same as tests

QA Steps

// TODO: These must be filled out, or the issue title must include "[No QA]."
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
    • MacOS: Desktop
  • 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 verified there are no new alerts related to the canBeMissing param for useOnyx
  • I followed proper code patterns (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick)
    • 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 shown in the product is localized by adding it to src/languages/* files and using the translation method
      • If any non-english text was added/modified, I used JaimeGPT to get English > Spanish translation. I then posted it in #expensify-open-source and it was approved by an internal Expensify engineer. Link to Slack message:
    • I verified all numbers, amounts, dates and phone numbers shown in the product are using the localization methods
    • 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)
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • 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)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.ts or at the top of the file that uses the constant) are defined as such
  • I verified that if a function's arguments changed that all usages have also been updated correctly
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • 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.
  • If a new page is added, I verified it's using the ScrollView component to make it scrollable when more elements are added to the page.
  • 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
Android-Native-1.mp4
Android: mWeb Chrome
Android-mWeb-2.mp4
iOS: Native
iOS: mWeb Safari
MacOS: Chrome / Safari
Mac-Chrome.mp4

@TaduJR TaduJR requested review from a team as code owners May 26, 2026 19:12
@melvin-bot melvin-bot Bot requested review from JmillsExpensify and marufsharifi and removed request for a team and marufsharifi May 26, 2026 19:12
@melvin-bot

melvin-bot Bot commented May 26, 2026

Copy link
Copy Markdown

@marufsharifi 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]

@TaduJR

TaduJR commented May 26, 2026

Copy link
Copy Markdown
Contributor Author

cc @mkhutornyi

@TaduJR TaduJR marked this pull request as draft May 26, 2026 19:13
Comment thread src/libs/Accessibility/index.ts Outdated
Comment thread src/libs/NavigationFocusReturn/index.native.ts
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@codecov

codecov Bot commented May 26, 2026

Copy link
Copy Markdown

Codecov Report

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

Files with missing lines Coverage Δ
...mponents/FocusTrap/FocusTrapForModal/index.web.tsx 100.00% <100.00%> (+27.77%) ⬆️
...ponents/FocusTrap/FocusTrapForScreen/index.web.tsx 0.00% <ø> (ø)
src/components/HeaderWithBackButton/index.tsx 89.21% <100.00%> (+0.10%) ⬆️
...oneyRequestConfirmationList/sections/TaxFields.tsx 81.81% <100.00%> (+0.33%) ⬆️
...cPressable/implementation/BaseGenericPressable.tsx 80.99% <100.00%> (+1.71%) ⬆️
...rc/components/Search/SearchRouter/SearchButton.tsx 100.00% <ø> (ø)
...dRules/configuration/SpendRuleMerchantEditBase.tsx 100.00% <ø> (+100.00%) ⬆️
src/components/SubStepForms/ConfirmationStep.tsx 95.45% <100.00%> (+95.45%) ⬆️
src/components/Table/TableRow.tsx 75.32% <100.00%> (+0.32%) ⬆️
src/hooks/useActiveElementRole/index.ts 100.00% <ø> (ø)
... and 39 more
... and 12 files with indirect coverage changes

@TaduJR

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@TaduJR

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@TaduJR

This comment was marked as resolved.

@chatgpt-codex-connector

This comment was marked as outdated.

@TaduJR

This comment was marked as resolved.

@TaduJR

This comment was marked as resolved.

@mkhutornyi

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@TaduJR

This comment was marked as resolved.

…Reader-Global-Focus-is-lost-when-returning-to-the-previous-screen
@MelvinBot

Copy link
Copy Markdown
Contributor

Code Review (re-review @ 96a6a0c)

I re-reviewed the full focus architecture (web + native) at the latest commit with three parallel reviewers reading the post-PR files in full. No blockers. Every item from my earlier passes is resolved, and the newest commit's native retry-loop changes check out:

  • Native crash-guardfireFocusEvent now wraps sendAccessibilityEvent in try/catch + Log.warn, and scheduleRefocus routes through it (fireFocusEvent/index.native.ts:8-13).
  • restoreFocusWithModality trap balance — now records wasAlreadyPaused and only unpauses the captured trap when it actually paused it (restoreFocusWithModality.ts:9-23).
  • Native back-nav churn for sighted usersscheduleRestore early-returns on isScreenReaderKnownOff() before any transition wait / rAF / Log.warn (index.native.ts:156-159).
  • Newest commit's retry loopMAX_RESTORE_FRAMES budget is exactly 5 (no off-by-one), every rAF/transition handle is cancelled by the single arm-time pendingRestore.cancel closure, the rawRouteKey split is correctly hoisted out of the loop, and FIFO cap + per-removed-key pruning prevent map growth.

One thing worth a quick look before merge (Low)

useInitialFocusRef returns a non-memoized ref callback. useInitialFocusRef/index.ts:7-14 allocates a fresh arrow each render, so at the source level React detaches (null) then re-attaches the ref every commit, re-running the useScreenInitialFocus effect and restarting its rAF claim loop before the first claim lands. React Compiler memoizes this in the shipped build (the only dep, setNode, is stable), so there's no production impact — but the code reads as buggy and correctness silently depends on the compiler. Wrapping the returned callback in useCallback([]) (the functional setNode updater needs no deps) makes it robust regardless. Used on HeaderWithBackButton and HeaderView.

Cosmetic nits (non-blocking) + areas confirmed clean

Cosmetic

  • restoreFocusWithModality.ts:18 — the comment still says the unpause is "Unconditional", but the code is now guarded by !wasAlreadyPaused. Update the comment to avoid future confusion.
  • ConfirmationStep key fallback ${description}-${index} can't collide (index always disambiguates within one array); the only effect of the documented index-shift caveat is a harmless remount of a static display row when a conditional toggles — no captured input/focus state is lost. Informational only.

Confirmed clean (read in full, not just the patch)

  • Web core (NavigationFocusReturn/index.ts, ScreenFocusArbiter, claimInitialFocus, programmaticFocus, LauncherStack, FocusTrapForModal, navigationStateDiff): listener/timer lifecycle is balanced (every addEventListener/setTimeout/rAF has a matching teardown), arbiter priority logic (AUTO > INITIAL, lazy cycle expiry) is internally consistent, LauncherStack's in-loop splice iterates descending (safe), and SSR/typeof document guards are present on every DOM entry point.
  • Enter-to-submit safety on IOU forms: the back-button INITIAL claim can't hijack the auto-focused input — claimInitialFocus bails when activeElement !== body, and AUTO preempts INITIAL in the arbiter. The shouldSkipFocusRestore: !isEditing flip on Merchant/Description is correct for create/edit/split (isEditing derives from the immutable action route param and is snapshotted at arm time).
  • Native registries: triggerMap FIFO-capped at 64 with compound-prefix pruning per removed route key; pressableRegistry correctly keyed only by raw route keys (no compound pruning needed) and bounded by per-pressable unmount unsubscribe. No unbounded growth.
  • key / pressableTestID stabilizations (TaxFields, SpendRuleMerchantsBase, ConfirmationStep): no collisions, no incorrect remounts.
  • SearchButton dropping its self-blur is deliberate and correct — the trigger must retain focus for captureTriggerForRoute to restore it on close.

Next Steps: Reply with @MelvinBot <your feedback> to dig into any of these.

@TaduJR

TaduJR commented Jun 25, 2026

Copy link
Copy Markdown
Contributor Author

One thing worth a quick look before merge (Low)
useInitialFocusRef returns a non-memoized ref callback. useInitialFocusRef/index.ts:7-14 allocates a fresh arrow each render, so at the source level React detaches (null) then re-attaches the ref every commit, re-running the useScreenInitialFocus effect and restarting its rAF claim loop before the first claim lands. React Compiler memoizes this in the shipped build (the only dep, setNode, is stable), so there's no production impact — but the code reads as buggy and correctness silently depends on the compiler. Wrapping the returned callback in useCallback([]) (the functional setNode updater needs no deps) makes it robust regardless. Used on HeaderWithBackButton and HeaderView.

Same as Codex. Invalid

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@TaduJR

TaduJR commented Jun 25, 2026

Copy link
Copy Markdown
Contributor Author

@roryabraham

1. What problem are we solving?

When a screen-reader user (TalkBack / VoiceOver) navigates forward by tapping a row, then navigates back, focus should land on the row they tapped so the screen reader can announce it again. Today it doesn't; focus collapses to the screen root / <body>, the screen reader has nothing to announce, and the user loses their place. Issue #77411 fails WCAG 2.4.3 (Focus Order).

2. What's the simplest naive solution, and why doesn't it work?

Naive solution A "just call .focus() after going back."
React Native has no JS event for accessibility focus, and react-native-screens can detach the prior screen's host views, nulling the JS ref of the element we want to refocus. So we can't just hold the ref and call .focus() later.

Naive solution B "use the right navigation library; @react-navigation/native-stack handles this."
This was Rushat's suggestion.

  • Apple's own View Controller Programming Guide Supporting Accessibility states: "When pushing a new view controller onto a navigation controller's stack, the VoiceOver cursor falls on the Back button of the navigation bar." The documented remedy is for the app to post UIAccessibilityScreenChangedNotification. UIKit's UINavigationController does not auto-restore prior focus. AOSP FragmentManager is the same.
  • We already use @react-navigation/native-stack on native (createPlatformStackNavigatorComponent.native.tsx wraps <NativeStackView>). It's a thin wrapper over react-native-screens, which mounts real UINavigationController / Fragment instances so we already inherit the system controller, and the bug still occurs.
  • Open community confirmations: react-navigation #11189 (the exact bug), #7056, #10468, #12724; react-native-screens #3147.

There is no library configuration that obviates the rescue. The contract has to live in the app.

3. What's the proposed solution?

A small, two-step rescue:

  1. Capture. When a screen-reader user presses a row, we record {ref, identifier} against the screen they're navigating away from. The identifier is the row's existing id/nativeID/testID no new prop, no behavior change for sighted users.
  2. Restore. When the user navigates back, we look up that capture and fire AccessibilityInfo.sendAccessibilityEvent(view, 'focus') once the navigation transition settles.

Web is the sibling that already shipped (PR #87852); this PR extends the same idea to native + closes a handful of mobile-web edges discovered along the way.

4. Can the PR be broken up?

I'd recommend keeping it as one PR. The diff looks large (82 files, +4486) but 76% of net additions are tests. Production code is 49 files / +945 net lines. We already achieved the core issue ~20th commit, but there were many edge cases to handle in review like flicking, slow navigation re-attach, form submit. We tried to covered every-possible cases.

cc @rushatgabhane

@TaduJR

TaduJR commented Jun 25, 2026

Copy link
Copy Markdown
Contributor Author

@mkhutornyi

PR is ready for re-review. I tried to test all possible cases again after refactor. I didn't find any issues.

All yours! LMK if there is from your side.

Both Codex and Melvin doesn't have any concerns to address.

Thanks so much for staying and supporting on this journey!

@rushatgabhane

rushatgabhane commented Jun 25, 2026

Copy link
Copy Markdown
Member

If it helps simplify the solution, we can solve this for native first and web later. what do you think? @TaduJR

@TaduJR

TaduJR commented Jun 25, 2026

Copy link
Copy Markdown
Contributor Author

If it helps simplify the solution, we can solve this for native first and web later. what do you think? @TaduJR

@rushatgabhane

Web is already implemented in this PR #87852 previously if you recalled.

Web is the sibling that already shipped (PR #87852); this PR extends the same idea to native + closes a handful of mobile-web edges discovered along the way.

@rushatgabhane

Copy link
Copy Markdown
Member

ah cool

Comment thread src/hooks/useInitialFocusRef.ts
Comment thread src/hooks/useScreenInitialFocus/types.ts Outdated
Comment thread src/hooks/useScreenInitialFocus/types.ts Outdated
Comment thread src/libs/Accessibility/index.ts Outdated
Comment thread src/libs/isEffectivelyVisible.ts
* Subscribers `.then()` it to catch the boot-race — the platform listener only fires on toggles, never on the initial state.
* `refresh()` invalidates the memo and re-warms; used on AppState resume to recover from toggles that fire while no JS listener was active.
*/
function makeWarmCache<T>(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why do we need this function?
Should we use onyx instead of Set as our data structure

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.

AccessibilityInfo.isScreenReaderEnabled() is async, but capture/restore in NavigationFocusReturn.native.ts runs synchronously inside onPress and the navigation reducer can't await. The platform screenReaderChanged listener only fires on toggles, never the initial state. makeWarmCache bridges this: eager warm at module load, sync accessor, AppState refresh, generation token to discard superseded fetches. Same shape applies to reduce-motion, so it's one helper instead of two.

Set and Onyx: The Set<() => void> holds useSyncExternalStore subscriber callbacks non-serializable React-renderer functions. Onyx can't store functions; Onyx itself uses Map/Set for its own subscriber registry. Persisting the cached boolean across launches would also be wrong OS-level SR state can change while the app is killed, and Onyx reads are async (defeats the sync cache). Established convention in this codebase: AgentZeroReasoningStore, NetworkState, RevealedCardSecretsStore all use the same Set + useSyncExternalStore pattern.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Onyx can't store functions

Why are we storing functions? Can we store the params to call those functions instead?

I thought we are storing the ID of a view, and focusing on it as per the simplified solution posted above

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.

Two stores being conflated here. The Set in Accessibility/index.ts (screenReaderSubscribers) is NOT the view registry it's the useSyncExternalStore subscriber callback registry for the useScreenReaderStatus / useScreenReaderState hooks. When a component calls those hooks, React itself passes an opaque internal callback into our subscribe function; we keep it in the Set so we can notify React to re-read getSnapshot on AppState resume + warm-cache refresh. The callback takes zero arguments Set<() => void> there are no params to factor out the function IS React's notification protocol. Reimplementing it with IDs would mean rewriting useSyncExternalStore itself imo.

@rushatgabhane rushatgabhane Jun 26, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I see

A small, two-step rescue:

  1. Capture. When a screen-reader user presses a row, we record {ref, identifier} against the screen they're navigating away from. The identifier is the row's existing id/nativeID/testID no new prop, no behavior change for sighted users.
  2. Restore. When the user navigates back, we look up that capture and fire AccessibilityInfo.sendAccessibilityEvent(view, 'focus') once the navigation transition settles.

How does that fit in this?

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.

That's the design already implemented in this PR

Should I continue explaining on how?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

yes please, im trying to understand what the code does, and why are we doing things in the current way

TaduJR added 4 commits June 26, 2026 18:37
…Reader-Global-Focus-is-lost-when-returning-to-the-previous-screen
…Reader-Global-Focus-is-lost-when-returning-to-the-previous-screen

# Conflicts:
#	src/pages/workspace/expensifyCard/WorkspaceExpensifyCardListPage.tsx
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