react-native-keyboard-controller 1.3.0 → 1.4.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/android/src/main/java/com/reactnativekeyboardcontroller/KeyboardAnimationCallback.kt +8 -4
- package/android/src/main/java/com/reactnativekeyboardcontroller/KeyboardControllerViewManagerImpl.kt +6 -3
- package/android/src/main/java/com/reactnativekeyboardcontroller/events/KeyboardTransitionEvent.kt +2 -6
- package/ios/.swiftlint.yml +4 -0
- package/ios/KeyboardControllerView.mm +24 -6
- package/ios/KeyboardControllerViewManager.mm +2 -0
- package/ios/KeyboardControllerViewManager.swift +4 -1
- package/ios/KeyboardMoveEvent.h +1 -0
- package/ios/KeyboardMoveEvent.m +2 -1
- package/ios/KeyboardMovementObserver.swift +89 -4
- package/lib/commonjs/animated.js +32 -6
- package/lib/commonjs/animated.js.map +1 -1
- package/lib/commonjs/context.js +2 -1
- package/lib/commonjs/context.js.map +1 -1
- package/lib/commonjs/hooks.js +26 -1
- package/lib/commonjs/hooks.js.map +1 -1
- package/lib/commonjs/internal.js +66 -2
- package/lib/commonjs/internal.js.map +1 -1
- package/lib/commonjs/specs/KeyboardControllerViewNativeComponent.js.map +1 -1
- package/lib/commonjs/types.js.map +1 -1
- package/lib/commonjs/utils.js +11 -0
- package/lib/commonjs/utils.js.map +1 -0
- package/lib/module/animated.js +34 -8
- package/lib/module/animated.js.map +1 -1
- package/lib/module/context.js +2 -1
- package/lib/module/context.js.map +1 -1
- package/lib/module/hooks.js +19 -0
- package/lib/module/hooks.js.map +1 -1
- package/lib/module/internal.js +64 -3
- package/lib/module/internal.js.map +1 -1
- package/lib/module/specs/KeyboardControllerViewNativeComponent.js.map +1 -1
- package/lib/module/types.js.map +1 -1
- package/lib/module/utils.js +2 -0
- package/lib/module/utils.js.map +1 -0
- package/lib/typescript/context.d.ts +3 -1
- package/lib/typescript/hooks.d.ts +4 -0
- package/lib/typescript/internal.d.ts +18 -1
- package/lib/typescript/specs/KeyboardControllerViewNativeComponent.d.ts +2 -0
- package/lib/typescript/types.d.ts +10 -1
- package/lib/typescript/utils.d.ts +1 -0
- package/package.json +1 -1
- package/src/animated.tsx +34 -7
- package/src/context.ts +4 -1
- package/src/hooks.ts +29 -1
- package/src/internal.ts +64 -4
- package/src/specs/KeyboardControllerViewNativeComponent.ts +4 -0
- package/src/types.ts +19 -1
- package/src/utils.ts +1 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["codegenNativeComponent"],"sources":["KeyboardControllerViewNativeComponent.ts"],"sourcesContent":["import type { HostComponent } from 'react-native';\n// @ts-expect-error - no type definition\nimport type { ViewProps } from 'react-native/Libraries/Components/View/ViewPropTypes';\nimport type {\n DirectEventHandler,\n Float,\n Int32,\n} from 'react-native/Libraries/Types/CodegenTypes';\nimport codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent';\n\ntype KeyboardMoveEvent = Readonly<{\n height: Int32;\n progress: Float;\n}>;\n\nexport interface NativeProps extends ViewProps {\n statusBarTranslucent?: boolean;\n onKeyboardMove?: DirectEventHandler<KeyboardMoveEvent>;\n}\n\nexport default codegenNativeComponent<NativeProps>(\n 'KeyboardControllerView'\n) as HostComponent<NativeProps>;\n"],"mappings":";;;;;;;AAQA;;;;AAPA;
|
|
1
|
+
{"version":3,"names":["codegenNativeComponent"],"sources":["KeyboardControllerViewNativeComponent.ts"],"sourcesContent":["import type { HostComponent } from 'react-native';\n// @ts-expect-error - no type definition\nimport type { ViewProps } from 'react-native/Libraries/Components/View/ViewPropTypes';\nimport type {\n DirectEventHandler,\n Float,\n Int32,\n} from 'react-native/Libraries/Types/CodegenTypes';\nimport codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent';\n\ntype KeyboardMoveEvent = Readonly<{\n height: Int32;\n progress: Float;\n}>;\n\nexport interface NativeProps extends ViewProps {\n // props\n statusBarTranslucent?: boolean;\n // callbacks\n onKeyboardMove?: DirectEventHandler<KeyboardMoveEvent>;\n onKeyboardMoveStart?: DirectEventHandler<KeyboardMoveEvent>;\n onKeyboardMoveEnd?: DirectEventHandler<KeyboardMoveEvent>;\n}\n\nexport default codegenNativeComponent<NativeProps>(\n 'KeyboardControllerView'\n) as HostComponent<NativeProps>;\n"],"mappings":";;;;;;;AAQA;;;;AAPA;eAuBe,IAAAA,+BAAA,EACb,wBADa,C"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":[],"sources":["types.ts"],"sourcesContent":["import type { NativeSyntheticEvent, ViewProps } from 'react-native';\n\n// DirectEventHandler events declaration\n\nexport type NativeEvent = {\n progress: number;\n height: number;\n};\nexport type EventWithName<T> = {\n eventName: string;\n} & T;\n\n// native View/Module declarations\n\nexport type KeyboardControllerProps = {\n onKeyboardMove: (e: NativeSyntheticEvent<EventWithName<NativeEvent
|
|
1
|
+
{"version":3,"names":[],"sources":["types.ts"],"sourcesContent":["import type { NativeSyntheticEvent, ViewProps } from 'react-native';\n\n// DirectEventHandler events declaration\n\nexport type NativeEvent = {\n progress: number;\n height: number;\n};\nexport type EventWithName<T> = {\n eventName: string;\n} & T;\n\n// native View/Module declarations\n\nexport type KeyboardControllerProps = {\n onKeyboardMoveStart?: (\n e: NativeSyntheticEvent<EventWithName<NativeEvent>>\n ) => void;\n onKeyboardMove?: (\n e: NativeSyntheticEvent<EventWithName<NativeEvent>>\n ) => void;\n onKeyboardMoveEnd?: (\n e: NativeSyntheticEvent<EventWithName<NativeEvent>>\n ) => void;\n // fake prop used to activate reanimated bindings\n onKeyboardMoveReanimated: (\n e: NativeSyntheticEvent<EventWithName<NativeEvent>>\n ) => void;\n statusBarTranslucent?: boolean;\n} & ViewProps;\n\nexport type KeyboardControllerModule = {\n // android only\n setDefaultMode: () => void;\n setInputMode: (mode: number) => void;\n // native event module stuff\n addListener: (eventName: string) => void;\n removeListeners: (count: number) => void;\n};\n\n// Event module declarations\n\nexport type KeyboardControllerEvents =\n | 'keyboardWillShow'\n | 'keyboardDidShow'\n | 'keyboardWillHide'\n | 'keyboardDidHide';\nexport type KeyboardEventData = {\n height: number;\n};\n\n// package types\n\nexport type Handlers<T> = Record<string, T | undefined>;\nexport type KeyboardHandler = {\n onStart?: (e: NativeEvent) => void;\n onMove?: (e: NativeEvent) => void;\n onEnd?: (e: NativeEvent) => void;\n};\nexport type KeyboardHandlers = Handlers<KeyboardHandler>;\n"],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["uuid","Math","random","toString","slice"],"sources":["utils.ts"],"sourcesContent":["export const uuid = () => Math.random().toString(36).slice(-6);\n"],"mappings":";;;;;;;AAAO,MAAMA,IAAI,GAAG,MAAMC,IAAI,CAACC,MAAL,GAAcC,QAAd,CAAuB,EAAvB,EAA2BC,KAA3B,CAAiC,CAAC,CAAlC,CAAnB"}
|
package/lib/module/animated.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import React, { useMemo, useRef } from 'react';
|
|
2
|
-
import { Animated, StyleSheet } from 'react-native';
|
|
2
|
+
import { Animated, Platform, StyleSheet } from 'react-native';
|
|
3
3
|
import Reanimated, { useSharedValue } from 'react-native-reanimated';
|
|
4
4
|
import { KeyboardContext } from './context';
|
|
5
|
-
import { useAnimatedKeyboardHandler } from './internal';
|
|
5
|
+
import { useAnimatedKeyboardHandler, useSharedHandlers } from './internal';
|
|
6
6
|
import { KeyboardControllerView } from './native';
|
|
7
7
|
const KeyboardControllerViewAnimated = Reanimated.createAnimatedComponent(Animated.createAnimatedComponent(KeyboardControllerView));
|
|
8
8
|
export const styles = StyleSheet.create({
|
|
@@ -24,17 +24,22 @@ export const KeyboardProvider = _ref => {
|
|
|
24
24
|
const height = useRef(new Animated.Value(0)).current; // shared values
|
|
25
25
|
|
|
26
26
|
const progressSV = useSharedValue(0);
|
|
27
|
-
const heightSV = useSharedValue(0);
|
|
27
|
+
const heightSV = useSharedValue(0);
|
|
28
|
+
const {
|
|
29
|
+
setHandlers,
|
|
30
|
+
broadcast
|
|
31
|
+
} = useSharedHandlers(); // memo
|
|
28
32
|
|
|
29
33
|
const context = useMemo(() => ({
|
|
30
34
|
animated: {
|
|
31
35
|
progress: progress,
|
|
32
|
-
height: height
|
|
36
|
+
height: Animated.multiply(height, -1)
|
|
33
37
|
},
|
|
34
38
|
reanimated: {
|
|
35
39
|
progress: progressSV,
|
|
36
40
|
height: heightSV
|
|
37
|
-
}
|
|
41
|
+
},
|
|
42
|
+
setHandlers
|
|
38
43
|
}), []);
|
|
39
44
|
const style = useMemo(() => [styles.hidden, {
|
|
40
45
|
transform: [{
|
|
@@ -52,19 +57,40 @@ export const KeyboardProvider = _ref => {
|
|
|
52
57
|
useNativeDriver: true
|
|
53
58
|
}), []); // handlers
|
|
54
59
|
|
|
60
|
+
const updateSharedValues = (event, platforms) => {
|
|
61
|
+
'worklet';
|
|
62
|
+
|
|
63
|
+
if (platforms.includes(Platform.OS)) {
|
|
64
|
+
progressSV.value = event.progress;
|
|
65
|
+
heightSV.value = -event.height;
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
|
|
55
69
|
const handler = useAnimatedKeyboardHandler({
|
|
70
|
+
onKeyboardMoveStart: event => {
|
|
71
|
+
'worklet';
|
|
72
|
+
|
|
73
|
+
broadcast('onStart', event);
|
|
74
|
+
updateSharedValues(event, ['ios']);
|
|
75
|
+
},
|
|
56
76
|
onKeyboardMove: event => {
|
|
57
77
|
'worklet';
|
|
58
78
|
|
|
59
|
-
|
|
60
|
-
|
|
79
|
+
broadcast('onMove', event);
|
|
80
|
+
updateSharedValues(event, ['android']);
|
|
81
|
+
},
|
|
82
|
+
onKeyboardMoveEnd: event => {
|
|
83
|
+
'worklet';
|
|
84
|
+
|
|
85
|
+
broadcast('onEnd', event);
|
|
61
86
|
}
|
|
62
87
|
}, []);
|
|
63
88
|
return /*#__PURE__*/React.createElement(KeyboardContext.Provider, {
|
|
64
89
|
value: context
|
|
65
90
|
}, /*#__PURE__*/React.createElement(KeyboardControllerViewAnimated, {
|
|
66
91
|
onKeyboardMoveReanimated: handler,
|
|
67
|
-
onKeyboardMove:
|
|
92
|
+
onKeyboardMoveStart: Platform.OS === 'ios' ? onKeyboardMove : undefined,
|
|
93
|
+
onKeyboardMove: Platform.OS === 'android' ? onKeyboardMove : undefined,
|
|
68
94
|
statusBarTranslucent: statusBarTranslucent,
|
|
69
95
|
style: styles.container
|
|
70
96
|
}, /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Animated.View, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["React","useMemo","useRef","Animated","StyleSheet","Reanimated","useSharedValue","KeyboardContext","useAnimatedKeyboardHandler","KeyboardControllerView","KeyboardControllerViewAnimated","createAnimatedComponent","styles","create","container","flex","hidden","display","position","KeyboardProvider","children","statusBarTranslucent","progress","Value","current","height","progressSV","heightSV","context","animated","reanimated","style","transform","translateX","translateY","onKeyboardMove","event","nativeEvent","useNativeDriver","
|
|
1
|
+
{"version":3,"names":["React","useMemo","useRef","Animated","Platform","StyleSheet","Reanimated","useSharedValue","KeyboardContext","useAnimatedKeyboardHandler","useSharedHandlers","KeyboardControllerView","KeyboardControllerViewAnimated","createAnimatedComponent","styles","create","container","flex","hidden","display","position","KeyboardProvider","children","statusBarTranslucent","progress","Value","current","height","progressSV","heightSV","setHandlers","broadcast","context","animated","multiply","reanimated","style","transform","translateX","translateY","onKeyboardMove","event","nativeEvent","useNativeDriver","updateSharedValues","platforms","includes","OS","value","handler","onKeyboardMoveStart","onKeyboardMoveEnd","undefined"],"sources":["animated.tsx"],"sourcesContent":["import React, { useMemo, useRef } from 'react';\nimport { Animated, Platform, StyleSheet, ViewStyle } from 'react-native';\nimport Reanimated, { useSharedValue } from 'react-native-reanimated';\n\nimport { KeyboardContext } from './context';\nimport { useAnimatedKeyboardHandler, useSharedHandlers } from './internal';\nimport { KeyboardControllerView } from './native';\n\nimport type {\n KeyboardControllerProps,\n KeyboardHandler,\n NativeEvent,\n} from './types';\n\nconst KeyboardControllerViewAnimated = Reanimated.createAnimatedComponent(\n Animated.createAnimatedComponent(\n KeyboardControllerView\n ) as React.FC<KeyboardControllerProps>\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\ntype KeyboardProviderProps = {\n children: React.ReactNode;\n /**\n * Set the value to `true`, if you use translucent status bar on Android.\n * If you already control status bar translucency via `react-native-screens`\n * or `StatusBar` component from `react-native`, you can ignore it.\n * Defaults to `false`.\n *\n * @see https://github.com/kirillzyusko/react-native-keyboard-controller/issues/14\n * @platform android\n */\n statusBarTranslucent?: boolean;\n};\n\nexport const KeyboardProvider = ({\n children,\n statusBarTranslucent,\n}: KeyboardProviderProps) => {\n // animated values\n const progress = useRef(new Animated.Value(0)).current;\n const height = useRef(new Animated.Value(0)).current;\n // shared values\n const progressSV = useSharedValue(0);\n const heightSV = useSharedValue(0);\n const { setHandlers, broadcast } = useSharedHandlers<KeyboardHandler>();\n // memo\n const context = useMemo(\n () => ({\n animated: { progress: progress, height: Animated.multiply(height, -1) },\n reanimated: { progress: progressSV, height: heightSV },\n setHandlers,\n }),\n []\n );\n const style = useMemo(\n () => [\n styles.hidden,\n { transform: [{ translateX: height }, { translateY: progress }] },\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 // handlers\n const updateSharedValues = (event: NativeEvent, platforms: string[]) => {\n 'worklet';\n\n if (platforms.includes(Platform.OS)) {\n progressSV.value = event.progress;\n heightSV.value = -event.height;\n }\n };\n const handler = useAnimatedKeyboardHandler(\n {\n onKeyboardMoveStart: (event: NativeEvent) => {\n 'worklet';\n\n broadcast('onStart', event);\n updateSharedValues(event, ['ios']);\n },\n onKeyboardMove: (event: NativeEvent) => {\n 'worklet';\n\n broadcast('onMove', event);\n updateSharedValues(event, ['android']);\n },\n onKeyboardMoveEnd: (event: NativeEvent) => {\n 'worklet';\n\n broadcast('onEnd', event);\n },\n },\n []\n );\n\n return (\n <KeyboardContext.Provider value={context}>\n <KeyboardControllerViewAnimated\n onKeyboardMoveReanimated={handler}\n onKeyboardMoveStart={Platform.OS === 'ios' ? onKeyboardMove : undefined}\n onKeyboardMove={Platform.OS === 'android' ? onKeyboardMove : undefined}\n statusBarTranslucent={statusBarTranslucent}\n style={styles.container}\n >\n <>\n <Animated.View\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 style={style}\n />\n {children}\n </>\n </KeyboardControllerViewAnimated>\n </KeyboardContext.Provider>\n );\n};\n"],"mappings":"AAAA,OAAOA,KAAP,IAAgBC,OAAhB,EAAyBC,MAAzB,QAAuC,OAAvC;AACA,SAASC,QAAT,EAAmBC,QAAnB,EAA6BC,UAA7B,QAA0D,cAA1D;AACA,OAAOC,UAAP,IAAqBC,cAArB,QAA2C,yBAA3C;AAEA,SAASC,eAAT,QAAgC,WAAhC;AACA,SAASC,0BAAT,EAAqCC,iBAArC,QAA8D,YAA9D;AACA,SAASC,sBAAT,QAAuC,UAAvC;AAQA,MAAMC,8BAA8B,GAAGN,UAAU,CAACO,uBAAX,CACrCV,QAAQ,CAACU,uBAAT,CACEF,sBADF,CADqC,CAAvC;AAWA,OAAO,MAAMG,MAAM,GAAGT,UAAU,CAACU,MAAX,CAA0B;EAC9CC,SAAS,EAAE;IACTC,IAAI,EAAE;EADG,CADmC;EAI9CC,MAAM,EAAE;IACNC,OAAO,EAAE,MADH;IAENC,QAAQ,EAAE;EAFJ;AAJsC,CAA1B,CAAf;AAwBP,OAAO,MAAMC,gBAAgB,GAAG,QAGH;EAAA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F,CADF,CADF;AA0BD,CApGM"}
|
package/lib/module/context.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["createContext","Animated","defaultContext","animated","progress","Value","height","reanimated","value","KeyboardContext"],"sources":["context.ts"],"sourcesContent":["import { createContext } from 'react';\nimport { Animated } from 'react-native';\n\nimport type { SharedValue } from 'react-native-reanimated';\n\nexport type AnimatedContext = {\n progress: Animated.Value;\n height: Animated.
|
|
1
|
+
{"version":3,"names":["createContext","Animated","defaultContext","animated","progress","Value","height","reanimated","value","setHandlers","KeyboardContext"],"sources":["context.ts"],"sourcesContent":["import { createContext } from 'react';\nimport { Animated } from 'react-native';\n\nimport type { SharedValue } from 'react-native-reanimated';\nimport type { KeyboardHandlers } from './types';\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 animated: AnimatedContext;\n reanimated: ReanimatedContext;\n setHandlers: (handlers: KeyboardHandlers) => void;\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 setHandlers: () => {},\n};\nexport const KeyboardContext = createContext(defaultContext);\n"],"mappings":"AAAA,SAASA,aAAT,QAA8B,OAA9B;AACA,SAASC,QAAT,QAAyB,cAAzB;AAkBA,MAAMC,cAAwC,GAAG;EAC/CC,QAAQ,EAAE;IACRC,QAAQ,EAAE,IAAIH,QAAQ,CAACI,KAAb,CAAmB,CAAnB,CADF;IAERC,MAAM,EAAE,IAAIL,QAAQ,CAACI,KAAb,CAAmB,CAAnB;EAFA,CADqC;EAK/CE,UAAU,EAAE;IACVH,QAAQ,EAAE;MAAEI,KAAK,EAAE;IAAT,CADA;IAEVF,MAAM,EAAE;MAAEE,KAAK,EAAE;IAAT;EAFE,CALmC;EAS/CC,WAAW,EAAE,MAAM,CAAE;AAT0B,CAAjD;AAWA,OAAO,MAAMC,eAAe,gBAAGV,aAAa,CAACE,cAAD,CAArC"}
|
package/lib/module/hooks.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useContext, useEffect } from 'react';
|
|
2
2
|
import { KeyboardContext } from './context';
|
|
3
3
|
import { AndroidSoftInputModes, KeyboardController } from './native';
|
|
4
|
+
import { uuid } from './utils';
|
|
4
5
|
export const useResizeMode = () => {
|
|
5
6
|
useEffect(() => {
|
|
6
7
|
KeyboardController.setInputMode(AndroidSoftInputModes.SOFT_INPUT_ADJUST_RESIZE);
|
|
@@ -17,4 +18,22 @@ export const useReanimatedKeyboardAnimation = () => {
|
|
|
17
18
|
const context = useContext(KeyboardContext);
|
|
18
19
|
return context.reanimated;
|
|
19
20
|
};
|
|
21
|
+
export function useGenericKeyboardHandler(handler, deps) {
|
|
22
|
+
const context = useContext(KeyboardContext);
|
|
23
|
+
useEffect(() => {
|
|
24
|
+
const key = uuid();
|
|
25
|
+
context.setHandlers({
|
|
26
|
+
[key]: handler
|
|
27
|
+
});
|
|
28
|
+
return () => {
|
|
29
|
+
context.setHandlers({
|
|
30
|
+
[key]: undefined
|
|
31
|
+
});
|
|
32
|
+
};
|
|
33
|
+
}, deps);
|
|
34
|
+
}
|
|
35
|
+
export function useKeyboardHandler(handler, deps) {
|
|
36
|
+
useResizeMode();
|
|
37
|
+
useGenericKeyboardHandler(handler, deps);
|
|
38
|
+
}
|
|
20
39
|
//# sourceMappingURL=hooks.js.map
|
package/lib/module/hooks.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["useContext","useEffect","KeyboardContext","AndroidSoftInputModes","KeyboardController","useResizeMode","setInputMode","SOFT_INPUT_ADJUST_RESIZE","setDefaultMode","useKeyboardAnimation","context","animated","useReanimatedKeyboardAnimation","reanimated"],"sources":["hooks.ts"],"sourcesContent":["import { useContext, useEffect } from 'react';\n\nimport { AnimatedContext, KeyboardContext, ReanimatedContext } from './context';\nimport { AndroidSoftInputModes, KeyboardController } from './native';\n\nexport const useResizeMode = () => {\n useEffect(() => {\n KeyboardController.setInputMode(\n AndroidSoftInputModes.SOFT_INPUT_ADJUST_RESIZE\n );\n\n return () => KeyboardController.setDefaultMode();\n }, []);\n};\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"],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"names":["useContext","useEffect","KeyboardContext","AndroidSoftInputModes","KeyboardController","uuid","useResizeMode","setInputMode","SOFT_INPUT_ADJUST_RESIZE","setDefaultMode","useKeyboardAnimation","context","animated","useReanimatedKeyboardAnimation","reanimated","useGenericKeyboardHandler","handler","deps","key","setHandlers","undefined","useKeyboardHandler"],"sources":["hooks.ts"],"sourcesContent":["import { DependencyList, useContext, useEffect } from 'react';\n\nimport { AnimatedContext, KeyboardContext, ReanimatedContext } from './context';\nimport { AndroidSoftInputModes, KeyboardController } from './native';\nimport { uuid } from './utils';\n\nimport type { KeyboardHandler } from './types';\n\nexport const useResizeMode = () => {\n useEffect(() => {\n KeyboardController.setInputMode(\n AndroidSoftInputModes.SOFT_INPUT_ADJUST_RESIZE\n );\n\n return () => KeyboardController.setDefaultMode();\n }, []);\n};\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\nexport function useGenericKeyboardHandler(\n handler: KeyboardHandler,\n deps?: DependencyList\n) {\n const context = useContext(KeyboardContext);\n\n useEffect(() => {\n const key = uuid();\n\n context.setHandlers({ [key]: handler });\n\n return () => {\n context.setHandlers({ [key]: undefined });\n };\n }, deps);\n}\n\nexport function useKeyboardHandler(\n handler: KeyboardHandler,\n deps?: DependencyList\n) {\n useResizeMode();\n useGenericKeyboardHandler(handler, deps);\n}\n"],"mappings":"AAAA,SAAyBA,UAAzB,EAAqCC,SAArC,QAAsD,OAAtD;AAEA,SAA0BC,eAA1B,QAAoE,WAApE;AACA,SAASC,qBAAT,EAAgCC,kBAAhC,QAA0D,UAA1D;AACA,SAASC,IAAT,QAAqB,SAArB;AAIA,OAAO,MAAMC,aAAa,GAAG,MAAM;EACjCL,SAAS,CAAC,MAAM;IACdG,kBAAkB,CAACG,YAAnB,CACEJ,qBAAqB,CAACK,wBADxB;IAIA,OAAO,MAAMJ,kBAAkB,CAACK,cAAnB,EAAb;EACD,CANQ,EAMN,EANM,CAAT;AAOD,CARM;AAUP,OAAO,MAAMC,oBAAoB,GAAG,MAAuB;EACzDJ,aAAa;EACb,MAAMK,OAAO,GAAGX,UAAU,CAACE,eAAD,CAA1B;EAEA,OAAOS,OAAO,CAACC,QAAf;AACD,CALM;AAOP,OAAO,MAAMC,8BAA8B,GAAG,MAAyB;EACrEP,aAAa;EACb,MAAMK,OAAO,GAAGX,UAAU,CAACE,eAAD,CAA1B;EAEA,OAAOS,OAAO,CAACG,UAAf;AACD,CALM;AAOP,OAAO,SAASC,yBAAT,CACLC,OADK,EAELC,IAFK,EAGL;EACA,MAAMN,OAAO,GAAGX,UAAU,CAACE,eAAD,CAA1B;EAEAD,SAAS,CAAC,MAAM;IACd,MAAMiB,GAAG,GAAGb,IAAI,EAAhB;IAEAM,OAAO,CAACQ,WAAR,CAAoB;MAAE,CAACD,GAAD,GAAOF;IAAT,CAApB;IAEA,OAAO,MAAM;MACXL,OAAO,CAACQ,WAAR,CAAoB;QAAE,CAACD,GAAD,GAAOE;MAAT,CAApB;IACD,CAFD;EAGD,CARQ,EAQNH,IARM,CAAT;AASD;AAED,OAAO,SAASI,kBAAT,CACLL,OADK,EAELC,IAFK,EAGL;EACAX,aAAa;EACbS,yBAAyB,CAACC,OAAD,EAAUC,IAAV,CAAzB;AACD"}
|
package/lib/module/internal.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useCallback, useRef } from 'react';
|
|
2
|
+
import { useEvent, useHandler, useSharedValue } from 'react-native-reanimated';
|
|
2
3
|
export function useAnimatedKeyboardHandler(handlers, dependencies) {
|
|
3
4
|
const {
|
|
4
5
|
context,
|
|
@@ -8,12 +9,72 @@ export function useAnimatedKeyboardHandler(handlers, dependencies) {
|
|
|
8
9
|
'worklet';
|
|
9
10
|
|
|
10
11
|
const {
|
|
11
|
-
|
|
12
|
+
onKeyboardMoveStart,
|
|
13
|
+
onKeyboardMove,
|
|
14
|
+
onKeyboardMoveEnd
|
|
12
15
|
} = handlers;
|
|
13
16
|
|
|
17
|
+
if (onKeyboardMoveStart && event.eventName.endsWith('onKeyboardMoveStart')) {
|
|
18
|
+
onKeyboardMoveStart(event, context);
|
|
19
|
+
}
|
|
20
|
+
|
|
14
21
|
if (onKeyboardMove && event.eventName.endsWith('onKeyboardMove')) {
|
|
15
22
|
onKeyboardMove(event, context);
|
|
16
23
|
}
|
|
17
|
-
|
|
24
|
+
|
|
25
|
+
if (onKeyboardMoveEnd && event.eventName.endsWith('onKeyboardMoveEnd')) {
|
|
26
|
+
onKeyboardMoveEnd(event, context);
|
|
27
|
+
}
|
|
28
|
+
}, ['onKeyboardMoveStart', 'onKeyboardMove', 'onKeyboardMoveEnd'], doDependenciesDiffer);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Hook for storing worklet handlers (objects with keys, where values are worklets).
|
|
32
|
+
* Returns methods for setting handlers and broadcasting events in them.
|
|
33
|
+
*
|
|
34
|
+
* T is a generic that looks like:
|
|
35
|
+
* @example
|
|
36
|
+
* {
|
|
37
|
+
* onEvent: () => {},
|
|
38
|
+
* onEvent2: () => {},
|
|
39
|
+
* }
|
|
40
|
+
*/
|
|
41
|
+
|
|
42
|
+
export function useSharedHandlers() {
|
|
43
|
+
const handlers = useSharedValue({});
|
|
44
|
+
const jsHandlers = useRef({}); // since js -> worklet -> js call is asynchronous, we can not write handlers
|
|
45
|
+
// straight into shared variable (using current shared value as a previous result),
|
|
46
|
+
// since there may be a race condition in a call, and closure may have out-of-dated
|
|
47
|
+
// values. As a result, some of handlers may be not written to "all handlers" object.
|
|
48
|
+
// Below we are writing all handlers to `ref` and afterwards synchronize them with
|
|
49
|
+
// shared value (since `refs` are not referring to actual value in worklets).
|
|
50
|
+
// This approach allow us to update synchronously handlers in js thread (and it assures,
|
|
51
|
+
// that it will have all of them) and then update them in worklet thread (calls are
|
|
52
|
+
// happening in FIFO order, so we will always have actual value).
|
|
53
|
+
|
|
54
|
+
const updateSharedHandlers = () => {
|
|
55
|
+
handlers.value = jsHandlers.current;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const setHandlers = useCallback(handler => {
|
|
59
|
+
jsHandlers.current = { ...jsHandlers.current,
|
|
60
|
+
...handler
|
|
61
|
+
};
|
|
62
|
+
updateSharedHandlers();
|
|
63
|
+
}, []);
|
|
64
|
+
|
|
65
|
+
const broadcast = (type, event) => {
|
|
66
|
+
'worklet';
|
|
67
|
+
|
|
68
|
+
Object.keys(handlers.value).forEach(key => {
|
|
69
|
+
var _handlers$value$key, _handlers$value$key$t;
|
|
70
|
+
|
|
71
|
+
return (_handlers$value$key = handlers.value[key]) === null || _handlers$value$key === void 0 ? void 0 : (_handlers$value$key$t = _handlers$value$key[type]) === null || _handlers$value$key$t === void 0 ? void 0 : _handlers$value$key$t.call(_handlers$value$key, event);
|
|
72
|
+
});
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
setHandlers,
|
|
77
|
+
broadcast
|
|
78
|
+
};
|
|
18
79
|
}
|
|
19
80
|
//# sourceMappingURL=internal.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["useEvent","useHandler","useAnimatedKeyboardHandler","handlers","dependencies","context","doDependenciesDiffer","event","onKeyboardMove","eventName","endsWith"],"sources":["internal.ts"],"sourcesContent":["import { useEvent, useHandler } from 'react-native-reanimated';\n\nimport type { EventWithName, NativeEvent } from './types';\n\nexport function useAnimatedKeyboardHandler<\n TContext extends Record<string, unknown>\n>(\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 }
|
|
1
|
+
{"version":3,"names":["useCallback","useRef","useEvent","useHandler","useSharedValue","useAnimatedKeyboardHandler","handlers","dependencies","context","doDependenciesDiffer","event","onKeyboardMoveStart","onKeyboardMove","onKeyboardMoveEnd","eventName","endsWith","useSharedHandlers","jsHandlers","updateSharedHandlers","value","current","setHandlers","handler","broadcast","type","Object","keys","forEach","key"],"sources":["internal.ts"],"sourcesContent":["import { useCallback, useRef } from 'react';\nimport { useEvent, useHandler, useSharedValue } from 'react-native-reanimated';\n\nimport type { EventWithName, Handlers, NativeEvent } from './types';\n\nexport function useAnimatedKeyboardHandler<\n TContext extends Record<string, unknown>\n>(\n handlers: {\n onKeyboardMoveStart?: (e: NativeEvent, context: TContext) => void;\n onKeyboardMove?: (e: NativeEvent, context: TContext) => void;\n onKeyboardMoveEnd?: (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 { onKeyboardMoveStart, onKeyboardMove, onKeyboardMoveEnd } =\n handlers;\n\n if (\n onKeyboardMoveStart &&\n event.eventName.endsWith('onKeyboardMoveStart')\n ) {\n onKeyboardMoveStart(event, context);\n }\n\n if (onKeyboardMove && event.eventName.endsWith('onKeyboardMove')) {\n onKeyboardMove(event, context);\n }\n\n if (onKeyboardMoveEnd && event.eventName.endsWith('onKeyboardMoveEnd')) {\n onKeyboardMoveEnd(event, context);\n }\n },\n ['onKeyboardMoveStart', 'onKeyboardMove', 'onKeyboardMoveEnd'],\n doDependenciesDiffer\n );\n}\n\n/**\n * Hook for storing worklet handlers (objects with keys, where values are worklets).\n * Returns methods for setting handlers and broadcasting events in them.\n *\n * T is a generic that looks like:\n * @example\n * {\n * onEvent: () => {},\n * onEvent2: () => {},\n * }\n */\nexport function useSharedHandlers<T extends Record<string, Function>>() {\n const handlers = useSharedValue<Handlers<T>>({});\n const jsHandlers = useRef<Handlers<T>>({});\n\n // since js -> worklet -> js call is asynchronous, we can not write handlers\n // straight into shared variable (using current shared value as a previous result),\n // since there may be a race condition in a call, and closure may have out-of-dated\n // values. As a result, some of handlers may be not written to \"all handlers\" object.\n // Below we are writing all handlers to `ref` and afterwards synchronize them with\n // shared value (since `refs` are not referring to actual value in worklets).\n // This approach allow us to update synchronously handlers in js thread (and it assures,\n // that it will have all of them) and then update them in worklet thread (calls are\n // happening in FIFO order, so we will always have actual value).\n const updateSharedHandlers = () => {\n handlers.value = jsHandlers.current;\n };\n const setHandlers = useCallback((handler: Handlers<T>) => {\n jsHandlers.current = {\n ...jsHandlers.current,\n ...handler,\n };\n updateSharedHandlers();\n }, []);\n const broadcast = (type: keyof T, event: NativeEvent) => {\n 'worklet';\n\n Object.keys(handlers.value).forEach((key) =>\n handlers.value[key]?.[type]?.(event)\n );\n };\n\n return { setHandlers, broadcast };\n}\n"],"mappings":"AAAA,SAASA,WAAT,EAAsBC,MAAtB,QAAoC,OAApC;AACA,SAASC,QAAT,EAAmBC,UAAnB,EAA+BC,cAA/B,QAAqD,yBAArD;AAIA,OAAO,SAASC,0BAAT,CAGLC,QAHK,EAQLC,YARK,EASL;EACA,MAAM;IAAEC,OAAF;IAAWC;EAAX,IAAoCN,UAAU,CAACG,QAAD,EAAWC,YAAX,CAApD;EAEA,OAAOL,QAAQ,CACZQ,KAAD,IAA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,IAAP,CAAYpB,QAAQ,CAACa,KAArB,EAA4BQ,OAA5B,CAAqCC,GAAD;MAAA;;MAAA,8BAClCtB,QAAQ,CAACa,KAAT,CAAeS,GAAf,CADkC,iFAClC,oBAAsBJ,IAAtB,CADkC,0DAClC,gDAA8Bd,KAA9B,CADkC;IAAA,CAApC;EAGD,CAND;;EAQA,OAAO;IAAEW,WAAF;IAAeE;EAAf,CAAP;AACD"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["codegenNativeComponent"],"sources":["KeyboardControllerViewNativeComponent.ts"],"sourcesContent":["import type { HostComponent } from 'react-native';\n// @ts-expect-error - no type definition\nimport type { ViewProps } from 'react-native/Libraries/Components/View/ViewPropTypes';\nimport type {\n DirectEventHandler,\n Float,\n Int32,\n} from 'react-native/Libraries/Types/CodegenTypes';\nimport codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent';\n\ntype KeyboardMoveEvent = Readonly<{\n height: Int32;\n progress: Float;\n}>;\n\nexport interface NativeProps extends ViewProps {\n statusBarTranslucent?: boolean;\n onKeyboardMove?: DirectEventHandler<KeyboardMoveEvent>;\n}\n\nexport default codegenNativeComponent<NativeProps>(\n 'KeyboardControllerView'\n) as HostComponent<NativeProps>;\n"],"mappings":"AACA;AAOA,OAAOA,sBAAP,MAAmC,yDAAnC;
|
|
1
|
+
{"version":3,"names":["codegenNativeComponent"],"sources":["KeyboardControllerViewNativeComponent.ts"],"sourcesContent":["import type { HostComponent } from 'react-native';\n// @ts-expect-error - no type definition\nimport type { ViewProps } from 'react-native/Libraries/Components/View/ViewPropTypes';\nimport type {\n DirectEventHandler,\n Float,\n Int32,\n} from 'react-native/Libraries/Types/CodegenTypes';\nimport codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent';\n\ntype KeyboardMoveEvent = Readonly<{\n height: Int32;\n progress: Float;\n}>;\n\nexport interface NativeProps extends ViewProps {\n // props\n statusBarTranslucent?: boolean;\n // callbacks\n onKeyboardMove?: DirectEventHandler<KeyboardMoveEvent>;\n onKeyboardMoveStart?: DirectEventHandler<KeyboardMoveEvent>;\n onKeyboardMoveEnd?: DirectEventHandler<KeyboardMoveEvent>;\n}\n\nexport default codegenNativeComponent<NativeProps>(\n 'KeyboardControllerView'\n) as HostComponent<NativeProps>;\n"],"mappings":"AACA;AAOA,OAAOA,sBAAP,MAAmC,yDAAnC;AAgBA,eAAeA,sBAAsB,CACnC,wBADmC,CAArC"}
|
package/lib/module/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":[],"sources":["types.ts"],"sourcesContent":["import type { NativeSyntheticEvent, ViewProps } from 'react-native';\n\n// DirectEventHandler events declaration\n\nexport type NativeEvent = {\n progress: number;\n height: number;\n};\nexport type EventWithName<T> = {\n eventName: string;\n} & T;\n\n// native View/Module declarations\n\nexport type KeyboardControllerProps = {\n onKeyboardMove: (e: NativeSyntheticEvent<EventWithName<NativeEvent
|
|
1
|
+
{"version":3,"names":[],"sources":["types.ts"],"sourcesContent":["import type { NativeSyntheticEvent, ViewProps } from 'react-native';\n\n// DirectEventHandler events declaration\n\nexport type NativeEvent = {\n progress: number;\n height: number;\n};\nexport type EventWithName<T> = {\n eventName: string;\n} & T;\n\n// native View/Module declarations\n\nexport type KeyboardControllerProps = {\n onKeyboardMoveStart?: (\n e: NativeSyntheticEvent<EventWithName<NativeEvent>>\n ) => void;\n onKeyboardMove?: (\n e: NativeSyntheticEvent<EventWithName<NativeEvent>>\n ) => void;\n onKeyboardMoveEnd?: (\n e: NativeSyntheticEvent<EventWithName<NativeEvent>>\n ) => void;\n // fake prop used to activate reanimated bindings\n onKeyboardMoveReanimated: (\n e: NativeSyntheticEvent<EventWithName<NativeEvent>>\n ) => void;\n statusBarTranslucent?: boolean;\n} & ViewProps;\n\nexport type KeyboardControllerModule = {\n // android only\n setDefaultMode: () => void;\n setInputMode: (mode: number) => void;\n // native event module stuff\n addListener: (eventName: string) => void;\n removeListeners: (count: number) => void;\n};\n\n// Event module declarations\n\nexport type KeyboardControllerEvents =\n | 'keyboardWillShow'\n | 'keyboardDidShow'\n | 'keyboardWillHide'\n | 'keyboardDidHide';\nexport type KeyboardEventData = {\n height: number;\n};\n\n// package types\n\nexport type Handlers<T> = Record<string, T | undefined>;\nexport type KeyboardHandler = {\n onStart?: (e: NativeEvent) => void;\n onMove?: (e: NativeEvent) => void;\n onEnd?: (e: NativeEvent) => void;\n};\nexport type KeyboardHandlers = Handlers<KeyboardHandler>;\n"],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["uuid","Math","random","toString","slice"],"sources":["utils.ts"],"sourcesContent":["export const uuid = () => Math.random().toString(36).slice(-6);\n"],"mappings":"AAAA,OAAO,MAAMA,IAAI,GAAG,MAAMC,IAAI,CAACC,MAAL,GAAcC,QAAd,CAAuB,EAAvB,EAA2BC,KAA3B,CAAiC,CAAC,CAAlC,CAAnB"}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
2
|
import { Animated } from 'react-native';
|
|
3
3
|
import type { SharedValue } from 'react-native-reanimated';
|
|
4
|
+
import type { KeyboardHandlers } from './types';
|
|
4
5
|
export declare type AnimatedContext = {
|
|
5
6
|
progress: Animated.Value;
|
|
6
|
-
height: Animated.
|
|
7
|
+
height: Animated.AnimatedMultiplication<number>;
|
|
7
8
|
};
|
|
8
9
|
export declare type ReanimatedContext = {
|
|
9
10
|
progress: SharedValue<number>;
|
|
@@ -12,5 +13,6 @@ export declare type ReanimatedContext = {
|
|
|
12
13
|
export declare type KeyboardAnimationContext = {
|
|
13
14
|
animated: AnimatedContext;
|
|
14
15
|
reanimated: ReanimatedContext;
|
|
16
|
+
setHandlers: (handlers: KeyboardHandlers) => void;
|
|
15
17
|
};
|
|
16
18
|
export declare const KeyboardContext: import("react").Context<KeyboardAnimationContext>;
|
|
@@ -1,4 +1,8 @@
|
|
|
1
|
+
import { DependencyList } from 'react';
|
|
1
2
|
import { AnimatedContext, ReanimatedContext } from './context';
|
|
3
|
+
import type { KeyboardHandler } from './types';
|
|
2
4
|
export declare const useResizeMode: () => void;
|
|
3
5
|
export declare const useKeyboardAnimation: () => AnimatedContext;
|
|
4
6
|
export declare const useReanimatedKeyboardAnimation: () => ReanimatedContext;
|
|
7
|
+
export declare function useGenericKeyboardHandler(handler: KeyboardHandler, deps?: DependencyList): void;
|
|
8
|
+
export declare function useKeyboardHandler(handler: KeyboardHandler, deps?: DependencyList): void;
|
|
@@ -1,4 +1,21 @@
|
|
|
1
|
-
import type { EventWithName, NativeEvent } from './types';
|
|
1
|
+
import type { EventWithName, Handlers, NativeEvent } from './types';
|
|
2
2
|
export declare function useAnimatedKeyboardHandler<TContext extends Record<string, unknown>>(handlers: {
|
|
3
|
+
onKeyboardMoveStart?: (e: NativeEvent, context: TContext) => void;
|
|
3
4
|
onKeyboardMove?: (e: NativeEvent, context: TContext) => void;
|
|
5
|
+
onKeyboardMoveEnd?: (e: NativeEvent, context: TContext) => void;
|
|
4
6
|
}, dependencies?: ReadonlyArray<unknown>): (e: import("react-native").NativeSyntheticEvent<EventWithName<NativeEvent>>) => void;
|
|
7
|
+
/**
|
|
8
|
+
* Hook for storing worklet handlers (objects with keys, where values are worklets).
|
|
9
|
+
* Returns methods for setting handlers and broadcasting events in them.
|
|
10
|
+
*
|
|
11
|
+
* T is a generic that looks like:
|
|
12
|
+
* @example
|
|
13
|
+
* {
|
|
14
|
+
* onEvent: () => {},
|
|
15
|
+
* onEvent2: () => {},
|
|
16
|
+
* }
|
|
17
|
+
*/
|
|
18
|
+
export declare function useSharedHandlers<T extends Record<string, Function>>(): {
|
|
19
|
+
setHandlers: (handler: Handlers<T>) => void;
|
|
20
|
+
broadcast: (type: keyof T, event: NativeEvent) => void;
|
|
21
|
+
};
|
|
@@ -8,6 +8,8 @@ declare type KeyboardMoveEvent = Readonly<{
|
|
|
8
8
|
export interface NativeProps extends ViewProps {
|
|
9
9
|
statusBarTranslucent?: boolean;
|
|
10
10
|
onKeyboardMove?: DirectEventHandler<KeyboardMoveEvent>;
|
|
11
|
+
onKeyboardMoveStart?: DirectEventHandler<KeyboardMoveEvent>;
|
|
12
|
+
onKeyboardMoveEnd?: DirectEventHandler<KeyboardMoveEvent>;
|
|
11
13
|
}
|
|
12
14
|
declare const _default: HostComponent<NativeProps>;
|
|
13
15
|
export default _default;
|
|
@@ -7,7 +7,9 @@ export declare type EventWithName<T> = {
|
|
|
7
7
|
eventName: string;
|
|
8
8
|
} & T;
|
|
9
9
|
export declare type KeyboardControllerProps = {
|
|
10
|
-
|
|
10
|
+
onKeyboardMoveStart?: (e: NativeSyntheticEvent<EventWithName<NativeEvent>>) => void;
|
|
11
|
+
onKeyboardMove?: (e: NativeSyntheticEvent<EventWithName<NativeEvent>>) => void;
|
|
12
|
+
onKeyboardMoveEnd?: (e: NativeSyntheticEvent<EventWithName<NativeEvent>>) => void;
|
|
11
13
|
onKeyboardMoveReanimated: (e: NativeSyntheticEvent<EventWithName<NativeEvent>>) => void;
|
|
12
14
|
statusBarTranslucent?: boolean;
|
|
13
15
|
} & ViewProps;
|
|
@@ -21,3 +23,10 @@ export declare type KeyboardControllerEvents = 'keyboardWillShow' | 'keyboardDid
|
|
|
21
23
|
export declare type KeyboardEventData = {
|
|
22
24
|
height: number;
|
|
23
25
|
};
|
|
26
|
+
export declare type Handlers<T> = Record<string, T | undefined>;
|
|
27
|
+
export declare type KeyboardHandler = {
|
|
28
|
+
onStart?: (e: NativeEvent) => void;
|
|
29
|
+
onMove?: (e: NativeEvent) => void;
|
|
30
|
+
onEnd?: (e: NativeEvent) => void;
|
|
31
|
+
};
|
|
32
|
+
export declare type KeyboardHandlers = Handlers<KeyboardHandler>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const uuid: () => string;
|
package/package.json
CHANGED
package/src/animated.tsx
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
import React, { useMemo, useRef } from 'react';
|
|
2
|
-
import { Animated, StyleSheet, ViewStyle } from 'react-native';
|
|
2
|
+
import { Animated, Platform, StyleSheet, ViewStyle } from 'react-native';
|
|
3
3
|
import Reanimated, { useSharedValue } from 'react-native-reanimated';
|
|
4
4
|
|
|
5
5
|
import { KeyboardContext } from './context';
|
|
6
|
-
import { useAnimatedKeyboardHandler } from './internal';
|
|
6
|
+
import { useAnimatedKeyboardHandler, useSharedHandlers } from './internal';
|
|
7
7
|
import { KeyboardControllerView } from './native';
|
|
8
8
|
|
|
9
|
-
import type {
|
|
9
|
+
import type {
|
|
10
|
+
KeyboardControllerProps,
|
|
11
|
+
KeyboardHandler,
|
|
12
|
+
NativeEvent,
|
|
13
|
+
} from './types';
|
|
10
14
|
|
|
11
15
|
const KeyboardControllerViewAnimated = Reanimated.createAnimatedComponent(
|
|
12
16
|
Animated.createAnimatedComponent(
|
|
@@ -53,11 +57,13 @@ export const KeyboardProvider = ({
|
|
|
53
57
|
// shared values
|
|
54
58
|
const progressSV = useSharedValue(0);
|
|
55
59
|
const heightSV = useSharedValue(0);
|
|
60
|
+
const { setHandlers, broadcast } = useSharedHandlers<KeyboardHandler>();
|
|
56
61
|
// memo
|
|
57
62
|
const context = useMemo(
|
|
58
63
|
() => ({
|
|
59
|
-
animated: { progress: progress, height: height },
|
|
64
|
+
animated: { progress: progress, height: Animated.multiply(height, -1) },
|
|
60
65
|
reanimated: { progress: progressSV, height: heightSV },
|
|
66
|
+
setHandlers,
|
|
61
67
|
}),
|
|
62
68
|
[]
|
|
63
69
|
);
|
|
@@ -84,12 +90,32 @@ export const KeyboardProvider = ({
|
|
|
84
90
|
[]
|
|
85
91
|
);
|
|
86
92
|
// handlers
|
|
93
|
+
const updateSharedValues = (event: NativeEvent, platforms: string[]) => {
|
|
94
|
+
'worklet';
|
|
95
|
+
|
|
96
|
+
if (platforms.includes(Platform.OS)) {
|
|
97
|
+
progressSV.value = event.progress;
|
|
98
|
+
heightSV.value = -event.height;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
87
101
|
const handler = useAnimatedKeyboardHandler(
|
|
88
102
|
{
|
|
103
|
+
onKeyboardMoveStart: (event: NativeEvent) => {
|
|
104
|
+
'worklet';
|
|
105
|
+
|
|
106
|
+
broadcast('onStart', event);
|
|
107
|
+
updateSharedValues(event, ['ios']);
|
|
108
|
+
},
|
|
89
109
|
onKeyboardMove: (event: NativeEvent) => {
|
|
90
110
|
'worklet';
|
|
91
|
-
|
|
92
|
-
|
|
111
|
+
|
|
112
|
+
broadcast('onMove', event);
|
|
113
|
+
updateSharedValues(event, ['android']);
|
|
114
|
+
},
|
|
115
|
+
onKeyboardMoveEnd: (event: NativeEvent) => {
|
|
116
|
+
'worklet';
|
|
117
|
+
|
|
118
|
+
broadcast('onEnd', event);
|
|
93
119
|
},
|
|
94
120
|
},
|
|
95
121
|
[]
|
|
@@ -99,7 +125,8 @@ export const KeyboardProvider = ({
|
|
|
99
125
|
<KeyboardContext.Provider value={context}>
|
|
100
126
|
<KeyboardControllerViewAnimated
|
|
101
127
|
onKeyboardMoveReanimated={handler}
|
|
102
|
-
|
|
128
|
+
onKeyboardMoveStart={Platform.OS === 'ios' ? onKeyboardMove : undefined}
|
|
129
|
+
onKeyboardMove={Platform.OS === 'android' ? onKeyboardMove : undefined}
|
|
103
130
|
statusBarTranslucent={statusBarTranslucent}
|
|
104
131
|
style={styles.container}
|
|
105
132
|
>
|
package/src/context.ts
CHANGED
|
@@ -2,10 +2,11 @@ import { createContext } from 'react';
|
|
|
2
2
|
import { Animated } from 'react-native';
|
|
3
3
|
|
|
4
4
|
import type { SharedValue } from 'react-native-reanimated';
|
|
5
|
+
import type { KeyboardHandlers } from './types';
|
|
5
6
|
|
|
6
7
|
export type AnimatedContext = {
|
|
7
8
|
progress: Animated.Value;
|
|
8
|
-
height: Animated.
|
|
9
|
+
height: Animated.AnimatedMultiplication<number>;
|
|
9
10
|
};
|
|
10
11
|
export type ReanimatedContext = {
|
|
11
12
|
progress: SharedValue<number>;
|
|
@@ -14,6 +15,7 @@ export type ReanimatedContext = {
|
|
|
14
15
|
export type KeyboardAnimationContext = {
|
|
15
16
|
animated: AnimatedContext;
|
|
16
17
|
reanimated: ReanimatedContext;
|
|
18
|
+
setHandlers: (handlers: KeyboardHandlers) => void;
|
|
17
19
|
};
|
|
18
20
|
const defaultContext: KeyboardAnimationContext = {
|
|
19
21
|
animated: {
|
|
@@ -24,5 +26,6 @@ const defaultContext: KeyboardAnimationContext = {
|
|
|
24
26
|
progress: { value: 0 },
|
|
25
27
|
height: { value: 0 },
|
|
26
28
|
},
|
|
29
|
+
setHandlers: () => {},
|
|
27
30
|
};
|
|
28
31
|
export const KeyboardContext = createContext(defaultContext);
|
package/src/hooks.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
import { useContext, useEffect } from 'react';
|
|
1
|
+
import { DependencyList, useContext, useEffect } from 'react';
|
|
2
2
|
|
|
3
3
|
import { AnimatedContext, KeyboardContext, ReanimatedContext } from './context';
|
|
4
4
|
import { AndroidSoftInputModes, KeyboardController } from './native';
|
|
5
|
+
import { uuid } from './utils';
|
|
6
|
+
|
|
7
|
+
import type { KeyboardHandler } from './types';
|
|
5
8
|
|
|
6
9
|
export const useResizeMode = () => {
|
|
7
10
|
useEffect(() => {
|
|
@@ -26,3 +29,28 @@ export const useReanimatedKeyboardAnimation = (): ReanimatedContext => {
|
|
|
26
29
|
|
|
27
30
|
return context.reanimated;
|
|
28
31
|
};
|
|
32
|
+
|
|
33
|
+
export function useGenericKeyboardHandler(
|
|
34
|
+
handler: KeyboardHandler,
|
|
35
|
+
deps?: DependencyList
|
|
36
|
+
) {
|
|
37
|
+
const context = useContext(KeyboardContext);
|
|
38
|
+
|
|
39
|
+
useEffect(() => {
|
|
40
|
+
const key = uuid();
|
|
41
|
+
|
|
42
|
+
context.setHandlers({ [key]: handler });
|
|
43
|
+
|
|
44
|
+
return () => {
|
|
45
|
+
context.setHandlers({ [key]: undefined });
|
|
46
|
+
};
|
|
47
|
+
}, deps);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function useKeyboardHandler(
|
|
51
|
+
handler: KeyboardHandler,
|
|
52
|
+
deps?: DependencyList
|
|
53
|
+
) {
|
|
54
|
+
useResizeMode();
|
|
55
|
+
useGenericKeyboardHandler(handler, deps);
|
|
56
|
+
}
|