|
| 1 | +import { useCallback } from "react"; |
1 | 2 | import { scrollTo, useAnimatedReaction } from "react-native-reanimated"; |
2 | 3 |
|
3 | 4 | import { isScrollAtEnd, shouldShiftContent } from "../useChatKeyboard/helpers"; |
@@ -55,6 +56,24 @@ function useExtraContentPadding(options: UseExtraContentPaddingOptions): void { |
55 | 56 | freeze, |
56 | 57 | } = options; |
57 | 58 |
|
| 59 | + const scrollToTarget = useCallback( |
| 60 | + (target: number) => { |
| 61 | + "worklet"; |
| 62 | + |
| 63 | + if (contentOffsetY) { |
| 64 | + // eslint-disable-next-line react-compiler/react-compiler |
| 65 | + contentOffsetY.value = target; |
| 66 | + } else { |
| 67 | + // Defer scrollTo so the animatedProps inset commit lands first; |
| 68 | + // otherwise the native ScrollView clamps to the old range. |
| 69 | + requestAnimationFrame(() => { |
| 70 | + scrollTo(scrollViewRef, 0, target, false); |
| 71 | + }); |
| 72 | + } |
| 73 | + }, |
| 74 | + [scrollViewRef, contentOffsetY], |
| 75 | + ); |
| 76 | + |
58 | 77 | useAnimatedReaction( |
59 | 78 | () => extraContentPadding.value, |
60 | 79 | (current, previous) => { |
@@ -107,30 +126,15 @@ function useExtraContentPadding(options: UseExtraContentPaddingOptions): void { |
107 | 126 | if (inverted) { |
108 | 127 | const target = Math.max(scroll.value - effectiveDelta, -currentTotal); |
109 | 128 |
|
110 | | - if (contentOffsetY) { |
111 | | - // eslint-disable-next-line react-compiler/react-compiler |
112 | | - contentOffsetY.value = target; |
113 | | - } else { |
114 | | - // Defer scrollTo so the animatedProps inset commit lands first; |
115 | | - // otherwise the native ScrollView clamps to the old range. |
116 | | - requestAnimationFrame(() => { |
117 | | - scrollTo(scrollViewRef, 0, target, false); |
118 | | - }); |
119 | | - } |
| 129 | + scrollToTarget(target); |
120 | 130 | } else { |
121 | 131 | const maxScroll = Math.max( |
122 | 132 | size.value.height - layout.value.height + currentTotal, |
123 | 133 | 0, |
124 | 134 | ); |
125 | 135 | const target = Math.min(scroll.value + effectiveDelta, maxScroll); |
126 | 136 |
|
127 | | - if (contentOffsetY) { |
128 | | - contentOffsetY.value = target; |
129 | | - } else { |
130 | | - requestAnimationFrame(() => { |
131 | | - scrollTo(scrollViewRef, 0, target, false); |
132 | | - }); |
133 | | - } |
| 137 | + scrollToTarget(target); |
134 | 138 | } |
135 | 139 | }, |
136 | 140 | [inverted, keyboardLiftBehavior, freeze], |
|
0 commit comments