React Native Popup實現(xiàn)示例
React Native 官方提供了 Modal 組件,但 Modal 是屬于全屏的彈出層,當(dāng) Modal 顯示時,操作區(qū)域只有 Modal 里的元素,而且焦點會被 Modal 劫持。雖然移動端不常見,但有些場景還是希望可以用輕量級一點的 Popup。
在 React Native 里,元素的層級是不可以被穿透的,子元素?zé)o論如何都不能遮擋父元素。所以選擇了在頂層添加 Popup,設(shè)置絕對定位,顯示時根據(jù)指定元素來動態(tài)調(diào)整 Popup 的位置的方案。
具體實現(xiàn)
Popup 會有顯示或隱藏兩種狀態(tài),使用一個 state 來控制。
const Component = () => {
const [visible, setVisible] = useState(false);
return (
<>
{visible && <></>}
</>
);
};Popup 的 屬于視圖類組件,UI 結(jié)構(gòu)包括:
- 一個作為容器的
View,由于 iOS 有劉海,所以在 iOS 上需要使用SafeAreaView來避免被劉海遮擋。同時添加一個點擊事件監(jiān)聽當(dāng)點擊時關(guān)閉Popup。 - 一個指向目標(biāo)對象的三角形。
- 一個包裹內(nèi)容的
View。
由于 Popup 的位置和內(nèi)容是動態(tài)的,所以需要兩個 state 存儲相關(guān)數(shù)據(jù)。
- 一個存儲位置相關(guān)的 CSS。
- 一個存儲動態(tài)內(nèi)容。
const Component = ({ style, ...other }) => {
const [visible, setVisible] = useState(false);
const [popupStyle, setPopupStyle] = useState({});
const [content, setContent] = useState(null);
const onPress = useCallback(() => {
setVisible(false);
}, []);
return (
<>
{visible &&
createElement(
Platform.OS === 'ios' ? SafeAreaView : View,
{
style: {
...styles.popup,
...popupStyle,
},
},
<TouchableOpacity onPress={onPress}>
<View style={styles.triangle} />
<View style={{ ...styles.content, ...style }} {...other}>
{content}
</View>
</TouchableOpacity>,
)}
</>
);
};
const styles = StyleSheet.create({
popup: {
position: 'absolute',
zIndex: 99,
shadowColor: '#333',
shadowOpacity: 0.12,
shadowOffset: { width: 2 },
borderRadius: 4,
},
triangle: {
width: 0,
height: 0,
marginLeft: 12,
borderLeftWidth: 8,
borderLeftColor: 'transparent',
borderRightWidth: 8,
borderRightColor: 'transparent',
borderBottomWidth: 8,
borderBottomColor: 'white',
},
content: {
backgroundColor: 'white',
},
});因為是全局的 Popup,所以選擇了一個全局變量來提供 Popup 相關(guān)的操作方法。
如果全局
Popup不適用,可以改成在需要時插入Popup并使用ref來提供操作方法。
目標(biāo)元素,動態(tài)內(nèi)容和一些相關(guān)的可選配置都是在調(diào)用 show 方法時通過參數(shù)傳入的,
useEffect(() => {
global.$popup = {
show: (triggerRef, render, options = {}) => {
const { x: offsetX = 0, y: offsetY = 0 } = options.offset || {};
triggerRef.current.measure((x, y, width, height, left, top) => {
setPopupStyle({
top: top + height + offsetY,
left: left + offsetX,
});
setContent(render());
setVisible(true);
});
},
hide: () => {
setVisible(false);
},
};
}, []);完整代碼
import React, {
createElement,
forwardRef,
useState,
useEffect,
useCallback,
} from 'react';
import PropTypes from 'prop-types';
import {
View,
SafeAreaView,
Platform,
TouchableOpacity,
StyleSheet,
} from 'react-native';
const Component = ({ style, ...other }, ref) => {
const [visible, setVisible] = useState(false);
const [popupStyle, setPopupStyle] = useState({});
const [content, setContent] = useState(null);
const onPress = useCallback(() => {
setVisible(false);
}, []);
useEffect(() => {
global.$popup = {
show: (triggerRef, render, options = {}) => {
const { x: offsetX = 0, y: offsetY = 0 } = options.offset || {};
triggerRef.current.measure((x, y, width, height, left, top) => {
setPopupStyle({
top: top + height + offsetY,
left: left + offsetX,
});
setContent(render());
setVisible(true);
});
},
hide: () => {
setVisible(false);
},
};
}, []);
return (
<>
{visible &&
createElement(
Platform.OS === 'ios' ? SafeAreaView : View,
{
style: {
...styles.popup,
...popupStyle,
},
},
<TouchableOpacity onPress={onPress}>
<View style={styles.triangle} />
<View style={{ ...styles.content, ...style }} {...other}>
{content}
</View>
</TouchableOpacity>,
)}
</>
);
};
Component.displayName = 'Popup';
Component.prototype = {};
const styles = StyleSheet.create({
popup: {
position: 'absolute',
zIndex: 99,
shadowColor: '#333',
shadowOpacity: 0.12,
shadowOffset: { width: 2 },
borderRadius: 4,
},
triangle: {
width: 0,
height: 0,
marginLeft: 12,
borderLeftWidth: 8,
borderLeftColor: 'transparent',
borderRightWidth: 8,
borderRightColor: 'transparent',
borderBottomWidth: 8,
borderBottomColor: 'white',
},
content: {
backgroundColor: 'white',
},
});
export default forwardRef(Component);使用方法
在入口文件頁面內(nèi)容的末尾插入
Popup元素。// App.jsx import Popup from './Popup'; const App = () => { return ( <> ... <Popup /> </> ); };使用全局變量控制。
// 顯示 $popup.show(); // 隱藏 $popup.hide();
到此這篇關(guān)于React Native Popup實現(xiàn)示例的文章就介紹到這了,更多相關(guān)React Native Popup內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
React Native第三方平臺分享的實例(Android,IOS雙平臺)
本篇文章主要介紹了React Native第三方平臺分享的實例(Android,IOS雙平臺),具有一定的參考價值,有興趣的可以了解一下2017-08-08
React?TypeScript?應(yīng)用中便捷使用Redux?Toolkit方法詳解
這篇文章主要為大家介紹了React?TypeScript?應(yīng)用中便捷使用Redux?Toolkit方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
react-intl實現(xiàn)React國際化多語言的方法
這篇文章主要介紹了react-intl實現(xiàn)React國際化多語言的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
簡易的redux?createStore手寫實現(xiàn)示例
這篇文章主要介紹了簡易的redux?createStore手寫實現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10

