Skip to content

Commit 4b3d16b

Browse files
Merge pull request #4176 from tloncorp/lb/send-scroll-button
native: avoid showing "scroll to" button on message send
2 parents 3f6d0e3 + 77858d1 commit 4b3d16b

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

packages/ui/src/components/Channel/Scroller.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,6 @@ const Scroller = forwardRef(
141141
[channel]
142142
);
143143

144-
const [isAtBottom, setIsAtBottom] = useState(true);
145-
146144
const [hasPressedGoToBottom, setHasPressedGoToBottom] = useState(false);
147145
const [viewReactionsPost, setViewReactionsPost] = useState<null | db.Post>(
148146
null
@@ -396,7 +394,8 @@ const Scroller = forwardRef(
396394
);
397395
}, [renderEmptyComponentFn]);
398396

399-
const handleScroll = useScrollDirectionTracker(setIsAtBottom);
397+
const [isAtBottom, setIsAtBottom] = useState(true);
398+
const handleScroll = useScrollDirectionTracker({ setIsAtBottom });
400399

401400
const scrollIndicatorInsets = useMemo(() => {
402401
return {

packages/ui/src/contexts/scroll.tsx

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import React, { createContext, useContext } from 'react';
1+
import React, {
2+
createContext,
3+
useContext,
4+
useEffect,
5+
useMemo,
6+
useState,
7+
} from 'react';
8+
import { Dimensions, Platform } from 'react-native';
29
import {
310
Easing,
411
type SharedValue,
@@ -18,12 +25,22 @@ export const ScrollContext = createContext<ScrollContextTuple>(INITIAL_VALUE);
1825

1926
export const useScrollContext = () => useContext(ScrollContext);
2027

21-
export const useScrollDirectionTracker = (
22-
setIsAtBottom: (isAtBottom: boolean) => void
23-
) => {
28+
export const useScrollDirectionTracker = ({
29+
setIsAtBottom,
30+
atBottomThreshold = 1, // multiple of screen/viewport height
31+
}: {
32+
setIsAtBottom: (isAtBottom: boolean) => void;
33+
atBottomThreshold?: number;
34+
}) => {
2435
const [scrollValue] = useScrollContext();
2536
const previousScrollValue = useSharedValue(0);
2637
const isAtBottom = useSharedValue(true);
38+
const viewportHeight = useViewportHeight();
39+
40+
const AT_BOTTOM_THRESHOLD = useMemo(
41+
() => viewportHeight * atBottomThreshold,
42+
[viewportHeight, atBottomThreshold]
43+
);
2744

2845
return useAnimatedScrollHandler((event) => {
2946
const { y } = event.contentOffset;
@@ -40,14 +57,30 @@ export const useScrollDirectionTracker = (
4057

4158
previousScrollValue.value = y;
4259

43-
const atBottom = y <= 0;
60+
const atBottom = y <= AT_BOTTOM_THRESHOLD;
4461
if (isAtBottom.value !== atBottom) {
4562
isAtBottom.value = atBottom;
4663
runOnJS(setIsAtBottom)(atBottom);
4764
}
4865
});
4966
};
5067

68+
function useViewportHeight() {
69+
const [height, setHeight] = useState(
70+
Platform.OS === 'web' ? window.innerHeight : Dimensions.get('window').height
71+
);
72+
73+
useEffect(() => {
74+
if (Platform.OS === 'web') {
75+
const handleResize = () => setHeight(window.innerHeight);
76+
window.addEventListener('resize', handleResize);
77+
return () => window.removeEventListener('resize', handleResize);
78+
}
79+
}, []);
80+
81+
return height;
82+
}
83+
5184
export const ScrollContextProvider: React.FC<React.PropsWithChildren> = ({
5285
children,
5386
}) => {

0 commit comments

Comments
 (0)