-
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathindex.ts
More file actions
149 lines (131 loc) · 4.39 KB
/
index.ts
File metadata and controls
149 lines (131 loc) · 4.39 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { useCallback } from "react";
import { Platform } from "react-native";
import { scrollTo, useAnimatedReaction } from "react-native-reanimated";
import { IS_FABRIC } from "../../../architecture";
import { isScrollAtEnd, shouldShiftContent } from "../useChatKeyboard/helpers";
import type { KeyboardLiftBehavior } from "../useChatKeyboard/types";
import type { AnimatedRef, SharedValue } from "react-native-reanimated";
import type Reanimated from "react-native-reanimated";
type UseExtraContentPaddingOptions = {
scrollViewRef: AnimatedRef<Reanimated.ScrollView>;
extraContentPadding: SharedValue<number>;
/** Keyboard-only padding from useChatKeyboard — used to compute total padding for clamping. */
keyboardPadding: SharedValue<number>;
/** Minimum inset floor — used to absorb keyboard and extraContentPadding changes. */
blankSpace: SharedValue<number>;
/** Current vertical scroll offset. */
scroll: SharedValue<number>;
/** Visible viewport dimensions. */
layout: SharedValue<{ width: number; height: number }>;
/** Total content dimensions. */
size: SharedValue<{ width: number; height: number }>;
/** IOS only — when provided, sets contentOffset atomically with contentInset. */
contentOffsetY?: SharedValue<number>;
inverted: boolean;
keyboardLiftBehavior: KeyboardLiftBehavior;
freeze: SharedValue<boolean>;
};
/**
* Hook that reacts to `extraContentPadding` changes and conditionally
* adjusts the scroll position using `scrollTo` on both iOS and Android.
*
* Padding extension (scrollable range) is handled externally via a
* `useDerivedValue` that sums keyboard padding + extra content padding.
* This hook only handles the scroll correction.
*
* @param options - Configuration and shared values.
* @example
* ```tsx
* useExtraContentPadding({ scrollViewRef, extraContentPadding, ... });
* ```
*/
function useExtraContentPadding(options: UseExtraContentPaddingOptions): void {
const {
scrollViewRef,
extraContentPadding,
keyboardPadding,
blankSpace,
scroll,
layout,
size,
contentOffsetY,
inverted,
keyboardLiftBehavior,
freeze,
} = options;
const scrollToTarget = useCallback(
(target: number) => {
"worklet";
if (contentOffsetY && IS_FABRIC) {
// eslint-disable-next-line react-compiler/react-compiler
contentOffsetY.value = target;
} else if (Platform.OS === "android") {
// Defer scrollTo so the animatedProps inset commit lands first;
// otherwise the native ScrollView clamps to the old range.
requestAnimationFrame(() => {
scrollTo(scrollViewRef, 0, target, false);
});
} else {
scrollTo(scrollViewRef, 0, target, false);
}
},
[scrollViewRef, contentOffsetY],
);
useAnimatedReaction(
() => extraContentPadding.value,
(current, previous) => {
if (freeze.value || previous === null) {
return;
}
const rawDelta = current - previous;
if (rawDelta === 0) {
return;
}
// Compute effective delta considering blankSpace floor
const previousTotal = Math.max(
blankSpace.value,
keyboardPadding.value + previous,
);
const currentTotal = Math.max(
blankSpace.value,
keyboardPadding.value + current,
);
const effectiveDelta = currentTotal - previousTotal;
if (effectiveDelta === 0) {
// blankSpace absorbed the change
return;
}
const atEnd = isScrollAtEnd(
scroll.value,
layout.value.height,
size.value.height,
inverted,
);
// "persistent": scroll on grow, hold position on shrink (unless at end)
if (
keyboardLiftBehavior === "persistent" &&
effectiveDelta < 0 &&
!atEnd
) {
return;
}
if (!shouldShiftContent(keyboardLiftBehavior, atEnd)) {
return;
}
if (inverted) {
const target = Math.max(scroll.value - effectiveDelta, -currentTotal);
scrollToTarget(target);
} else {
const maxScroll = Math.max(
size.value.height - layout.value.height + currentTotal,
0,
);
const target = Math.min(scroll.value + effectiveDelta, maxScroll);
scrollToTarget(target);
}
},
[inverted, keyboardLiftBehavior],
);
}
export { useExtraContentPadding };
export type { UseExtraContentPaddingOptions };