diff --git a/CHANGELOG.md b/CHANGELOG.md index 868e8cab..5c62f827 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ - **Android**: A sheet whose keyboard starts rising as the present slide ends, such as one that focuses an input around `onWillPresent`, now finishes presenting. It used to never fire `onDidPresent` or resolve `present()`, and kept swallowing taps. ([#1012](https://github.com/lodev09/react-native-true-sheet/pull/1012) by [@JacquesLeupin](https://github.com/JacquesLeupin)) - **Android**: When a sheet presented over another sheet changes its content size right before it dismisses, the sheet behind it now slides back instead of staying pushed down and not draggable. ([#1015](https://github.com/lodev09/react-native-true-sheet/pull/1015) by [@idlework](https://github.com/idlework)) - **Android**: The keyboard no longer counts against `maxContentHeight`, as on iOS. A sheet now rises above the keyboard instead of stopping at `maxContentHeight` with the keyboard covering the bottom of its content, such as a button below a text input. A sheet already at `maxContentHeight` now rises with the keyboard too, instead of staying put. ([#1017](https://github.com/lodev09/react-native-true-sheet/pull/1017) by [@wildseansy](https://github.com/wildseansy)) +- **Android**: Calling `resize()`, `dismiss()` or `dismissStack()` on a sheet whose native view is already gone, for example after the activity was destroyed while JS kept running, now resolves instead of rejecting with "TrueSheetView with tag X not found". `present()` still rejects. ([#1026](https://github.com/lodev09/react-native-true-sheet/pull/1026) by [@SamuelBrucksch](https://github.com/SamuelBrucksch)) ### 💡 Others diff --git a/jest.setup.js b/jest.setup.js index 5ce5e6c2..29dbf7c7 100644 --- a/jest.setup.js +++ b/jest.setup.js @@ -6,6 +6,8 @@ jest.mock('./src/specs/NativeTrueSheetModule', () => ({ default: { presentByRef: jest.fn(), dismissByRef: jest.fn(), + dismissStackByRef: jest.fn(), + resizeByRef: jest.fn(), dismissAll: jest.fn(), handleBackPress: jest.fn(), }, diff --git a/src/TrueSheet.tsx b/src/TrueSheet.tsx index 869d4675..40c83c82 100644 --- a/src/TrueSheet.tsx +++ b/src/TrueSheet.tsx @@ -99,6 +99,16 @@ const stopResponderNegotiation = (event: GestureResponderEvent) => { return false; }; +// The native view can be dropped while JS still holds the sheet (e.g. the Android +// activity is destroyed but the JS runtime keeps running). Native then rejects with +// VIEW_NOT_FOUND (Android) or SHEET_NOT_FOUND (iOS). A sheet that no longer exists +// has nothing left to resize or dismiss, so those calls resolve instead. +const ignoreMissingView = (error: unknown): void => { + const code = (error as { code?: unknown } | null)?.code; + if (code === 'VIEW_NOT_FOUND' || code === 'SHEET_NOT_FOUND') return; + throw error; +}; + interface TrueSheetState { shouldRenderNativeView: boolean; initialPresentReady: boolean; @@ -550,7 +560,7 @@ export class TrueSheet * @param index - The detent index to resize to */ public async resize(index: number): Promise { - await TrueSheetModule?.resizeByRef(this.handle, index); + await TrueSheetModule?.resizeByRef(this.handle, index).catch(ignoreMissingView); } /** @@ -561,7 +571,7 @@ export class TrueSheet // Dismissing before the lazy mount finishes cancels the present — nothing was presented if (this.cancelPendingPresent()) return; - return TrueSheetModule?.dismissByRef(this.handle, animated); + return TrueSheetModule?.dismissByRef(this.handle, animated).catch(ignoreMissingView); } /** @@ -570,7 +580,7 @@ export class TrueSheet * @param animated - Whether to animate the dismissal (default: true) */ public async dismissStack(animated: boolean = true): Promise { - return TrueSheetModule?.dismissStackByRef(this.handle, animated); + return TrueSheetModule?.dismissStackByRef(this.handle, animated).catch(ignoreMissingView); } componentDidMount(): void { diff --git a/src/__tests__/TrueSheet.test.tsx b/src/__tests__/TrueSheet.test.tsx index 5c2bfd62..5d3c2162 100644 --- a/src/__tests__/TrueSheet.test.tsx +++ b/src/__tests__/TrueSheet.test.tsx @@ -806,6 +806,43 @@ describe('TrueSheet', () => { }); }); + describe('Missing native view', () => { + const missingView = (code: string) => + Object.assign(new Error('TrueSheetView with tag 1 not found'), { code }); + + const renderSheet = (name: string) => { + render( + + Content + + ); + return TrueSheet['instances'][name]!; + }; + + it.each(['VIEW_NOT_FOUND', 'SHEET_NOT_FOUND'])( + 'should resolve resize, dismiss and dismissStack on %s', + async (code) => { + const sheetRef = renderSheet(`missing-view-${code}`); + + jest.mocked(TrueSheetModule!.resizeByRef).mockRejectedValueOnce(missingView(code)); + jest.mocked(TrueSheetModule!.dismissByRef).mockRejectedValueOnce(missingView(code)); + jest.mocked(TrueSheetModule!.dismissStackByRef).mockRejectedValueOnce(missingView(code)); + + await expect(sheetRef.resize(0)).resolves.toBeUndefined(); + await expect(sheetRef.dismiss()).resolves.toBeUndefined(); + await expect(sheetRef.dismissStack()).resolves.toBeUndefined(); + } + ); + + it('should still reject other native errors', async () => { + const sheetRef = renderSheet('missing-view-other-error'); + const error = Object.assign(new Error('Failed to resize'), { code: 'RESIZE_FAILED' }); + jest.mocked(TrueSheetModule!.resizeByRef).mockRejectedValueOnce(error); + + await expect(sheetRef.resize(0)).rejects.toBe(error); + }); + }); + describe('Focus/Blur Events', () => { it('should call onWillFocus when triggered', async () => { const onWillFocusMock = jest.fn();