Skip to content

Commit b696c63

Browse files
committed
DRY out the scroll to target code.
1 parent 8545cf7 commit b696c63

1 file changed

Lines changed: 21 additions & 17 deletions

File tree

  • src/components/KeyboardChatScrollView/useExtraContentPadding

src/components/KeyboardChatScrollView/useExtraContentPadding/index.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useCallback } from "react";
12
import { scrollTo, useAnimatedReaction } from "react-native-reanimated";
23

34
import { isScrollAtEnd, shouldShiftContent } from "../useChatKeyboard/helpers";
@@ -55,6 +56,24 @@ function useExtraContentPadding(options: UseExtraContentPaddingOptions): void {
5556
freeze,
5657
} = options;
5758

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+
5877
useAnimatedReaction(
5978
() => extraContentPadding.value,
6079
(current, previous) => {
@@ -107,30 +126,15 @@ function useExtraContentPadding(options: UseExtraContentPaddingOptions): void {
107126
if (inverted) {
108127
const target = Math.max(scroll.value - effectiveDelta, -currentTotal);
109128

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);
120130
} else {
121131
const maxScroll = Math.max(
122132
size.value.height - layout.value.height + currentTotal,
123133
0,
124134
);
125135
const target = Math.min(scroll.value + effectiveDelta, maxScroll);
126136

127-
if (contentOffsetY) {
128-
contentOffsetY.value = target;
129-
} else {
130-
requestAnimationFrame(() => {
131-
scrollTo(scrollViewRef, 0, target, false);
132-
});
133-
}
137+
scrollToTarget(target);
134138
}
135139
},
136140
[inverted, keyboardLiftBehavior, freeze],

0 commit comments

Comments
 (0)