Skip to content

Commit

Permalink
fix: closes #104, where exit completion never fired
Browse files Browse the repository at this point in the history
  • Loading branch information
nandorojo committed Sep 1, 2021
1 parent aa8c953 commit da4eafa
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 4 deletions.
3 changes: 2 additions & 1 deletion examples/with-expo/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
// export { default } from './src/Moti.Loop'
// export { default } from './src/Moti.Skeleton'
// export { default } from './src/Moti.Gallery'
export { default } from './src/Moti.Sequence'
// export { default } from './src/Moti.Sequence'
export { default } from './src/Moti.KeyPresence'
// export { default } from './src/Moti.Presence'
// export { default } from './src/Headless-Exit'
// export { default } from './src/Moti.Progress'
Expand Down
76 changes: 76 additions & 0 deletions examples/with-expo/src/Moti.KeyPresence.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import React, { useReducer } from 'react'
import { StyleSheet, Pressable } from 'react-native'
import { View, AnimatePresence } from 'moti'

function Shape() {
return (
<View
from={{
opacity: 0,
scale: 0.9,
}}
animate={{
opacity: 1,
scale: 1,
}}
exit={{
opacity: 0,
scale: 0.9,
}}
exitTransition={{
type: 'timing',
duration: 2500,
}}
style={styles.shape}
/>
)
}

export default function Presence() {
const [key, increment] = useReducer((state) => state + 1, 0)

return (
<Pressable onPress={increment} style={styles.container}>
<AnimatePresence exitBeforeEnter>
<View
from={{
opacity: 0,
scale: 0.9,
}}
animate={{
opacity: 1,
scale: 1,
}}
exit={{
opacity: 0,
scale: 0.9,
}}
exitTransition={{
type: 'timing',
duration: 100,
}}
key={key}
style={styles.shape}
/>
</AnimatePresence>
</Pressable>
)
}

const styles = StyleSheet.create({
shape: {
justifyContent: 'center',
height: 250,
width: 250,
borderRadius: 25,
marginRight: 10,
backgroundColor: 'white',
},
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
flexDirection: 'row',
backgroundColor: '#9c1aff',
},
})
9 changes: 6 additions & 3 deletions packages/core/src/use-map-animate-to-style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,10 @@ export default function useMapAnimateToStyle<Animate>({

const reanimatedSafeToUnmount = useRef(() => {
safeToUnmount?.()
}).current
})
useEffect(function updateSafeToUnmount() {
reanimatedSafeToUnmount.current = () => safeToUnmount?.()
})

const reanimatedOnDidAnimated = useCallback<NonNullable<typeof onDidAnimate>>(
(...args) => {
Expand Down Expand Up @@ -339,7 +342,7 @@ export default function useMapAnimateToStyle<Animate>({
)
// if this is true, then we've finished our exit animations
if (!areStylesExiting) {
runOnJS(reanimatedSafeToUnmount)()
runOnJS(reanimatedSafeToUnmount.current)()
}
}
}
Expand Down Expand Up @@ -507,7 +510,7 @@ export default function useMapAnimateToStyle<Animate>({
useEffect(
function allowUnMountIfMissingExit() {
if (!isPresent && !hasExitStyle) {
reanimatedSafeToUnmount()
reanimatedSafeToUnmount.current()
}
},
[hasExitStyle, isPresent, reanimatedSafeToUnmount]
Expand Down

0 comments on commit da4eafa

Please sign in to comment.