Skip to content

[v2] Refactor ReanimatedModal/index.tsx to follow Rules of React and compile with React Compiler#90555

Draft
roryabraham wants to merge 18 commits into
mainfrom
Rory-ReanimatedModal-v2
Draft

[v2] Refactor ReanimatedModal/index.tsx to follow Rules of React and compile with React Compiler#90555
roryabraham wants to merge 18 commits into
mainfrom
Rory-ReanimatedModal-v2

Conversation

@roryabraham
Copy link
Copy Markdown
Contributor

@roryabraham roryabraham commented May 13, 2026

Explanation of Change

This is v2 of #90358, which was reverted due to staging regressions. This version contains the original refactoring changes plus fixes for all four reported regressions.

Regressions fixed:

  • #90510 — Web: chats flicker briefly after closing the Search RHP
  • #90438 — Web: RHP does not animate after changing distance rate unit
  • #90463 — Android: modal not displayed after dismissing upgrade prompt
  • #90442 — Android: confirmation modal does not appear (delete, remove member, etc.)

Root cause: The original refactor derived isTransitioning from isVisible !== isContainerOpen. When isVisible oscillated back to match isContainerOpen mid-animation (e.g. rapid RHP navigation), isTransitioning incorrectly became false, prematurely ending transition handles and allowing the Container to re-mount mid-animation.

Fix: Replace the two booleans with a four-state enum (closed | opening | open | closing). State advances only from stable states (closed → opening, open → closing) and is latched through animations, so mid-animation oscillations are ignored. Move TransitionTracker/InteractionManager handle management out of React state and into a new useAnimationTransition hook that ties directly to component mount/unmount and animation callbacks, bypassing React's render cycle.

Fixed Issues

$
PROPOSAL:

Tests

  1. Open any modal (bottom sheet, right-hand panel, popover) and verify it opens and closes with the correct animation on all platforms.
  2. Verify the back button (Android) and Escape key (web) dismiss the modal correctly.
  3. Verify tapping the backdrop dismisses the modal.
  4. Verify no errors appear in the JS console.

Regression #90510 — web flash on RHP close:
5. On web, open the Search RHP (tap the search icon in the Inbox header).
6. Close it by clicking outside or pressing Escape.
7. Verify the chat list does not briefly reappear over the area where the RHP was.

Regression #90438 — RHP animation missing after navigation:
8. On web, go to Workspace Settings → Distance rates → Settings → Unit.
9. Select a different unit and navigate back.
10. Verify the RHP animates on every open/close without getting stuck unanimated.

Regression #90463 — modal not displayed on Android:
11. On Android: Workspace → Tags → More → Import spreadsheet → Multi-level tags → Upgrade → Got it, thanks → Switch Tag Levels.
12. Verify the Import spreadsheet modal appears with a "Choose file" button.

Regression #90442 — confirmation modal missing on Android:
13. On Android: create an expense → open the expense report → More → Delete → Delete.
14. Verify the expense is successfully deleted. Also verify delete/remove actions work for: saved searches, workspace members, categories, tags, distance rates, taxes, per diem rates.

Offline tests

N/A — no offline-specific behavior changed.

QA Steps

  1. Open a bottom-docked modal (e.g. long-press a report in the LHN) and tap the backdrop — verify it animates out correctly on all platforms.
  2. Open a right-docked panel (e.g. Settings), press Escape (web) or back button (Android) — verify it dismisses correctly.
  3. Verify no errors appear in the JS console.

Regression #90510 — web flash on RHP close (web):
4. Navigate to Inbox → click the search icon → close the Search RHP by clicking outside or pressing Escape.
5. Verify chats do not briefly reappear on screen where the RHP was displayed.

Regression #90438 — RHP animation stops (web):
6. Go to Workspace Settings → Distance rates → Settings → Unit → select another unit.
7. Verify the RHP animates on every subsequent open/close.

Regression #90463 — modal not displayed on Android:
8. On Android: Workspace → Tags → More → Import spreadsheet → Multi-level tags → Upgrade → Got it, thanks → Switch Tag Levels.
9. Verify the Import spreadsheet modal appears with a "Choose file" button.

Regression #90442 — confirmation modal missing on Android:
10. On Android: create an expense → open the expense report → More → Delete → Delete.
11. Verify the expense is successfully deleted. Also verify delete/remove actions work for: saved searches, workspace members, categories, tags, distance rates, taxes, per diem rates.

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 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 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: mWeb Chrome
iOS: Native
iOS: mWeb Safari
MacOS: Chrome / Safari

Made with Cursor

  • I verified that similar component doesn't exist in the codebase
  • I verified that all props are defined accurately and each prop has a /** comment above it */
  • I verified that each file is named correctly
  • I verified that each component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
  • I verified that the only data being stored in component state is data necessary for rendering and nothing else
  • In component if we are not using the full Onyx data that we loaded, I've added the proper selector in order to ensure the component only re-renders when the data it is using changes
  • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
  • I verified that component internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
  • I verified that all JSX used for rendering exists in the render method
  • I verified that each 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

Screenshots/Videos

…Compiler

- Group useEffects by responsibility; co-locate cleanup with creation
- Extract transition callbacks with useEffectEvent to fix stale closure
- Derive isTransitioning and eliminate redundant isVisibleState state
- Remove manual memoization (useCallback/useMemo) superseded by React Compiler
- DRY up clearTransitionHandles helper
- Sort component internals: hooks, refs, derived, callbacks, effects

Co-authored-by: Cursor <cursoragent@cursor.com>
@codecov
Copy link
Copy Markdown

codecov Bot commented May 13, 2026

Codecov Report

❌ Looks like you've decreased code coverage for some files. Please write tests to increase, or at least maintain, the existing level of code coverage. See our documentation here for how to interpret this table.

Files with missing lines Coverage Δ
...Modal/ReanimatedModal/Container/GestureHandler.tsx 19.35% <100.00%> (-2.53%) ⬇️
...mponents/Modal/ReanimatedModal/Container/index.tsx 100.00% <100.00%> (ø)
src/hooks/useAnimationTransition.ts 100.00% <100.00%> (ø)
src/hooks/useOnValueChange.ts 100.00% <100.00%> (ø)
...omponents/Modal/ReanimatedModal/Backdrop/index.tsx 83.33% <62.50%> (-10.42%) ⬇️
src/components/Modal/ReanimatedModal/index.tsx 81.69% <84.61%> (+3.48%) ⬆️
...ents/Modal/ReanimatedModal/Container/index.web.tsx 0.00% <0.00%> (ø)
... and 398 files with indirect coverage changes

roryabraham and others added 15 commits May 13, 2026 13:03
Covers four staging regressions from the React Compiler refactor:
- #90438 RHP no animation (derived isTransitioning loses "in-progress" semantics on oscillation)
- #90442 Android can't confirm deletion (interaction handle cleared prematurely)
- #90463 Android modal not displayed (interaction handle cleared prematurely)
- #90510 Web flickering on close (Container unmounted before exit animation plays)

Tests are intentionally red until the fixes are applied.

Co-authored-by: Cursor <cursoragent@cursor.com>
Wraps onBackButtonPressHandler and handleEscape in useEffectEvent so
they hold a stable reference across renders without needing deps.
The back handler effect can now use [] as its dep array, eliminating
re-registration on every render cycle.

Fixes #90442 and #90463.

Co-authored-by: Cursor <cursoragent@cursor.com>
Replaces two booleans (isContainerOpen + derived isTransitioning) with a
single explicit ModalState enum: 'closed' | 'opening' | 'open' | 'closing'.

The enum makes impossible states impossible and fixes oscillation bugs:
- modalState only advances via animation callbacks (onOpenCallBack /
  onCloseCallBack), so it can never flip back due to isVisible oscillating.
- The isVisible → state transition uses the render-time state adjustment
  pattern instead of a useEffect, eliminating extra renders.
- Container visibility uses (modalState === 'opening' || modalState === 'open')
  so the Container unmounts when closing begins, triggering Reanimated's ghost-
  node exit animation, and does not re-mount during isVisible oscillation.

Updates regression tests to use reliable spy-count assertions; removes
dependency on InteractionManager.runAfterInteractions which is mocked to
fire immediately in the test environment.

Fixes #90438, #90510.

Co-authored-by: Cursor <cursoragent@cursor.com>
Extract TransitionTracker and InteractionManager handle management out
of React state and into a new useAnimationTransition hook. Handles now
start on component mount/unmount (via useLayoutEffect) and end when the
animation callback fires (onAnimationComplete), bypassing React state
as an intermediary.

This fixes a class of bugs where React's re-render cycle could clear or
create handles at the wrong time, and removes the useEffect that was
responsible for the premature cleanup regressions.

Update the Container mock in tests to use the real useAnimationTransition
hook so spy assertions on TransitionTracker and InteractionManager remain
meaningful. Remove the useEffect safety net from useAnimationTransition
as it ran synchronously on unmount, immediately ending the exit-animation
handle before Reanimated's ghost-node could complete its animation.

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Add useOnValueChange<T>(value, onChange) — a thin wrapper around React's
render-time state adjustment pattern. Runs onChange synchronously during
render (no intermediate DOM commit), unlike useEffect which fires after
paint. Uses Object.is for comparison to match React's own semantics.

Use it in ReanimatedModal to replace the manual prevIsVisible state variable.

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
GestureHandler and Container already default swipeThreshold to 100,
so the required constraint on the prop type was unnecessarily strict.

Co-authored-by: Cursor <cursoragent@cursor.com>
Modals don't require content to render — the prop should be optional
to allow unit tests and other callers to omit it.

Co-authored-by: Cursor <cursoragent@cursor.com>
When a modal is mounted with isVisible=true (e.g. ConfirmModalWrapper),
useOnValueChange only triggers on changes, so the initial 'closed' state
was never advanced, preventing the modal content from ever rendering.

Co-authored-by: Cursor <cursoragent@cursor.com>
@roryabraham roryabraham force-pushed the Rory-ReanimatedModal-v2 branch from 16a034d to d62eb4d Compare May 14, 2026 08:36
@github-actions
Copy link
Copy Markdown
Contributor

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

@github-actions

This comment was marked as outdated.

…er with React Compiler

- Backdrop/index.tsx: inline BackdropOverlay JSX variable (partial render anti-pattern)
- Container/index.tsx: remove manual useMemo for Entering/Exiting; replace
  Partial<ReanimatedModalProps> with self-contained ContainerProps
- Container/index.web.tsx: remove manual useMemo for Exiting; add animationIn
  to useAnimatedStyle deps
- GestureHandler.tsx: remove manual useMemo for panGesture
- types.ts: expand ContainerProps with native-only fields; remove
  animationInDelay from BackdropProps (never consumed)
- index.tsx: remove animationInDelay pass-through to Container and Backdrop

Co-authored-by: Cursor <cursoragent@cursor.com>
@github-actions
Copy link
Copy Markdown
Contributor

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

Co-authored-by: Cursor <cursoragent@cursor.com>
@roryabraham
Copy link
Copy Markdown
Contributor Author

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.

1 participant