Skip to content

Commit d3f20ae

Browse files
committed
fix(flick): fix flick on first focus
1 parent dc49865 commit d3f20ae

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/Provider.tsx

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import Animated, {
1313
useAnimatedScrollHandler,
1414
useAnimatedStyle,
1515
useSharedValue,
16+
withDelay,
1617
withTiming,
1718
type AnimatedRef,
1819
type AnimatedScrollViewProps,
@@ -99,7 +100,7 @@ const SmartScrollProvider = ({ children }: { children: React.ReactNode }) => {
99100
const scrollRef = useAnimatedRef<Animated.ScrollView>();
100101
const scrollY = useSharedValue(0);
101102
const wrapperRef = React.useRef<View>(null);
102-
const [isReady, setIsReady] = useState(true);
103+
const [isReady, setIsReady] = useState(false);
103104
const [elements, setElements] = useState<Elements>({});
104105
const [wrapperOffset, setWrapperOffset] = useState(0);
105106
const [inputs, setInputs] = useState<InputType>({});
@@ -115,7 +116,9 @@ const SmartScrollProvider = ({ children }: { children: React.ReactNode }) => {
115116
// we have a flick on first focus so we make the scrollview wait a bit before animate
116117
useLayoutEffect(() => {
117118
if (currentFocus && !isReady) {
118-
setTimeout(() => setIsReady(true), isAndroid ? 250 : 100);
119+
requestAnimationFrame(() => {
120+
setIsReady(true);
121+
});
119122
}
120123
}, [currentFocus]);
121124

@@ -276,8 +279,22 @@ export function useFormSmartScroll({
276279
);
277280

278281
const translateStyle = useAnimatedStyle(() => {
282+
if (!isReady && currentFocus) {
283+
// On first focus, delay the animation with native driver
284+
return {
285+
transform: [
286+
{
287+
translateY: withDelay(
288+
Platform.OS === 'android' ? 150 : 16,
289+
withTiming(translateY.value)
290+
),
291+
},
292+
],
293+
};
294+
}
295+
279296
return {
280-
transform: [{ translateY: isReady ? translateY.value : 0 }],
297+
transform: [{ translateY: translateY.value }],
281298
};
282299
}, [currentFocus]);
283300

0 commit comments

Comments
 (0)