Skip to content

Commit 512a2b2

Browse files
fix(mobile): use existing reduced-motion hook for scrolling
1 parent b8099f7 commit 512a2b2

1 file changed

Lines changed: 7 additions & 28 deletions

File tree

‎apps/mobile/src/features/threads/ThreadDetailScreen.tsx‎

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ import type { ActiveTurnComposerAction } from "@t3tools/client-runtime/state/com
6060
import * as Haptics from "expo-haptics";
6161
import { useContext, useEffect, useLayoutEffect, useRef, useState } from "react";
6262
import {
63-
AccessibilityInfo,
6463
AppState,
6564
Keyboard,
6665
Platform,
@@ -82,6 +81,7 @@ import Animated, {
8281
useAnimatedReaction,
8382
useAnimatedStyle,
8483
useDerivedValue,
84+
useReducedMotion,
8585
useSharedValue,
8686
withTiming,
8787
} from "react-native-reanimated";
@@ -727,27 +727,7 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
727727
[userInputCoverageApplies],
728728
);
729729
const { freeze, scrollMessageToEnd } = useKeyboardScrollToEnd({ listRef });
730-
// Avoid animating until the system preference is known.
731-
const reduceMotionRef = useRef(true);
732-
useEffect(() => {
733-
// A change event or cleanup makes a still-pending initial query stale.
734-
let superseded = false;
735-
const subscription = AccessibilityInfo.addEventListener("reduceMotionChanged", (enabled) => {
736-
superseded = true;
737-
reduceMotionRef.current = enabled;
738-
});
739-
void AccessibilityInfo.isReduceMotionEnabled()
740-
.then((enabled) => {
741-
if (!superseded) reduceMotionRef.current = enabled;
742-
})
743-
.catch(() => {
744-
// Keep the current preference if the native query fails.
745-
});
746-
return () => {
747-
superseded = true;
748-
subscription.remove();
749-
};
750-
}, []);
730+
const reduceMotion = useReducedMotion();
751731
// Repin timers fire after the render that scheduled them, so they read follow
752732
// state from the ref the writers below keep in step with it.
753733
const endFollowEnabledRef = useRef(true);
@@ -918,7 +898,7 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
918898
return;
919899
}
920900
return scrollMessageToEnd({
921-
animated: !reduceMotionRef.current,
901+
animated: !reduceMotion,
922902
closeKeyboard: false,
923903
});
924904
})
@@ -941,6 +921,7 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
941921
contentPresentationKind,
942922
props.queuedMessages,
943923
selectedThreadFeed,
924+
reduceMotion,
944925
scrollMessageToEnd,
945926
selectedThreadKey,
946927
]);
@@ -1006,11 +987,9 @@ export const ThreadDetailScreen = function ThreadDetailScreen(props: ThreadDetai
1006987

1007988
const handleScrollToEnd = () => {
1008989
void Haptics.selectionAsync();
1009-
void scrollMessageToEnd({ animated: !reduceMotionRef.current, closeKeyboard: false }).catch(
1010-
() => {
1011-
freeze.set(false);
1012-
},
1013-
);
990+
void scrollMessageToEnd({ animated: !reduceMotion, closeKeyboard: false }).catch(() => {
991+
freeze.set(false);
992+
});
1014993
};
1015994

1016995
const showScrollToEndButton = contentPresentationKind === "ready" && !endFollowEnabled;

0 commit comments

Comments
 (0)