-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(foundation): implement gesture to slide type modal
- Loading branch information
Showing
6 changed files
with
161 additions
and
51 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
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
140 changes: 121 additions & 19 deletions
140
packages/uikit-react-native-foundation/src/ui/Modal/index.tsx
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,64 +1,166 @@ | ||
import React, { useEffect, useRef } from 'react'; | ||
import React, { useEffect, useRef, useState } from 'react'; | ||
import { | ||
Animated, | ||
Dimensions, | ||
ModalProps, | ||
PanResponder, | ||
Platform, | ||
Pressable, | ||
Modal as RNModal, | ||
StyleProp, | ||
StyleSheet, | ||
TouchableWithoutFeedback, | ||
View, | ||
ViewStyle, | ||
} from 'react-native'; | ||
|
||
import createStyleSheet from '../../styles/createStyleSheet'; | ||
import useUIKitTheme from '../../theme/useUIKitTheme'; | ||
|
||
type Props = { backgroundStyle?: StyleProp<ViewStyle>; onPressBackground?: () => void } & ModalProps; | ||
type Props = { type?: 'slide' | 'fade'; onClose: () => void; backgroundStyle?: StyleProp<ViewStyle> } & Omit< | ||
ModalProps, | ||
'animationType' | 'onRequestClose' | ||
>; | ||
|
||
/** | ||
* Modal Open: Triggered by Modal.props.visible state changed to true | ||
* - visible true -> modalVisible true -> animation start | ||
* | ||
* Modal Close: Triggered by Modal.props.onClose() call | ||
* - Modal.props.onClose() -> visible false -> animation start -> modalVisible false | ||
* */ | ||
const Modal: React.FC<Props> = ({ | ||
children, | ||
onClose, | ||
backgroundStyle, | ||
onPressBackground, | ||
onDismiss, | ||
type = 'fade', | ||
visible = false, | ||
...props | ||
}) => { | ||
const { palette } = useUIKitTheme(); | ||
const { content, backdrop, showTransition, hideTransition } = useModalAnimation(type); | ||
const panResponder = useModalPanResponder(type, content.translateY, showTransition, onClose); | ||
|
||
const [modalVisible, setModalVisible] = useState(false); | ||
const showAction = () => setModalVisible(true); | ||
const hideAction = () => hideTransition(() => setModalVisible(false)); | ||
|
||
useEffect(() => { | ||
if (visible) showAction(); | ||
else hideAction(); | ||
}, [visible]); | ||
|
||
useOnDismiss(visible, onDismiss); | ||
useOnDismiss(modalVisible, onDismiss); | ||
|
||
return ( | ||
<RNModal | ||
transparent | ||
visible={visible} | ||
hardwareAccelerated | ||
visible={modalVisible} | ||
onRequestClose={onClose} | ||
onShow={() => showTransition()} | ||
onDismiss={onDismiss} | ||
supportedOrientations={['portrait', 'portrait-upside-down', 'landscape', 'landscape-left', 'landscape-right']} | ||
animationType={'fade'} | ||
animationType={'none'} | ||
{...props} | ||
> | ||
<TouchableWithoutFeedback onPress={onPressBackground}> | ||
<View style={[StyleSheet.absoluteFill, { backgroundColor: palette.onBackgroundLight03 }]} /> | ||
<TouchableWithoutFeedback onPress={onClose}> | ||
<Animated.View | ||
style={[StyleSheet.absoluteFill, { opacity: backdrop.opacity, backgroundColor: palette.onBackgroundLight03 }]} | ||
/> | ||
</TouchableWithoutFeedback> | ||
<View pointerEvents={'box-none'} style={[styles.background, backgroundStyle]}> | ||
{children} | ||
</View> | ||
<Animated.View | ||
style={[ | ||
styles.background, | ||
backgroundStyle, | ||
{ opacity: content.opacity, transform: [{ translateY: content.translateY }] }, | ||
]} | ||
pointerEvents={'box-none'} | ||
{...panResponder.panHandlers} | ||
> | ||
{/* NOTE: https://github.com/facebook/react-native/issues/14295 */} | ||
<Pressable>{children}</Pressable> | ||
</Animated.View> | ||
</RNModal> | ||
); | ||
}; | ||
|
||
const useModalPanResponder = ( | ||
type: 'slide' | 'fade', | ||
translateY: Animated.Value, | ||
show: () => void, | ||
hide: () => void, | ||
) => { | ||
if (type === 'fade') return { panHandlers: {} }; | ||
return React.useRef( | ||
PanResponder.create({ | ||
onMoveShouldSetPanResponderCapture: (_, { dy }) => dy > 8, | ||
// @ts-ignore | ||
onPanResponderGrant: () => translateY.setOffset(translateY.__getValue()), | ||
onPanResponderMove: Animated.event([null, { dy: translateY }], { useNativeDriver: false }), | ||
onPanResponderRelease: (_, { dy, vy }) => { | ||
const isHideGesture = dy > 125 || (dy > 0 && vy > 0.1); | ||
if (isHideGesture) hide(); | ||
else show(); | ||
}, | ||
}), | ||
).current; | ||
}; | ||
|
||
const useModalAnimation = (type: 'slide' | 'fade') => { | ||
const initialY = type === 'slide' ? Dimensions.get('window').height : 0; | ||
const baseAnimationVal = useRef(new Animated.Value(0)).current; | ||
const baseTranslateVal = useRef(new Animated.Value(initialY)).current; | ||
|
||
const content = { | ||
opacity: baseAnimationVal.interpolate({ | ||
inputRange: [0, 1], | ||
outputRange: [type === 'slide' ? 1 : 0, 1], | ||
}), | ||
translateY: baseTranslateVal, | ||
}; | ||
const backdrop = { | ||
opacity: baseAnimationVal.interpolate({ | ||
inputRange: [0, 1], | ||
outputRange: [0, 1], | ||
}), | ||
}; | ||
const createTransition = (toValue: 0 | 1) => { | ||
const config = { duration: 250, useNativeDriver: false }; | ||
return Animated.parallel([ | ||
Animated.timing(baseAnimationVal, { toValue, ...config }), | ||
Animated.timing(baseTranslateVal, { toValue: toValue === 0 ? initialY : 0, ...config }), | ||
]).start; | ||
}; | ||
return { | ||
content, | ||
backdrop, | ||
showTransition: createTransition(1), | ||
hideTransition: createTransition(0), | ||
}; | ||
}; | ||
|
||
// NOTE: onDismiss is supports iOS only | ||
const useOnDismiss = (visible: boolean, onDismiss?: () => void) => { | ||
const prevVisibleState = useRef(false); | ||
const prevVisible = usePrevProp(visible); | ||
useEffect(() => { | ||
if (Platform.OS === 'ios') return; | ||
if (prevVisibleState.current && !visible) onDismiss?.(); | ||
prevVisibleState.current = visible; | ||
}, [visible]); | ||
if (prevVisible && !visible) onDismiss?.(); | ||
}, [prevVisible, visible]); | ||
}; | ||
|
||
const usePrevProp = <T,>(prop: T) => { | ||
const prev = useRef(prop); | ||
const curr = useRef(prop); | ||
useEffect(() => { | ||
prev.current = curr.current; | ||
curr.current = prop; | ||
}); | ||
return prev.current; | ||
}; | ||
|
||
const styles = createStyleSheet({ | ||
background: { | ||
flex: 1, | ||
}, | ||
background: { flex: 1 }, | ||
}); | ||
|
||
export default Modal; |
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