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.
- package/LICENSE +21 -0
- package/README.md +88 -0
- package/android/.gradle/6.8/executionHistory/executionHistory.lock +0 -0
- package/android/.gradle/6.8/fileChanges/last-build.bin +0 -0
- package/android/.gradle/6.8/fileHashes/fileHashes.lock +0 -0
- package/android/.gradle/6.8/gc.properties +0 -0
- package/android/.gradle/buildOutputCleanup/buildOutputCleanup.lock +0 -0
- package/android/.gradle/buildOutputCleanup/cache.properties +2 -0
- package/android/.gradle/checksums/checksums.lock +0 -0
- package/android/.gradle/configuration-cache/gc.properties +0 -0
- package/android/.gradle/vcs-1/gc.properties +0 -0
- package/android/build.gradle +129 -0
- package/android/gradle/wrapper/gradle-wrapper.jar +0 -0
- package/android/gradle/wrapper/gradle-wrapper.properties +5 -0
- package/android/gradle.properties +3 -0
- package/android/gradlew +185 -0
- package/android/gradlew.bat +89 -0
- package/android/src/main/AndroidManifest.xml +4 -0
- package/android/src/main/java/com/reactnativekeyboardcontroller/EdgeToEdgeReactViewGroup.kt +18 -0
- package/android/src/main/java/com/reactnativekeyboardcontroller/KeyboardControllerModule.kt +39 -0
- package/android/src/main/java/com/reactnativekeyboardcontroller/KeyboardControllerPackage.kt +16 -0
- package/android/src/main/java/com/reactnativekeyboardcontroller/KeyboardControllerViewManager.kt +57 -0
- package/android/src/main/java/com/reactnativekeyboardcontroller/TranslateDeferringInsetsAnimationCallback.kt +150 -0
- package/android/src/main/java/com/reactnativekeyboardcontroller/events/KeyboardTransitionEvent.kt +23 -0
- package/ios/KeyboardController-Bridging-Header.h +1 -0
- package/ios/KeyboardController.xcodeproj/project.pbxproj +293 -0
- package/ios/KeyboardController.xcodeproj/project.xcworkspace/contents.xcworkspacedata +7 -0
- package/ios/KeyboardController.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -0
- package/ios/KeyboardController.xcodeproj/project.xcworkspace/xcuserdata/kiryl.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
- package/ios/KeyboardController.xcodeproj/xcuserdata/kiryl.xcuserdatad/xcschemes/xcschememanagement.plist +14 -0
- package/ios/KeyboardControllerModule-Header.h +10 -0
- package/ios/KeyboardControllerModule.m +21 -0
- package/ios/KeyboardControllerModule.swift +38 -0
- package/ios/KeyboardControllerViewManager.m +7 -0
- package/ios/KeyboardControllerViewManager.swift +74 -0
- package/ios/KeyboardMoveEvent.swift +37 -0
- package/lib/commonjs/animated.js +145 -0
- package/lib/commonjs/animated.js.map +1 -0
- package/lib/commonjs/index.js +45 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/native.js +48 -0
- package/lib/commonjs/native.js.map +1 -0
- package/lib/commonjs/replicas.js +138 -0
- package/lib/commonjs/replicas.js.map +1 -0
- package/lib/module/animated.js +117 -0
- package/lib/module/animated.js.map +1 -0
- package/lib/module/index.js +4 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/native.js +32 -0
- package/lib/module/native.js.map +1 -0
- package/lib/module/replicas.js +114 -0
- package/lib/module/replicas.js.map +1 -0
- package/lib/typescript/animated.d.ts +22 -0
- package/lib/typescript/index.d.ts +3 -0
- package/lib/typescript/native.d.ts +36 -0
- package/lib/typescript/replicas.d.ts +45 -0
- package/package.json +149 -0
- package/react-native-keyboard-controller.podspec +19 -0
- package/src/animated.tsx +166 -0
- package/src/index.ts +3 -0
- package/src/native.ts +82 -0
- package/src/replicas.ts +152 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["animated.tsx"],"names":["React","useContext","useMemo","Animated","StyleSheet","Reanimated","useEvent","useHandler","useSharedValue","KeyboardControllerView","useResizeMode","KeyboardControllerViewAnimated","createAnimatedComponent","defaultContext","animated","progress","Value","height","reanimated","value","KeyboardContext","createContext","useKeyboardAnimation","context","useReanimatedKeyboardAnimation","useAnimatedKeyboardHandler","handlers","dependencies","doDependenciesDiffer","event","onKeyboardMove","eventName","endsWith","styles","create","container","flex","hidden","display","position","KeyboardProvider","children","progressSV","heightSV","nativeEvent","useNativeDriver","handler","transform","translateX","translateY"],"mappings":"AAAA,OAAOA,KAAP,IAAgBC,UAAhB,EAA4BC,OAA5B,QAA2C,OAA3C;AACA,SAASC,QAAT,EAAmBC,UAAnB,QAAgD,cAAhD;AACA,OAAOC,UAAP,IACEC,QADF,EAEEC,UAFF,EAGEC,cAHF,QAIO,yBAJP;AAKA,SAGEC,sBAHF,EAKEC,aALF,QAMO,UANP;AAQA,MAAMC,8BAA8B,GAAGN,UAAU,CAACO,uBAAX,CACrCT,QAAQ,CAACS,uBAAT,CACEH,sBADF,CADqC,CAAvC;AAkBA,MAAMI,cAAwC,GAAG;AAC/CC,EAAAA,QAAQ,EAAE;AACRC,IAAAA,QAAQ,EAAE,IAAIZ,QAAQ,CAACa,KAAb,CAAmB,CAAnB,CADF;AAERC,IAAAA,MAAM,EAAE,IAAId,QAAQ,CAACa,KAAb,CAAmB,CAAnB;AAFA,GADqC;AAK/CE,EAAAA,UAAU,EAAE;AACVH,IAAAA,QAAQ,EAAE;AAAEI,MAAAA,KAAK,EAAE;AAAT,KADA;AAEVF,IAAAA,MAAM,EAAE;AAAEE,MAAAA,KAAK,EAAE;AAAT;AAFE;AALmC,CAAjD;AAUA,MAAMC,eAAe,gBAAGpB,KAAK,CAACqB,aAAN,CAAoBR,cAApB,CAAxB;AAEA,OAAO,MAAMS,oBAAoB,GAAG,MAAuB;AACzDZ,EAAAA,aAAa;AACb,QAAMa,OAAO,GAAGtB,UAAU,CAACmB,eAAD,CAA1B;AAEA,SAAOG,OAAO,CAACT,QAAf;AACD,CALM;AAOP,OAAO,MAAMU,8BAA8B,GAAG,MAAyB;AACrEd,EAAAA,aAAa;AACb,QAAMa,OAAO,GAAGtB,UAAU,CAACmB,eAAD,CAA1B;AAEA,SAAOG,OAAO,CAACL,UAAf;AACD,CALM;;AAOP,SAASO,0BAAT,CACEC,QADF,EA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IAAAA,UAAU,EAAE;AAAEH,MAAAA,QAAQ,EAAE2B,UAAZ;AAAwBzB,MAAAA,MAAM,EAAE0B;AAAhC;AAFP,GAAP,CADqB,EAKrB,EALqB,CAAvB;AAQA,QAAMb,cAAc,GAAG5B,OAAO,CAC5B,MACEC,QAAQ,CAAC0B,KAAT,CACE,CACE;AACEe,IAAAA,WAAW,EAAE;AACX7B,MAAAA,QADW;AAEXE,MAAAA;AAFW;AADf,GADF,CADF,EASE;AAAE4B,IAAAA,eAAe,EAAE;AAAnB,GATF,CAF0B,EAa5B,EAb4B,CAA9B;AAgBA,QAAMC,OAAO,GAAGrB,0BAA0B,CACxC;AACEK,IAAAA,cAAc,EAAGD,KAAD,IAAwB;AACtC;;AACAa,MAAAA,UAAU,CAACvB,KAAX,GAAmBU,KAAK,CAACd,QAAzB;AACA4B,MAAAA,QAAQ,CAACxB,KAAT,GAAiBU,KAAK,CAACZ,MAAvB;AACD;AALH,GADwC,EAQxC,EARwC,CAA1C;AAWA,sBACE,oBAAC,eAAD,CAAiB,QAAjB;AAA0B,IAAA,KAAK,EAAEM;AAAjC,kBACE,oBAAC,8BAAD;AACE,IAAA,wBAAwB,EAAEuB,OAD5B;AAEE,IAAA,cAAc,EAAEhB,cAFlB;AAGE,IAAA,KAAK,EAAEG,MAAM,CAACE;AAHhB,kBAKE,oBAAC,QAAD,CAAU,IAAV;AACE,IAAA,KAAK,EAAE,CACL;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACAF,IAAAA,MAAM,CAACI,MATF,EAUL;AAAEU,MAAAA,SAAS,EAAE,CAAC;AAAEC,QAAAA,UAAU,EAAE/B;AAAd,OAAD,EAAyB;AAAEgC,QAAAA,UAAU,EAAElC;AAAd,OAAzB;AAAb,KAVK;AADT,IALF,EAmBG0B,QAnBH,CADF,CADF;AAyBD,CArEM","sourcesContent":["import React, { useContext, useMemo } from 'react';\nimport { Animated, StyleSheet, ViewStyle } from 'react-native';\nimport Reanimated, {\n useEvent,\n useHandler,\n useSharedValue,\n} from 'react-native-reanimated';\nimport {\n EventWithName,\n KeyboardControllerProps,\n KeyboardControllerView,\n NativeEvent,\n useResizeMode,\n} from './native';\n\nconst KeyboardControllerViewAnimated = Reanimated.createAnimatedComponent(\n Animated.createAnimatedComponent(\n KeyboardControllerView\n ) as React.FC<KeyboardControllerProps>\n);\n\ntype AnimatedContext = {\n progress: Animated.Value;\n height: Animated.Value;\n};\ntype ReanimatedContext = {\n progress: Reanimated.SharedValue<number>;\n height: Reanimated.SharedValue<number>;\n};\ntype KeyboardAnimationContext = {\n animated: AnimatedContext;\n reanimated: ReanimatedContext;\n};\nconst defaultContext: KeyboardAnimationContext = {\n animated: {\n progress: new Animated.Value(0),\n height: new Animated.Value(0),\n },\n reanimated: {\n progress: { value: 0 },\n height: { value: 0 },\n },\n};\nconst KeyboardContext = React.createContext(defaultContext);\n\nexport const useKeyboardAnimation = (): AnimatedContext => {\n useResizeMode();\n const context = useContext(KeyboardContext);\n\n return context.animated;\n};\n\nexport const useReanimatedKeyboardAnimation = (): ReanimatedContext => {\n useResizeMode();\n const context = useContext(KeyboardContext);\n\n return context.reanimated;\n};\n\nfunction useAnimatedKeyboardHandler<TContext extends Record<string, unknown>>(\n handlers: {\n onKeyboardMove?: (e: NativeEvent, context: TContext) => void;\n },\n dependencies?: ReadonlyArray<unknown>\n) {\n const { context, doDependenciesDiffer } = useHandler(handlers, dependencies);\n\n return useEvent(\n (event: EventWithName<NativeEvent>) => {\n 'worklet';\n const { onKeyboardMove } = handlers;\n\n if (onKeyboardMove && event.eventName.endsWith('onKeyboardMove')) {\n onKeyboardMove(event, context);\n }\n },\n ['onKeyboardMove'],\n doDependenciesDiffer\n );\n}\n\ntype Styles = {\n container: ViewStyle;\n hidden: ViewStyle;\n};\n\nexport const styles = StyleSheet.create<Styles>({\n container: {\n flex: 1,\n },\n hidden: {\n display: 'none',\n position: 'absolute',\n },\n});\n\nexport const KeyboardProvider = ({\n children,\n}: {\n children: React.ReactNode;\n}) => {\n const progress = useMemo(() => new Animated.Value(0), []);\n const height = useMemo(() => new Animated.Value(0), []);\n const progressSV = useSharedValue(0);\n const heightSV = useSharedValue(0);\n const context = useMemo(\n () => ({\n animated: { progress: progress, height: height },\n reanimated: { progress: progressSV, height: heightSV },\n }),\n []\n );\n\n const onKeyboardMove = useMemo(\n () =>\n Animated.event(\n [\n {\n nativeEvent: {\n progress,\n height,\n },\n },\n ],\n { useNativeDriver: true }\n ),\n []\n );\n\n const handler = useAnimatedKeyboardHandler(\n {\n onKeyboardMove: (event: NativeEvent) => {\n 'worklet';\n progressSV.value = event.progress;\n heightSV.value = event.height;\n },\n },\n []\n );\n\n return (\n <KeyboardContext.Provider value={context}>\n <KeyboardControllerViewAnimated\n onKeyboardMoveReanimated={handler}\n onKeyboardMove={onKeyboardMove}\n style={styles.container}\n >\n <Animated.View\n style={[\n // we are using this small hack, because if the component (where\n // animated value has been used) is unmounted, then animation will\n // stop receiving events (seems like it's react-native optimization).\n // So we need to keep a reference to the animated value, to keep it's\n // always mounted (keep a reference to an animated value).\n //\n // To test why it's needed, try to open screen which consumes Animated.Value\n // then close it and open it again (for example 'Animated transition').\n styles.hidden,\n { transform: [{ translateX: height }, { translateY: progress }] },\n ]}\n />\n {children}\n </KeyboardControllerViewAnimated>\n </KeyboardContext.Provider>\n );\n};\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAd;AACA,cAAc,YAAd;AACA,cAAc,YAAd","sourcesContent":["export * from './native';\nexport * from './animated';\nexport * from './replicas';\n"]}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { useEffect } from 'react';
|
|
2
|
+
import { requireNativeComponent, UIManager, Platform, NativeModules, NativeEventEmitter } from 'react-native';
|
|
3
|
+
const LINKING_ERROR = `The package 'react-native-keyboard-controller' doesn't seem to be linked. Make sure: \n\n` + Platform.select({
|
|
4
|
+
ios: "- You have run 'pod install'\n",
|
|
5
|
+
default: ''
|
|
6
|
+
}) + '- You rebuilt the app after installing the package\n' + '- You are not using Expo managed workflow\n';
|
|
7
|
+
export let AndroidSoftInputModes;
|
|
8
|
+
|
|
9
|
+
(function (AndroidSoftInputModes) {
|
|
10
|
+
AndroidSoftInputModes[AndroidSoftInputModes["SOFT_INPUT_ADJUST_NOTHING"] = 48] = "SOFT_INPUT_ADJUST_NOTHING";
|
|
11
|
+
AndroidSoftInputModes[AndroidSoftInputModes["SOFT_INPUT_ADJUST_PAN"] = 32] = "SOFT_INPUT_ADJUST_PAN";
|
|
12
|
+
AndroidSoftInputModes[AndroidSoftInputModes["SOFT_INPUT_ADJUST_RESIZE"] = 16] = "SOFT_INPUT_ADJUST_RESIZE";
|
|
13
|
+
AndroidSoftInputModes[AndroidSoftInputModes["SOFT_INPUT_ADJUST_UNSPECIFIED"] = 0] = "SOFT_INPUT_ADJUST_UNSPECIFIED";
|
|
14
|
+
})(AndroidSoftInputModes || (AndroidSoftInputModes = {}));
|
|
15
|
+
|
|
16
|
+
const ComponentName = 'KeyboardControllerView';
|
|
17
|
+
const RCTKeyboardController = NativeModules.KeyboardController;
|
|
18
|
+
export const KeyboardController = RCTKeyboardController;
|
|
19
|
+
const eventEmitter = new NativeEventEmitter(RCTKeyboardController);
|
|
20
|
+
export const KeyboardEvents = {
|
|
21
|
+
addListener: (name, cb) => eventEmitter.addListener('KeyboardController::' + name, cb)
|
|
22
|
+
};
|
|
23
|
+
export const KeyboardControllerView = UIManager.getViewManagerConfig(ComponentName) != null ? requireNativeComponent(ComponentName) : () => {
|
|
24
|
+
throw new Error(LINKING_ERROR);
|
|
25
|
+
};
|
|
26
|
+
export const useResizeMode = () => {
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
KeyboardController.setInputMode(AndroidSoftInputModes.SOFT_INPUT_ADJUST_RESIZE);
|
|
29
|
+
return () => KeyboardController.setDefaultMode();
|
|
30
|
+
}, []);
|
|
31
|
+
};
|
|
32
|
+
//# sourceMappingURL=native.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["native.ts"],"names":["useEffect","requireNativeComponent","UIManager","Platform","NativeModules","NativeEventEmitter","LINKING_ERROR","select","ios","default","AndroidSoftInputModes","ComponentName","RCTKeyboardController","KeyboardController","eventEmitter","KeyboardEvents","addListener","name","cb","KeyboardControllerView","getViewManagerConfig","Error","useResizeMode","setInputMode","SOFT_INPUT_ADJUST_RESIZE","setDefaultMode"],"mappings":"AAAA,SAAgBA,SAAhB,QAAiC,OAAjC;AACA,SACEC,sBADF,EAEEC,SAFF,EAGEC,QAHF,EAKEC,aALF,EAMEC,kBANF,QAQO,cARP;AAUA,MAAMC,aAAa,GAChB,2FAAD,GACAH,QAAQ,CAACI,MAAT,CAAgB;AAAEC,EAAAA,GAAG,EAAE,gCAAP;AAAyCC,EAAAA,OAAO,EAAE;AAAlD,CAAhB,CADA,GAEA,sDAFA,GAGA,6CAJF;AAMA,WAAYC,qBAAZ;;WAAYA,qB;AAAAA,EAAAA,qB,CAAAA,qB;AAAAA,EAAAA,qB,CAAAA,qB;AAAAA,EAAAA,qB,CAAAA,qB;AAAAA,EAAAA,qB,CAAAA,qB;GAAAA,qB,KAAAA,qB;;AA6BZ,MAAMC,aAAa,GAAG,wBAAtB;AAEA,MAAMC,qBAAqB,GAAGR,aAAa,CAACS,kBAA5C;AACA,OAAO,MAAMA,kBAAkB,GAAGD,qBAA3B;AAEP,MAAME,YAAY,GAAG,IAAIT,kBAAJ,CAAuBO,qBAAvB,CAArB;AASA,OAAO,MAAMG,cAAc,GAAG;AAC5BC,EAAAA,WAAW,EAAE,CACXC,IADW,EAEXC,EAFW,KAGRJ,YAAY,CAACE,WAAb,CAAyB,yBAAyBC,IAAlD,EAAwDC,EAAxD;AAJuB,CAAvB;AAMP,OAAO,MAAMC,sBAAsB,GACjCjB,SAAS,CAACkB,oBAAV,CAA+BT,aAA/B,KAAiD,IAAjD,GACIV,sBAAsB,CAA0BU,aAA1B,CAD1B,GAEI,MAAM;AACJ,QAAM,IAAIU,KAAJ,CAAUf,aAAV,CAAN;AACD,CALA;AAOP,OAAO,MAAMgB,aAAa,GAAG,MAAM;AACjCtB,EAAAA,SAAS,CAAC,MAAM;AACda,IAAAA,kBAAkB,CAACU,YAAnB,CACEb,qBAAqB,CAACc,wBADxB;AAIA,WAAO,MAAMX,kBAAkB,CAACY,cAAnB,EAAb;AACD,GANQ,EAMN,EANM,CAAT;AAOD,CARM","sourcesContent":["import React, { useEffect } from 'react';\nimport {\n requireNativeComponent,\n UIManager,\n Platform,\n ViewStyle,\n NativeModules,\n NativeEventEmitter,\n NativeSyntheticEvent,\n} from 'react-native';\n\nconst LINKING_ERROR =\n `The package 'react-native-keyboard-controller' doesn't seem to be linked. Make sure: \\n\\n` +\n Platform.select({ ios: \"- You have run 'pod install'\\n\", default: '' }) +\n '- You rebuilt the app after installing the package\\n' +\n '- You are not using Expo managed workflow\\n';\n\nexport enum AndroidSoftInputModes {\n SOFT_INPUT_ADJUST_NOTHING = 48,\n SOFT_INPUT_ADJUST_PAN = 32,\n SOFT_INPUT_ADJUST_RESIZE = 16,\n SOFT_INPUT_ADJUST_UNSPECIFIED = 0,\n}\n\nexport type NativeEvent = {\n progress: number;\n height: number;\n};\nexport type EventWithName<T> = {\n eventName: string;\n} & T;\nexport type KeyboardControllerProps = {\n style?: ViewStyle;\n children: React.ReactNode;\n onKeyboardMove: (e: NativeSyntheticEvent<EventWithName<NativeEvent>>) => void;\n // fake prop used to activate reanimated bindings\n onKeyboardMoveReanimated: (\n e: NativeSyntheticEvent<EventWithName<NativeEvent>>\n ) => void;\n};\ntype KeyboardController = {\n // android only\n setDefaultMode: () => void;\n setInputMode: (mode: AndroidSoftInputModes) => void;\n};\n\nconst ComponentName = 'KeyboardControllerView';\n\nconst RCTKeyboardController = NativeModules.KeyboardController;\nexport const KeyboardController = RCTKeyboardController as KeyboardController;\n\nconst eventEmitter = new NativeEventEmitter(RCTKeyboardController);\ntype KeyboardControllerEvents =\n | 'keyboardWillShow'\n | 'keyboardDidShow'\n | 'keyboardWillHide'\n | 'keyboardDidHide';\ntype KeyboardEvent = {\n height: number;\n};\nexport const KeyboardEvents = {\n addListener: (\n name: KeyboardControllerEvents,\n cb: (e: KeyboardEvent) => void\n ) => eventEmitter.addListener('KeyboardController::' + name, cb),\n};\nexport const KeyboardControllerView =\n UIManager.getViewManagerConfig(ComponentName) != null\n ? requireNativeComponent<KeyboardControllerProps>(ComponentName)\n : () => {\n throw new Error(LINKING_ERROR);\n };\n\nexport const useResizeMode = () => {\n useEffect(() => {\n KeyboardController.setInputMode(\n AndroidSoftInputModes.SOFT_INPUT_ADJUST_RESIZE\n );\n\n return () => KeyboardController.setDefaultMode();\n }, []);\n};\n"]}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { useRef, useEffect, useMemo } from 'react';
|
|
2
|
+
import { Animated, Easing, Keyboard, Platform } from 'react-native';
|
|
3
|
+
import { runOnUI, useAnimatedReaction, useDerivedValue, useSharedValue, useWorkletCallback, withSpring } from 'react-native-reanimated';
|
|
4
|
+
import { useReanimatedKeyboardAnimation } from './animated';
|
|
5
|
+
import { AndroidSoftInputModes, KeyboardController } from './native';
|
|
6
|
+
const availableOSEventType = Platform.OS === 'ios' ? 'Will' : 'Did'; // cubic-bezier(.17,.67,.34,.94)
|
|
7
|
+
|
|
8
|
+
export const defaultAndroidEasing = Easing.bezier(0.4, 0.0, 0.2, 1);
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* An experimental implementation of tracing keyboard appearance.
|
|
12
|
+
* Switch an input mode to adjust resize mode. In this case all did* events
|
|
13
|
+
* are triggering before keyboard appears, and using some approximations
|
|
14
|
+
* it tries to mimicries a native transition.
|
|
15
|
+
*
|
|
16
|
+
* @returns {Animated.Value}
|
|
17
|
+
*/
|
|
18
|
+
export const useKeyboardAnimationReplica = () => {
|
|
19
|
+
const height = useRef(new Animated.Value(0));
|
|
20
|
+
const progress = useRef(new Animated.Value(0));
|
|
21
|
+
const animation = useMemo(() => ({
|
|
22
|
+
height: height.current,
|
|
23
|
+
progress: progress.current
|
|
24
|
+
}), []);
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
KeyboardController.setInputMode(AndroidSoftInputModes.SOFT_INPUT_ADJUST_RESIZE);
|
|
27
|
+
return () => KeyboardController.setDefaultMode();
|
|
28
|
+
}, []);
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
const listener = Keyboard.addListener(`keyboard${availableOSEventType}Show`, e => {
|
|
31
|
+
Animated.timing(height.current, {
|
|
32
|
+
toValue: -e.endCoordinates.height,
|
|
33
|
+
duration: e.duration !== 0 ? e.duration : 300,
|
|
34
|
+
easing: Easing.bezier(0.4, 0.0, 0.2, 1),
|
|
35
|
+
useNativeDriver: true
|
|
36
|
+
}).start();
|
|
37
|
+
return () => listener.remove();
|
|
38
|
+
});
|
|
39
|
+
}, []);
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
const listener = Keyboard.addListener(`keyboard${availableOSEventType}Hide`, e => {
|
|
42
|
+
Animated.timing(height.current, {
|
|
43
|
+
toValue: 0,
|
|
44
|
+
duration: e.duration !== 0 ? e.duration : 300,
|
|
45
|
+
easing: Easing.bezier(0.4, 0.0, 0.2, 1),
|
|
46
|
+
useNativeDriver: true
|
|
47
|
+
}).start();
|
|
48
|
+
return () => listener.remove();
|
|
49
|
+
});
|
|
50
|
+
}, []);
|
|
51
|
+
return animation;
|
|
52
|
+
};
|
|
53
|
+
const IOS_SPRING_CONFIG = {
|
|
54
|
+
damping: 500,
|
|
55
|
+
stiffness: 1000,
|
|
56
|
+
mass: 3,
|
|
57
|
+
overshootClamping: true,
|
|
58
|
+
restDisplacementThreshold: 10,
|
|
59
|
+
restSpeedThreshold: 10
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* A close replica to native iOS keyboard animation. The problem is that
|
|
63
|
+
* iOS (unlike Android) can not fire events for each keyboard frame movement.
|
|
64
|
+
* As a result we can not get gradual values (for example, for progress it always
|
|
65
|
+
* will be 1 or 0). So if you want to rely on gradual values you will need to use
|
|
66
|
+
* this replica.
|
|
67
|
+
*
|
|
68
|
+
* The transition is hardcoded and may vary from one to another OS versions. But it
|
|
69
|
+
* seems like last time it has been changed in iOS 7. Since RN supports at least iOS
|
|
70
|
+
* 11 it doesn't make sense to replicate iOS 7 behavior. If it changes in next OS
|
|
71
|
+
* versions, then this implementation should be revisited and reflect necessary changes.
|
|
72
|
+
*
|
|
73
|
+
* @returns {height, progress} - animated values
|
|
74
|
+
*/
|
|
75
|
+
|
|
76
|
+
export const useReanimatedKeyboardAnimationReplica = () => {
|
|
77
|
+
const height = useSharedValue(0);
|
|
78
|
+
const heightEvent = useSharedValue(0);
|
|
79
|
+
const progress = useDerivedValue(() => height.value / heightEvent.value);
|
|
80
|
+
const handler = useWorkletCallback(_height => {
|
|
81
|
+
heightEvent.value = _height;
|
|
82
|
+
}, []);
|
|
83
|
+
useAnimatedReaction(() => ({
|
|
84
|
+
_keyboardHeight: heightEvent.value
|
|
85
|
+
}), (result, _previousResult) => {
|
|
86
|
+
const {
|
|
87
|
+
_keyboardHeight
|
|
88
|
+
} = result;
|
|
89
|
+
|
|
90
|
+
const _previousKeyboardHeight = _previousResult === null || _previousResult === void 0 ? void 0 : _previousResult._keyboardHeight;
|
|
91
|
+
|
|
92
|
+
if (_keyboardHeight !== _previousKeyboardHeight) {
|
|
93
|
+
height.value = withSpring(_keyboardHeight, IOS_SPRING_CONFIG);
|
|
94
|
+
}
|
|
95
|
+
}, []);
|
|
96
|
+
useEffect(() => {
|
|
97
|
+
const show = Keyboard.addListener('keyboardWillShow', e => {
|
|
98
|
+
runOnUI(handler)(-e.endCoordinates.height);
|
|
99
|
+
});
|
|
100
|
+
const hide = Keyboard.addListener('keyboardWillHide', () => {
|
|
101
|
+
runOnUI(handler)(0);
|
|
102
|
+
});
|
|
103
|
+
return () => {
|
|
104
|
+
show.remove();
|
|
105
|
+
hide.remove();
|
|
106
|
+
};
|
|
107
|
+
}, []);
|
|
108
|
+
return {
|
|
109
|
+
height,
|
|
110
|
+
progress
|
|
111
|
+
};
|
|
112
|
+
};
|
|
113
|
+
export const useGradualKeyboardAnimation = Platform.OS === 'ios' ? useReanimatedKeyboardAnimationReplica : useReanimatedKeyboardAnimation;
|
|
114
|
+
//# sourceMappingURL=replicas.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["replicas.ts"],"names":["useRef","useEffect","useMemo","Animated","Easing","Keyboard","Platform","runOnUI","useAnimatedReaction","useDerivedValue","useSharedValue","useWorkletCallback","withSpring","useReanimatedKeyboardAnimation","AndroidSoftInputModes","KeyboardController","availableOSEventType","OS","defaultAndroidEasing","bezier","useKeyboardAnimationReplica","height","Value","progress","animation","current","setInputMode","SOFT_INPUT_ADJUST_RESIZE","setDefaultMode","listener","addListener","e","timing","toValue","endCoordinates","duration","easing","useNativeDriver","start","remove","IOS_SPRING_CONFIG","damping","stiffness","mass","overshootClamping","restDisplacementThreshold","restSpeedThreshold","useReanimatedKeyboardAnimationReplica","heightEvent","value","handler","_height","_keyboardHeight","result","_previousResult","_previousKeyboardHeight","show","hide","useGradualKeyboardAnimation"],"mappings":"AAAA,SAASA,MAAT,EAAiBC,SAAjB,EAA4BC,OAA5B,QAA2C,OAA3C;AACA,SAASC,QAAT,EAAmBC,MAAnB,EAA2BC,QAA3B,EAAqCC,QAArC,QAAqD,cAArD;AACA,SACEC,OADF,EAEEC,mBAFF,EAGEC,eAHF,EAIEC,cAJF,EAKEC,kBALF,EAMEC,UANF,QAOO,yBAPP;AAQA,SAASC,8BAAT,QAA+C,YAA/C;AAEA,SAASC,qBAAT,EAAgCC,kBAAhC,QAA0D,UAA1D;AAEA,MAAMC,oBAAoB,GAAGV,QAAQ,CAACW,EAAT,KAAgB,KAAhB,GAAwB,MAAxB,GAAiC,KAA9D,C,CAEA;;AACA,OAAO,MAAMC,oBAAoB,GAAGd,MAAM,CAACe,MAAP,CAAc,GAAd,EAAmB,GA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lC,8BAHC","sourcesContent":["import { useRef, useEffect, useMemo } from 'react';\nimport { Animated, Easing, Keyboard, Platform } from 'react-native';\nimport {\n runOnUI,\n useAnimatedReaction,\n useDerivedValue,\n useSharedValue,\n useWorkletCallback,\n withSpring,\n} from 'react-native-reanimated';\nimport { useReanimatedKeyboardAnimation } from './animated';\n\nimport { AndroidSoftInputModes, KeyboardController } from './native';\n\nconst availableOSEventType = Platform.OS === 'ios' ? 'Will' : 'Did';\n\n// cubic-bezier(.17,.67,.34,.94)\nexport const defaultAndroidEasing = Easing.bezier(0.4, 0.0, 0.2, 1);\ntype KeyboardAnimation = {\n progress: Animated.Value;\n height: Animated.Value;\n};\n\n/**\n * An experimental implementation of tracing keyboard appearance.\n * Switch an input mode to adjust resize mode. In this case all did* events\n * are triggering before keyboard appears, and using some approximations\n * it tries to mimicries a native transition.\n *\n * @returns {Animated.Value}\n */\nexport const useKeyboardAnimationReplica = (): KeyboardAnimation => {\n const height = useRef(new Animated.Value(0));\n const progress = useRef(new Animated.Value(0));\n const animation = useMemo(\n () => ({\n height: height.current,\n progress: progress.current,\n }),\n []\n );\n\n useEffect(() => {\n KeyboardController.setInputMode(\n AndroidSoftInputModes.SOFT_INPUT_ADJUST_RESIZE\n );\n\n return () => KeyboardController.setDefaultMode();\n }, []);\n useEffect(() => {\n const listener = Keyboard.addListener(\n `keyboard${availableOSEventType}Show`,\n (e) => {\n Animated.timing(height.current, {\n toValue: -e.endCoordinates.height,\n duration: e.duration !== 0 ? e.duration : 300,\n easing: Easing.bezier(0.4, 0.0, 0.2, 1),\n useNativeDriver: true,\n }).start();\n\n return () => listener.remove();\n }\n );\n }, []);\n useEffect(() => {\n const listener = Keyboard.addListener(\n `keyboard${availableOSEventType}Hide`,\n (e) => {\n Animated.timing(height.current, {\n toValue: 0,\n duration: e.duration !== 0 ? e.duration : 300,\n easing: Easing.bezier(0.4, 0.0, 0.2, 1),\n useNativeDriver: true,\n }).start();\n\n return () => listener.remove();\n }\n );\n }, []);\n\n return animation;\n};\n\nconst IOS_SPRING_CONFIG = {\n damping: 500,\n stiffness: 1000,\n mass: 3,\n overshootClamping: true,\n restDisplacementThreshold: 10,\n restSpeedThreshold: 10,\n};\n\n/**\n * A close replica to native iOS keyboard animation. The problem is that\n * iOS (unlike Android) can not fire events for each keyboard frame movement.\n * As a result we can not get gradual values (for example, for progress it always\n * will be 1 or 0). So if you want to rely on gradual values you will need to use\n * this replica.\n *\n * The transition is hardcoded and may vary from one to another OS versions. But it\n * seems like last time it has been changed in iOS 7. Since RN supports at least iOS\n * 11 it doesn't make sense to replicate iOS 7 behavior. If it changes in next OS\n * versions, then this implementation should be revisited and reflect necessary changes.\n *\n * @returns {height, progress} - animated values\n */\nexport const useReanimatedKeyboardAnimationReplica = () => {\n const height = useSharedValue(0);\n const heightEvent = useSharedValue(0);\n\n const progress = useDerivedValue(() => height.value / heightEvent.value);\n\n const handler = useWorkletCallback((_height: number) => {\n heightEvent.value = _height;\n }, []);\n\n useAnimatedReaction(\n () => ({\n _keyboardHeight: heightEvent.value,\n }),\n (result, _previousResult) => {\n const { _keyboardHeight } = result;\n const _previousKeyboardHeight = _previousResult?._keyboardHeight;\n\n if (_keyboardHeight !== _previousKeyboardHeight) {\n height.value = withSpring(_keyboardHeight, IOS_SPRING_CONFIG);\n }\n },\n []\n );\n\n useEffect(() => {\n const show = Keyboard.addListener('keyboardWillShow', (e) => {\n runOnUI(handler)(-e.endCoordinates.height);\n });\n const hide = Keyboard.addListener('keyboardWillHide', () => {\n runOnUI(handler)(0);\n });\n\n return () => {\n show.remove();\n hide.remove();\n };\n }, []);\n\n return { height, progress };\n};\n\nexport const useGradualKeyboardAnimation =\n Platform.OS === 'ios'\n ? useReanimatedKeyboardAnimationReplica\n : useReanimatedKeyboardAnimation;\n"]}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { Animated, ViewStyle } from 'react-native';
|
|
3
|
+
import Reanimated from 'react-native-reanimated';
|
|
4
|
+
declare type AnimatedContext = {
|
|
5
|
+
progress: Animated.Value;
|
|
6
|
+
height: Animated.Value;
|
|
7
|
+
};
|
|
8
|
+
declare type ReanimatedContext = {
|
|
9
|
+
progress: Reanimated.SharedValue<number>;
|
|
10
|
+
height: Reanimated.SharedValue<number>;
|
|
11
|
+
};
|
|
12
|
+
export declare const useKeyboardAnimation: () => AnimatedContext;
|
|
13
|
+
export declare const useReanimatedKeyboardAnimation: () => ReanimatedContext;
|
|
14
|
+
declare type Styles = {
|
|
15
|
+
container: ViewStyle;
|
|
16
|
+
hidden: ViewStyle;
|
|
17
|
+
};
|
|
18
|
+
export declare const styles: Styles;
|
|
19
|
+
export declare const KeyboardProvider: ({ children, }: {
|
|
20
|
+
children: React.ReactNode;
|
|
21
|
+
}) => JSX.Element;
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { ViewStyle, NativeSyntheticEvent } from 'react-native';
|
|
3
|
+
export declare enum AndroidSoftInputModes {
|
|
4
|
+
SOFT_INPUT_ADJUST_NOTHING = 48,
|
|
5
|
+
SOFT_INPUT_ADJUST_PAN = 32,
|
|
6
|
+
SOFT_INPUT_ADJUST_RESIZE = 16,
|
|
7
|
+
SOFT_INPUT_ADJUST_UNSPECIFIED = 0
|
|
8
|
+
}
|
|
9
|
+
export declare type NativeEvent = {
|
|
10
|
+
progress: number;
|
|
11
|
+
height: number;
|
|
12
|
+
};
|
|
13
|
+
export declare type EventWithName<T> = {
|
|
14
|
+
eventName: string;
|
|
15
|
+
} & T;
|
|
16
|
+
export declare type KeyboardControllerProps = {
|
|
17
|
+
style?: ViewStyle;
|
|
18
|
+
children: React.ReactNode;
|
|
19
|
+
onKeyboardMove: (e: NativeSyntheticEvent<EventWithName<NativeEvent>>) => void;
|
|
20
|
+
onKeyboardMoveReanimated: (e: NativeSyntheticEvent<EventWithName<NativeEvent>>) => void;
|
|
21
|
+
};
|
|
22
|
+
declare type KeyboardController = {
|
|
23
|
+
setDefaultMode: () => void;
|
|
24
|
+
setInputMode: (mode: AndroidSoftInputModes) => void;
|
|
25
|
+
};
|
|
26
|
+
export declare const KeyboardController: KeyboardController;
|
|
27
|
+
declare type KeyboardControllerEvents = 'keyboardWillShow' | 'keyboardDidShow' | 'keyboardWillHide' | 'keyboardDidHide';
|
|
28
|
+
declare type KeyboardEvent = {
|
|
29
|
+
height: number;
|
|
30
|
+
};
|
|
31
|
+
export declare const KeyboardEvents: {
|
|
32
|
+
addListener: (name: KeyboardControllerEvents, cb: (e: KeyboardEvent) => void) => import("react-native").EmitterSubscription;
|
|
33
|
+
};
|
|
34
|
+
export declare const KeyboardControllerView: import("react-native").HostComponent<KeyboardControllerProps> | (() => never);
|
|
35
|
+
export declare const useResizeMode: () => void;
|
|
36
|
+
export {};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/// <reference types="react-native-reanimated" />
|
|
2
|
+
import { Animated } from 'react-native';
|
|
3
|
+
export declare const defaultAndroidEasing: import("react-native").EasingFunction;
|
|
4
|
+
declare type KeyboardAnimation = {
|
|
5
|
+
progress: Animated.Value;
|
|
6
|
+
height: Animated.Value;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* An experimental implementation of tracing keyboard appearance.
|
|
10
|
+
* Switch an input mode to adjust resize mode. In this case all did* events
|
|
11
|
+
* are triggering before keyboard appears, and using some approximations
|
|
12
|
+
* it tries to mimicries a native transition.
|
|
13
|
+
*
|
|
14
|
+
* @returns {Animated.Value}
|
|
15
|
+
*/
|
|
16
|
+
export declare const useKeyboardAnimationReplica: () => KeyboardAnimation;
|
|
17
|
+
/**
|
|
18
|
+
* A close replica to native iOS keyboard animation. The problem is that
|
|
19
|
+
* iOS (unlike Android) can not fire events for each keyboard frame movement.
|
|
20
|
+
* As a result we can not get gradual values (for example, for progress it always
|
|
21
|
+
* will be 1 or 0). So if you want to rely on gradual values you will need to use
|
|
22
|
+
* this replica.
|
|
23
|
+
*
|
|
24
|
+
* The transition is hardcoded and may vary from one to another OS versions. But it
|
|
25
|
+
* seems like last time it has been changed in iOS 7. Since RN supports at least iOS
|
|
26
|
+
* 11 it doesn't make sense to replicate iOS 7 behavior. If it changes in next OS
|
|
27
|
+
* versions, then this implementation should be revisited and reflect necessary changes.
|
|
28
|
+
*
|
|
29
|
+
* @returns {height, progress} - animated values
|
|
30
|
+
*/
|
|
31
|
+
export declare const useReanimatedKeyboardAnimationReplica: () => {
|
|
32
|
+
height: import("react-native-reanimated").SharedValue<number>;
|
|
33
|
+
progress: Readonly<{
|
|
34
|
+
value: number;
|
|
35
|
+
}>;
|
|
36
|
+
};
|
|
37
|
+
export declare const useGradualKeyboardAnimation: () => {
|
|
38
|
+
progress: {
|
|
39
|
+
value: number;
|
|
40
|
+
};
|
|
41
|
+
height: {
|
|
42
|
+
value: number;
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "react-native-keyboard-controller",
|
|
3
|
+
"version": "1.0.0-alpha.0",
|
|
4
|
+
"description": "Platform agnostic keyboard manager",
|
|
5
|
+
"main": "lib/commonjs/index",
|
|
6
|
+
"module": "lib/module/index",
|
|
7
|
+
"types": "lib/typescript/index.d.ts",
|
|
8
|
+
"react-native": "src/index",
|
|
9
|
+
"source": "src/index",
|
|
10
|
+
"files": [
|
|
11
|
+
"src",
|
|
12
|
+
"lib",
|
|
13
|
+
"android",
|
|
14
|
+
"ios",
|
|
15
|
+
"cpp",
|
|
16
|
+
"react-native-keyboard-controller.podspec",
|
|
17
|
+
"!lib/typescript/example",
|
|
18
|
+
"!android/build",
|
|
19
|
+
"!ios/build",
|
|
20
|
+
"!**/__tests__",
|
|
21
|
+
"!**/__fixtures__",
|
|
22
|
+
"!**/__mocks__"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"test": "jest",
|
|
26
|
+
"typescript": "tsc --noEmit",
|
|
27
|
+
"lint": "eslint \"**/*.{js,ts,tsx}\"",
|
|
28
|
+
"prepare": "bob build",
|
|
29
|
+
"release": "release-it",
|
|
30
|
+
"example": "yarn --cwd example",
|
|
31
|
+
"pods": "cd example && pod-install --quiet",
|
|
32
|
+
"bootstrap": "yarn example && yarn && yarn pods"
|
|
33
|
+
},
|
|
34
|
+
"keywords": [
|
|
35
|
+
"react-native",
|
|
36
|
+
"ios",
|
|
37
|
+
"android"
|
|
38
|
+
],
|
|
39
|
+
"repository": "https://github.com/kirillzyusko/react-native-keyboard-controller",
|
|
40
|
+
"author": "Kiryl Ziusko <zyusko.kirik@gmail.com> (https://github.com/kirillzyusko)",
|
|
41
|
+
"license": "MIT",
|
|
42
|
+
"bugs": {
|
|
43
|
+
"url": "https://github.com/kirillzyusko/react-native-keyboard-controller/issues"
|
|
44
|
+
},
|
|
45
|
+
"homepage": "https://github.com/kirillzyusko/react-native-keyboard-controller#readme",
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"registry": "https://registry.npmjs.org/"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@commitlint/config-conventional": "^11.0.0",
|
|
51
|
+
"@react-native-community/eslint-config": "^2.0.0",
|
|
52
|
+
"@release-it/conventional-changelog": "^2.0.0",
|
|
53
|
+
"@types/jest": "^27.4.1",
|
|
54
|
+
"@types/react": "^17.0.39",
|
|
55
|
+
"@types/react-native": "0.67.6",
|
|
56
|
+
"commitlint": "^11.0.0",
|
|
57
|
+
"eslint": "^7.2.0",
|
|
58
|
+
"eslint-config-prettier": "^7.0.0",
|
|
59
|
+
"eslint-plugin-prettier": "^3.1.3",
|
|
60
|
+
"husky": "^6.0.0",
|
|
61
|
+
"jest": "^27.5.1",
|
|
62
|
+
"pod-install": "^0.1.0",
|
|
63
|
+
"prettier": "^2.6.2",
|
|
64
|
+
"react": "17.0.2",
|
|
65
|
+
"react-native": "0.68.1",
|
|
66
|
+
"react-native-builder-bob": "^0.18.0",
|
|
67
|
+
"react-native-reanimated": "2.8.0",
|
|
68
|
+
"release-it": "^14.2.2",
|
|
69
|
+
"typescript": "^4.6.3"
|
|
70
|
+
},
|
|
71
|
+
"peerDependencies": {
|
|
72
|
+
"react": "*",
|
|
73
|
+
"react-native": "*",
|
|
74
|
+
"react-native-reanimated": "^2.3.0"
|
|
75
|
+
},
|
|
76
|
+
"jest": {
|
|
77
|
+
"preset": "react-native",
|
|
78
|
+
"modulePathIgnorePatterns": [
|
|
79
|
+
"<rootDir>/example/node_modules",
|
|
80
|
+
"<rootDir>/lib/"
|
|
81
|
+
]
|
|
82
|
+
},
|
|
83
|
+
"commitlint": {
|
|
84
|
+
"extends": [
|
|
85
|
+
"@commitlint/config-conventional"
|
|
86
|
+
]
|
|
87
|
+
},
|
|
88
|
+
"release-it": {
|
|
89
|
+
"git": {
|
|
90
|
+
"commitMessage": "chore: release ${version}",
|
|
91
|
+
"tagName": "v${version}"
|
|
92
|
+
},
|
|
93
|
+
"npm": {
|
|
94
|
+
"publish": true
|
|
95
|
+
},
|
|
96
|
+
"github": {
|
|
97
|
+
"release": true
|
|
98
|
+
},
|
|
99
|
+
"plugins": {
|
|
100
|
+
"@release-it/conventional-changelog": {
|
|
101
|
+
"preset": "angular"
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
"eslintConfig": {
|
|
106
|
+
"root": true,
|
|
107
|
+
"extends": [
|
|
108
|
+
"@react-native-community",
|
|
109
|
+
"prettier"
|
|
110
|
+
],
|
|
111
|
+
"rules": {
|
|
112
|
+
"prettier/prettier": [
|
|
113
|
+
"error",
|
|
114
|
+
{
|
|
115
|
+
"quoteProps": "consistent",
|
|
116
|
+
"singleQuote": true,
|
|
117
|
+
"tabWidth": 2,
|
|
118
|
+
"trailingComma": "es5",
|
|
119
|
+
"useTabs": false
|
|
120
|
+
}
|
|
121
|
+
]
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
"eslintIgnore": [
|
|
125
|
+
"node_modules/",
|
|
126
|
+
"lib/"
|
|
127
|
+
],
|
|
128
|
+
"prettier": {
|
|
129
|
+
"quoteProps": "consistent",
|
|
130
|
+
"singleQuote": true,
|
|
131
|
+
"tabWidth": 2,
|
|
132
|
+
"trailingComma": "es5",
|
|
133
|
+
"useTabs": false
|
|
134
|
+
},
|
|
135
|
+
"react-native-builder-bob": {
|
|
136
|
+
"source": "src",
|
|
137
|
+
"output": "lib",
|
|
138
|
+
"targets": [
|
|
139
|
+
"commonjs",
|
|
140
|
+
"module",
|
|
141
|
+
[
|
|
142
|
+
"typescript",
|
|
143
|
+
{
|
|
144
|
+
"project": "tsconfig.build.json"
|
|
145
|
+
}
|
|
146
|
+
]
|
|
147
|
+
]
|
|
148
|
+
}
|
|
149
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
|
|
3
|
+
package = JSON.parse(File.read(File.join(__dir__, "package.json")))
|
|
4
|
+
|
|
5
|
+
Pod::Spec.new do |s|
|
|
6
|
+
s.name = "react-native-keyboard-controller"
|
|
7
|
+
s.version = package["version"]
|
|
8
|
+
s.summary = package["description"]
|
|
9
|
+
s.homepage = package["homepage"]
|
|
10
|
+
s.license = package["license"]
|
|
11
|
+
s.authors = package["author"]
|
|
12
|
+
|
|
13
|
+
s.platforms = { :ios => "10.0" }
|
|
14
|
+
s.source = { :git => "https://github.com/kirillzyusko/react-native-keyboard-controller.git", :tag => "#{s.version}" }
|
|
15
|
+
|
|
16
|
+
s.source_files = "ios/**/*.{h,m,mm,swift}"
|
|
17
|
+
|
|
18
|
+
s.dependency "React-Core"
|
|
19
|
+
end
|
package/src/animated.tsx
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import React, { useContext, useMemo } from 'react';
|
|
2
|
+
import { Animated, StyleSheet, ViewStyle } from 'react-native';
|
|
3
|
+
import Reanimated, {
|
|
4
|
+
useEvent,
|
|
5
|
+
useHandler,
|
|
6
|
+
useSharedValue,
|
|
7
|
+
} from 'react-native-reanimated';
|
|
8
|
+
import {
|
|
9
|
+
EventWithName,
|
|
10
|
+
KeyboardControllerProps,
|
|
11
|
+
KeyboardControllerView,
|
|
12
|
+
NativeEvent,
|
|
13
|
+
useResizeMode,
|
|
14
|
+
} from './native';
|
|
15
|
+
|
|
16
|
+
const KeyboardControllerViewAnimated = Reanimated.createAnimatedComponent(
|
|
17
|
+
Animated.createAnimatedComponent(
|
|
18
|
+
KeyboardControllerView
|
|
19
|
+
) as React.FC<KeyboardControllerProps>
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
type AnimatedContext = {
|
|
23
|
+
progress: Animated.Value;
|
|
24
|
+
height: Animated.Value;
|
|
25
|
+
};
|
|
26
|
+
type ReanimatedContext = {
|
|
27
|
+
progress: Reanimated.SharedValue<number>;
|
|
28
|
+
height: Reanimated.SharedValue<number>;
|
|
29
|
+
};
|
|
30
|
+
type KeyboardAnimationContext = {
|
|
31
|
+
animated: AnimatedContext;
|
|
32
|
+
reanimated: ReanimatedContext;
|
|
33
|
+
};
|
|
34
|
+
const defaultContext: KeyboardAnimationContext = {
|
|
35
|
+
animated: {
|
|
36
|
+
progress: new Animated.Value(0),
|
|
37
|
+
height: new Animated.Value(0),
|
|
38
|
+
},
|
|
39
|
+
reanimated: {
|
|
40
|
+
progress: { value: 0 },
|
|
41
|
+
height: { value: 0 },
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
const KeyboardContext = React.createContext(defaultContext);
|
|
45
|
+
|
|
46
|
+
export const useKeyboardAnimation = (): AnimatedContext => {
|
|
47
|
+
useResizeMode();
|
|
48
|
+
const context = useContext(KeyboardContext);
|
|
49
|
+
|
|
50
|
+
return context.animated;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export const useReanimatedKeyboardAnimation = (): ReanimatedContext => {
|
|
54
|
+
useResizeMode();
|
|
55
|
+
const context = useContext(KeyboardContext);
|
|
56
|
+
|
|
57
|
+
return context.reanimated;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
function useAnimatedKeyboardHandler<TContext extends Record<string, unknown>>(
|
|
61
|
+
handlers: {
|
|
62
|
+
onKeyboardMove?: (e: NativeEvent, context: TContext) => void;
|
|
63
|
+
},
|
|
64
|
+
dependencies?: ReadonlyArray<unknown>
|
|
65
|
+
) {
|
|
66
|
+
const { context, doDependenciesDiffer } = useHandler(handlers, dependencies);
|
|
67
|
+
|
|
68
|
+
return useEvent(
|
|
69
|
+
(event: EventWithName<NativeEvent>) => {
|
|
70
|
+
'worklet';
|
|
71
|
+
const { onKeyboardMove } = handlers;
|
|
72
|
+
|
|
73
|
+
if (onKeyboardMove && event.eventName.endsWith('onKeyboardMove')) {
|
|
74
|
+
onKeyboardMove(event, context);
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
['onKeyboardMove'],
|
|
78
|
+
doDependenciesDiffer
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
type Styles = {
|
|
83
|
+
container: ViewStyle;
|
|
84
|
+
hidden: ViewStyle;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
export const styles = StyleSheet.create<Styles>({
|
|
88
|
+
container: {
|
|
89
|
+
flex: 1,
|
|
90
|
+
},
|
|
91
|
+
hidden: {
|
|
92
|
+
display: 'none',
|
|
93
|
+
position: 'absolute',
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
export const KeyboardProvider = ({
|
|
98
|
+
children,
|
|
99
|
+
}: {
|
|
100
|
+
children: React.ReactNode;
|
|
101
|
+
}) => {
|
|
102
|
+
const progress = useMemo(() => new Animated.Value(0), []);
|
|
103
|
+
const height = useMemo(() => new Animated.Value(0), []);
|
|
104
|
+
const progressSV = useSharedValue(0);
|
|
105
|
+
const heightSV = useSharedValue(0);
|
|
106
|
+
const context = useMemo(
|
|
107
|
+
() => ({
|
|
108
|
+
animated: { progress: progress, height: height },
|
|
109
|
+
reanimated: { progress: progressSV, height: heightSV },
|
|
110
|
+
}),
|
|
111
|
+
[]
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
const onKeyboardMove = useMemo(
|
|
115
|
+
() =>
|
|
116
|
+
Animated.event(
|
|
117
|
+
[
|
|
118
|
+
{
|
|
119
|
+
nativeEvent: {
|
|
120
|
+
progress,
|
|
121
|
+
height,
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
],
|
|
125
|
+
{ useNativeDriver: true }
|
|
126
|
+
),
|
|
127
|
+
[]
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
const handler = useAnimatedKeyboardHandler(
|
|
131
|
+
{
|
|
132
|
+
onKeyboardMove: (event: NativeEvent) => {
|
|
133
|
+
'worklet';
|
|
134
|
+
progressSV.value = event.progress;
|
|
135
|
+
heightSV.value = event.height;
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
[]
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
return (
|
|
142
|
+
<KeyboardContext.Provider value={context}>
|
|
143
|
+
<KeyboardControllerViewAnimated
|
|
144
|
+
onKeyboardMoveReanimated={handler}
|
|
145
|
+
onKeyboardMove={onKeyboardMove}
|
|
146
|
+
style={styles.container}
|
|
147
|
+
>
|
|
148
|
+
<Animated.View
|
|
149
|
+
style={[
|
|
150
|
+
// we are using this small hack, because if the component (where
|
|
151
|
+
// animated value has been used) is unmounted, then animation will
|
|
152
|
+
// stop receiving events (seems like it's react-native optimization).
|
|
153
|
+
// So we need to keep a reference to the animated value, to keep it's
|
|
154
|
+
// always mounted (keep a reference to an animated value).
|
|
155
|
+
//
|
|
156
|
+
// To test why it's needed, try to open screen which consumes Animated.Value
|
|
157
|
+
// then close it and open it again (for example 'Animated transition').
|
|
158
|
+
styles.hidden,
|
|
159
|
+
{ transform: [{ translateX: height }, { translateY: progress }] },
|
|
160
|
+
]}
|
|
161
|
+
/>
|
|
162
|
+
{children}
|
|
163
|
+
</KeyboardControllerViewAnimated>
|
|
164
|
+
</KeyboardContext.Provider>
|
|
165
|
+
);
|
|
166
|
+
};
|
package/src/index.ts
ADDED