Skip to content

Commit f46a4cb

Browse files
authored
Make react-native-avoid-softinput an optional dependency (48) (#820)
* Make `react-native-avoid-softinput` an optional dependency * Update imports of optional dependency * Update comment
1 parent f2ec1eb commit f46a4cb

File tree

3 files changed

+48
-17
lines changed

3 files changed

+48
-17
lines changed

packages/core/package.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
"lodash.isnumber": "^3.0.3",
5757
"lodash.omit": "^4.5.0",
5858
"lodash.tonumber": "^4.0.3",
59-
"react-native-avoid-softinput": "^4.0.1",
6059
"react-native-confirmation-code-field": "^7.3.1",
6160
"react-native-deck-swiper": "^2.0.12",
6261
"react-native-dropdown-picker": "^5.4.7-beta.1",
@@ -74,14 +73,23 @@
7473
"react-native-youtube-iframe": "^2.2.2",
7574
"react-youtube": "^10.1.0"
7675
},
76+
"peerDependencies": {
77+
"react-native-avoid-softinput": "^4.0.1"
78+
},
79+
"peerDependenciesMeta": {
80+
"react-native-avoid-softinput": {
81+
"optional": true
82+
}
83+
},
7784
"devDependencies": {
7885
"@babel/plugin-proposal-class-properties": "^7.18.6",
7986
"@babel/plugin-proposal-export-namespace-from": "^7.18.9",
8087
"@types/color": "^3.0.1",
8188
"@types/dateformat": "^3.0.1",
8289
"@types/lodash.isnumber": "^3.0.6",
8390
"@types/lodash.omit": "^4.5.6",
84-
"@types/lodash.tonumber": "^4.0.6"
91+
"@types/lodash.tonumber": "^4.0.6",
92+
"react-native-avoid-softinput": "^4.0.1"
8593
},
8694
"eslintIgnore": [
8795
"node_modules/",

packages/core/src/components/AvoidKeyboardView.tsx

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
11
import React from "react";
2-
import {
3-
AvoidSoftInput,
4-
AvoidSoftInputView,
2+
import { View } from "react-native";
3+
import type {
4+
AvoidSoftInput as AvoidSoftInputType,
5+
AvoidSoftInputView as AvoidSoftInputViewType,
56
AvoidSoftInputViewProps,
67
} from "react-native-avoid-softinput";
78

9+
// `react-native-avoid-softinput` is an optional dependency and there is a possibility that it is not installed resulting in undefined references
10+
let AvoidSoftInput: typeof AvoidSoftInputType | null = null;
11+
let AvoidSoftInputView: typeof AvoidSoftInputViewType | null = null;
12+
13+
try {
14+
const avoidSoftInputPackage = require("react-native-avoid-softinput");
15+
AvoidSoftInput = avoidSoftInputPackage.AvoidSoftInput;
16+
AvoidSoftInputView = avoidSoftInputPackage.AvoidSoftInputView;
17+
} catch (_) {
18+
console.warn(
19+
"AvoidKeyboardView: `react-native-avoid-softinput` is not installed, falling back to `View`. No keyboard avoiding capabilties will be used."
20+
);
21+
}
22+
823
/**
924
* Requires additional setup: https://mateusz1913.github.io/react-native-avoid-softinput/docs/guides
1025
* Cannot be used with Expo Go
@@ -26,19 +41,27 @@ const AvoidKeyboardView: React.FC<AvoidKeyboardViewProps> = ({
2641
...rest
2742
}) => {
2843
React.useEffect(() => {
29-
AvoidSoftInput.setShouldMimicIOSBehavior(true);
44+
if (AvoidSoftInput) {
45+
AvoidSoftInput.setShouldMimicIOSBehavior(true);
46+
}
3047
return () => {
31-
AvoidSoftInput.setShouldMimicIOSBehavior(false);
48+
if (AvoidSoftInput) {
49+
AvoidSoftInput.setShouldMimicIOSBehavior(false);
50+
}
3251
};
3352
}, []);
3453

35-
return (
36-
<AvoidSoftInputView
37-
onSoftInputHidden={onKeyboardHidden}
38-
onSoftInputShown={onKeyboardShown}
39-
{...rest}
40-
/>
41-
);
54+
if (AvoidSoftInputView) {
55+
return (
56+
<AvoidSoftInputView
57+
onSoftInputHidden={onKeyboardHidden}
58+
onSoftInputShown={onKeyboardShown}
59+
{...rest}
60+
/>
61+
);
62+
} else {
63+
return <View {...rest} />;
64+
}
4265
};
4366

4467
export default AvoidKeyboardView;

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15316,9 +15316,9 @@ react-is@^16.13.0, react-is@^16.13.1, react-is@^16.7.0:
1531615316
integrity sha512-w2GsyukL62IJnlaff/nRegPQR94C/XXamvMWmSHRJ4y7Ts/4ocGRmTHvOs8PSE6pB3dWOrD/nueuU5sduBsQ4w==
1531715317

1531815318
react-native-avoid-softinput@^4.0.1:
15319-
version "4.0.1"
15320-
resolved "https://registry.yarnpkg.com/react-native-avoid-softinput/-/react-native-avoid-softinput-4.0.1.tgz#c08802f66c46ddc91f72d05d1b80644102f80a30"
15321-
integrity sha512-feMt+Pb/wEcuobbIRDHXj1leXT15uC8CekgwMb/t8s61kWy5ifCGtX/YqDZMDRiD0sqeFLZx0gykLQOSrCpybA==
15319+
version "4.0.2"
15320+
resolved "https://registry.yarnpkg.com/react-native-avoid-softinput/-/react-native-avoid-softinput-4.0.2.tgz#30825e7666b530c5d3e555a7778bd50f1431d0c3"
15321+
integrity sha512-/i8xNWGjpu3hEPCVjW2WsWTsM/mC4zTBr3DabOrdPfgw9Ww0Na69YQY1TSXCjbTjJmWwfTToPK5OEVm+ZwHmiQ==
1532215322

1532315323
react-native-codegen@^0.71.5:
1532415324
version "0.71.5"

0 commit comments

Comments
 (0)