Skip to content

Commit

Permalink
fix: prevent animations callback from being run only episodically
Browse files Browse the repository at this point in the history
This issue was caused by a misuse of the `finished` value provided to `Animated.some_method().start()`'s callback: it simply indicates if an animation was run without interruption. It's it not a value that indicates if the animation is done being run and a callback can now be called.
As explain in the docs, simply providing a callback is actually enough, there was no need to check for `finished` truthiness; and in our case, that was actually causing bugs.

See: https://reactnative.dev/docs/animated#working-with-animations.

Fixes #120
  • Loading branch information
CharlesMangwa committed Aug 4, 2023
1 parent b57aad8 commit d5febf2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 24 deletions.
7 changes: 5 additions & 2 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import DemoModal from './components/DemoModal'
import IntroModal from './components/IntroModal'
import IntroButton from './components/IntroButton'

interface Modal<N, C extends 'lightsalmon' | 'deepskyblue' | 'deeppink'> {
interface Modal<N, C> {
origin: 'Hooks' | 'Class' | 'Plain JS'
color: C
color: C | 'darkgreen'
// 👆 Comment this one and uncomment that one 👇 to remove all the TypeScript errors
// color: 'lightsalmon' | 'deepskyblue' | 'deeppink' | 'darkgreen'
// Note: the TS errors were left voluntarily to showcase the type inference & autocomplete possibilities
name: N
}

Expand Down
10 changes: 4 additions & 6 deletions src/lib/ModalStack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,10 @@ const ModalStack = <P extends ModalfyParams>(props: Props<P>) => {
easing: Easing.inOut(Easing.ease),
duration: backdropAnimationDuration,
useNativeDriver: true,
}).start(({ finished }) => {
if (finished) {
setStackStatus('hidden')
setBackdropClosedItems([])
translateY.setValue(sh(100))
}
}).start(() => {
setStackStatus('hidden')
setBackdropClosedItems([])
translateY.setValue(sh(100))
})
}
}, [backdropAnimationDuration, opacity, translateY, stackStatus])
Expand Down
16 changes: 6 additions & 10 deletions src/lib/StackItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,9 @@ const StackItem = <P extends ModalfyParams>({
toValue,
useNativeDriver: true,
...(closeModalCallback ? animateOutConfig : animateInConfig),
}).start(({ finished }) => {
if (finished) {
closeModalCallback?.(stackItem)
modalStackItemCallback?.()
}
}).start(() => {
closeModalCallback?.(stackItem)
modalStackItemCallback?.()
})
}
},
Expand Down Expand Up @@ -203,11 +201,9 @@ const StackItem = <P extends ModalfyParams>({
toValue,
useNativeDriver: true,
...animateOutConfig,
}).start(({ finished }) => {
if (finished) {
onCloseListener.current({ type: 'closeModal', origin: 'fling' })
closeModal(stackItem)
}
}).start(() => {
onCloseListener.current({ type: 'closeModal', origin: 'fling' })
closeModal(stackItem)
})
}
},
Expand Down
8 changes: 2 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,7 @@ export interface ModalOptions {
* easing: Easing.inOut(Easing.exp),
* useNativeDriver: true,
* }),
* ]).start(({ finished }) => {
* if (finished) callback?.()
* })
* ]).start() => callback?.())
* }
* @see [API reference](https://colorfy-software.gitbook.io/react-native-modalfy/api/types/modaloptions#animationin).
*/
Expand Down Expand Up @@ -314,9 +312,7 @@ export interface ModalOptions {
* easing: Easing.inOut(Easing.exp),
* useNativeDriver: true,
* }),
* ]).start(({ finished }) => {
* if (finished) callback()
* })
* ]).start(() => callback())
* }
* @see [API reference](https://colorfy-software.gitbook.io/react-native-modalfy/api/types/modaloptions#animationout).
*/
Expand Down

0 comments on commit d5febf2

Please sign in to comment.