react-native-keyboard-controller 1.0.0-alpha.0

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 (62) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +88 -0
  3. package/android/.gradle/6.8/executionHistory/executionHistory.lock +0 -0
  4. package/android/.gradle/6.8/fileChanges/last-build.bin +0 -0
  5. package/android/.gradle/6.8/fileHashes/fileHashes.lock +0 -0
  6. package/android/.gradle/6.8/gc.properties +0 -0
  7. package/android/.gradle/buildOutputCleanup/buildOutputCleanup.lock +0 -0
  8. package/android/.gradle/buildOutputCleanup/cache.properties +2 -0
  9. package/android/.gradle/checksums/checksums.lock +0 -0
  10. package/android/.gradle/configuration-cache/gc.properties +0 -0
  11. package/android/.gradle/vcs-1/gc.properties +0 -0
  12. package/android/build.gradle +129 -0
  13. package/android/gradle/wrapper/gradle-wrapper.jar +0 -0
  14. package/android/gradle/wrapper/gradle-wrapper.properties +5 -0
  15. package/android/gradle.properties +3 -0
  16. package/android/gradlew +185 -0
  17. package/android/gradlew.bat +89 -0
  18. package/android/src/main/AndroidManifest.xml +4 -0
  19. package/android/src/main/java/com/reactnativekeyboardcontroller/EdgeToEdgeReactViewGroup.kt +18 -0
  20. package/android/src/main/java/com/reactnativekeyboardcontroller/KeyboardControllerModule.kt +39 -0
  21. package/android/src/main/java/com/reactnativekeyboardcontroller/KeyboardControllerPackage.kt +16 -0
  22. package/android/src/main/java/com/reactnativekeyboardcontroller/KeyboardControllerViewManager.kt +57 -0
  23. package/android/src/main/java/com/reactnativekeyboardcontroller/TranslateDeferringInsetsAnimationCallback.kt +150 -0
  24. package/android/src/main/java/com/reactnativekeyboardcontroller/events/KeyboardTransitionEvent.kt +23 -0
  25. package/ios/KeyboardController-Bridging-Header.h +1 -0
  26. package/ios/KeyboardController.xcodeproj/project.pbxproj +293 -0
  27. package/ios/KeyboardController.xcodeproj/project.xcworkspace/contents.xcworkspacedata +7 -0
  28. package/ios/KeyboardController.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -0
  29. package/ios/KeyboardController.xcodeproj/project.xcworkspace/xcuserdata/kiryl.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  30. package/ios/KeyboardController.xcodeproj/xcuserdata/kiryl.xcuserdatad/xcschemes/xcschememanagement.plist +14 -0
  31. package/ios/KeyboardControllerModule-Header.h +10 -0
  32. package/ios/KeyboardControllerModule.m +21 -0
  33. package/ios/KeyboardControllerModule.swift +38 -0
  34. package/ios/KeyboardControllerViewManager.m +7 -0
  35. package/ios/KeyboardControllerViewManager.swift +74 -0
  36. package/ios/KeyboardMoveEvent.swift +37 -0
  37. package/lib/commonjs/animated.js +145 -0
  38. package/lib/commonjs/animated.js.map +1 -0
  39. package/lib/commonjs/index.js +45 -0
  40. package/lib/commonjs/index.js.map +1 -0
  41. package/lib/commonjs/native.js +48 -0
  42. package/lib/commonjs/native.js.map +1 -0
  43. package/lib/commonjs/replicas.js +138 -0
  44. package/lib/commonjs/replicas.js.map +1 -0
  45. package/lib/module/animated.js +117 -0
  46. package/lib/module/animated.js.map +1 -0
  47. package/lib/module/index.js +4 -0
  48. package/lib/module/index.js.map +1 -0
  49. package/lib/module/native.js +32 -0
  50. package/lib/module/native.js.map +1 -0
  51. package/lib/module/replicas.js +114 -0
  52. package/lib/module/replicas.js.map +1 -0
  53. package/lib/typescript/animated.d.ts +22 -0
  54. package/lib/typescript/index.d.ts +3 -0
  55. package/lib/typescript/native.d.ts +36 -0
  56. package/lib/typescript/replicas.d.ts +45 -0
  57. package/package.json +149 -0
  58. package/react-native-keyboard-controller.podspec +19 -0
  59. package/src/animated.tsx +166 -0
  60. package/src/index.ts +3 -0
  61. package/src/native.ts +82 -0
  62. package/src/replicas.ts +152 -0
package/src/native.ts ADDED
@@ -0,0 +1,82 @@
1
+ import React, { useEffect } from 'react';
2
+ import {
3
+ requireNativeComponent,
4
+ UIManager,
5
+ Platform,
6
+ ViewStyle,
7
+ NativeModules,
8
+ NativeEventEmitter,
9
+ NativeSyntheticEvent,
10
+ } from 'react-native';
11
+
12
+ const LINKING_ERROR =
13
+ `The package 'react-native-keyboard-controller' doesn't seem to be linked. Make sure: \n\n` +
14
+ Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) +
15
+ '- You rebuilt the app after installing the package\n' +
16
+ '- You are not using Expo managed workflow\n';
17
+
18
+ export enum AndroidSoftInputModes {
19
+ SOFT_INPUT_ADJUST_NOTHING = 48,
20
+ SOFT_INPUT_ADJUST_PAN = 32,
21
+ SOFT_INPUT_ADJUST_RESIZE = 16,
22
+ SOFT_INPUT_ADJUST_UNSPECIFIED = 0,
23
+ }
24
+
25
+ export type NativeEvent = {
26
+ progress: number;
27
+ height: number;
28
+ };
29
+ export type EventWithName<T> = {
30
+ eventName: string;
31
+ } & T;
32
+ export type KeyboardControllerProps = {
33
+ style?: ViewStyle;
34
+ children: React.ReactNode;
35
+ onKeyboardMove: (e: NativeSyntheticEvent<EventWithName<NativeEvent>>) => void;
36
+ // fake prop used to activate reanimated bindings
37
+ onKeyboardMoveReanimated: (
38
+ e: NativeSyntheticEvent<EventWithName<NativeEvent>>
39
+ ) => void;
40
+ };
41
+ type KeyboardController = {
42
+ // android only
43
+ setDefaultMode: () => void;
44
+ setInputMode: (mode: AndroidSoftInputModes) => void;
45
+ };
46
+
47
+ const ComponentName = 'KeyboardControllerView';
48
+
49
+ const RCTKeyboardController = NativeModules.KeyboardController;
50
+ export const KeyboardController = RCTKeyboardController as KeyboardController;
51
+
52
+ const eventEmitter = new NativeEventEmitter(RCTKeyboardController);
53
+ type KeyboardControllerEvents =
54
+ | 'keyboardWillShow'
55
+ | 'keyboardDidShow'
56
+ | 'keyboardWillHide'
57
+ | 'keyboardDidHide';
58
+ type KeyboardEvent = {
59
+ height: number;
60
+ };
61
+ export const KeyboardEvents = {
62
+ addListener: (
63
+ name: KeyboardControllerEvents,
64
+ cb: (e: KeyboardEvent) => void
65
+ ) => eventEmitter.addListener('KeyboardController::' + name, cb),
66
+ };
67
+ export const KeyboardControllerView =
68
+ UIManager.getViewManagerConfig(ComponentName) != null
69
+ ? requireNativeComponent<KeyboardControllerProps>(ComponentName)
70
+ : () => {
71
+ throw new Error(LINKING_ERROR);
72
+ };
73
+
74
+ export const useResizeMode = () => {
75
+ useEffect(() => {
76
+ KeyboardController.setInputMode(
77
+ AndroidSoftInputModes.SOFT_INPUT_ADJUST_RESIZE
78
+ );
79
+
80
+ return () => KeyboardController.setDefaultMode();
81
+ }, []);
82
+ };
@@ -0,0 +1,152 @@
1
+ import { useRef, useEffect, useMemo } from 'react';
2
+ import { Animated, Easing, Keyboard, Platform } from 'react-native';
3
+ import {
4
+ runOnUI,
5
+ useAnimatedReaction,
6
+ useDerivedValue,
7
+ useSharedValue,
8
+ useWorkletCallback,
9
+ withSpring,
10
+ } from 'react-native-reanimated';
11
+ import { useReanimatedKeyboardAnimation } from './animated';
12
+
13
+ import { AndroidSoftInputModes, KeyboardController } from './native';
14
+
15
+ const availableOSEventType = Platform.OS === 'ios' ? 'Will' : 'Did';
16
+
17
+ // cubic-bezier(.17,.67,.34,.94)
18
+ export const defaultAndroidEasing = Easing.bezier(0.4, 0.0, 0.2, 1);
19
+ type KeyboardAnimation = {
20
+ progress: Animated.Value;
21
+ height: Animated.Value;
22
+ };
23
+
24
+ /**
25
+ * An experimental implementation of tracing keyboard appearance.
26
+ * Switch an input mode to adjust resize mode. In this case all did* events
27
+ * are triggering before keyboard appears, and using some approximations
28
+ * it tries to mimicries a native transition.
29
+ *
30
+ * @returns {Animated.Value}
31
+ */
32
+ export const useKeyboardAnimationReplica = (): KeyboardAnimation => {
33
+ const height = useRef(new Animated.Value(0));
34
+ const progress = useRef(new Animated.Value(0));
35
+ const animation = useMemo(
36
+ () => ({
37
+ height: height.current,
38
+ progress: progress.current,
39
+ }),
40
+ []
41
+ );
42
+
43
+ useEffect(() => {
44
+ KeyboardController.setInputMode(
45
+ AndroidSoftInputModes.SOFT_INPUT_ADJUST_RESIZE
46
+ );
47
+
48
+ return () => KeyboardController.setDefaultMode();
49
+ }, []);
50
+ useEffect(() => {
51
+ const listener = Keyboard.addListener(
52
+ `keyboard${availableOSEventType}Show`,
53
+ (e) => {
54
+ Animated.timing(height.current, {
55
+ toValue: -e.endCoordinates.height,
56
+ duration: e.duration !== 0 ? e.duration : 300,
57
+ easing: Easing.bezier(0.4, 0.0, 0.2, 1),
58
+ useNativeDriver: true,
59
+ }).start();
60
+
61
+ return () => listener.remove();
62
+ }
63
+ );
64
+ }, []);
65
+ useEffect(() => {
66
+ const listener = Keyboard.addListener(
67
+ `keyboard${availableOSEventType}Hide`,
68
+ (e) => {
69
+ Animated.timing(height.current, {
70
+ toValue: 0,
71
+ duration: e.duration !== 0 ? e.duration : 300,
72
+ easing: Easing.bezier(0.4, 0.0, 0.2, 1),
73
+ useNativeDriver: true,
74
+ }).start();
75
+
76
+ return () => listener.remove();
77
+ }
78
+ );
79
+ }, []);
80
+
81
+ return animation;
82
+ };
83
+
84
+ const IOS_SPRING_CONFIG = {
85
+ damping: 500,
86
+ stiffness: 1000,
87
+ mass: 3,
88
+ overshootClamping: true,
89
+ restDisplacementThreshold: 10,
90
+ restSpeedThreshold: 10,
91
+ };
92
+
93
+ /**
94
+ * A close replica to native iOS keyboard animation. The problem is that
95
+ * iOS (unlike Android) can not fire events for each keyboard frame movement.
96
+ * As a result we can not get gradual values (for example, for progress it always
97
+ * will be 1 or 0). So if you want to rely on gradual values you will need to use
98
+ * this replica.
99
+ *
100
+ * The transition is hardcoded and may vary from one to another OS versions. But it
101
+ * seems like last time it has been changed in iOS 7. Since RN supports at least iOS
102
+ * 11 it doesn't make sense to replicate iOS 7 behavior. If it changes in next OS
103
+ * versions, then this implementation should be revisited and reflect necessary changes.
104
+ *
105
+ * @returns {height, progress} - animated values
106
+ */
107
+ export const useReanimatedKeyboardAnimationReplica = () => {
108
+ const height = useSharedValue(0);
109
+ const heightEvent = useSharedValue(0);
110
+
111
+ const progress = useDerivedValue(() => height.value / heightEvent.value);
112
+
113
+ const handler = useWorkletCallback((_height: number) => {
114
+ heightEvent.value = _height;
115
+ }, []);
116
+
117
+ useAnimatedReaction(
118
+ () => ({
119
+ _keyboardHeight: heightEvent.value,
120
+ }),
121
+ (result, _previousResult) => {
122
+ const { _keyboardHeight } = result;
123
+ const _previousKeyboardHeight = _previousResult?._keyboardHeight;
124
+
125
+ if (_keyboardHeight !== _previousKeyboardHeight) {
126
+ height.value = withSpring(_keyboardHeight, IOS_SPRING_CONFIG);
127
+ }
128
+ },
129
+ []
130
+ );
131
+
132
+ useEffect(() => {
133
+ const show = Keyboard.addListener('keyboardWillShow', (e) => {
134
+ runOnUI(handler)(-e.endCoordinates.height);
135
+ });
136
+ const hide = Keyboard.addListener('keyboardWillHide', () => {
137
+ runOnUI(handler)(0);
138
+ });
139
+
140
+ return () => {
141
+ show.remove();
142
+ hide.remove();
143
+ };
144
+ }, []);
145
+
146
+ return { height, progress };
147
+ };
148
+
149
+ export const useGradualKeyboardAnimation =
150
+ Platform.OS === 'ios'
151
+ ? useReanimatedKeyboardAnimationReplica
152
+ : useReanimatedKeyboardAnimation;