Skip to content

Commit a27c256

Browse files
authored
Make react-native-avoid-softinput an optional dependency (#819)
* Make `react-native-avoid-softinput` an optional dependency * Update imports of optional dependency * Update comment
1 parent 868c424 commit a27c256

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
@@ -57,7 +57,6 @@
5757
"lodash.isnumber": "^3.0.3",
5858
"lodash.omit": "^4.5.0",
5959
"lodash.tonumber": "^4.0.3",
60-
"react-native-avoid-softinput": "^4.0.1",
6160
"react-native-confirmation-code-field": "^7.3.1",
6261
"react-native-deck-swiper": "^2.0.12",
6362
"react-native-dropdown-picker": "^5.4.7-beta.1",
@@ -75,14 +74,23 @@
7574
"react-native-youtube-iframe": "^2.2.2",
7675
"react-youtube": "^10.1.0"
7776
},
77+
"peerDependencies": {
78+
"react-native-avoid-softinput": "^4.0.1"
79+
},
80+
"peerDependenciesMeta": {
81+
"react-native-avoid-softinput": {
82+
"optional": true
83+
}
84+
},
7885
"devDependencies": {
7986
"@babel/plugin-proposal-class-properties": "^7.18.6",
8087
"@babel/plugin-proposal-export-namespace-from": "^7.18.9",
8188
"@types/color": "^3.0.1",
8289
"@types/dateformat": "^3.0.1",
8390
"@types/lodash.isnumber": "^3.0.6",
8491
"@types/lodash.omit": "^4.5.6",
85-
"@types/lodash.tonumber": "^4.0.6"
92+
"@types/lodash.tonumber": "^4.0.6",
93+
"react-native-avoid-softinput": "^4.0.1"
8694
},
8795
"eslintIgnore": [
8896
"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
@@ -12782,9 +12782,9 @@ react-is@^16.13.0, react-is@^16.13.1, react-is@^16.7.0:
1278212782
integrity sha512-w2GsyukL62IJnlaff/nRegPQR94C/XXamvMWmSHRJ4y7Ts/4ocGRmTHvOs8PSE6pB3dWOrD/nueuU5sduBsQ4w==
1278312783

1278412784
react-native-avoid-softinput@^4.0.1:
12785-
version "4.0.1"
12786-
resolved "https://registry.yarnpkg.com/react-native-avoid-softinput/-/react-native-avoid-softinput-4.0.1.tgz#c08802f66c46ddc91f72d05d1b80644102f80a30"
12787-
integrity sha512-feMt+Pb/wEcuobbIRDHXj1leXT15uC8CekgwMb/t8s61kWy5ifCGtX/YqDZMDRiD0sqeFLZx0gykLQOSrCpybA==
12785+
version "4.0.2"
12786+
resolved "https://registry.yarnpkg.com/react-native-avoid-softinput/-/react-native-avoid-softinput-4.0.2.tgz#30825e7666b530c5d3e555a7778bd50f1431d0c3"
12787+
integrity sha512-/i8xNWGjpu3hEPCVjW2WsWTsM/mC4zTBr3DabOrdPfgw9Ww0Na69YQY1TSXCjbTjJmWwfTToPK5OEVm+ZwHmiQ==
1278812788

1278912789
react-native-confirmation-code-field@^7.3.1:
1279012790
version "7.3.1"

0 commit comments

Comments
 (0)