-
-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathindex.ios.ts
More file actions
260 lines (225 loc) · 7 KB
/
index.ios.ts
File metadata and controls
260 lines (225 loc) · 7 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import { useSharedValue } from "react-native-reanimated";
import { useKeyboardHandler } from "../../../hooks";
import useScrollState from "../../hooks/useScrollState";
import {
computeIOSContentOffset,
getEffectiveHeight,
getScrollEffective,
getVisibleMinimumPaddingFraction,
isScrollAtEnd,
shouldShiftContent,
} from "./helpers";
import type { UseChatKeyboardOptions, UseChatKeyboardReturn } from "./types";
import type { AnimatedRef } from "react-native-reanimated";
import type Reanimated from "react-native-reanimated";
/**
* Hook that manages keyboard-driven scrolling for chat-style scroll views.
* Calculates padding (extra scrollable space) and content shift values,
* using iOS-specific strategy (contentOffset set once in onStart).
*
* @param scrollViewRef - Animated ref to the scroll view.
* @param options - Configuration for inverted and keyboardLiftBehavior.
* @returns Shared values for padding and contentOffsetY.
* @example
* ```tsx
* const { padding, contentOffsetY } = useChatKeyboard(ref, {
* inverted: false,
* keyboardLiftBehavior: "always",
* });
* ```
*/
function useChatKeyboard(
scrollViewRef: AnimatedRef<Reanimated.ScrollView>,
options: UseChatKeyboardOptions,
): UseChatKeyboardReturn {
const {
inverted,
keyboardLiftBehavior,
freeze,
offset,
blankSpace,
extraContentPadding,
} = options;
const padding = useSharedValue(0);
const currentHeight = useSharedValue(0);
const contentOffsetY = useSharedValue(0);
const targetKeyboardHeight = useSharedValue(0);
const prevAbsorption = useSharedValue(0);
const {
layout,
size,
offset: scroll,
onLayout,
onContentSizeChange,
} = useScrollState(scrollViewRef);
useKeyboardHandler(
{
onStart: (e) => {
"worklet";
if (freeze.value) {
return;
}
if (e.height > 0) {
// eslint-disable-next-line react-compiler/react-compiler
targetKeyboardHeight.value = e.height;
}
const effective = getEffectiveHeight(
e.height,
targetKeyboardHeight.value,
offset,
);
const atEnd = isScrollAtEnd(
scroll.value,
layout.value.height,
size.value.height,
inverted,
);
// Scale minimum padding absorption by how much of it is visible.
// Fully visible → full absorption; fully off-screen → no absorption.
const visibleFraction = getVisibleMinimumPaddingFraction(
scroll.value,
layout.value.height,
size.value.height,
blankSpace.value,
inverted,
);
const visiblePadding = visibleFraction * blankSpace.value;
const minimumPaddingAbsorbed = Math.max(
0,
visiblePadding - extraContentPadding.value,
);
const scrollEffective = getScrollEffective(
effective,
minimumPaddingAbsorbed,
);
const actualTotalPadding = Math.max(
blankSpace.value,
effective + extraContentPadding.value,
);
// persistent mode: when keyboard shrinks, clamp to valid range
if (
keyboardLiftBehavior === "persistent" &&
effective < padding.value
) {
padding.value = effective;
prevAbsorption.value = minimumPaddingAbsorbed;
if (inverted) {
const maxScroll = Math.max(
size.value.height - layout.value.height,
0,
);
contentOffsetY.value = Math.max(
-actualTotalPadding,
Math.min(scroll.value, maxScroll),
);
} else {
const maxScroll = Math.max(
size.value.height - layout.value.height + actualTotalPadding,
0,
);
contentOffsetY.value = Math.max(
0,
Math.min(scroll.value, maxScroll),
);
}
return;
}
// never mode: when keyboard shrinks, clamp to valid range
// to avoid ghost padding
if (
keyboardLiftBehavior === "never" &&
effective < padding.value &&
atEnd
) {
padding.value = effective;
prevAbsorption.value = minimumPaddingAbsorbed;
if (inverted) {
const maxScroll = Math.max(
size.value.height - layout.value.height,
0,
);
contentOffsetY.value = Math.max(
-actualTotalPadding,
Math.min(scroll.value, maxScroll),
);
} else {
const maxScroll = Math.max(
size.value.height - layout.value.height + actualTotalPadding,
0,
);
contentOffsetY.value = Math.max(
0,
Math.min(scroll.value, maxScroll),
);
}
return;
}
// Undo only the scroll displacement that was actually applied
// (not the full padding, which includes the absorbed portion).
// Use the stored absorption from the previous event so that
// the unwind matches the shift that was originally applied.
const prevScrollEffective = getScrollEffective(
padding.value,
prevAbsorption.value,
);
const relativeScroll = inverted
? scroll.value + prevScrollEffective
: scroll.value - prevScrollEffective;
padding.value = effective;
prevAbsorption.value = minimumPaddingAbsorbed;
if (!shouldShiftContent(keyboardLiftBehavior, atEnd)) {
// Preserve current scroll position so animated props
// don't re-apply a stale contentOffset when padding changes
contentOffsetY.value = scroll.value;
return;
}
// When blankSpace fully absorbs the keyboard opening, preserve current scroll position
// (only when keyboard is open — effective > 0 — not when closing)
if (
scrollEffective === 0 &&
minimumPaddingAbsorbed > 0 &&
effective > 0
) {
contentOffsetY.value = scroll.value;
return;
}
contentOffsetY.value = computeIOSContentOffset(
relativeScroll,
scrollEffective,
size.value.height,
layout.value.height,
inverted,
actualTotalPadding,
);
},
onMove: () => {
"worklet";
// iOS doesn't need per-frame updates (contentOffset handles it)
},
onEnd: (e) => {
"worklet";
if (freeze.value) {
return;
}
const effective = getEffectiveHeight(
e.height,
targetKeyboardHeight.value,
offset,
);
padding.value = effective;
},
},
[inverted, keyboardLiftBehavior, offset, extraContentPadding],
);
return {
padding,
currentHeight,
contentOffsetY,
scroll,
layout,
size,
onLayout,
onContentSizeChange,
};
}
export { useChatKeyboard };