-
-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathindex.tsx
55 lines (46 loc) · 1.36 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import React, { forwardRef, useMemo } from "react";
import Reanimated, { clamp, useAnimatedStyle } from "react-native-reanimated";
import { useReanimatedKeyboardAnimation } from "react-native-keyboard-controller";
import type { View, ViewProps } from "react-native";
type KeyboardStickyViewProps = {
/**
* Specify additional offset to the view for given keyboard state.
*/
offset?: {
/**
* Adds additional `translateY` when keyboard is close. By default `0`.
*/
closed?: number;
/**
* Adds additional `translateY` when keyboard is open. By default `0`.
*/
opened?: number;
};
} & ViewProps;
const KeyboardStickyView = forwardRef<
View,
React.PropsWithChildren<KeyboardStickyViewProps>
>(
(
{ children, offset: { closed = 0, opened = 0 } = {}, style, ...props },
ref,
) => {
const { height } = useReanimatedKeyboardAnimation();
const stickyViewStyle = useAnimatedStyle(() => {
const offset = clamp(height.value, closed, opened + height.value);
return {
transform: [{ translateY: offset }],
};
}, [closed, opened]);
const styles = useMemo(
() => [style, stickyViewStyle],
[style, stickyViewStyle],
);
return (
<Reanimated.View ref={ref} style={styles} {...props}>
{children}
</Reanimated.View>
);
},
);
export default KeyboardStickyView;