react-native-keyboard-controller 1.9.5 → 1.10.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 (169) hide show
  1. package/README.md +1 -1
  2. package/android/src/fabric/java/com/reactnativekeyboardcontroller/KeyboardControllerModule.kt +4 -0
  3. package/android/src/main/java/com/reactnativekeyboardcontroller/events/FocusedInputTextChangedEvent.kt +20 -0
  4. package/android/src/main/java/com/reactnativekeyboardcontroller/extensions/EditText.kt +36 -0
  5. package/android/src/main/java/com/reactnativekeyboardcontroller/extensions/WindowInsetsAnimationCompat.kt +7 -0
  6. package/android/src/main/java/com/reactnativekeyboardcontroller/listeners/FocusedInputObserver.kt +17 -0
  7. package/android/src/main/java/com/reactnativekeyboardcontroller/listeners/KeyboardAnimationCallback.kt +12 -0
  8. package/android/src/main/java/com/reactnativekeyboardcontroller/managers/KeyboardControllerViewManagerImpl.kt +2 -0
  9. package/android/src/main/java/com/reactnativekeyboardcontroller/modules/KeyboardControllerModuleImpl.kt +13 -0
  10. package/android/src/paper/java/com/reactnativekeyboardcontroller/KeyboardControllerModule.kt +5 -0
  11. package/ios/.clang-format +30 -30
  12. package/ios/.swiftlint.yml +1 -1
  13. package/ios/KeyboardController.xcodeproj/project.pbxproj +10 -0
  14. package/ios/KeyboardControllerModule.mm +14 -0
  15. package/ios/events/FocusedInputTextChangedEvent.h +16 -0
  16. package/ios/events/FocusedInputTextChangedEvent.m +72 -0
  17. package/ios/observers/FocusedInputObserver.swift +29 -14
  18. package/ios/observers/TextChangeObserver.swift +41 -0
  19. package/ios/views/KeyboardControllerView.mm +53 -31
  20. package/ios/views/KeyboardControllerViewManager.mm +1 -0
  21. package/ios/views/KeyboardControllerViewManager.swift +21 -10
  22. package/jest/index.js +6 -4
  23. package/lib/commonjs/animated.js +33 -27
  24. package/lib/commonjs/animated.js.map +1 -1
  25. package/lib/commonjs/bindings.js +1 -0
  26. package/lib/commonjs/bindings.js.map +1 -1
  27. package/lib/commonjs/bindings.native.js +6 -6
  28. package/lib/commonjs/bindings.native.js.map +1 -1
  29. package/lib/commonjs/components/KeyboardAvoidingView/hooks.js +5 -3
  30. package/lib/commonjs/components/KeyboardAvoidingView/hooks.js.map +1 -1
  31. package/lib/commonjs/components/KeyboardAvoidingView/index.js +17 -9
  32. package/lib/commonjs/components/KeyboardAvoidingView/index.js.map +1 -1
  33. package/lib/commonjs/components/KeyboardAwareScrollView/index.js +199 -0
  34. package/lib/commonjs/components/KeyboardAwareScrollView/index.js.map +1 -0
  35. package/lib/commonjs/components/KeyboardAwareScrollView/useSmoothKeyboardHandler.js +100 -0
  36. package/lib/commonjs/components/KeyboardAwareScrollView/useSmoothKeyboardHandler.js.map +1 -0
  37. package/lib/commonjs/components/KeyboardAwareScrollView/utils.js +28 -0
  38. package/lib/commonjs/components/KeyboardAwareScrollView/utils.js.map +1 -0
  39. package/lib/commonjs/components/KeyboardStickyView/index.js +7 -4
  40. package/lib/commonjs/components/KeyboardStickyView/index.js.map +1 -1
  41. package/lib/commonjs/components/hooks/useKeyboardInterpolation.js +81 -0
  42. package/lib/commonjs/components/hooks/useKeyboardInterpolation.js.map +1 -0
  43. package/lib/commonjs/components/index.js +7 -0
  44. package/lib/commonjs/components/index.js.map +1 -1
  45. package/lib/commonjs/constants.js.map +1 -1
  46. package/lib/commonjs/context.js +2 -1
  47. package/lib/commonjs/context.js.map +1 -1
  48. package/lib/commonjs/hooks.js +17 -2
  49. package/lib/commonjs/hooks.js.map +1 -1
  50. package/lib/commonjs/index.js +8 -1
  51. package/lib/commonjs/index.js.map +1 -1
  52. package/lib/commonjs/internal.js +3 -6
  53. package/lib/commonjs/internal.js.map +1 -1
  54. package/lib/commonjs/monkey-patch.android.js +1 -1
  55. package/lib/commonjs/monkey-patch.android.js.map +1 -1
  56. package/lib/commonjs/monkey-patch.js.map +1 -1
  57. package/lib/commonjs/reanimated.js +5 -3
  58. package/lib/commonjs/reanimated.js.map +1 -1
  59. package/lib/commonjs/reanimated.native.js +29 -12
  60. package/lib/commonjs/reanimated.native.js.map +1 -1
  61. package/lib/commonjs/replicas.js +7 -5
  62. package/lib/commonjs/replicas.js.map +1 -1
  63. package/lib/commonjs/specs/KeyboardControllerViewNativeComponent.js +1 -1
  64. package/lib/commonjs/specs/KeyboardControllerViewNativeComponent.js.map +1 -1
  65. package/lib/commonjs/specs/KeyboardGestureAreaNativeComponent.js +2 -2
  66. package/lib/commonjs/specs/KeyboardGestureAreaNativeComponent.js.map +1 -1
  67. package/lib/commonjs/specs/NativeKeyboardController.js +1 -1
  68. package/lib/commonjs/specs/NativeKeyboardController.js.map +1 -1
  69. package/lib/commonjs/specs/NativeStatusBarManagerCompat.js +1 -1
  70. package/lib/commonjs/specs/NativeStatusBarManagerCompat.js.map +1 -1
  71. package/lib/commonjs/types.js.map +1 -1
  72. package/lib/commonjs/utils.js.map +1 -1
  73. package/lib/module/animated.js +41 -35
  74. package/lib/module/animated.js.map +1 -1
  75. package/lib/module/bindings.js +2 -1
  76. package/lib/module/bindings.js.map +1 -1
  77. package/lib/module/bindings.native.js +7 -7
  78. package/lib/module/bindings.native.js.map +1 -1
  79. package/lib/module/components/KeyboardAvoidingView/hooks.js +7 -5
  80. package/lib/module/components/KeyboardAvoidingView/hooks.js.map +1 -1
  81. package/lib/module/components/KeyboardAvoidingView/index.js +20 -13
  82. package/lib/module/components/KeyboardAvoidingView/index.js.map +1 -1
  83. package/lib/module/components/KeyboardAwareScrollView/index.js +190 -0
  84. package/lib/module/components/KeyboardAwareScrollView/index.js.map +1 -0
  85. package/lib/module/components/KeyboardAwareScrollView/useSmoothKeyboardHandler.js +93 -0
  86. package/lib/module/components/KeyboardAwareScrollView/useSmoothKeyboardHandler.js.map +1 -0
  87. package/lib/module/components/KeyboardAwareScrollView/utils.js +21 -0
  88. package/lib/module/components/KeyboardAwareScrollView/utils.js.map +1 -0
  89. package/lib/module/components/KeyboardStickyView/index.js +9 -7
  90. package/lib/module/components/KeyboardStickyView/index.js.map +1 -1
  91. package/lib/module/components/hooks/useKeyboardInterpolation.js +74 -0
  92. package/lib/module/components/hooks/useKeyboardInterpolation.js.map +1 -0
  93. package/lib/module/components/index.js +3 -2
  94. package/lib/module/components/index.js.map +1 -1
  95. package/lib/module/context.js +4 -3
  96. package/lib/module/context.js.map +1 -1
  97. package/lib/module/hooks.js +21 -7
  98. package/lib/module/hooks.js.map +1 -1
  99. package/lib/module/index.js +8 -8
  100. package/lib/module/index.js.map +1 -1
  101. package/lib/module/internal.js +6 -9
  102. package/lib/module/internal.js.map +1 -1
  103. package/lib/module/monkey-patch.android.js +2 -2
  104. package/lib/module/monkey-patch.android.js.map +1 -1
  105. package/lib/module/monkey-patch.js.map +1 -1
  106. package/lib/module/reanimated.js +2 -1
  107. package/lib/module/reanimated.js.map +1 -1
  108. package/lib/module/reanimated.native.js +27 -11
  109. package/lib/module/reanimated.native.js.map +1 -1
  110. package/lib/module/replicas.js +13 -11
  111. package/lib/module/replicas.js.map +1 -1
  112. package/lib/module/specs/KeyboardControllerViewNativeComponent.js +2 -2
  113. package/lib/module/specs/KeyboardControllerViewNativeComponent.js.map +1 -1
  114. package/lib/module/specs/KeyboardGestureAreaNativeComponent.js +3 -3
  115. package/lib/module/specs/KeyboardGestureAreaNativeComponent.js.map +1 -1
  116. package/lib/module/specs/NativeKeyboardController.js +2 -2
  117. package/lib/module/specs/NativeKeyboardController.js.map +1 -1
  118. package/lib/module/specs/NativeStatusBarManagerCompat.js +2 -2
  119. package/lib/module/specs/NativeStatusBarManagerCompat.js.map +1 -1
  120. package/lib/module/types.js.map +1 -1
  121. package/lib/module/utils.js.map +1 -1
  122. package/lib/typescript/animated.d.ts +1 -1
  123. package/lib/typescript/bindings.d.ts +1 -1
  124. package/lib/typescript/bindings.native.d.ts +1 -1
  125. package/lib/typescript/components/KeyboardAvoidingView/index.d.ts +4 -4
  126. package/lib/typescript/components/KeyboardAwareScrollView/index.d.ts +45 -0
  127. package/lib/typescript/components/KeyboardAwareScrollView/useSmoothKeyboardHandler.d.ts +6 -0
  128. package/lib/typescript/components/KeyboardAwareScrollView/utils.d.ts +1 -0
  129. package/lib/typescript/components/KeyboardStickyView/index.d.ts +2 -2
  130. package/lib/typescript/components/hooks/useKeyboardInterpolation.d.ts +20 -0
  131. package/lib/typescript/components/index.d.ts +3 -2
  132. package/lib/typescript/context.d.ts +6 -5
  133. package/lib/typescript/hooks.d.ts +4 -3
  134. package/lib/typescript/index.d.ts +8 -8
  135. package/lib/typescript/internal.d.ts +9 -6
  136. package/lib/typescript/reanimated.d.ts +3 -2
  137. package/lib/typescript/reanimated.native.d.ts +3 -2
  138. package/lib/typescript/replicas.d.ts +1 -1
  139. package/lib/typescript/specs/KeyboardControllerViewNativeComponent.d.ts +7 -3
  140. package/lib/typescript/specs/KeyboardGestureAreaNativeComponent.d.ts +4 -4
  141. package/lib/typescript/specs/NativeKeyboardController.d.ts +2 -1
  142. package/lib/typescript/specs/NativeStatusBarManagerCompat.d.ts +1 -1
  143. package/lib/typescript/types.d.ts +18 -5
  144. package/package.json +12 -11
  145. package/react-native-keyboard-controller.podspec +1 -1
  146. package/src/animated.tsx +59 -42
  147. package/src/bindings.native.ts +11 -11
  148. package/src/bindings.ts +4 -3
  149. package/src/components/KeyboardAvoidingView/hooks.ts +9 -6
  150. package/src/components/KeyboardAvoidingView/index.tsx +30 -27
  151. package/src/components/KeyboardAwareScrollView/index.tsx +262 -0
  152. package/src/components/KeyboardAwareScrollView/useSmoothKeyboardHandler.ts +123 -0
  153. package/src/components/KeyboardAwareScrollView/utils.ts +26 -0
  154. package/src/components/KeyboardStickyView/index.tsx +12 -18
  155. package/src/components/hooks/useKeyboardInterpolation.ts +96 -0
  156. package/src/components/index.ts +3 -2
  157. package/src/context.ts +15 -9
  158. package/src/hooks.ts +30 -13
  159. package/src/index.ts +12 -8
  160. package/src/internal.ts +22 -13
  161. package/src/monkey-patch.android.ts +2 -2
  162. package/src/reanimated.native.ts +43 -18
  163. package/src/reanimated.ts +9 -3
  164. package/src/replicas.ts +18 -17
  165. package/src/specs/KeyboardControllerViewNativeComponent.ts +12 -5
  166. package/src/specs/KeyboardGestureAreaNativeComponent.ts +7 -7
  167. package/src/specs/NativeKeyboardController.ts +4 -3
  168. package/src/specs/NativeStatusBarManagerCompat.ts +3 -3
  169. package/src/types.ts +45 -19
@@ -0,0 +1,190 @@
1
+ function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
2
+ import React, { useCallback, useMemo } from "react";
3
+ import { useWindowDimensions } from "react-native";
4
+ import Reanimated, { interpolate, scrollTo, useAnimatedReaction, useAnimatedRef, useAnimatedScrollHandler, useAnimatedStyle, useSharedValue } from "react-native-reanimated";
5
+ import { useFocusedInputHandler, useReanimatedFocusedInput } from "react-native-keyboard-controller";
6
+ import { useSmoothKeyboardHandler } from "./useSmoothKeyboardHandler";
7
+ import { debounce } from "./utils";
8
+ /**
9
+ * Everything begins from `onStart` handler. This handler is called every time,
10
+ * when keyboard changes its size or when focused `TextInput` was changed. In
11
+ * this handler we are calculating/memoizing values which later will be used
12
+ * during layout movement. For that we calculate:
13
+ * - layout of focused field (`layout`) - to understand whether there will be overlap
14
+ * - initial keyboard size (`initialKeyboardSize`) - used in scroll interpolation
15
+ * - future keyboard height (`keyboardHeight`) - used in scroll interpolation
16
+ * - current scroll position (`scrollPosition`) - used to scroll from this point
17
+ *
18
+ * Once we've calculated all necessary variables - we can actually start to use them.
19
+ * It happens in `onMove` handler - this function simply calls `maybeScroll` with
20
+ * current keyboard frame height. This functions makes the smooth transition.
21
+ *
22
+ * When the transition has finished we go to `onEnd` handler. In this handler
23
+ * we verify, that the current field is not overlapped within a keyboard frame.
24
+ * For full `onStart`/`onMove`/`onEnd` flow it may look like a redundant thing,
25
+ * however there could be some cases, when `onMove` is not called:
26
+ * - on iOS when TextInput was changed - keyboard transition is instant
27
+ * - on Android when TextInput was changed and keyboard size wasn't changed
28
+ * So `onEnd` handler handle the case, when `onMove` wasn't triggered.
29
+ *
30
+ * ====================================================================================================================+
31
+ * -----------------------------------------------------Flow chart-----------------------------------------------------+
32
+ * ====================================================================================================================+
33
+ *
34
+ * +============================+ +============================+ +==================================+
35
+ * + User Press on TextInput + => + Keyboard starts showing + => + As keyboard moves frame by frame + =>
36
+ * + + + (run `onStart`) + + `onMove` is getting called +
37
+ * +============================+ +============================+ +==================================+
38
+ *
39
+ *
40
+ * +============================+ +============================+ +=====================================+
41
+ * + Keyboard is shown and we + => + User moved focus to + => + Only `onStart`/`onEnd` maybe called +
42
+ * + call `onEnd` handler + + another `TextInput` + + (without involving `onMove`) +
43
+ * +============================+ +============================+ +=====================================+
44
+ *
45
+ */
46
+ const KeyboardAwareScrollView = _ref => {
47
+ let {
48
+ children,
49
+ bottomOffset = 0,
50
+ ...rest
51
+ } = _ref;
52
+ const scrollViewAnimatedRef = useAnimatedRef();
53
+ const scrollPosition = useSharedValue(0);
54
+ const position = useSharedValue(0);
55
+ const keyboardHeight = useSharedValue(0);
56
+ const tag = useSharedValue(-1);
57
+ const initialKeyboardSize = useSharedValue(0);
58
+ const scrollBeforeKeyboardMovement = useSharedValue(0);
59
+ const {
60
+ input
61
+ } = useReanimatedFocusedInput();
62
+ const layout = useSharedValue(null);
63
+ const {
64
+ height
65
+ } = useWindowDimensions();
66
+ const onScroll = useAnimatedScrollHandler({
67
+ onScroll: e => {
68
+ position.value = e.contentOffset.y;
69
+ }
70
+ }, []);
71
+
72
+ /**
73
+ * Function that will scroll a ScrollView as keyboard gets moving
74
+ */
75
+ const maybeScroll = useCallback(function (e) {
76
+ "worklet";
77
+
78
+ var _layout$value, _layout$value2;
79
+ let animated = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
80
+ const visibleRect = height - keyboardHeight.value;
81
+ const absoluteY = ((_layout$value = layout.value) === null || _layout$value === void 0 ? void 0 : _layout$value.layout.absoluteY) || 0;
82
+ const inputHeight = ((_layout$value2 = layout.value) === null || _layout$value2 === void 0 ? void 0 : _layout$value2.layout.height) || 0;
83
+ const point = absoluteY + inputHeight;
84
+ if (visibleRect - point <= bottomOffset) {
85
+ const interpolatedScrollTo = interpolate(e, [initialKeyboardSize.value, keyboardHeight.value], [0, keyboardHeight.value - (height - point) + bottomOffset]);
86
+ const targetScrollY = Math.max(interpolatedScrollTo, 0) + scrollPosition.value;
87
+ scrollTo(scrollViewAnimatedRef, 0, targetScrollY, animated);
88
+ return interpolatedScrollTo;
89
+ }
90
+ if (absoluteY < 0) {
91
+ const positionOnScreen = visibleRect - inputHeight - bottomOffset;
92
+ const topOfScreen = scrollPosition.value + absoluteY;
93
+ scrollTo(scrollViewAnimatedRef, 0, topOfScreen - positionOnScreen, animated);
94
+ }
95
+ return 0;
96
+ }, [bottomOffset]);
97
+ const onChangeText = useCallback(() => {
98
+ "worklet";
99
+
100
+ // if typing a text caused layout shift, then we need to ignore this handler
101
+ // because this event will be handled in `useAnimatedReaction` below
102
+ var _layout$value3, _input$value;
103
+ if (((_layout$value3 = layout.value) === null || _layout$value3 === void 0 ? void 0 : _layout$value3.layout.height) !== ((_input$value = input.value) === null || _input$value === void 0 ? void 0 : _input$value.layout.height)) {
104
+ return;
105
+ }
106
+ const prevScrollPosition = scrollPosition.value;
107
+ const prevLayout = layout.value;
108
+ scrollPosition.value = position.value;
109
+ layout.value = input.value;
110
+ maybeScroll(keyboardHeight.value, true);
111
+ scrollPosition.value = prevScrollPosition;
112
+ layout.value = prevLayout;
113
+ }, [maybeScroll]);
114
+ const onChangeTextHandler = useMemo(() => debounce(onChangeText, 200), [onChangeText]);
115
+ useFocusedInputHandler({
116
+ onChangeText: onChangeTextHandler
117
+ }, [onChangeTextHandler]);
118
+ useSmoothKeyboardHandler({
119
+ onStart: e => {
120
+ "worklet";
121
+
122
+ const keyboardWillChangeSize = keyboardHeight.value !== e.height && e.height > 0;
123
+ const keyboardWillAppear = e.height > 0 && keyboardHeight.value === 0;
124
+ const keyboardWillHide = e.height === 0;
125
+ const focusWasChanged = tag.value !== e.target && e.target !== -1 || keyboardWillChangeSize;
126
+ if (keyboardWillChangeSize) {
127
+ initialKeyboardSize.value = keyboardHeight.value;
128
+ }
129
+ if (keyboardWillHide) {
130
+ // on back transition need to interpolate as [0, keyboardHeight]
131
+ initialKeyboardSize.value = 0;
132
+ scrollPosition.value = scrollBeforeKeyboardMovement.value;
133
+ }
134
+ if (keyboardWillAppear || keyboardWillChangeSize || focusWasChanged) {
135
+ // persist scroll value
136
+ scrollPosition.value = position.value;
137
+ // just persist height - later will be used in interpolation
138
+ keyboardHeight.value = e.height;
139
+ }
140
+
141
+ // focus was changed
142
+ if (focusWasChanged) {
143
+ tag.value = e.target;
144
+
145
+ // save position of focused text input when keyboard starts to move
146
+ layout.value = input.value;
147
+ // save current scroll position - when keyboard will hide we'll reuse
148
+ // this value to achieve smooth hide effect
149
+ scrollBeforeKeyboardMovement.value = position.value;
150
+ }
151
+ if (focusWasChanged && !keyboardWillAppear) {
152
+ // update position on scroll value, so `onEnd` handler
153
+ // will pick up correct values
154
+ position.value += maybeScroll(e.height, true);
155
+ }
156
+ },
157
+ onMove: e => {
158
+ "worklet";
159
+
160
+ maybeScroll(e.height);
161
+ },
162
+ onEnd: e => {
163
+ "worklet";
164
+
165
+ keyboardHeight.value = e.height;
166
+ scrollPosition.value = position.value;
167
+ }
168
+ }, [height, maybeScroll]);
169
+ useAnimatedReaction(() => input.value, (current, previous) => {
170
+ if ((current === null || current === void 0 ? void 0 : current.target) === (previous === null || previous === void 0 ? void 0 : previous.target) && (current === null || current === void 0 ? void 0 : current.layout.height) !== (previous === null || previous === void 0 ? void 0 : previous.layout.height)) {
171
+ const prevLayout = layout.value;
172
+ layout.value = input.value;
173
+ scrollPosition.value += maybeScroll(keyboardHeight.value, true);
174
+ layout.value = prevLayout;
175
+ }
176
+ }, []);
177
+ const view = useAnimatedStyle(() => ({
178
+ paddingBottom: keyboardHeight.value
179
+ }), []);
180
+ return /*#__PURE__*/React.createElement(Reanimated.ScrollView, _extends({
181
+ ref: scrollViewAnimatedRef
182
+ }, rest, {
183
+ onScroll: onScroll,
184
+ scrollEventThrottle: 16
185
+ }), /*#__PURE__*/React.createElement(Reanimated.View, {
186
+ style: view
187
+ }, children));
188
+ };
189
+ export default KeyboardAwareScrollView;
190
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["React","useCallback","useMemo","useWindowDimensions","Reanimated","interpolate","scrollTo","useAnimatedReaction","useAnimatedRef","useAnimatedScrollHandler","useAnimatedStyle","useSharedValue","useFocusedInputHandler","useReanimatedFocusedInput","useSmoothKeyboardHandler","debounce","KeyboardAwareScrollView","_ref","children","bottomOffset","rest","scrollViewAnimatedRef","scrollPosition","position","keyboardHeight","tag","initialKeyboardSize","scrollBeforeKeyboardMovement","input","layout","height","onScroll","e","value","contentOffset","y","maybeScroll","_layout$value","_layout$value2","animated","arguments","length","undefined","visibleRect","absoluteY","inputHeight","point","interpolatedScrollTo","targetScrollY","Math","max","positionOnScreen","topOfScreen","onChangeText","_layout$value3","_input$value","prevScrollPosition","prevLayout","onChangeTextHandler","onStart","keyboardWillChangeSize","keyboardWillAppear","keyboardWillHide","focusWasChanged","target","onMove","onEnd","current","previous","view","paddingBottom","createElement","ScrollView","_extends","ref","scrollEventThrottle","View","style"],"sources":["index.tsx"],"sourcesContent":["import React, { useCallback, useMemo } from \"react\";\nimport { useWindowDimensions } from \"react-native\";\nimport Reanimated, {\n interpolate,\n scrollTo,\n useAnimatedReaction,\n useAnimatedRef,\n useAnimatedScrollHandler,\n useAnimatedStyle,\n useSharedValue,\n} from \"react-native-reanimated\";\n\nimport {\n useFocusedInputHandler,\n useReanimatedFocusedInput,\n} from \"react-native-keyboard-controller\";\n\nimport { useSmoothKeyboardHandler } from \"./useSmoothKeyboardHandler\";\nimport { debounce } from \"./utils\";\n\nimport type { FC } from \"react\";\nimport type { ScrollViewProps } from \"react-native\";\nimport type { FocusedInputLayoutChangedEvent } from \"react-native-keyboard-controller\";\n\ntype KeyboardAwareScrollViewProps = {\n bottomOffset?: number;\n} & ScrollViewProps;\n\n/**\n * Everything begins from `onStart` handler. This handler is called every time,\n * when keyboard changes its size or when focused `TextInput` was changed. In\n * this handler we are calculating/memoizing values which later will be used\n * during layout movement. For that we calculate:\n * - layout of focused field (`layout`) - to understand whether there will be overlap\n * - initial keyboard size (`initialKeyboardSize`) - used in scroll interpolation\n * - future keyboard height (`keyboardHeight`) - used in scroll interpolation\n * - current scroll position (`scrollPosition`) - used to scroll from this point\n *\n * Once we've calculated all necessary variables - we can actually start to use them.\n * It happens in `onMove` handler - this function simply calls `maybeScroll` with\n * current keyboard frame height. This functions makes the smooth transition.\n *\n * When the transition has finished we go to `onEnd` handler. In this handler\n * we verify, that the current field is not overlapped within a keyboard frame.\n * For full `onStart`/`onMove`/`onEnd` flow it may look like a redundant thing,\n * however there could be some cases, when `onMove` is not called:\n * - on iOS when TextInput was changed - keyboard transition is instant\n * - on Android when TextInput was changed and keyboard size wasn't changed\n * So `onEnd` handler handle the case, when `onMove` wasn't triggered.\n *\n * ====================================================================================================================+\n * -----------------------------------------------------Flow chart-----------------------------------------------------+\n * ====================================================================================================================+\n *\n * +============================+ +============================+ +==================================+\n * + User Press on TextInput + => + Keyboard starts showing + => + As keyboard moves frame by frame + =>\n * + + + (run `onStart`) + + `onMove` is getting called +\n * +============================+ +============================+ +==================================+\n *\n *\n * +============================+ +============================+ +=====================================+\n * + Keyboard is shown and we + => + User moved focus to + => + Only `onStart`/`onEnd` maybe called +\n * + call `onEnd` handler + + another `TextInput` + + (without involving `onMove`) +\n * +============================+ +============================+ +=====================================+\n *\n */\nconst KeyboardAwareScrollView: FC<KeyboardAwareScrollViewProps> = ({\n children,\n bottomOffset = 0,\n ...rest\n}) => {\n const scrollViewAnimatedRef = useAnimatedRef<Reanimated.ScrollView>();\n const scrollPosition = useSharedValue(0);\n const position = useSharedValue(0);\n const keyboardHeight = useSharedValue(0);\n const tag = useSharedValue(-1);\n const initialKeyboardSize = useSharedValue(0);\n const scrollBeforeKeyboardMovement = useSharedValue(0);\n const { input } = useReanimatedFocusedInput();\n const layout = useSharedValue<FocusedInputLayoutChangedEvent | null>(null);\n\n const { height } = useWindowDimensions();\n\n const onScroll = useAnimatedScrollHandler(\n {\n onScroll: (e) => {\n position.value = e.contentOffset.y;\n },\n },\n [],\n );\n\n /**\n * Function that will scroll a ScrollView as keyboard gets moving\n */\n const maybeScroll = useCallback(\n (e: number, animated: boolean = false) => {\n \"worklet\";\n\n const visibleRect = height - keyboardHeight.value;\n const absoluteY = layout.value?.layout.absoluteY || 0;\n const inputHeight = layout.value?.layout.height || 0;\n const point = absoluteY + inputHeight;\n\n if (visibleRect - point <= bottomOffset) {\n const interpolatedScrollTo = interpolate(\n e,\n [initialKeyboardSize.value, keyboardHeight.value],\n [0, keyboardHeight.value - (height - point) + bottomOffset],\n );\n const targetScrollY =\n Math.max(interpolatedScrollTo, 0) + scrollPosition.value;\n scrollTo(scrollViewAnimatedRef, 0, targetScrollY, animated);\n\n return interpolatedScrollTo;\n }\n\n if (absoluteY < 0) {\n const positionOnScreen = visibleRect - inputHeight - bottomOffset;\n const topOfScreen = scrollPosition.value + absoluteY;\n\n scrollTo(\n scrollViewAnimatedRef,\n 0,\n topOfScreen - positionOnScreen,\n animated,\n );\n }\n\n return 0;\n },\n [bottomOffset],\n );\n\n const onChangeText = useCallback(() => {\n \"worklet\";\n\n // if typing a text caused layout shift, then we need to ignore this handler\n // because this event will be handled in `useAnimatedReaction` below\n if (layout.value?.layout.height !== input.value?.layout.height) {\n return;\n }\n\n const prevScrollPosition = scrollPosition.value;\n const prevLayout = layout.value;\n\n scrollPosition.value = position.value;\n layout.value = input.value;\n maybeScroll(keyboardHeight.value, true);\n scrollPosition.value = prevScrollPosition;\n layout.value = prevLayout;\n }, [maybeScroll]);\n const onChangeTextHandler = useMemo(\n () => debounce(onChangeText, 200),\n [onChangeText],\n );\n\n useFocusedInputHandler(\n {\n onChangeText: onChangeTextHandler,\n },\n [onChangeTextHandler],\n );\n\n useSmoothKeyboardHandler(\n {\n onStart: (e) => {\n \"worklet\";\n\n const keyboardWillChangeSize =\n keyboardHeight.value !== e.height && e.height > 0;\n const keyboardWillAppear = e.height > 0 && keyboardHeight.value === 0;\n const keyboardWillHide = e.height === 0;\n const focusWasChanged =\n (tag.value !== e.target && e.target !== -1) || keyboardWillChangeSize;\n\n if (keyboardWillChangeSize) {\n initialKeyboardSize.value = keyboardHeight.value;\n }\n\n if (keyboardWillHide) {\n // on back transition need to interpolate as [0, keyboardHeight]\n initialKeyboardSize.value = 0;\n scrollPosition.value = scrollBeforeKeyboardMovement.value;\n }\n\n if (keyboardWillAppear || keyboardWillChangeSize || focusWasChanged) {\n // persist scroll value\n scrollPosition.value = position.value;\n // just persist height - later will be used in interpolation\n keyboardHeight.value = e.height;\n }\n\n // focus was changed\n if (focusWasChanged) {\n tag.value = e.target;\n\n // save position of focused text input when keyboard starts to move\n layout.value = input.value;\n // save current scroll position - when keyboard will hide we'll reuse\n // this value to achieve smooth hide effect\n scrollBeforeKeyboardMovement.value = position.value;\n }\n\n if (focusWasChanged && !keyboardWillAppear) {\n // update position on scroll value, so `onEnd` handler\n // will pick up correct values\n position.value += maybeScroll(e.height, true);\n }\n },\n onMove: (e) => {\n \"worklet\";\n\n maybeScroll(e.height);\n },\n onEnd: (e) => {\n \"worklet\";\n\n keyboardHeight.value = e.height;\n scrollPosition.value = position.value;\n },\n },\n [height, maybeScroll],\n );\n\n useAnimatedReaction(\n () => input.value,\n (current, previous) => {\n if (\n current?.target === previous?.target &&\n current?.layout.height !== previous?.layout.height\n ) {\n const prevLayout = layout.value;\n\n layout.value = input.value;\n scrollPosition.value += maybeScroll(keyboardHeight.value, true);\n layout.value = prevLayout;\n }\n },\n [],\n );\n\n const view = useAnimatedStyle(\n () => ({\n paddingBottom: keyboardHeight.value,\n }),\n [],\n );\n\n return (\n <Reanimated.ScrollView\n ref={scrollViewAnimatedRef}\n {...rest}\n onScroll={onScroll}\n scrollEventThrottle={16}\n >\n <Reanimated.View style={view}>{children}</Reanimated.View>\n </Reanimated.ScrollView>\n );\n};\n\nexport default KeyboardAwareScrollView;\n"],"mappings":";AAAA,OAAOA,KAAK,IAAIC,WAAW,EAAEC,OAAO,QAAQ,OAAO;AACnD,SAASC,mBAAmB,QAAQ,cAAc;AAClD,OAAOC,UAAU,IACfC,WAAW,EACXC,QAAQ,EACRC,mBAAmB,EACnBC,cAAc,EACdC,wBAAwB,EACxBC,gBAAgB,EAChBC,cAAc,QACT,yBAAyB;AAEhC,SACEC,sBAAsB,EACtBC,yBAAyB,QACpB,kCAAkC;AAEzC,SAASC,wBAAwB,QAAQ,4BAA4B;AACrE,SAASC,QAAQ,QAAQ,SAAS;AAUlC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMC,uBAAyD,GAAGC,IAAA,IAI5D;EAAA,IAJ6D;IACjEC,QAAQ;IACRC,YAAY,GAAG,CAAC;IAChB,GAAGC;EACL,CAAC,GAAAH,IAAA;EACC,MAAMI,qBAAqB,GAAGb,cAAc,CAAwB,CAAC;EACrE,MAAMc,cAAc,GAAGX,cAAc,CAAC,CAAC,CAAC;EACxC,MAAMY,QAAQ,GAAGZ,cAAc,CAAC,CAAC,CAAC;EAClC,MAAMa,cAAc,GAAGb,cAAc,CAAC,CAAC,CAAC;EACxC,MAAMc,GAAG,GAAGd,cAAc,CAAC,CAAC,CAAC,CAAC;EAC9B,MAAMe,mBAAmB,GAAGf,cAAc,CAAC,CAAC,CAAC;EAC7C,MAAMgB,4BAA4B,GAAGhB,cAAc,CAAC,CAAC,CAAC;EACtD,MAAM;IAAEiB;EAAM,CAA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CAAAuE,aAAA,CAACnE,UAAU,CAACoE,UAAU,EAAAC,QAAA;IACpBC,GAAG,EAAErD;EAAsB,GACvBD,IAAI;IACRW,QAAQ,EAAEA,QAAS;IACnB4C,mBAAmB,EAAE;EAAG,iBAExB3E,KAAA,CAAAuE,aAAA,CAACnE,UAAU,CAACwE,IAAI;IAACC,KAAK,EAAER;EAAK,GAAEnD,QAA0B,CACpC,CAAC;AAE5B,CAAC;AAED,eAAeF,uBAAuB"}
@@ -0,0 +1,93 @@
1
+ import { Platform } from "react-native";
2
+ import { Easing, useAnimatedReaction, useSharedValue, withTiming } from "react-native-reanimated";
3
+ import { useKeyboardHandler } from "react-native-keyboard-controller";
4
+ const IS_ANDROID_ELEVEN_OR_HIGHER = Platform.OS === "android" && Platform.Version >= 30;
5
+ // on these platforms keyboard transitions will be smooth
6
+ const IS_ANDROID_ELEVEN_OR_HIGHER_OR_IOS = IS_ANDROID_ELEVEN_OR_HIGHER || Platform.OS === "ios";
7
+ // on Android Telegram is not using androidx.core values and uses custom interpolation
8
+ // duration is taken from here: https://github.com/DrKLO/Telegram/blob/e9a35cea54c06277c69d41b8e25d94b5d7ede065/TMessagesProj/src/main/java/org/telegram/ui/ActionBar/AdjustPanLayoutHelper.java#L39
9
+ // and bezier is taken from: https://github.com/DrKLO/Telegram/blob/e9a35cea54c06277c69d41b8e25d94b5d7ede065/TMessagesProj/src/main/java/androidx/recyclerview/widget/ChatListItemAnimator.java#L40
10
+ const TELEGRAM_ANDROID_TIMING_CONFIG = {
11
+ duration: 250,
12
+ easing: Easing.bezier(0.19919472913616398, 0.010644531250000006, 0.27920937042459737, 0.91025390625)
13
+ };
14
+
15
+ /**
16
+ * Hook that uses default transitions for iOS and Android > 11, and uses
17
+ * custom interpolation on Android < 11 to achieve more smooth animation
18
+ */
19
+ export const useSmoothKeyboardHandler = (handler, deps) => {
20
+ const target = useSharedValue(-1);
21
+ const persistedHeight = useSharedValue(0);
22
+ const animatedKeyboardHeight = useSharedValue(0);
23
+ useAnimatedReaction(() => {
24
+ if (IS_ANDROID_ELEVEN_OR_HIGHER_OR_IOS) {
25
+ return;
26
+ }
27
+ const event = {
28
+ // it'll be always 250, since we're running animation via `withTiming` where
29
+ // duration in config (TELEGRAM_ANDROID_TIMING_CONFIG.duration) = 250ms
30
+ duration: 250,
31
+ target: target.value,
32
+ height: animatedKeyboardHeight.value,
33
+ progress: animatedKeyboardHeight.value / persistedHeight.value
34
+ };
35
+ return event;
36
+ }, evt => {
37
+ var _handler$onMove;
38
+ if (!evt) {
39
+ return;
40
+ }
41
+ (_handler$onMove = handler.onMove) === null || _handler$onMove === void 0 ? void 0 : _handler$onMove.call(handler, evt);
42
+
43
+ // dispatch `onEnd`
44
+ if (evt.height === persistedHeight.value) {
45
+ var _handler$onEnd;
46
+ (_handler$onEnd = handler.onEnd) === null || _handler$onEnd === void 0 ? void 0 : _handler$onEnd.call(handler, evt);
47
+ }
48
+ }, [handler]);
49
+ useKeyboardHandler({
50
+ onStart: e => {
51
+ "worklet";
52
+
53
+ // immediately dispatch onStart/onEnd events if onStart dispatched with the same height
54
+ // and don't wait for animation 250ms
55
+ var _handler$onStart2;
56
+ if (!IS_ANDROID_ELEVEN_OR_HIGHER_OR_IOS && e.height === persistedHeight.value) {
57
+ var _handler$onStart, _handler$onEnd2;
58
+ (_handler$onStart = handler.onStart) === null || _handler$onStart === void 0 ? void 0 : _handler$onStart.call(handler, e);
59
+ (_handler$onEnd2 = handler.onEnd) === null || _handler$onEnd2 === void 0 ? void 0 : _handler$onEnd2.call(handler, e);
60
+ return;
61
+ }
62
+ target.value = e.target;
63
+ if (e.height > 0) {
64
+ persistedHeight.value = e.height;
65
+ }
66
+ // if we are running on Android < 9, then we are using custom interpolation
67
+ // to achieve smoother animation and use `animatedKeyboardHeight` as animation
68
+ // driver
69
+ if (!IS_ANDROID_ELEVEN_OR_HIGHER_OR_IOS) {
70
+ animatedKeyboardHeight.value = withTiming(e.height, TELEGRAM_ANDROID_TIMING_CONFIG);
71
+ }
72
+ (_handler$onStart2 = handler.onStart) === null || _handler$onStart2 === void 0 ? void 0 : _handler$onStart2.call(handler, e);
73
+ },
74
+ onMove: e => {
75
+ "worklet";
76
+
77
+ if (IS_ANDROID_ELEVEN_OR_HIGHER_OR_IOS) {
78
+ var _handler$onMove2;
79
+ (_handler$onMove2 = handler.onMove) === null || _handler$onMove2 === void 0 ? void 0 : _handler$onMove2.call(handler, e);
80
+ }
81
+ },
82
+ onEnd: e => {
83
+ "worklet";
84
+
85
+ if (IS_ANDROID_ELEVEN_OR_HIGHER_OR_IOS) {
86
+ var _handler$onEnd3;
87
+ (_handler$onEnd3 = handler.onEnd) === null || _handler$onEnd3 === void 0 ? void 0 : _handler$onEnd3.call(handler, e);
88
+ }
89
+ persistedHeight.value = e.height;
90
+ }
91
+ }, deps);
92
+ };
93
+ //# sourceMappingURL=useSmoothKeyboardHandler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["Platform","Easing","useAnimatedReaction","useSharedValue","withTiming","useKeyboardHandler","IS_ANDROID_ELEVEN_OR_HIGHER","OS","Version","IS_ANDROID_ELEVEN_OR_HIGHER_OR_IOS","TELEGRAM_ANDROID_TIMING_CONFIG","duration","easing","bezier","useSmoothKeyboardHandler","handler","deps","target","persistedHeight","animatedKeyboardHeight","event","value","height","progress","evt","_handler$onMove","onMove","call","_handler$onEnd","onEnd","onStart","e","_handler$onStart2","_handler$onStart","_handler$onEnd2","_handler$onMove2","_handler$onEnd3"],"sources":["useSmoothKeyboardHandler.ts"],"sourcesContent":["import { Platform } from \"react-native\";\nimport {\n Easing,\n useAnimatedReaction,\n useSharedValue,\n withTiming,\n} from \"react-native-reanimated\";\n\nimport { useKeyboardHandler } from \"react-native-keyboard-controller\";\n\nconst IS_ANDROID_ELEVEN_OR_HIGHER =\n Platform.OS === \"android\" && Platform.Version >= 30;\n// on these platforms keyboard transitions will be smooth\nconst IS_ANDROID_ELEVEN_OR_HIGHER_OR_IOS =\n IS_ANDROID_ELEVEN_OR_HIGHER || Platform.OS === \"ios\";\n// on Android Telegram is not using androidx.core values and uses custom interpolation\n// duration is taken from here: https://github.com/DrKLO/Telegram/blob/e9a35cea54c06277c69d41b8e25d94b5d7ede065/TMessagesProj/src/main/java/org/telegram/ui/ActionBar/AdjustPanLayoutHelper.java#L39\n// and bezier is taken from: https://github.com/DrKLO/Telegram/blob/e9a35cea54c06277c69d41b8e25d94b5d7ede065/TMessagesProj/src/main/java/androidx/recyclerview/widget/ChatListItemAnimator.java#L40\nconst TELEGRAM_ANDROID_TIMING_CONFIG = {\n duration: 250,\n easing: Easing.bezier(\n 0.19919472913616398,\n 0.010644531250000006,\n 0.27920937042459737,\n 0.91025390625,\n ),\n};\n\n/**\n * Hook that uses default transitions for iOS and Android > 11, and uses\n * custom interpolation on Android < 11 to achieve more smooth animation\n */\nexport const useSmoothKeyboardHandler: typeof useKeyboardHandler = (\n handler,\n deps,\n) => {\n const target = useSharedValue(-1);\n const persistedHeight = useSharedValue(0);\n const animatedKeyboardHeight = useSharedValue(0);\n\n useAnimatedReaction(\n () => {\n if (IS_ANDROID_ELEVEN_OR_HIGHER_OR_IOS) {\n return;\n }\n const event = {\n // it'll be always 250, since we're running animation via `withTiming` where\n // duration in config (TELEGRAM_ANDROID_TIMING_CONFIG.duration) = 250ms\n duration: 250,\n target: target.value,\n height: animatedKeyboardHeight.value,\n progress: animatedKeyboardHeight.value / persistedHeight.value,\n };\n return event;\n },\n (evt) => {\n if (!evt) {\n return;\n }\n handler.onMove?.(evt);\n\n // dispatch `onEnd`\n if (evt.height === persistedHeight.value) {\n handler.onEnd?.(evt);\n }\n },\n [handler],\n );\n\n useKeyboardHandler(\n {\n onStart: (e) => {\n \"worklet\";\n\n // immediately dispatch onStart/onEnd events if onStart dispatched with the same height\n // and don't wait for animation 250ms\n if (\n !IS_ANDROID_ELEVEN_OR_HIGHER_OR_IOS &&\n e.height === persistedHeight.value\n ) {\n handler.onStart?.(e);\n handler.onEnd?.(e);\n\n return;\n }\n\n target.value = e.target;\n\n if (e.height > 0) {\n persistedHeight.value = e.height;\n }\n // if we are running on Android < 9, then we are using custom interpolation\n // to achieve smoother animation and use `animatedKeyboardHeight` as animation\n // driver\n if (!IS_ANDROID_ELEVEN_OR_HIGHER_OR_IOS) {\n animatedKeyboardHeight.value = withTiming(\n e.height,\n TELEGRAM_ANDROID_TIMING_CONFIG,\n );\n }\n\n handler.onStart?.(e);\n },\n onMove: (e) => {\n \"worklet\";\n\n if (IS_ANDROID_ELEVEN_OR_HIGHER_OR_IOS) {\n handler.onMove?.(e);\n }\n },\n onEnd: (e) => {\n \"worklet\";\n\n if (IS_ANDROID_ELEVEN_OR_HIGHER_OR_IOS) {\n handler.onEnd?.(e);\n }\n\n persistedHeight.value = e.height;\n },\n },\n deps,\n );\n};\n"],"mappings":"AAAA,SAASA,QAAQ,QAAQ,cAAc;AACvC,SACEC,MAAM,EACNC,mBAAmB,EACnBC,cAAc,EACdC,UAAU,QACL,yBAAyB;AAEhC,SAASC,kBAAkB,QAAQ,kCAAkC;AAErE,MAAMC,2BAA2B,GAC/BN,QAAQ,CAACO,EAAE,KAAK,SAAS,IAAIP,QAAQ,CAACQ,OAAO,IAAI,EAAE;AACrD;AACA,MAAMC,kCAAkC,GACtCH,2BAA2B,IAAIN,QAAQ,CAACO,EAAE,KAAK,KAAK;AACtD;AACA;AACA;AACA,MAAMG,8BAA8B,GAAG;EACrCC,QAAQ,EAAE,GAAG;EACbC,MAAM,EAAEX,MAAM,CAACY,MAAM,CACnB,mBAAmB,EACnB,oBAAoB,EACpB,mBAAmB,EACnB,aACF;AACF,CAAC;;AAED;AACA;AACA;AACA;AACA,OAAO,MAAMC,wBAAmD,GAAGA,CACjEC,OAAO,EACPC,IAAI,KACD;EACH,MAAMC,MAAM,GAAGd,cAAc,CAAC,CAAC,CAAC,CAAC;EACjC,MAAMe,eAAe,GAAGf,cAAc,CAAC,CAAC,CAAC;EACzC,MAAMgB,sBAAsB,GAAGhB,cAAc,CAAC,CAAC,C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EAd,MAAM,CAACI,KAAK,GAAGU,CAAC,CAACd,MAAM;MAEvB,IAAIc,CAAC,CAACT,MAAM,GAAG,CAAC,EAAE;QAChBJ,eAAe,CAACG,KAAK,GAAGU,CAAC,CAACT,MAAM;MAClC;MACA;MACA;MACA;MACA,IAAI,CAACb,kCAAkC,EAAE;QACvCU,sBAAsB,CAACE,KAAK,GAAGjB,UAAU,CACvC2B,CAAC,CAACT,MAAM,EACRZ,8BACF,CAAC;MACH;MAEA,CAAAsB,iBAAA,GAAAjB,OAAO,CAACe,OAAO,cAAAE,iBAAA,uBAAfA,iBAAA,CAAAL,IAAA,CAAAZ,OAAO,EAAWgB,CAAC,CAAC;IACtB,CAAC;IACDL,MAAM,EAAGK,CAAC,IAAK;MACb,SAAS;;MAET,IAAItB,kCAAkC,EAAE;QAAA,IAAA0B,gBAAA;QACtC,CAAAA,gBAAA,GAAApB,OAAO,CAACW,MAAM,cAAAS,gBAAA,uBAAdA,gBAAA,CAAAR,IAAA,CAAAZ,OAAO,EAAUgB,CAAC,CAAC;MACrB;IACF,CAAC;IACDF,KAAK,EAAGE,CAAC,IAAK;MACZ,SAAS;;MAET,IAAItB,kCAAkC,EAAE;QAAA,IAAA2B,eAAA;QACtC,CAAAA,eAAA,GAAArB,OAAO,CAACc,KAAK,cAAAO,eAAA,uBAAbA,eAAA,CAAAT,IAAA,CAAAZ,OAAO,EAASgB,CAAC,CAAC;MACpB;MAEAb,eAAe,CAACG,KAAK,GAAGU,CAAC,CAACT,MAAM;IAClC;EACF,CAAC,EACDN,IACF,CAAC;AACH,CAAC"}
@@ -0,0 +1,21 @@
1
+ export const debounce = function (worklet) {
2
+ "worklet";
3
+
4
+ let wait = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
5
+ const value = {
6
+ time: 0
7
+ };
8
+ return function () {
9
+ "worklet";
10
+
11
+ const t = Date.now();
12
+ const now = t - value.time;
13
+ if (now < wait) {
14
+ value.time = t;
15
+ return;
16
+ }
17
+ value.time = t;
18
+ return worklet(...arguments);
19
+ };
20
+ };
21
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["debounce","worklet","wait","arguments","length","undefined","value","time","t","Date","now"],"sources":["utils.ts"],"sourcesContent":["export const debounce = <F extends (...args: Parameters<F>) => ReturnType<F>>(\n worklet: F,\n wait = 0,\n) => {\n \"worklet\";\n\n const value = {\n time: 0,\n };\n\n return (...args: Parameters<F>): ReturnType<F> | void => {\n \"worklet\";\n\n const t = Date.now();\n const now = t - value.time;\n\n if (now < wait) {\n value.time = t;\n return;\n }\n\n value.time = t;\n\n return worklet(...args);\n };\n};\n"],"mappings":"AAAA,OAAO,MAAMA,QAAQ,GAAG,SAAAA,CACtBC,OAAU,EAEP;EACH,SAAS;;EAAC,IAFVC,IAAI,GAAAC,SAAA,CAAAC,MAAA,QAAAD,SAAA,QAAAE,SAAA,GAAAF,SAAA,MAAG,CAAC;EAIR,MAAMG,KAAK,GAAG;IACZC,IAAI,EAAE;EACR,CAAC;EAED,OAAO,YAAkD;IACvD,SAAS;;IAET,MAAMC,CAAC,GAAGC,IAAI,CAACC,GAAG,CAAC,CAAC;IACpB,MAAMA,GAAG,GAAGF,CAAC,GAAGF,KAAK,CAACC,IAAI;IAE1B,IAAIG,GAAG,GAAGR,IAAI,EAAE;MACdI,KAAK,CAACC,IAAI,GAAGC,CAAC;MACd;IACF;IAEAF,KAAK,CAACC,IAAI,GAAGC,CAAC;IAEd,OAAOP,OAAO,CAAC,GAAAE,SAAO,CAAC;EACzB,CAAC;AACH,CAAC"}
@@ -1,7 +1,8 @@
1
1
  function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
2
- import React, { forwardRef, useMemo } from 'react';
3
- import Reanimated, { interpolate, useAnimatedStyle } from 'react-native-reanimated';
4
- import { useReanimatedKeyboardAnimation } from '../../hooks';
2
+ import React, { forwardRef, useMemo } from "react";
3
+ import Reanimated, { useAnimatedStyle } from "react-native-reanimated";
4
+ import { useReanimatedKeyboardAnimation } from "../../hooks";
5
+ import useKeyboardInterpolation from "../hooks/useKeyboardInterpolation";
5
6
  const KeyboardStickyView = /*#__PURE__*/forwardRef((_ref, ref) => {
6
7
  let {
7
8
  children,
@@ -13,11 +14,13 @@ const KeyboardStickyView = /*#__PURE__*/forwardRef((_ref, ref) => {
13
14
  ...props
14
15
  } = _ref;
15
16
  const {
16
- height,
17
- progress
17
+ height
18
18
  } = useReanimatedKeyboardAnimation();
19
+ const {
20
+ interpolate
21
+ } = useKeyboardInterpolation();
19
22
  const stickyViewStyle = useAnimatedStyle(() => {
20
- const offset = interpolate(progress.value, [0, 1], [closed, opened]);
23
+ const offset = interpolate(-height.value, [closed, opened]);
21
24
  return {
22
25
  transform: [{
23
26
  translateY: height.value + offset
@@ -26,7 +29,6 @@ const KeyboardStickyView = /*#__PURE__*/forwardRef((_ref, ref) => {
26
29
  }, [closed, opened]);
27
30
  const styles = useMemo(() => [style, stickyViewStyle], [style, stickyViewStyle]);
28
31
  return /*#__PURE__*/React.createElement(Reanimated.View, _extends({
29
- // @ts-expect-error because `ref` from reanimated is not compatible with react-native
30
32
  ref: ref,
31
33
  style: styles
32
34
  }, props), children);
@@ -1 +1 @@
1
- {"version":3,"names":["React","forwardRef","useMemo","Reanimated","interpolate","useAnimatedStyle","useReanimatedKeyboardAnimation","KeyboardStickyView","ref","children","offset","closed","opened","style","props","height","progress","stickyViewStyle","value","transform","translateY","styles"],"sources":["index.tsx"],"sourcesContent":["import React, { forwardRef, useMemo } from 'react';\nimport Reanimated, {\n interpolate,\n useAnimatedStyle,\n} from 'react-native-reanimated';\n\nimport { useReanimatedKeyboardAnimation } from '../../hooks';\n\nimport type { View, ViewProps } from 'react-native';\n\ntype KeyboardStickyViewProps = {\n /**\n * Specify additional offset to the view for given keyboard state.\n */\n offset?: {\n /**\n * Adds additional `translateY` when keyboard is close. By default `0`.\n */\n closed?: number;\n /**\n * Adds additional `translateY` when keyboard is open. By default `0`.\n */\n opened?: number;\n };\n} & ViewProps;\n\nconst KeyboardStickyView = forwardRef<\n View,\n React.PropsWithChildren<KeyboardStickyViewProps>\n>(\n (\n { children, offset: { closed = 0, opened = 0 } = {}, style, ...props },\n ref\n ) => {\n const { height, progress } = useReanimatedKeyboardAnimation();\n\n const stickyViewStyle = useAnimatedStyle(() => {\n const offset = interpolate(progress.value, [0, 1], [closed, opened]);\n\n return {\n transform: [{ translateY: height.value + offset }],\n };\n }, [closed, opened]);\n\n const styles = useMemo(\n () => [style, stickyViewStyle],\n [style, stickyViewStyle]\n );\n\n return (\n <Reanimated.View\n // @ts-expect-error because `ref` from reanimated is not compatible with react-native\n ref={ref}\n style={styles}\n {...props}\n >\n {children}\n </Reanimated.View>\n );\n }\n);\n\nexport default KeyboardStickyView;\n"],"mappings":";AAAA,OAAOA,KAAK,IAAIC,UAAU,EAAEC,OAAO,QAAQ,OAAO;AAClD,OAAOC,UAAU,IACfC,WAAW,EACXC,gBAAgB,QACX,yBAAyB;AAEhC,SAASC,8BAA8B,QAAQ,aAAa;AAoB5D,MAAMC,kBAAkB,gBAAGN,UAAU,CAInC,OAEEO,GAAG,KACA;EAAA,IAFH;IAAEC,QAAQ;IAAEC,MAAM,EAAE;MAAEC,MAAM,GAAG,CAAC;MAAEC,MAAM,GAAG;IAAE,CAAC,GAAG,CAAC,CAAC;IAAEC,KAAK;IAAE,GAAGC;EAAM,CAAC;EAGtE,MAAM;IAAEC,MAAM;IAAEC;EAAS,CAAC,GAAGV,8BAA8B,EAAE;EAE7D,MAAMW,eAAe,GAAGZ,gBAAgB,CAAC,MAAM;IAC7C,MAAMK,MAAM,GAAGN,WAAW,CAACY,QAAQ,CAACE,KAAK,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAACP,MAAM,EAAEC,MAAM,CAAC,CAAC;IAEpE,OAAO;MACLO,SAAS,EAAE,CAAC;QAAEC,UAAU,EAAEL,MAAM,CAACG,KAAK,GAAGR;MAAO,CAAC;IACnD,CAAC;EACH,CAAC,EAAE,CAACC,MAAM,EAAEC,MAAM,CAAC,CAAC;EAEpB,MAAMS,MAAM,GAAGnB,OAAO,CACpB,MAAM,CAACW,KAAK,EAAEI,eAAe,CAAC,EAC9B,CAACJ,KAAK,EAAEI,eAAe,CAAC,CACzB;EAED,oBACE,oBAAC,UAAU,CAAC,IAAI;IACd;IACA,GAAG,EAAET,GAAI;IACT,KAAK,EAAEa;EAAO,GACVP,KAAK,GAERL,QAAQ,CACO;AAEtB,CAAC,CACF;AAED,eAAeF,kBAAkB"}
1
+ {"version":3,"names":["React","forwardRef","useMemo","Reanimated","useAnimatedStyle","useReanimatedKeyboardAnimation","useKeyboardInterpolation","KeyboardStickyView","_ref","ref","children","offset","closed","opened","style","props","height","interpolate","stickyViewStyle","value","transform","translateY","styles","createElement","View","_extends"],"sources":["index.tsx"],"sourcesContent":["import React, { forwardRef, useMemo } from \"react\";\nimport Reanimated, { useAnimatedStyle } from \"react-native-reanimated\";\n\nimport { useReanimatedKeyboardAnimation } from \"../../hooks\";\nimport useKeyboardInterpolation from \"../hooks/useKeyboardInterpolation\";\n\nimport type { View, ViewProps } from \"react-native\";\n\ntype KeyboardStickyViewProps = {\n /**\n * Specify additional offset to the view for given keyboard state.\n */\n offset?: {\n /**\n * Adds additional `translateY` when keyboard is close. By default `0`.\n */\n closed?: number;\n /**\n * Adds additional `translateY` when keyboard is open. By default `0`.\n */\n opened?: number;\n };\n} & ViewProps;\n\nconst KeyboardStickyView = forwardRef<\n View,\n React.PropsWithChildren<KeyboardStickyViewProps>\n>(\n (\n { children, offset: { closed = 0, opened = 0 } = {}, style, ...props },\n ref,\n ) => {\n const { height } = useReanimatedKeyboardAnimation();\n const { interpolate } = useKeyboardInterpolation();\n\n const stickyViewStyle = useAnimatedStyle(() => {\n const offset = interpolate(-height.value, [closed, opened]);\n\n return {\n transform: [{ translateY: height.value + offset }],\n };\n }, [closed, opened]);\n\n const styles = useMemo(\n () => [style, stickyViewStyle],\n [style, stickyViewStyle],\n );\n\n return (\n <Reanimated.View ref={ref} style={styles} {...props}>\n {children}\n </Reanimated.View>\n );\n },\n);\n\nexport default KeyboardStickyView;\n"],"mappings":";AAAA,OAAOA,KAAK,IAAIC,UAAU,EAAEC,OAAO,QAAQ,OAAO;AAClD,OAAOC,UAAU,IAAIC,gBAAgB,QAAQ,yBAAyB;AAEtE,SAASC,8BAA8B,QAAQ,aAAa;AAC5D,OAAOC,wBAAwB,MAAM,mCAAmC;AAoBxE,MAAMC,kBAAkB,gBAAGN,UAAU,CAInC,CAAAO,IAAA,EAEEC,GAAG,KACA;EAAA,IAFH;IAAEC,QAAQ;IAAEC,MAAM,EAAE;MAAEC,MAAM,GAAG,CAAC;MAAEC,MAAM,GAAG;IAAE,CAAC,GAAG,CAAC,CAAC;IAAEC,KAAK;IAAE,GAAGC;EAAM,CAAC,GAAAP,IAAA;EAGtE,MAAM;IAAEQ;EAAO,CAAC,GAAGX,8BAA8B,CAAC,CAAC;EACnD,MAAM;IAAEY;EAAY,CAAC,GAAGX,wBAAwB,CAAC,CAAC;EAElD,MAAMY,eAAe,GAAGd,gBAAgB,CAAC,MAAM;IAC7C,MAAMO,MAAM,GAAGM,WAAW,CAAC,CAACD,MAAM,CAACG,KAAK,EAAE,CAACP,MAAM,EAAEC,MAAM,CAAC,CAAC;IAE3D,OAAO;MACLO,SAAS,EAAE,CAAC;QAAEC,UAAU,EAAEL,MAAM,CAACG,KAAK,GAAGR;MAAO,CAAC;IACnD,CAAC;EACH,CAAC,EAAE,CAACC,MAAM,EAAEC,MAAM,CAAC,CAAC;EAEpB,MAAMS,MAAM,GAAGpB,OAAO,CACpB,MAAM,CAACY,KAAK,EAAEI,eAAe,CAAC,EAC9B,CAACJ,KAAK,EAAEI,eAAe,CACzB,CAAC;EAED,oBACElB,KAAA,CAAAuB,aAAA,CAACpB,UAAU,CAACqB,IAAI,EAAAC,QAAA;IAAChB,GAAG,EAAEA,GAAI;IAACK,KAAK,EAAEQ;EAAO,GAAKP,KAAK,GAChDL,QACc,CAAC;AAEtB,CACF,CAAC;AAED,eAAeH,kBAAkB"}
@@ -0,0 +1,74 @@
1
+ import { interpolate as interpolateREA, useSharedValue } from "react-native-reanimated";
2
+ import { useKeyboardHandler } from "../../hooks";
3
+ /**
4
+ * Hook that can be used for interpolation keyboard movement. The main concern is the thing
5
+ * when keyboard is opened and gets resized on Android. Let's say we are interpolating from
6
+ * closed to open [0, 200] and we want to interpolate it to [0, 230] (to achieve nice parallax effect).
7
+ * Then let's say keyboard changes its height to 220 (and we want to interpolate the value to 250, +30
8
+ * to keyboard height). If we interpolate based on `progress` value, then we will have a jump on first frame:
9
+ * the last interpolated position was 230, now we will interpolate to 250, but first frame will be calculated
10
+ * as 200 / 220 * 250 = 227 (and last interpolated position was 230) so we will have a jump.
11
+ *
12
+ * This hook handles it, and when keyboard changes its size it does an interpolation as:
13
+ * [200, 220] -> [230, 250], i. e. we preserve last interpolated value and use it as initial value for interpolation
14
+ * and because of that we will not have a jump and animation will start from the last frame and will be smooth.
15
+ *
16
+ * @see https://github.com/kirillzyusko/react-native-keyboard-controller/issues/315
17
+ */
18
+ const useKeyboardInterpolation = () => {
19
+ // keyboard heights
20
+ const nextKeyboardHeight = useSharedValue(0);
21
+ const prevKeyboardHeight = useSharedValue(0);
22
+ // save latest interpolated position
23
+ const lastInterpolation = useSharedValue(0);
24
+ // boolean flag indicating which output range should be used
25
+ const shouldUseInternalInterpolation = useSharedValue(false);
26
+ const interpolate = (keyboardPosition, output) => {
27
+ "worklet";
28
+
29
+ lastInterpolation.value = interpolateREA(keyboardPosition, [prevKeyboardHeight.value, nextKeyboardHeight.value], shouldUseInternalInterpolation.value ? [lastInterpolation.value, output[1]] : output);
30
+ return lastInterpolation.value;
31
+ };
32
+ useKeyboardHandler({
33
+ onStart: e => {
34
+ "worklet";
35
+
36
+ const keyboardWillBeHidden = e.height === 0;
37
+
38
+ // keyboard will be hidden
39
+ if (keyboardWillBeHidden) {
40
+ shouldUseInternalInterpolation.value = false;
41
+ prevKeyboardHeight.value = 0;
42
+ }
43
+
44
+ // keyboard will change its size
45
+ if (
46
+ // keyboard is shown on screen
47
+ nextKeyboardHeight.value !== 0 &&
48
+ // it really changes size (handles iOS case when after interactive keyboard gets shown again)
49
+ nextKeyboardHeight.value !== e.height &&
50
+ // keyboard is not hiding
51
+ !keyboardWillBeHidden) {
52
+ prevKeyboardHeight.value = nextKeyboardHeight.value;
53
+ shouldUseInternalInterpolation.value = true;
54
+ }
55
+
56
+ // keyboard will show or change size
57
+ if (!keyboardWillBeHidden) {
58
+ nextKeyboardHeight.value = e.height;
59
+ }
60
+ },
61
+ onEnd: e => {
62
+ "worklet";
63
+
64
+ // handles case show -> resize -> hide -> show
65
+ // here we reset value to 0 when keyboard is hidden
66
+ nextKeyboardHeight.value = e.height;
67
+ }
68
+ }, []);
69
+ return {
70
+ interpolate
71
+ };
72
+ };
73
+ export default useKeyboardInterpolation;
74
+ //# sourceMappingURL=useKeyboardInterpolation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["interpolate","interpolateREA","useSharedValue","useKeyboardHandler","useKeyboardInterpolation","nextKeyboardHeight","prevKeyboardHeight","lastInterpolation","shouldUseInternalInterpolation","keyboardPosition","output","value","onStart","e","keyboardWillBeHidden","height","onEnd"],"sources":["useKeyboardInterpolation.ts"],"sourcesContent":["import {\n interpolate as interpolateREA,\n useSharedValue,\n} from \"react-native-reanimated\";\n\nimport { useKeyboardHandler } from \"../../hooks\";\n\ntype KeyboardInterpolationOutput = [number, number];\n\n/**\n * Hook that can be used for interpolation keyboard movement. The main concern is the thing\n * when keyboard is opened and gets resized on Android. Let's say we are interpolating from\n * closed to open [0, 200] and we want to interpolate it to [0, 230] (to achieve nice parallax effect).\n * Then let's say keyboard changes its height to 220 (and we want to interpolate the value to 250, +30\n * to keyboard height). If we interpolate based on `progress` value, then we will have a jump on first frame:\n * the last interpolated position was 230, now we will interpolate to 250, but first frame will be calculated\n * as 200 / 220 * 250 = 227 (and last interpolated position was 230) so we will have a jump.\n *\n * This hook handles it, and when keyboard changes its size it does an interpolation as:\n * [200, 220] -> [230, 250], i. e. we preserve last interpolated value and use it as initial value for interpolation\n * and because of that we will not have a jump and animation will start from the last frame and will be smooth.\n *\n * @see https://github.com/kirillzyusko/react-native-keyboard-controller/issues/315\n */\nconst useKeyboardInterpolation = () => {\n // keyboard heights\n const nextKeyboardHeight = useSharedValue(0);\n const prevKeyboardHeight = useSharedValue(0);\n // save latest interpolated position\n const lastInterpolation = useSharedValue(0);\n // boolean flag indicating which output range should be used\n const shouldUseInternalInterpolation = useSharedValue(false);\n\n const interpolate = (\n keyboardPosition: number,\n output: KeyboardInterpolationOutput,\n ) => {\n \"worklet\";\n\n lastInterpolation.value = interpolateREA(\n keyboardPosition,\n [prevKeyboardHeight.value, nextKeyboardHeight.value],\n shouldUseInternalInterpolation.value\n ? [lastInterpolation.value, output[1]]\n : output,\n );\n\n return lastInterpolation.value;\n };\n\n useKeyboardHandler(\n {\n onStart: (e) => {\n \"worklet\";\n\n const keyboardWillBeHidden = e.height === 0;\n\n // keyboard will be hidden\n if (keyboardWillBeHidden) {\n shouldUseInternalInterpolation.value = false;\n prevKeyboardHeight.value = 0;\n }\n\n // keyboard will change its size\n if (\n // keyboard is shown on screen\n nextKeyboardHeight.value !== 0 &&\n // it really changes size (handles iOS case when after interactive keyboard gets shown again)\n nextKeyboardHeight.value !== e.height &&\n // keyboard is not hiding\n !keyboardWillBeHidden\n ) {\n prevKeyboardHeight.value = nextKeyboardHeight.value;\n shouldUseInternalInterpolation.value = true;\n }\n\n // keyboard will show or change size\n if (!keyboardWillBeHidden) {\n nextKeyboardHeight.value = e.height;\n }\n },\n onEnd: (e) => {\n \"worklet\";\n\n // handles case show -> resize -> hide -> show\n // here we reset value to 0 when keyboard is hidden\n nextKeyboardHeight.value = e.height;\n },\n },\n [],\n );\n\n return { interpolate };\n};\n\nexport default useKeyboardInterpolation;\n"],"mappings":"AAAA,SACEA,WAAW,IAAIC,cAAc,EAC7BC,cAAc,QACT,yBAAyB;AAEhC,SAASC,kBAAkB,QAAQ,aAAa;AAIhD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMC,wBAAwB,GAAGA,CAAA,KAAM;EACrC;EACA,MAAMC,kBAAkB,GAAGH,cAAc,CAAC,CAAC,CAAC;EAC5C,MAAMI,kBAAkB,GAAGJ,cAAc,CAAC,CAAC,CAAC;EAC5C;EACA,MAAMK,iBAAiB,GAAGL,cAAc,CAAC,CAAC,CAAC;EAC3C;EACA,MAAMM,8BAA8B,GAAGN,cAAc,CAAC,KAAK,CAAC;EAE5D,MAAMF,WAAW,GAAGA,CAClBS,gBAAwB,EACxBC,MAAmC,KAChC;IACH,SAAS;;IAETH,iBAAiB,CAACI,KAAK,GAAGV,cAAc,CACtCQ,gBAAgB,EAChB,CAACH,kBAAkB,CAACK,KAAK,EAAEN,kBAAkB,CAACM,KAAK,CAAC,EACpDH,8BAA8B,CAACG,KAAK,GAChC,CAACJ,iBAAiB,CAACI,KAAK,EAAED,MAAM,CAAC,CAAC,CAAC,CAAC,GACpCA,MACN,CAAC;IAED,OAAOH,iBAAiB,CAACI,KAAK;EAChC,CAAC;EAEDR,kBAAkB,CAChB;IACES,OAAO,EAAGC,CAAC,IAAK;MACd,SAAS;;MAET,MAAMC,oBAAoB,GAAGD,CAAC,CAACE,MAAM,KAAK,CAAC;;MAE3C;MACA,IAAID,oBAAoB,EAAE;QACxBN,8BAA8B,CAACG,KAAK,GAAG,KAAK;QAC5CL,kBAAkB,CAACK,KAAK,GAAG,CAAC;MAC9B;;MAEA;MACA;MACE;MACAN,kBAAkB,CAACM,KAAK,KAAK,CAAC;MAC9B;MACAN,kBAAkB,CAACM,KAAK,KAAKE,CAAC,CAACE,MAAM;MACrC;MACA,CAACD,oBAAoB,EACrB;QACAR,kBAAkB,CAACK,KAAK,GAAGN,kBAAkB,CAACM,KAAK;QACnDH,8BAA8B,CAACG,KAAK,GAAG,IAAI;MAC7C;;MAEA;MACA,IAAI,CAACG,oBAAoB,EAAE;QACzBT,kBAAkB,CAACM,KAAK,GAAGE,CAAC,CAACE,MAAM;MACrC;IACF,CAAC;IACDC,KAAK,EAAGH,CAAC,IAAK;MACZ,SAAS;;MAET;MACA;MACAR,kBAAkB,CAACM,KAAK,GAAGE,CAAC,CAACE,MAAM;IACrC;EACF,CAAC,EACD,EACF,CAAC;EAED,OAAO;IAAEf;EAAY,CAAC;AACxB,CAAC;AAED,eAAeI,wBAAwB"}
@@ -1,3 +1,4 @@
1
- export { default as KeyboardAvoidingView } from './KeyboardAvoidingView';
2
- export { default as KeyboardStickyView } from './KeyboardStickyView';
1
+ export { default as KeyboardAvoidingView } from "./KeyboardAvoidingView";
2
+ export { default as KeyboardStickyView } from "./KeyboardStickyView";
3
+ export { default as KeyboardAwareScrollView } from "./KeyboardAwareScrollView";
3
4
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["default","KeyboardAvoidingView","KeyboardStickyView"],"sources":["index.ts"],"sourcesContent":["export { default as KeyboardAvoidingView } from './KeyboardAvoidingView';\nexport { default as KeyboardStickyView } from './KeyboardStickyView';\n"],"mappings":"AAAA,SAASA,OAAO,IAAIC,oBAAoB,QAAQ,wBAAwB;AACxE,SAASD,OAAO,IAAIE,kBAAkB,QAAQ,sBAAsB"}
1
+ {"version":3,"names":["default","KeyboardAvoidingView","KeyboardStickyView","KeyboardAwareScrollView"],"sources":["index.ts"],"sourcesContent":["export { default as KeyboardAvoidingView } from \"./KeyboardAvoidingView\";\nexport { default as KeyboardStickyView } from \"./KeyboardStickyView\";\nexport { default as KeyboardAwareScrollView } from \"./KeyboardAwareScrollView\";\n"],"mappings":"AAAA,SAASA,OAAO,IAAIC,oBAAoB,QAAQ,wBAAwB;AACxE,SAASD,OAAO,IAAIE,kBAAkB,QAAQ,sBAAsB;AACpE,SAASF,OAAO,IAAIG,uBAAuB,QAAQ,2BAA2B"}
@@ -1,5 +1,5 @@
1
- import { createContext, useContext } from 'react';
2
- import { Animated } from 'react-native';
1
+ import { createContext, useContext } from "react";
2
+ import { Animated } from "react-native";
3
3
  const NOOP = () => {};
4
4
  const withSharedValue = value => ({
5
5
  value,
@@ -20,7 +20,8 @@ const defaultContext = {
20
20
  height: DEFAULT_SHARED_VALUE
21
21
  },
22
22
  layout: DEFAULT_LAYOUT,
23
- setHandlers: NOOP,
23
+ setKeyboardHandlers: NOOP,
24
+ setInputHandlers: NOOP,
24
25
  setEnabled: NOOP
25
26
  };
26
27
  export const KeyboardContext = /*#__PURE__*/createContext(defaultContext);
@@ -1 +1 @@
1
- {"version":3,"names":["createContext","useContext","Animated","NOOP","withSharedValue","value","addListener","removeListener","modify","DEFAULT_SHARED_VALUE","DEFAULT_LAYOUT","defaultContext","enabled","animated","progress","Value","height","reanimated","layout","setHandlers","setEnabled","KeyboardContext","useKeyboardContext","context","__DEV__","console","warn"],"sources":["context.ts"],"sourcesContent":["import { createContext, useContext } from 'react';\nimport { Animated } from 'react-native';\n\nimport type { FocusedInputLayoutChangedEvent, KeyboardHandlers } from './types';\nimport type React from 'react';\nimport type { SharedValue } from 'react-native-reanimated';\n\nexport type AnimatedContext = {\n progress: Animated.Value;\n height: Animated.AnimatedMultiplication<number>;\n};\nexport type ReanimatedContext = {\n progress: SharedValue<number>;\n height: SharedValue<number>;\n};\nexport type KeyboardAnimationContext = {\n enabled: boolean;\n animated: AnimatedContext;\n reanimated: ReanimatedContext;\n layout: SharedValue<FocusedInputLayoutChangedEvent | null>;\n setHandlers: (handlers: KeyboardHandlers) => void;\n setEnabled: React.Dispatch<React.SetStateAction<boolean>>;\n};\nconst NOOP = () => {};\nconst withSharedValue = <T>(value: T): SharedValue<T> => ({\n value,\n addListener: NOOP,\n removeListener: NOOP,\n modify: NOOP,\n});\nconst DEFAULT_SHARED_VALUE = withSharedValue(0);\nconst DEFAULT_LAYOUT = withSharedValue<FocusedInputLayoutChangedEvent | null>(\n null\n);\nconst defaultContext: KeyboardAnimationContext = {\n enabled: true,\n animated: {\n progress: new Animated.Value(0),\n height: new Animated.Value(0),\n },\n reanimated: {\n progress: DEFAULT_SHARED_VALUE,\n height: DEFAULT_SHARED_VALUE,\n },\n layout: DEFAULT_LAYOUT,\n setHandlers: NOOP,\n setEnabled: NOOP,\n};\nexport const KeyboardContext = createContext(defaultContext);\nexport const useKeyboardContext = () => {\n const context = useContext(KeyboardContext);\n\n if (__DEV__ && context === defaultContext) {\n console.warn(\n \"Couldn't find real values for `KeyboardContext`. Please make sure you're inside of `KeyboardProvider` - otherwise functionality of `react-native-keyboard-controller` will not work.\"\n );\n }\n\n return context;\n};\n"],"mappings":"AAAA,SAASA,aAAa,EAAEC,UAAU,QAAQ,OAAO;AACjD,SAASC,QAAQ,QAAQ,cAAc;AAsBvC,MAAMC,IAAI,GAAG,MAAM,CAAC,CAAC;AACrB,MAAMC,eAAe,GAAOC,KAAQ,KAAsB;EACxDA,KAAK;EACLC,WAAW,EAAEH,IAAI;EACjBI,cAAc,EAAEJ,IAAI;EACpBK,MAAM,EAAEL;AACV,CAAC,CAAC;AACF,MAAMM,oBAAoB,GAAGL,eAAe,CAAC,CAAC,CAAC;AAC/C,MAAMM,cAAc,GAAGN,eAAe,CACpC,IAAI,CACL;AACD,MAAMO,cAAwC,GAAG;EAC/CC,OAAO,EAAE,IAAI;EACbC,QAAQ,EAAE;IACRC,QAAQ,EAAE,IAAIZ,QAAQ,CAACa,KAAK,CAAC,CAAC,CAAC;IAC/BC,MAAM,EAAE,IAAId,QAAQ,CAACa,KAAK,CAAC,CAAC;EAC9B,CAAC;EACDE,UAAU,EAAE;IACVH,QAAQ,EAAEL,oBAAoB;IAC9BO,MAAM,EAAEP;EACV,CAAC;EACDS,MAAM,EAAER,cAAc;EACtBS,WAAW,EAAEhB,IAAI;EACjBiB,UAAU,EAAEjB;AACd,CAAC;AACD,OAAO,MAAMkB,eAAe,gBAAGrB,aAAa,CAACW,cAAc,CAAC;AAC5D,OAAO,MAAMW,kBAAkB,GAAG,MAAM;EACtC,MAAMC,OAAO,GAAGtB,UAAU,CAACoB,eAAe,CAAC;EAE3C,IAAIG,OAAO,IAAID,OAAO,KAAKZ,cAAc,EAAE;IACzCc,OAAO,CAACC,IAAI,CACV,sLAAsL,CACvL;EACH;EAEA,OAAOH,OAAO;AAChB,CAAC"}
1
+ {"version":3,"names":["createContext","useContext","Animated","NOOP","withSharedValue","value","addListener","removeListener","modify","DEFAULT_SHARED_VALUE","DEFAULT_LAYOUT","defaultContext","enabled","animated","progress","Value","height","reanimated","layout","setKeyboardHandlers","setInputHandlers","setEnabled","KeyboardContext","useKeyboardContext","context","__DEV__","console","warn"],"sources":["context.ts"],"sourcesContent":["import { createContext, useContext } from \"react\";\nimport { Animated } from \"react-native\";\n\nimport type {\n FocusedInputHandlers,\n FocusedInputLayoutChangedEvent,\n KeyboardHandlers,\n} from \"./types\";\nimport type React from \"react\";\nimport type { SharedValue } from \"react-native-reanimated\";\n\nexport type AnimatedContext = {\n progress: Animated.Value;\n height: Animated.AnimatedMultiplication<number>;\n};\nexport type ReanimatedContext = {\n progress: SharedValue<number>;\n height: SharedValue<number>;\n};\nexport type KeyboardAnimationContext = {\n enabled: boolean;\n animated: AnimatedContext;\n reanimated: ReanimatedContext;\n layout: SharedValue<FocusedInputLayoutChangedEvent | null>;\n setKeyboardHandlers: (handlers: KeyboardHandlers) => void;\n setInputHandlers: (handlers: FocusedInputHandlers) => void;\n setEnabled: React.Dispatch<React.SetStateAction<boolean>>;\n};\nconst NOOP = () => {};\nconst withSharedValue = <T>(value: T): SharedValue<T> => ({\n value,\n addListener: NOOP,\n removeListener: NOOP,\n modify: NOOP,\n});\nconst DEFAULT_SHARED_VALUE = withSharedValue(0);\nconst DEFAULT_LAYOUT = withSharedValue<FocusedInputLayoutChangedEvent | null>(\n null,\n);\nconst defaultContext: KeyboardAnimationContext = {\n enabled: true,\n animated: {\n progress: new Animated.Value(0),\n height: new Animated.Value(0),\n },\n reanimated: {\n progress: DEFAULT_SHARED_VALUE,\n height: DEFAULT_SHARED_VALUE,\n },\n layout: DEFAULT_LAYOUT,\n setKeyboardHandlers: NOOP,\n setInputHandlers: NOOP,\n setEnabled: NOOP,\n};\nexport const KeyboardContext = createContext(defaultContext);\nexport const useKeyboardContext = () => {\n const context = useContext(KeyboardContext);\n\n if (__DEV__ && context === defaultContext) {\n console.warn(\n \"Couldn't find real values for `KeyboardContext`. Please make sure you're inside of `KeyboardProvider` - otherwise functionality of `react-native-keyboard-controller` will not work.\",\n );\n }\n\n return context;\n};\n"],"mappings":"AAAA,SAASA,aAAa,EAAEC,UAAU,QAAQ,OAAO;AACjD,SAASC,QAAQ,QAAQ,cAAc;AA2BvC,MAAMC,IAAI,GAAGA,CAAA,KAAM,CAAC,CAAC;AACrB,MAAMC,eAAe,GAAOC,KAAQ,KAAsB;EACxDA,KAAK;EACLC,WAAW,EAAEH,IAAI;EACjBI,cAAc,EAAEJ,IAAI;EACpBK,MAAM,EAAEL;AACV,CAAC,CAAC;AACF,MAAMM,oBAAoB,GAAGL,eAAe,CAAC,CAAC,CAAC;AAC/C,MAAMM,cAAc,GAAGN,eAAe,CACpC,IACF,CAAC;AACD,MAAMO,cAAwC,GAAG;EAC/CC,OAAO,EAAE,IAAI;EACbC,QAAQ,EAAE;IACRC,QAAQ,EAAE,IAAIZ,QAAQ,CAACa,KAAK,CAAC,CAAC,CAAC;IAC/BC,MAAM,EAAE,IAAId,QAAQ,CAACa,KAAK,CAAC,CAAC;EAC9B,CAAC;EACDE,UAAU,EAAE;IACVH,QAAQ,EAAEL,oBAAoB;IAC9BO,MAAM,EAAEP;EACV,CAAC;EACDS,MAAM,EAAER,cAAc;EACtBS,mBAAmB,EAAEhB,IAAI;EACzBiB,gBAAgB,EAAEjB,IAAI;EACtBkB,UAAU,EAAElB;AACd,CAAC;AACD,OAAO,MAAMmB,eAAe,gBAAGtB,aAAa,CAACW,cAAc,CAAC;AAC5D,OAAO,MAAMY,kBAAkB,GAAGA,CAAA,KAAM;EACtC,MAAMC,OAAO,GAAGvB,UAAU,CAACqB,eAAe,CAAC;EAE3C,IAAIG,OAAO,IAAID,OAAO,KAAKb,cAAc,EAAE;IACzCe,OAAO,CAACC,IAAI,CACV,sLACF,CAAC;EACH;EAEA,OAAOH,OAAO;AAChB,CAAC"}
@@ -1,8 +1,8 @@
1
- import { useEffect } from 'react';
2
- import { KeyboardController } from './bindings';
3
- import { AndroidSoftInputModes } from './constants';
4
- import { useKeyboardContext } from './context';
5
- import { uuid } from './utils';
1
+ import { useEffect } from "react";
2
+ import { KeyboardController } from "./bindings";
3
+ import { AndroidSoftInputModes } from "./constants";
4
+ import { useKeyboardContext } from "./context";
5
+ import { uuid } from "./utils";
6
6
  export const useResizeMode = () => {
7
7
  useEffect(() => {
8
8
  KeyboardController.setInputMode(AndroidSoftInputModes.SOFT_INPUT_ADJUST_RESIZE);
@@ -23,11 +23,11 @@ export function useGenericKeyboardHandler(handler, deps) {
23
23
  const context = useKeyboardContext();
24
24
  useEffect(() => {
25
25
  const key = uuid();
26
- context.setHandlers({
26
+ context.setKeyboardHandlers({
27
27
  [key]: handler
28
28
  });
29
29
  return () => {
30
- context.setHandlers({
30
+ context.setKeyboardHandlers({
31
31
  [key]: undefined
32
32
  });
33
33
  };
@@ -50,4 +50,18 @@ export function useReanimatedFocusedInput() {
50
50
  input: context.layout
51
51
  };
52
52
  }
53
+ export function useFocusedInputHandler(handler, deps) {
54
+ const context = useKeyboardContext();
55
+ useEffect(() => {
56
+ const key = uuid();
57
+ context.setInputHandlers({
58
+ [key]: handler
59
+ });
60
+ return () => {
61
+ context.setInputHandlers({
62
+ [key]: undefined
63
+ });
64
+ };
65
+ }, deps);
66
+ }
53
67
  //# sourceMappingURL=hooks.js.map