Skip to content

Commit d09907e

Browse files
committed
fix: fix showing animation when "onLayout" event is delayed
1 parent 07d9f22 commit d09907e

2 files changed

Lines changed: 20 additions & 5 deletions

File tree

src/components/NotifierRenderer.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,13 @@ const NotifierRendererComponent = forwardRef<
4545
NotifierRendererMethods,
4646
NotifierRendererProps
4747
>(({ notification, onHiddenCallback }, notificationRef) => {
48-
const { layoutAnimationValues, onLayout, updateHiddenValueByDirection, ref } =
49-
useLayout(notification);
48+
const {
49+
layoutAnimationValues,
50+
onLayout,
51+
updateHiddenValueByDirection,
52+
ref,
53+
isLayoutReady,
54+
} = useLayout(notification);
5055
const { shake, shakingAnimationValues } = useShaking(notification);
5156

5257
const { onGestureEvent, resetSwipeAnimation, swipeAnimationValues } =
@@ -115,8 +120,9 @@ const NotifierRendererComponent = forwardRef<
115120
}));
116121

117122
useEffect(() => {
118-
// if "hideNotification" method was called before show notification animation started - ignore it.
119-
if (isHidingRef.current) return;
123+
// Don't start the animation if layout still not ready (width / height of the component)
124+
// or if "hideNotification" method was called before show notification animation started.
125+
if (isHidingRef.current || !isLayoutReady) return;
120126

121127
Animated[notification.showAnimationConfig.method](animationState, {
122128
useNativeDriver: true,
@@ -131,6 +137,12 @@ const NotifierRendererComponent = forwardRef<
131137
clearTimeout(hideTimerRef.current);
132138
};
133139
// eslint-disable-next-line react-hooks/exhaustive-deps
140+
}, [isLayoutReady]);
141+
142+
useEffect(() => {
143+
return () => {
144+
clearTimeout(hideTimerRef.current);
145+
};
134146
}, []);
135147

136148
const onHandlerStateChange = ({

src/hooks/useLayout.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useCallback, useLayoutEffect, useRef } from 'react';
1+
import { useCallback, useLayoutEffect, useRef, useState } from 'react';
22
import { Animated, View } from 'react-native';
33
import { getHiddenTranslateValues } from '../utils/animationDirection';
44
import type { Direction, Notification } from '../types';
@@ -9,6 +9,7 @@ import { FABRIC_ENABLED, MAX_VALUE } from '../constants';
99
* Also, can update hiddenTranslate* values by using updateHiddenValueByDirection function depending on the direction
1010
*/
1111
export const useLayout = ({ enterFrom }: Notification) => {
12+
const [isLayoutReady, setLayoutReady] = useState(false);
1213
const ref = useRef<View>(null);
1314
// store current direction to handle the case when component dimensions changes (trigger onLayout)
1415
// while active direction != enterFrom (e.g. when exitTo is different than enterFrom, and notification started hiding animation).
@@ -65,6 +66,7 @@ export const useLayout = ({ enterFrom }: Notification) => {
6566
componentWidth.setValue(width);
6667

6768
updateHiddenValueByDirection(currentDirection.current);
69+
setLayoutReady(true);
6870
},
6971
[componentHeight, componentWidth, updateHiddenValueByDirection]
7072
);
@@ -112,5 +114,6 @@ export const useLayout = ({ enterFrom }: Notification) => {
112114
ref,
113115
onLayout,
114116
updateHiddenValueByDirection,
117+
isLayoutReady,
115118
};
116119
};

0 commit comments

Comments
 (0)