Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions jest.setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
Expand Down
16 changes: 13 additions & 3 deletions src/TrueSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -550,7 +560,7 @@ export class TrueSheet
* @param index - The detent index to resize to
*/
public async resize(index: number): Promise<void> {
await TrueSheetModule?.resizeByRef(this.handle, index);
await TrueSheetModule?.resizeByRef(this.handle, index).catch(ignoreMissingView);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -570,7 +580,7 @@ export class TrueSheet
* @param animated - Whether to animate the dismissal (default: true)
*/
public async dismissStack(animated: boolean = true): Promise<void> {
return TrueSheetModule?.dismissStackByRef(this.handle, animated);
return TrueSheetModule?.dismissStackByRef(this.handle, animated).catch(ignoreMissingView);
}

componentDidMount(): void {
Expand Down
37 changes: 37 additions & 0 deletions src/__tests__/TrueSheet.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<TrueSheet name={name}>
<Text>Content</Text>
</TrueSheet>
);
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();
Expand Down
Loading