forked from xcarpentier/react-native-country-picker-modal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f31c5a9
commit c872c39
Showing
6 changed files
with
192 additions
and
148 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,52 @@ | ||
import React from 'react' | ||
import { Animated, Dimensions } from 'react-native' | ||
import * as React from 'react' | ||
import { Animated, Dimensions, StyleSheet } from 'react-native' | ||
|
||
const { width: WINDOW_WIDTH, height: WINDOW_HEIGHT } = Dimensions.get('screen') | ||
const { height } = Dimensions.get('window') | ||
|
||
const duration = 300 | ||
const useNativeDriver = true | ||
|
||
interface Props { | ||
visible?: boolean | ||
children: React.ReactNode | ||
} | ||
|
||
const styles = { | ||
wrapper: { | ||
position: 'absolute', | ||
width: WINDOW_WIDTH, | ||
height: WINDOW_HEIGHT, | ||
left: 0, | ||
top: 0, | ||
zIndex: 100, | ||
}, | ||
export const AnimatedModal = ({ children, visible }: Props) => { | ||
const translateY = new Animated.Value(height) | ||
|
||
const showModal = Animated.timing(translateY, { | ||
toValue: 0, | ||
duration, | ||
useNativeDriver, | ||
}).start | ||
|
||
const hideModal = Animated.timing(translateY, { | ||
toValue: height, | ||
duration, | ||
useNativeDriver, | ||
}).start | ||
|
||
React.useEffect(() => { | ||
if (visible) { | ||
showModal() | ||
} else { | ||
hideModal() | ||
} | ||
}, [visible]) | ||
|
||
return ( | ||
<Animated.View | ||
style={{ | ||
...StyleSheet.absoluteFillObject, | ||
transform: [{ translateY }], | ||
zIndex: 99, | ||
}} | ||
> | ||
{children} | ||
</Animated.View> | ||
) | ||
} | ||
|
||
export class AnimatedModal extends React.Component<Props> { | ||
aniVal = new Animated.Value(WINDOW_HEIGHT) | ||
|
||
componentDidUpdate(prevProps: Props) { | ||
const { visible } = this.props | ||
if (visible && !prevProps.visible) { | ||
this.showModal() | ||
} | ||
if (!visible && prevProps.visible) { | ||
this.hideModal() | ||
} | ||
} | ||
|
||
hideModal(): void { | ||
Animated.timing(this.aniVal, { | ||
toValue: WINDOW_HEIGHT, | ||
duration: 300, | ||
}).start() | ||
} | ||
|
||
showModal(): void { | ||
Animated.timing(this.aniVal, { | ||
toValue: 0, | ||
duration: 300, | ||
}).start() | ||
} | ||
|
||
render() { | ||
return ( | ||
<Animated.View style={[styles.wrapper, { top: this.aniVal }]}> | ||
{this.props.children} | ||
</Animated.View> | ||
) | ||
} | ||
AnimatedModal.defaultProps = { | ||
visible: false, | ||
} |
Oops, something went wrong.