react-native-keyboard-controller 1.6.0 → 1.7.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 (48) hide show
  1. package/README.md +1 -0
  2. package/android/src/main/java/com/reactnativekeyboardcontroller/KeyboardAnimationCallback.kt +9 -1
  3. package/android/src/main/java/com/reactnativekeyboardcontroller/events/KeyboardTransitionEvent.kt +9 -9
  4. package/android/src/main/java/com/reactnativekeyboardcontroller/extensions/ViewGroup.kt +10 -0
  5. package/android/src/main/java/com/reactnativekeyboardcontroller/modules/KeyboardControllerModuleImpl.kt +13 -7
  6. package/android/src/main/java/com/reactnativekeyboardcontroller/views/EdgeToEdgeReactViewGroup.kt +58 -40
  7. package/ios/Extensions.swift +2 -2
  8. package/ios/KeyboardController-Bridging-Header.h +0 -1
  9. package/lib/commonjs/animated.js +1 -1
  10. package/lib/commonjs/animated.js.map +1 -1
  11. package/lib/commonjs/components/KeyboardAvoidingView/hooks.js +35 -0
  12. package/lib/commonjs/components/KeyboardAvoidingView/hooks.js.map +1 -0
  13. package/lib/commonjs/components/KeyboardAvoidingView/index.js +97 -0
  14. package/lib/commonjs/components/KeyboardAvoidingView/index.js.map +1 -0
  15. package/lib/commonjs/components/index.js +14 -0
  16. package/lib/commonjs/components/index.js.map +1 -0
  17. package/lib/commonjs/context.js +9 -6
  18. package/lib/commonjs/context.js.map +1 -1
  19. package/lib/commonjs/index.js +17 -0
  20. package/lib/commonjs/index.js.map +1 -1
  21. package/lib/commonjs/types.js.map +1 -1
  22. package/lib/module/animated.js +1 -1
  23. package/lib/module/animated.js.map +1 -1
  24. package/lib/module/components/KeyboardAvoidingView/hooks.js +28 -0
  25. package/lib/module/components/KeyboardAvoidingView/hooks.js.map +1 -0
  26. package/lib/module/components/KeyboardAvoidingView/index.js +88 -0
  27. package/lib/module/components/KeyboardAvoidingView/index.js.map +1 -0
  28. package/lib/module/components/index.js +2 -0
  29. package/lib/module/components/index.js.map +1 -0
  30. package/lib/module/context.js +9 -6
  31. package/lib/module/context.js.map +1 -1
  32. package/lib/module/index.js +1 -0
  33. package/lib/module/index.js.map +1 -1
  34. package/lib/module/types.js.map +1 -1
  35. package/lib/typescript/components/KeyboardAvoidingView/hooks.d.ts +5 -0
  36. package/lib/typescript/components/KeyboardAvoidingView/index.d.ts +29 -0
  37. package/lib/typescript/components/index.d.ts +1 -0
  38. package/lib/typescript/index.d.ts +1 -0
  39. package/lib/typescript/replicas.d.ts +2 -7
  40. package/lib/typescript/types.d.ts +1 -1
  41. package/package.json +5 -5
  42. package/src/animated.tsx +1 -1
  43. package/src/components/KeyboardAvoidingView/hooks.ts +29 -0
  44. package/src/components/KeyboardAvoidingView/index.tsx +147 -0
  45. package/src/components/index.ts +1 -0
  46. package/src/context.ts +9 -2
  47. package/src/index.ts +2 -0
  48. package/src/types.ts +1 -1
@@ -0,0 +1,147 @@
1
+ import React, { forwardRef, useCallback, useMemo } from 'react';
2
+ import {
3
+ LayoutRectangle,
4
+ useWindowDimensions,
5
+ View,
6
+ ViewProps,
7
+ } from 'react-native';
8
+ import Reanimated, {
9
+ useAnimatedStyle,
10
+ useWorkletCallback,
11
+ useSharedValue,
12
+ useDerivedValue,
13
+ interpolate,
14
+ } from 'react-native-reanimated';
15
+ import { useKeyboardAnimation } from './hooks';
16
+
17
+ type Props = {
18
+ /**
19
+ * Specify how to react to the presence of the keyboard.
20
+ */
21
+ behavior?: 'height' | 'position' | 'padding';
22
+
23
+ /**
24
+ * Style of the content container when `behavior` is 'position'.
25
+ */
26
+ contentContainerStyle?: ViewProps['style'];
27
+
28
+ /**
29
+ * Controls whether this `KeyboardAvoidingView` instance should take effect.
30
+ * This is useful when more than one is on the screen. Defaults to true.
31
+ */
32
+ enabled?: boolean;
33
+
34
+ /**
35
+ * Distance between the top of the user screen and the React Native view. This
36
+ * may be non-zero in some cases. Defaults to 0.
37
+ */
38
+ keyboardVerticalOffset?: number;
39
+ } & ViewProps;
40
+
41
+ const defaultLayout: LayoutRectangle = {
42
+ x: 0,
43
+ y: 0,
44
+ width: 0,
45
+ height: 0,
46
+ };
47
+
48
+ /**
49
+ * View that moves out of the way when the keyboard appears by automatically
50
+ * adjusting its height, position, or bottom padding.
51
+ */
52
+ const KeyboardAvoidingView = forwardRef<View, React.PropsWithChildren<Props>>(
53
+ (
54
+ {
55
+ behavior,
56
+ children,
57
+ contentContainerStyle,
58
+ enabled = true,
59
+ keyboardVerticalOffset = 0,
60
+ style,
61
+ onLayout: onLayoutProps,
62
+ ...props
63
+ },
64
+ ref
65
+ ) => {
66
+ const initialFrame = useSharedValue<LayoutRectangle | null>(null);
67
+ const frame = useDerivedValue(() => initialFrame.value || defaultLayout);
68
+
69
+ const keyboard = useKeyboardAnimation();
70
+ const { height: screenHeight } = useWindowDimensions();
71
+
72
+ const relativeKeyboardHeight = useWorkletCallback(() => {
73
+ const keyboardY =
74
+ screenHeight - keyboard.heightWhenOpened.value - keyboardVerticalOffset;
75
+
76
+ return Math.max(frame.value.y + frame.value.height - keyboardY, 0);
77
+ }, [screenHeight, keyboardVerticalOffset]);
78
+
79
+ const onLayout = useCallback<NonNullable<ViewProps['onLayout']>>(
80
+ (e) => {
81
+ if (initialFrame.value === null) {
82
+ initialFrame.value = e.nativeEvent.layout;
83
+ }
84
+ onLayoutProps?.(e);
85
+ },
86
+ [onLayoutProps]
87
+ );
88
+
89
+ const animatedStyle = useAnimatedStyle(() => {
90
+ const bottom = interpolate(
91
+ keyboard.progress.value,
92
+ [0, 1],
93
+ [0, relativeKeyboardHeight()]
94
+ );
95
+ const bottomHeight = enabled ? bottom : 0;
96
+
97
+ switch (behavior) {
98
+ case 'height':
99
+ if (bottomHeight > 0) {
100
+ return {
101
+ height: frame.value.height - bottomHeight,
102
+ flex: 0,
103
+ };
104
+ }
105
+
106
+ return {};
107
+
108
+ case 'position':
109
+ return { bottom: bottomHeight };
110
+
111
+ case 'padding':
112
+ return { paddingBottom: bottomHeight };
113
+
114
+ default:
115
+ return {};
116
+ }
117
+ }, [behavior, enabled, relativeKeyboardHeight]);
118
+ const isPositionBehavior = behavior === 'position';
119
+ const containerStyle = isPositionBehavior ? contentContainerStyle : style;
120
+ const combinedStyles = useMemo(
121
+ () => [containerStyle, animatedStyle],
122
+ [containerStyle, animatedStyle]
123
+ );
124
+
125
+ if (isPositionBehavior) {
126
+ return (
127
+ <View ref={ref} style={style} onLayout={onLayout} {...props}>
128
+ <Reanimated.View style={combinedStyles}>{children}</Reanimated.View>
129
+ </View>
130
+ );
131
+ }
132
+
133
+ return (
134
+ <Reanimated.View
135
+ // @ts-expect-error because `ref` from reanimated is not compatible with react-native
136
+ ref={ref}
137
+ onLayout={onLayout}
138
+ style={combinedStyles}
139
+ {...props}
140
+ >
141
+ {children}
142
+ </Reanimated.View>
143
+ );
144
+ }
145
+ );
146
+
147
+ export default KeyboardAvoidingView;
@@ -0,0 +1 @@
1
+ export { default as KeyboardAvoidingView } from './KeyboardAvoidingView';
package/src/context.ts CHANGED
@@ -17,14 +17,21 @@ export type KeyboardAnimationContext = {
17
17
  reanimated: ReanimatedContext;
18
18
  setHandlers: (handlers: KeyboardHandlers) => void;
19
19
  };
20
+ const NOOP = () => {};
21
+ const DEFAULT_SHARED_VALUE: SharedValue<number> = {
22
+ value: 0,
23
+ addListener: NOOP,
24
+ removeListener: NOOP,
25
+ modify: NOOP,
26
+ };
20
27
  const defaultContext: KeyboardAnimationContext = {
21
28
  animated: {
22
29
  progress: new Animated.Value(0),
23
30
  height: new Animated.Value(0),
24
31
  },
25
32
  reanimated: {
26
- progress: { value: 0 },
27
- height: { value: 0 },
33
+ progress: DEFAULT_SHARED_VALUE,
34
+ height: DEFAULT_SHARED_VALUE,
28
35
  },
29
36
  setHandlers: () => {},
30
37
  };
package/src/index.ts CHANGED
@@ -7,3 +7,5 @@ export * from './context';
7
7
  export * from './hooks';
8
8
  export * from './constants';
9
9
  export * from './types';
10
+
11
+ export { KeyboardAvoidingView } from './components';
package/src/types.ts CHANGED
@@ -88,7 +88,7 @@ export type KeyboardHandlerHook<TContext, Event> = (
88
88
  onKeyboardMoveEnd?: (e: NativeEvent, context: TContext) => void;
89
89
  onKeyboardMoveInteractive?: (e: NativeEvent, context: TContext) => void;
90
90
  },
91
- dependencies?: ReadonlyArray<unknown>
91
+ dependencies?: unknown[]
92
92
  ) => (e: NativeSyntheticEvent<Event>) => void;
93
93
 
94
94
  // package types