React Native ๊ธฐ๋ณธ Modal๋ง์ผ๋ก๋ ๊ตฌํ์ ๊ฐ๋ฅํ์ง๋งโฆ
๊ฐ์ ์์ฃผ ์ฐ๋ UX๋ฅผ ๋ง๋ค๋ ค๋ฉด ์ฝ๋๊ฐ ๊ธธ์ด์ง๊ธฐ ์ฌ์์.
๊ทธ๋์ ์ค๋์ react-native-modal์ด ์ ๊ณตํ๋ ๊ธฐ๋ฅ์ ๋ํด์ ์ ๋ฆฌํด๋ณด๋ คํฉ๋๋ค!
React Native ๊ณต์ ๋ฌธ์์ ์๋ ์์ ์ฝ๋๋ฅผ ๋ณด๋ฉด ์ด๋ฐ ๋๋์ด์ฃ .
animationType="slide" ๋ก ๋ฑ์ฅ ๋ฐฉ์ ์ ์ด transparent ๋ก ๋ฐฐ๊ฒฝ ํฌ๋ช
์ฒ๋ฆฌ visible ์ํ๋ก ์ด๊ณ ๋ซ๊ธฐ (์ด ๋ฐฉ์๋ ์ถฉ๋ถํ ์ข์ง๋ง, ๋ํ ์ผํ UX๋ฅผ ์ถ๊ฐํ ๋ ์ฝ๋๊ฐ ์ ์ ๊ธธ์ด์ง๋๋ค.)
react-native-modal์ RN ๊ธฐ๋ณธ Modal์ ๊ฐ์ธ์ ๋ชจ๋ฌ UX๋ฅผ ์ฝ๊ฒ ํ์ฅํด์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์์.
ํนํ ์ด๋ฐ ๊ธฐ๋ฅ์ด ํธํฉ๋๋ค.
onBackdropPress)swipeDirection, onSwipeComplete)animationIn, animationOut)backdropOpacity)avoidKeyboard)onModalHide)npm i react-native-modal
# ๋๋
yarn add react-native-modal
์ค๋ ๋ง๋ค ๋ชจ๋ฌ ์คํ์ ๋ฑ ์ด ์ ๋๋ง!
๐ ์ฝ๋ ๋ณด๊ธฐ์๋ ์ฝ๋๋ โํ์ํ ๊ธฐ๋ฅ๋งโ ๊ณจ๋ผ์ ๋ฃ์, ๊ฐ์ฅ ์ค์ ํ ๊ธฐ๋ณธ ํ ํ๋ฆฟ์ด์์.
import React, {useState} from 'react';
import {Alert, Modal, StyleSheet, Text, Pressable, View} from 'react-native';
import {SafeAreaView, SafeAreaProvider} from 'react-native-safe-area-context';
const App = () => {
const [modalVisible, setModalVisible] = useState(false);
return (
<SafeAreaProvider>
<SafeAreaView style={styles.centeredView}>
<Modal
animationType="slide"
transparent={true}
visible={modalVisible}
onRequestClose={() => {
Alert.alert('Modal has been closed.');
setModalVisible(!modalVisible);
}}>
<View style={styles.centeredView}>
<View style={styles.modalView}>
<Text style={styles.modalText}>Hello World!</Text>
<Pressable
style={[styles.button, styles.buttonClose]}
onPress={() => setModalVisible(!modalVisible)}>
<Text style={styles.textStyle}>Hide Modal</Text>
</Pressable>
</View>
</View>
</Modal>
<Pressable
style={[styles.button, styles.buttonOpen]}
onPress={() => setModalVisible(true)}>
<Text style={styles.textStyle}>Show Modal</Text>
</Pressable>
</SafeAreaView>
</SafeAreaProvider>
);
};
const styles = StyleSheet.create({
centeredView: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
modalView: {
margin: 20,
backgroundColor: 'white',
borderRadius: 20,
padding: 35,
alignItems: 'center',
shadowColor: '#000',
shadowOffset: { width: 0, height: 2 },
shadowOpacity: 0.25,
shadowRadius: 4,
elevation: 5,
},
button: {
borderRadius: 20,
padding: 10,
elevation: 2,
},
buttonOpen: { backgroundColor: '#F194FF' },
buttonClose: { backgroundColor: '#2196F3' },
textStyle: {
color: 'white',
fontWeight: 'bold',
textAlign: 'center',
},
modalText: {
marginBottom: 15,
textAlign: 'center',
},
});
export default App;
์๋๋ โ์ด ๊ธ์ ๋ชฉ์ (๊ฐ๋ณ๊ฒ ํ์ํ ๊ธฐ๋ฅ๋ง)โ์ ๋ง์ถฐ, ์์ฃผ ์ฐ๋ ์ต์ ๋ง ํ๋ซํผ๋ณ๋ก ์ ๋ฆฌํ ๋ด์ฉ์ ๋๋ค.
animationType๋ชจ๋ฌ์ด ์ด๋ค ์ ๋๋ฉ์ด์ ์ผ๋ก ๋ฑ์ฅํ ์ง ์ ์ด
<Modal animationType="slide" />
slide โ ์๋์์ ์๋ก ๋ฑ์ฅfade โ ์์ํ ๋ํ๋จnone โ ์ ๋๋ฉ์ด์
์์transparent๋ชจ๋ฌ ๋ฐฐ๊ฒฝ ํฌ๋ช ์ฌ๋ถ
<Modal transparent />
true โ ๋ฐฐ๊ฒฝ ํฌ๋ช
(์ปค์คํ
๋ฐฑ๋๋กญ ํ์)false โ ๊ธฐ๋ณธ ํฐ์ ๋ฐฐ๊ฒฝonRequestClose๋ชจ๋ฌ์ด ๋ซํ๋ ค๊ณ ํ ๋ ํธ์ถ๋๋ ์ฝ๋ฐฑ
<Modal onRequestClose={() => setVisible(false)} />
onShow๋ชจ๋ฌ์ด ํ๋ฉด์ ํ์๋ ์งํ ํธ์ถ
<Modal onShow={() => console.log('opened')} />
isVisible๋ชจ๋ฌ ์ด๋ฆผ / ๋ซํ ์ํ
<Modal isVisible={visible} />
onBackdropPress๋ชจ๋ฌ ๋ฐ๊นฅ ์์ญ(๋ฐฑ๋๋กญ)์ ๋๋ฌ ๋ซ๊ธฐ
<Modal onBackdropPress={() => setVisible(false)} />
backdropOpacity๋ฐฑ๋๋กญ ํฌ๋ช ๋(์ด๋์ด ์ ๋)
<Modal backdropOpacity={0.4} />
animationIn / animationOut๋ฑ์ฅ / ํด์ฅ ์ ๋๋ฉ์ด์ ์ค์
<Modal animationIn="slideInUp" animationOut="slideOutDown" />
useNativeDriver์ ๋๋ฉ์ด์ ์ฑ๋ฅ ์ต์ ํ ์ต์
<Modal useNativeDriver />
onDismiss๋ชจ๋ฌ์ด ์์ ํ ๋ซํ ํ ํธ์ถ
<Modal onDismiss={() => console.log('closed')} />
onOrientationChange๋ชจ๋ฌ์ด ๋ ์๋ ๋์ ํ๋ฉด ๋ฐฉํฅ ๋ณ๊ฒฝ ๊ฐ์ง
<Modal
onOrientationChange={(event) => {
console.log(event.orientation);
}}
/>
"portrait" โ ์ธ๋ก"landscape" โ ๊ฐ๋กallowSwipeDismissal์๋๋ก ์ค์์ดํํด์ ๋ชจ๋ฌ ๋ซ๊ธฐ ํ์ฉ ์ฌ๋ถ
<Modal allowSwipeDismissal />
swipeDirection์ค์์ดํ๋ก ๋ชจ๋ฌ ๋ซ๊ธฐ (์ ์ค์ฒ)
<Modal swipeDirection="down" />
"down" | "up" | "left" | "right"["down", "left"]onSwipeComplete์ค์์ดํ๊ฐ ์๋ฃ๋์ ๋ ํธ์ถ๋๋ ์ฝ๋ฐฑ
<Modal onSwipeComplete={() => setVisible(false)} />
hardwareAccelerated๋ชจ๋ฌ์ GPU ๋ ๋๋ง์ผ๋ก ๊ฐ์
<Modal hardwareAccelerated />
ํฌ๋ช View + ์ ๋๋ฉ์ด์ ์ ๋ฐ์ํ๋
Android ์ ์ฉ (iOS ๋ฌด์)
statusBarTranslucent๋ชจ๋ฌ์ด ์ํ๋ฐ ์์ญ ์๋๊น์ง ํ์ฅ๋ ์ง ์ฌ๋ถ
<Modal statusBarTranslucent />
true โ ์ํ๋ฐ๊น์ง ๋ฎ์false โ ์ํ๋ฐ ์๋๋ถํฐ ํ์React Native ๊ธฐ๋ณธ Modal๋ง์ผ๋ก๋ ๊ตฌํ์ ๊ฐ๋ฅํ์ง๋ง,
๋ฐฑ๋๋กญ ํญ / ์ค์์ดํ ๋ซ๊ธฐ / ์์ฐ์ค๋ฌ์ด ์ ๋๋ฉ์ด์
๊ฐ์ UX๋ฅผ ๊ตฌํํ๋ ค๋ฉด
์ถ๊ฐ ๊ตฌํ์ด ๋ง์ด ํ์ํด์.
react-native-modal์ ์ฌ์ฉํ๋ฉด ํ์ํ ๊ธฐ๋ฅ๋ง ๊ณจ๋ผ
๋ ๊ฐ๊ฒฐํ๊ฒ ์ค์ ๋ชจ๋ฌ์ ๋ง๋ค ์ ์์ต๋๋ค.
+๋ค ํฌ์คํธ์์ ๋์ฑ ๋ค์ฑ๋ก์ด ๊ธฐ๋ฅ์ด ์ถ๊ฐํ์ฌ ๊ฐ๋ณ๊ฒ modal์ ์ฌ์ฉํ๋ ๋ฒ๋ ์ฝ์ด๋ณด์ธ์ ! ๐
---