Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent modal crash when navigating #244

Merged
merged 3 commits into from
Jan 29, 2021
Merged
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
2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"start": "react-native start"
},
"dependencies": {
"@gorhom/portal": "^0.1.4",
"@gorhom/portal": "^0.2.0",
"@gorhom/showcase-template": "^1.0.2",
"@react-native-community/blur": "^3.6.0",
"@react-native-community/masked-view": "0.1.10",
Expand Down
8 changes: 4 additions & 4 deletions example/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -818,10 +818,10 @@
dependencies:
"@types/hammerjs" "^2.0.36"

"@gorhom/portal@^0.1.4":
version "0.1.4"
resolved "https://registry.yarnpkg.com/@gorhom/portal/-/portal-0.1.4.tgz#ef9e50d4b6c98ebe606a16d22d6cb58d661421b4"
integrity sha512-iU3D0i9NureT5ULTvD8moF2FWyFvVGcr/ahcRMDzBblUO1AwwixZRZ+Lf3d6uk0w4ujOQZ7+Az+uUnI+AsXXBw==
"@gorhom/portal@^0.2.0":
version "0.2.0"
resolved "https://registry.yarnpkg.com/@gorhom/portal/-/portal-0.2.0.tgz#65423726b1352e2b9e3c3b8da83a58e4dff48ce6"
integrity sha512-PoYYSliPCl624ORih+zyI1pVbH2Y2Mj+EY6nXeAIi2r1LGv10hItZLzdgnbPPfQ0neikTDYArlOzuB5zKIex5w==
dependencies:
lodash.isequal "^4.5.0"
nanoid "^3.1.20"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"bootstrap": "yarn install && yarn example"
},
"dependencies": {
"@gorhom/portal": "^0.1.4",
"@gorhom/portal": "^0.2.0",
"invariant": "^2.2.4",
"lodash.isequal": "^4.5.0",
"nanoid": "^3.1.20",
Expand Down
47 changes: 28 additions & 19 deletions src/components/bottomSheetModal/BottomSheetModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import React, {
useRef,
useState,
} from 'react';
import { Portal } from '@gorhom/portal';
import { Portal, usePortal } from '@gorhom/portal';
import { nanoid } from 'nanoid/non-secure';
import isEqual from 'lodash.isequal';
import BottomSheet from '../bottomSheet';
Expand Down Expand Up @@ -42,12 +42,15 @@ const BottomSheetModalComponent = forwardRef<
const [mount, setMount] = useState(false);
//#endregion

//#region hooks
const { unmount: unmountPortal } = usePortal();
const {
containerHeight,
mountSheet,
unmountSheet,
willUnmountSheet,
} = useBottomSheetModalInternal();
//#endregion

//#region refs
const bottomSheetRef = useRef<BottomSheet>(null);
Expand Down Expand Up @@ -76,13 +79,16 @@ const BottomSheetModalComponent = forwardRef<
if (_providedOnDismiss) {
_providedOnDismiss();
}
setMount(false);

unmountSheet(key);
unmountPortal(key);

setMount(false);

// reset
isMinimized.current = false;
isForcedDismissed.current = false;
}, [key, _providedOnDismiss, unmountSheet]);
}, [key, _providedOnDismiss, unmountSheet, unmountPortal]);
const handleOnChange = useCallback(
(_index: number) => {
if (isMinimized.current && !isForcedDismissed.current) {
Expand All @@ -105,21 +111,6 @@ const BottomSheetModalComponent = forwardRef<
);
//#endregion

//#region private methods
const handleMinimize = useCallback(() => {
if (!isMinimized.current) {
isMinimized.current = true;
bottomSheetRef.current?.close();
}
}, []);
const handleRestore = useCallback(() => {
if (isMinimized.current) {
isMinimized.current = false;
bottomSheetRef.current?.snapTo(nextIndexRef.current, true);
}
}, []);
//#endregion

//#region public methods
const handlePresent = useCallback(() => {
requestAnimationFrame(() => {
Expand Down Expand Up @@ -176,6 +167,24 @@ const BottomSheetModalComponent = forwardRef<
);
//#endregion

//#region private methods
const handleMinimize = useCallback(() => {
if (!isMinimized.current) {
isMinimized.current = true;
bottomSheetRef.current?.close();
}
}, []);
const handleRestore = useCallback(() => {
if (isMinimized.current) {
isMinimized.current = false;
bottomSheetRef.current?.snapTo(nextIndexRef.current, true);
}
}, []);
const handleOnUnmount = useCallback(() => {
handleDismiss(true);
}, [handleDismiss]);
//#endregion

//#region expose public methods
useImperativeHandle(ref, () => ({
present: handlePresent,
Expand All @@ -192,7 +201,7 @@ const BottomSheetModalComponent = forwardRef<

// render
return mount ? (
<Portal key={key} name={key}>
<Portal key={key} name={key} handleOnUnmount={handleOnUnmount}>
<BottomSheet
{...bottomSheetProps}
ref={bottomSheetRef}
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1181,10 +1181,10 @@
minimatch "^3.0.4"
strip-json-comments "^3.1.1"

"@gorhom/portal@^0.1.4":
version "0.1.4"
resolved "https://registry.yarnpkg.com/@gorhom/portal/-/portal-0.1.4.tgz#ef9e50d4b6c98ebe606a16d22d6cb58d661421b4"
integrity sha512-iU3D0i9NureT5ULTvD8moF2FWyFvVGcr/ahcRMDzBblUO1AwwixZRZ+Lf3d6uk0w4ujOQZ7+Az+uUnI+AsXXBw==
"@gorhom/portal@^0.2.0":
version "0.2.0"
resolved "https://registry.yarnpkg.com/@gorhom/portal/-/portal-0.2.0.tgz#65423726b1352e2b9e3c3b8da83a58e4dff48ce6"
integrity sha512-PoYYSliPCl624ORih+zyI1pVbH2Y2Mj+EY6nXeAIi2r1LGv10hItZLzdgnbPPfQ0neikTDYArlOzuB5zKIex5w==
dependencies:
lodash.isequal "^4.5.0"
nanoid "^3.1.20"
Expand Down