Last active
January 5, 2023 06:24
-
-
Save patissier-boulanger/301ed3c68e6c571b612fa0e37ebe3fb4 to your computer and use it in GitHub Desktop.
React-Native-modal-dimmed-Area
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, {useEffect} from 'react'; | |
import Animated, { | |
Easing, | |
useAnimatedStyle, | |
useSharedValue, | |
withTiming, | |
} from 'react-native-reanimated'; | |
type ModalPropType = { | |
showModal: boolean; | |
}; | |
export function Modal({showModal}: ModalPropType) { | |
const backgroundColor = useSharedValue(0); | |
const backdropAnimatedStyle = useAnimatedStyle(() => { | |
return { | |
position: 'absolute', | |
top: 0, | |
right: 0, | |
bottom: 0, | |
left: 0, | |
justifyContent: 'center', | |
alignItems: 'center', | |
backgroundColor: '#00000099', | |
opacity: backgroundColor.value, | |
zIndex: 1, | |
}; | |
}); | |
useEffect(() => { | |
if (showModal) { | |
backgroundColor.value = withTiming(1, { | |
duration: 250, | |
easing: Easing.linear, | |
}); | |
return; | |
} | |
}, [showModal, backgroundColor]); | |
return ( | |
<> | |
{showModal ? ( | |
<> | |
<Animated.View style={backdropAnimatedStyle} /> | |
</> | |
) : ( | |
<></> | |
)} | |
</> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment