@react-native-ohos/react-native-keyboard-controller 1.12.8 → 1.12.10-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (166) hide show
  1. package/LICENSE +21 -21
  2. package/README.OpenSource +10 -10
  3. package/README.md +603 -19
  4. package/harmony/keyboard_controller/BuildProfile.ets +16 -16
  5. package/harmony/keyboard_controller/Index.ets +8 -29
  6. package/harmony/keyboard_controller/ResourceTable.txt +8 -8
  7. package/harmony/keyboard_controller/build-profile.json5 +31 -31
  8. package/harmony/keyboard_controller/hvigorfile.ts +6 -6
  9. package/harmony/keyboard_controller/obfuscation-rules.txt +17 -17
  10. package/harmony/keyboard_controller/oh-package-lock.json5 +28 -18
  11. package/harmony/keyboard_controller/oh-package.json5 +11 -11
  12. package/harmony/keyboard_controller/src/main/cpp/CMakeLists.txt +7 -7
  13. package/harmony/keyboard_controller/src/main/cpp/EventEmitters.cpp +119 -96
  14. package/harmony/keyboard_controller/src/main/cpp/EventEmitters.h +90 -111
  15. package/harmony/keyboard_controller/src/main/cpp/KeyboardControllerViewComponentDescriptor.h +77 -98
  16. package/harmony/keyboard_controller/src/main/cpp/KeyboardControllerViewComponentInstance.cpp +837 -274
  17. package/harmony/keyboard_controller/src/main/cpp/KeyboardControllerViewComponentInstance.h +152 -101
  18. package/harmony/keyboard_controller/src/main/cpp/KeyboardControllerViewJSIBinder.h +43 -64
  19. package/harmony/keyboard_controller/src/main/cpp/KeyboardGestureAreaComponentDescriptor.h +64 -85
  20. package/harmony/keyboard_controller/src/main/cpp/KeyboardGestureAreaComponentInstance.cpp +88 -109
  21. package/harmony/keyboard_controller/src/main/cpp/KeyboardGestureAreaComponentInstance.h +63 -84
  22. package/harmony/keyboard_controller/src/main/cpp/KeyboardGestureAreaJSIBinder.h +39 -60
  23. package/harmony/keyboard_controller/src/main/cpp/RNKeyboardController.cpp +25 -46
  24. package/harmony/keyboard_controller/src/main/cpp/RNKeyboardController.h +27 -48
  25. package/harmony/keyboard_controller/src/main/cpp/RNKeyboardControllerPackage.cpp +43 -64
  26. package/harmony/keyboard_controller/src/main/cpp/RNKeyboardControllerPackage.h +56 -77
  27. package/harmony/keyboard_controller/src/main/cpp/RNStatusBarManagerCompat.cpp +24 -45
  28. package/harmony/keyboard_controller/src/main/cpp/RNStatusBarManagerCompat.h +28 -49
  29. package/harmony/keyboard_controller/src/main/cpp/ViewHierarchyNavigator.cpp +186 -186
  30. package/harmony/keyboard_controller/src/main/cpp/ViewHierarchyNavigator.h +112 -112
  31. package/harmony/keyboard_controller/src/main/cpp/keyboardControllerPackage.cpp +43 -64
  32. package/harmony/keyboard_controller/src/main/cpp/keyboardControllerPackage.h +56 -77
  33. package/harmony/keyboard_controller/src/main/ets/Logger.ts +45 -66
  34. package/harmony/keyboard_controller/src/main/ets/RNKeyboardControllerPackage.ets +49 -70
  35. package/harmony/keyboard_controller/src/main/ets/RNKeyboardControllerTurboModule.ts +405 -245
  36. package/harmony/keyboard_controller/src/main/ets/RNStatusBarManagerCompatTurboModule.ts +107 -119
  37. package/harmony/keyboard_controller/src/main/ets/Type.ts +41 -62
  38. package/harmony/keyboard_controller/src/main/module.json +29 -29
  39. package/harmony/keyboard_controller/src/main/module.json5 +11 -11
  40. package/harmony/keyboard_controller/src/main/resources/base/element/string.json +8 -8
  41. package/harmony/keyboard_controller/src/main/resources/en_US/element/string.json +8 -8
  42. package/harmony/keyboard_controller/src/main/resources/zh_CN/element/string.json +8 -8
  43. package/harmony/keyboard_controller/ts.ets +8 -29
  44. package/harmony/keyboard_controller.har +0 -0
  45. package/lib/commonjs/animated.js +4 -16
  46. package/lib/commonjs/animated.js.map +1 -1
  47. package/lib/commonjs/bindings.js +3 -3
  48. package/lib/commonjs/bindings.js.map +1 -1
  49. package/lib/commonjs/bindings.native.js +3 -3
  50. package/lib/commonjs/bindings.native.js.map +1 -1
  51. package/lib/commonjs/components/KeyboardAvoidingView/hooks.js.map +1 -1
  52. package/lib/commonjs/components/KeyboardAvoidingView/index.js +3 -3
  53. package/lib/commonjs/components/KeyboardAvoidingView/index.js.map +1 -1
  54. package/lib/commonjs/components/KeyboardAwareScrollView/index.js +39 -39
  55. package/lib/commonjs/components/KeyboardAwareScrollView/index.js.map +1 -1
  56. package/lib/commonjs/components/KeyboardAwareScrollView/useSmoothKeyboardHandler.js +3 -3
  57. package/lib/commonjs/components/KeyboardAwareScrollView/useSmoothKeyboardHandler.js.map +1 -1
  58. package/lib/commonjs/components/KeyboardAwareScrollView/utils.js.map +1 -1
  59. package/lib/commonjs/components/KeyboardStickyView/index.js.map +1 -1
  60. package/lib/commonjs/components/KeyboardToolbar/Arrow.js.map +1 -1
  61. package/lib/commonjs/components/KeyboardToolbar/Button.js.map +1 -1
  62. package/lib/commonjs/components/KeyboardToolbar/colors.js.map +1 -1
  63. package/lib/commonjs/components/KeyboardToolbar/colors.native.js.map +1 -1
  64. package/lib/commonjs/components/KeyboardToolbar/index.js +3 -3
  65. package/lib/commonjs/components/KeyboardToolbar/index.js.map +1 -1
  66. package/lib/commonjs/components/KeyboardToolbar/types.js.map +1 -1
  67. package/lib/commonjs/components/hooks/useColorScheme.js.map +1 -1
  68. package/lib/commonjs/components/index.js.map +1 -1
  69. package/lib/commonjs/constants.js.map +1 -1
  70. package/lib/commonjs/context.js.map +1 -1
  71. package/lib/commonjs/hooks/index.js.map +1 -1
  72. package/lib/commonjs/hooks/useWindowDimensions/index.android.js.map +1 -1
  73. package/lib/commonjs/hooks/useWindowDimensions/index.js.map +1 -1
  74. package/lib/commonjs/index.js.map +1 -1
  75. package/lib/commonjs/internal.js +18 -18
  76. package/lib/commonjs/internal.js.map +1 -1
  77. package/lib/commonjs/monkey-patch.android.js +4 -4
  78. package/lib/commonjs/monkey-patch.android.js.map +1 -1
  79. package/lib/commonjs/monkey-patch.js.map +1 -1
  80. package/lib/commonjs/reanimated.js.map +1 -1
  81. package/lib/commonjs/reanimated.native.js.map +1 -1
  82. package/lib/commonjs/specs/KeyboardControllerViewNativeComponent.js.map +1 -1
  83. package/lib/commonjs/specs/KeyboardGestureAreaNativeComponent.js.map +1 -1
  84. package/lib/commonjs/specs/NativeKeyboardController.js.map +1 -1
  85. package/lib/commonjs/specs/NativeStatusBarManagerCompat.js.map +1 -1
  86. package/lib/commonjs/types.js.map +1 -1
  87. package/lib/commonjs/utils.js.map +1 -1
  88. package/lib/module/animated.js +5 -17
  89. package/lib/module/animated.js.map +1 -1
  90. package/lib/module/bindings.js +3 -3
  91. package/lib/module/bindings.js.map +1 -1
  92. package/lib/module/bindings.native.js +3 -3
  93. package/lib/module/bindings.native.js.map +1 -1
  94. package/lib/module/components/KeyboardAvoidingView/hooks.js.map +1 -1
  95. package/lib/module/components/KeyboardAvoidingView/index.js +3 -3
  96. package/lib/module/components/KeyboardAvoidingView/index.js.map +1 -1
  97. package/lib/module/components/KeyboardAwareScrollView/index.js +39 -39
  98. package/lib/module/components/KeyboardAwareScrollView/index.js.map +1 -1
  99. package/lib/module/components/KeyboardAwareScrollView/useSmoothKeyboardHandler.js +3 -3
  100. package/lib/module/components/KeyboardAwareScrollView/useSmoothKeyboardHandler.js.map +1 -1
  101. package/lib/module/components/KeyboardAwareScrollView/utils.js.map +1 -1
  102. package/lib/module/components/KeyboardStickyView/index.js.map +1 -1
  103. package/lib/module/components/KeyboardToolbar/Arrow.js.map +1 -1
  104. package/lib/module/components/KeyboardToolbar/Button.js.map +1 -1
  105. package/lib/module/components/KeyboardToolbar/colors.js.map +1 -1
  106. package/lib/module/components/KeyboardToolbar/colors.native.js.map +1 -1
  107. package/lib/module/components/KeyboardToolbar/index.js +3 -3
  108. package/lib/module/components/KeyboardToolbar/index.js.map +1 -1
  109. package/lib/module/components/KeyboardToolbar/types.js.map +1 -1
  110. package/lib/module/components/hooks/useColorScheme.js.map +1 -1
  111. package/lib/module/components/index.js.map +1 -1
  112. package/lib/module/constants.js.map +1 -1
  113. package/lib/module/context.js.map +1 -1
  114. package/lib/module/hooks/index.js.map +1 -1
  115. package/lib/module/hooks/useWindowDimensions/index.android.js.map +1 -1
  116. package/lib/module/hooks/useWindowDimensions/index.js.map +1 -1
  117. package/lib/module/index.js.map +1 -1
  118. package/lib/module/internal.js +18 -18
  119. package/lib/module/internal.js.map +1 -1
  120. package/lib/module/monkey-patch.android.js +4 -4
  121. package/lib/module/monkey-patch.android.js.map +1 -1
  122. package/lib/module/monkey-patch.js.map +1 -1
  123. package/lib/module/reanimated.js.map +1 -1
  124. package/lib/module/reanimated.native.js.map +1 -1
  125. package/lib/module/specs/KeyboardControllerViewNativeComponent.js.map +1 -1
  126. package/lib/module/specs/KeyboardGestureAreaNativeComponent.js.map +1 -1
  127. package/lib/module/specs/NativeKeyboardController.js.map +1 -1
  128. package/lib/module/specs/NativeStatusBarManagerCompat.js.map +1 -1
  129. package/lib/module/types.js.map +1 -1
  130. package/lib/module/utils.js.map +1 -1
  131. package/lib/typescript/types.d.ts +1 -0
  132. package/package.json +181 -178
  133. package/src/animated.tsx +246 -253
  134. package/src/bindings.native.ts +57 -57
  135. package/src/bindings.ts +38 -38
  136. package/src/components/KeyboardAvoidingView/hooks.ts +58 -58
  137. package/src/components/KeyboardAvoidingView/index.tsx +156 -156
  138. package/src/components/KeyboardAwareScrollView/index.tsx +373 -373
  139. package/src/components/KeyboardAwareScrollView/useSmoothKeyboardHandler.ts +133 -133
  140. package/src/components/KeyboardAwareScrollView/utils.ts +41 -41
  141. package/src/components/KeyboardStickyView/index.tsx +57 -57
  142. package/src/components/KeyboardToolbar/Arrow.tsx +80 -80
  143. package/src/components/KeyboardToolbar/Button.tsx +98 -98
  144. package/src/components/KeyboardToolbar/colors.native.ts +37 -37
  145. package/src/components/KeyboardToolbar/colors.ts +16 -16
  146. package/src/components/KeyboardToolbar/index.tsx +211 -211
  147. package/src/components/KeyboardToolbar/types.ts +40 -40
  148. package/src/components/hooks/useColorScheme.ts +5 -5
  149. package/src/components/index.ts +11 -11
  150. package/src/constants.ts +19 -19
  151. package/src/context.ts +65 -65
  152. package/src/hooks/index.ts +90 -90
  153. package/src/hooks/useWindowDimensions/index.android.ts +33 -33
  154. package/src/hooks/useWindowDimensions/index.ts +1 -1
  155. package/src/index.ts +21 -21
  156. package/src/internal.ts +84 -84
  157. package/src/monkey-patch.android.ts +44 -44
  158. package/src/monkey-patch.ts +4 -4
  159. package/src/reanimated.native.ts +132 -132
  160. package/src/reanimated.ts +29 -29
  161. package/src/specs/KeyboardControllerViewNativeComponent.ts +70 -70
  162. package/src/specs/KeyboardGestureAreaNativeComponent.ts +15 -15
  163. package/src/specs/NativeKeyboardController.ts +19 -19
  164. package/src/specs/NativeStatusBarManagerCompat.ts +14 -14
  165. package/src/types.ts +212 -211
  166. package/src/utils.ts +1 -1
package/src/bindings.ts CHANGED
@@ -1,38 +1,38 @@
1
- import { View } from "react-native";
2
-
3
- import type {
4
- FocusedInputEventsModule,
5
- KeyboardControllerModule,
6
- KeyboardControllerProps,
7
- KeyboardEventsModule,
8
- KeyboardGestureAreaProps,
9
- WindowDimensionsEventsModule,
10
- } from "./types";
11
- import type { EmitterSubscription } from "react-native";
12
-
13
- const NOOP = () => {};
14
- export const KeyboardController: KeyboardControllerModule = {
15
- setDefaultMode: NOOP,
16
- setInputMode: NOOP,
17
- dismiss: NOOP,
18
- setFocusTo: NOOP,
19
- addListener: NOOP,
20
- removeListeners: NOOP,
21
- };
22
- export const KeyboardEvents: KeyboardEventsModule = {
23
- addListener: () => ({ remove: NOOP } as EmitterSubscription),
24
- };
25
- /**
26
- * This API is not documented, it's for internal usage only (for now), and is a subject to potential breaking changes in future.
27
- * Use it with cautious.
28
- */
29
- export const FocusedInputEvents: FocusedInputEventsModule = {
30
- addListener: () => ({ remove: NOOP } as EmitterSubscription),
31
- };
32
- export const WindowDimensionsEvents: WindowDimensionsEventsModule = {
33
- addListener: () => ({ remove: NOOP } as EmitterSubscription),
34
- };
35
- export const KeyboardControllerView =
36
- View as unknown as React.FC<KeyboardControllerProps>;
37
- export const KeyboardGestureArea =
38
- View as unknown as React.FC<KeyboardGestureAreaProps>;
1
+ import { View } from "react-native";
2
+
3
+ import type {
4
+ FocusedInputEventsModule,
5
+ KeyboardControllerModule,
6
+ KeyboardControllerProps,
7
+ KeyboardEventsModule,
8
+ KeyboardGestureAreaProps,
9
+ WindowDimensionsEventsModule,
10
+ } from "./types";
11
+ import type { EmitterSubscription } from "react-native";
12
+
13
+ const NOOP = () => {};
14
+ export const KeyboardController: KeyboardControllerModule = {
15
+ setDefaultMode: NOOP,
16
+ setInputMode: NOOP,
17
+ dismiss: NOOP,
18
+ setFocusTo: NOOP,
19
+ addListener: NOOP,
20
+ removeListeners: NOOP,
21
+ };
22
+ export const KeyboardEvents: KeyboardEventsModule = {
23
+ addListener: () => ({ remove: NOOP } as EmitterSubscription),
24
+ };
25
+ /**
26
+ * This API is not documented, it's for internal usage only (for now), and is a subject to potential breaking changes in future.
27
+ * Use it with cautious.
28
+ */
29
+ export const FocusedInputEvents: FocusedInputEventsModule = {
30
+ addListener: () => ({ remove: NOOP } as EmitterSubscription),
31
+ };
32
+ export const WindowDimensionsEvents: WindowDimensionsEventsModule = {
33
+ addListener: () => ({ remove: NOOP } as EmitterSubscription),
34
+ };
35
+ export const KeyboardControllerView =
36
+ View as unknown as React.FC<KeyboardControllerProps>;
37
+ export const KeyboardGestureArea =
38
+ View as unknown as React.FC<KeyboardGestureAreaProps>;
@@ -1,58 +1,58 @@
1
- import { useSharedValue } from "react-native-reanimated";
2
-
3
- import {useKeyboardHandler} from "../../hooks";
4
- import {useKeyboardContext} from "../../context";
5
- import { Platform } from "react-native";
6
-
7
- export const useKeyboardAnimation = () => {
8
- const { reanimated } = useKeyboardContext();
9
- const heightWhenOpened = useSharedValue(-reanimated.height.value);
10
- const height = useSharedValue(-reanimated.height.value);
11
- const progress = useSharedValue(reanimated.progress.value);
12
- const isClosed = useSharedValue(reanimated.progress.value === 0);
13
-
14
- useKeyboardHandler(
15
- {
16
- onStart: (e) => {
17
- "worklet";
18
-
19
- if (e.height > 0) {
20
- // eslint-disable-next-line react-compiler/react-compiler
21
- isClosed.value = false;
22
- heightWhenOpened.value = e.height;
23
- }
24
- },
25
- onMove: (e) => {
26
- "worklet";
27
-
28
- progress.value = e.progress;
29
- height.value = e.height;
30
- },
31
- onEnd: (e) => {
32
- "worklet";
33
- if(Platform.OS as string === 'harmony'){
34
- heightWhenOpened.value = e.height;
35
- }
36
-
37
- isClosed.value = e.height === 0;
38
-
39
- // `height` update happens in `onMove` handler
40
- // in `onEnd` we need to update only if `onMove`
41
- // wasn't called (i. e. duration === 0)
42
- //
43
- // we can not call this code without condition below
44
- // because in some corner cases (iOS with `secureTextEntry`)
45
- // `onEnd` can be emitted before `onMove` and in this case
46
- // it may lead to choppy/glitchy/jumpy UI
47
- // see https://github.com/kirillzyusko/react-native-keyboard-controller/issues/327
48
- if (e.duration === 0) {
49
- progress.value = e.progress;
50
- height.value = e.height;
51
- }
52
- },
53
- },
54
- [],
55
- );
56
-
57
- return { height, progress, heightWhenOpened, isClosed };
58
- };
1
+ import { useSharedValue } from "react-native-reanimated";
2
+
3
+ import {useKeyboardHandler} from "../../hooks";
4
+ import {useKeyboardContext} from "../../context";
5
+ import { Platform } from "react-native";
6
+
7
+ export const useKeyboardAnimation = () => {
8
+ const { reanimated } = useKeyboardContext();
9
+ const heightWhenOpened = useSharedValue(-reanimated.height.value);
10
+ const height = useSharedValue(-reanimated.height.value);
11
+ const progress = useSharedValue(reanimated.progress.value);
12
+ const isClosed = useSharedValue(reanimated.progress.value === 0);
13
+
14
+ useKeyboardHandler(
15
+ {
16
+ onStart: (e) => {
17
+ "worklet";
18
+
19
+ if (e.height > 0) {
20
+ // eslint-disable-next-line react-compiler/react-compiler
21
+ isClosed.value = false;
22
+ heightWhenOpened.value = e.height;
23
+ }
24
+ },
25
+ onMove: (e) => {
26
+ "worklet";
27
+
28
+ progress.value = e.progress;
29
+ height.value = e.height;
30
+ },
31
+ onEnd: (e) => {
32
+ "worklet";
33
+ if(Platform.OS as string === 'harmony'){
34
+ heightWhenOpened.value = e.height;
35
+ }
36
+
37
+ isClosed.value = e.height === 0;
38
+
39
+ // `height` update happens in `onMove` handler
40
+ // in `onEnd` we need to update only if `onMove`
41
+ // wasn't called (i. e. duration === 0)
42
+ //
43
+ // we can not call this code without condition below
44
+ // because in some corner cases (iOS with `secureTextEntry`)
45
+ // `onEnd` can be emitted before `onMove` and in this case
46
+ // it may lead to choppy/glitchy/jumpy UI
47
+ // see https://github.com/kirillzyusko/react-native-keyboard-controller/issues/327
48
+ if (e.duration === 0) {
49
+ progress.value = e.progress;
50
+ height.value = e.height;
51
+ }
52
+ },
53
+ },
54
+ [],
55
+ );
56
+
57
+ return { height, progress, heightWhenOpened, isClosed };
58
+ };
@@ -1,156 +1,156 @@
1
- import React, { forwardRef, useCallback, useMemo } from "react";
2
- import { View } from "react-native";
3
- import Reanimated, {
4
- interpolate,
5
- runOnUI,
6
- useAnimatedStyle,
7
- useDerivedValue,
8
- useSharedValue,
9
- } from "react-native-reanimated";
10
-
11
- import { useWindowDimensions } from "../../hooks";
12
-
13
- import { useKeyboardAnimation } from "./hooks";
14
-
15
- import type { LayoutRectangle, ViewProps } from "react-native";
16
-
17
- export type KeyboardAvoidingViewProps = {
18
- /**
19
- * Specify how to react to the presence of the keyboard.
20
- */
21
- behavior?: "height" | "position" | "padding";
22
-
23
- /**
24
- * Style of the content container when `behavior` is 'position'.
25
- */
26
- contentContainerStyle?: ViewProps["style"];
27
-
28
- /**
29
- * Controls whether this `KeyboardAvoidingView` instance should take effect.
30
- * This is useful when more than one is on the screen. Defaults to true.
31
- */
32
- enabled?: boolean;
33
-
34
- /**
35
- * Distance between the top of the user screen and the React Native view. This
36
- * may be non-zero in some cases. Defaults to 0.
37
- */
38
- keyboardVerticalOffset?: number;
39
- } & ViewProps;
40
-
41
- const defaultLayout: LayoutRectangle = {
42
- x: 0,
43
- y: 0,
44
- width: 0,
45
- height: 0,
46
- };
47
-
48
- /**
49
- * View that moves out of the way when the keyboard appears by automatically
50
- * adjusting its height, position, or bottom padding.
51
- */
52
- const KeyboardAvoidingView = forwardRef<
53
- View,
54
- React.PropsWithChildren<KeyboardAvoidingViewProps>
55
- >(
56
- (
57
- {
58
- behavior,
59
- children,
60
- contentContainerStyle,
61
- enabled = true,
62
- keyboardVerticalOffset = 0,
63
- style,
64
- onLayout: onLayoutProps,
65
- ...props
66
- },
67
- ref,
68
- ) => {
69
- const initialFrame = useSharedValue<LayoutRectangle | null>(null);
70
- const frame = useDerivedValue(() => initialFrame.value || defaultLayout);
71
-
72
- const keyboard = useKeyboardAnimation();
73
- const { height: screenHeight } = useWindowDimensions();
74
-
75
- const relativeKeyboardHeight = useCallback(() => {
76
- "worklet";
77
-
78
- const keyboardY =
79
- screenHeight - keyboard.heightWhenOpened.value - keyboardVerticalOffset;
80
-
81
- return Math.max(frame.value.y + frame.value.height - keyboardY, 0);
82
- }, [screenHeight, keyboardVerticalOffset]);
83
-
84
- const onLayoutWorklet = useCallback((layout: LayoutRectangle) => {
85
- "worklet";
86
-
87
- if (keyboard.isClosed.value || initialFrame.value === null) {
88
- // eslint-disable-next-line react-compiler/react-compiler
89
- initialFrame.value = layout;
90
- }
91
- }, []);
92
- const onLayout = useCallback<NonNullable<ViewProps["onLayout"]>>(
93
- (e) => {
94
- runOnUI(onLayoutWorklet)(e.nativeEvent.layout);
95
- onLayoutProps?.(e);
96
- },
97
- [onLayoutProps],
98
- );
99
-
100
- const animatedStyle = useAnimatedStyle(() => {
101
- const bottom = interpolate(
102
- keyboard.progress.value,
103
- [0, 1],
104
- [0, relativeKeyboardHeight()],
105
- );
106
- const bottomHeight = enabled ? bottom : 0;
107
-
108
- switch (behavior) {
109
- case "height":
110
- if (!keyboard.isClosed.value) {
111
- return {
112
- height: frame.value.height - bottomHeight,
113
- flex: 0,
114
- };
115
- }
116
-
117
- return {};
118
-
119
- case "position":
120
- return { bottom: bottomHeight };
121
-
122
- case "padding":
123
- return { paddingBottom: bottomHeight };
124
- default:
125
- return {};
126
- }
127
- }, [behavior, enabled, relativeKeyboardHeight]);
128
- const isPositionBehavior = behavior === "position";
129
- const containerStyle = isPositionBehavior ? contentContainerStyle : style;
130
- const combinedStyles = useMemo(
131
- () => [containerStyle, animatedStyle],
132
- [containerStyle, animatedStyle],
133
- );
134
-
135
- if (isPositionBehavior) {
136
- return (
137
- <View ref={ref} style={style} onLayout={onLayout} {...props}>
138
- <Reanimated.View style={combinedStyles}>{children}</Reanimated.View>
139
- </View>
140
- );
141
- }
142
-
143
- return (
144
- <Reanimated.View
145
- ref={ref}
146
- onLayout={onLayout}
147
- style={combinedStyles}
148
- {...props}
149
- >
150
- {children}
151
- </Reanimated.View>
152
- );
153
- },
154
- );
155
-
156
- export default KeyboardAvoidingView;
1
+ import React, { forwardRef, useCallback, useMemo } from "react";
2
+ import { View } from "react-native";
3
+ import Reanimated, {
4
+ interpolate,
5
+ runOnUI,
6
+ useAnimatedStyle,
7
+ useDerivedValue,
8
+ useSharedValue,
9
+ } from "react-native-reanimated";
10
+
11
+ import { useWindowDimensions } from "../../hooks";
12
+
13
+ import { useKeyboardAnimation } from "./hooks";
14
+
15
+ import type { LayoutRectangle, ViewProps } from "react-native";
16
+
17
+ export type KeyboardAvoidingViewProps = {
18
+ /**
19
+ * Specify how to react to the presence of the keyboard.
20
+ */
21
+ behavior?: "height" | "position" | "padding";
22
+
23
+ /**
24
+ * Style of the content container when `behavior` is 'position'.
25
+ */
26
+ contentContainerStyle?: ViewProps["style"];
27
+
28
+ /**
29
+ * Controls whether this `KeyboardAvoidingView` instance should take effect.
30
+ * This is useful when more than one is on the screen. Defaults to true.
31
+ */
32
+ enabled?: boolean;
33
+
34
+ /**
35
+ * Distance between the top of the user screen and the React Native view. This
36
+ * may be non-zero in some cases. Defaults to 0.
37
+ */
38
+ keyboardVerticalOffset?: number;
39
+ } & ViewProps;
40
+
41
+ const defaultLayout: LayoutRectangle = {
42
+ x: 0,
43
+ y: 0,
44
+ width: 0,
45
+ height: 0,
46
+ };
47
+
48
+ /**
49
+ * View that moves out of the way when the keyboard appears by automatically
50
+ * adjusting its height, position, or bottom padding.
51
+ */
52
+ const KeyboardAvoidingView = forwardRef<
53
+ View,
54
+ React.PropsWithChildren<KeyboardAvoidingViewProps>
55
+ >(
56
+ (
57
+ {
58
+ behavior,
59
+ children,
60
+ contentContainerStyle,
61
+ enabled = true,
62
+ keyboardVerticalOffset = 0,
63
+ style,
64
+ onLayout: onLayoutProps,
65
+ ...props
66
+ },
67
+ ref,
68
+ ) => {
69
+ const initialFrame = useSharedValue<LayoutRectangle | null>(null);
70
+ const frame = useDerivedValue(() => initialFrame.value || defaultLayout);
71
+
72
+ const keyboard = useKeyboardAnimation();
73
+ const { height: screenHeight } = useWindowDimensions();
74
+
75
+ const relativeKeyboardHeight = useCallback(() => {
76
+ "worklet";
77
+
78
+ const keyboardY =
79
+ screenHeight - keyboard.heightWhenOpened.value - keyboardVerticalOffset;
80
+
81
+ return Math.max(frame.value.y + frame.value.height - keyboardY, 0);
82
+ }, [screenHeight, keyboardVerticalOffset]);
83
+
84
+ const onLayoutWorklet = useCallback((layout: LayoutRectangle) => {
85
+ "worklet";
86
+
87
+ if (keyboard.isClosed.value || initialFrame.value === null) {
88
+ // eslint-disable-next-line react-compiler/react-compiler
89
+ initialFrame.value = layout;
90
+ }
91
+ }, []);
92
+ const onLayout = useCallback<NonNullable<ViewProps["onLayout"]>>(
93
+ (e) => {
94
+ runOnUI(onLayoutWorklet)(e.nativeEvent.layout);
95
+ onLayoutProps?.(e);
96
+ },
97
+ [onLayoutProps],
98
+ );
99
+
100
+ const animatedStyle = useAnimatedStyle(() => {
101
+ const bottom = interpolate(
102
+ keyboard.progress.value,
103
+ [0, 1],
104
+ [0, relativeKeyboardHeight()],
105
+ );
106
+ const bottomHeight = enabled ? bottom : 0;
107
+
108
+ switch (behavior) {
109
+ case "height":
110
+ if (!keyboard.isClosed.value) {
111
+ return {
112
+ height: frame.value.height - bottomHeight,
113
+ flex: 0,
114
+ };
115
+ }
116
+
117
+ return {};
118
+
119
+ case "position":
120
+ return { bottom: bottomHeight };
121
+
122
+ case "padding":
123
+ return { paddingBottom: bottomHeight };
124
+ default:
125
+ return {};
126
+ }
127
+ }, [behavior, enabled, relativeKeyboardHeight]);
128
+ const isPositionBehavior = behavior === "position";
129
+ const containerStyle = isPositionBehavior ? contentContainerStyle : style;
130
+ const combinedStyles = useMemo(
131
+ () => [containerStyle, animatedStyle],
132
+ [containerStyle, animatedStyle],
133
+ );
134
+
135
+ if (isPositionBehavior) {
136
+ return (
137
+ <View ref={ref} style={style} onLayout={onLayout} {...props}>
138
+ <Reanimated.View style={combinedStyles}>{children}</Reanimated.View>
139
+ </View>
140
+ );
141
+ }
142
+
143
+ return (
144
+ <Reanimated.View
145
+ ref={ref}
146
+ onLayout={onLayout}
147
+ style={combinedStyles}
148
+ {...props}
149
+ >
150
+ {children}
151
+ </Reanimated.View>
152
+ );
153
+ },
154
+ );
155
+
156
+ export default KeyboardAvoidingView;