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-View-UI組件庫封裝Loading加載中源碼
這篇文章主要介紹了React-View-UI組件庫封裝Loading加載樣式,主要包括組件介紹,組件源碼及組件測試源碼,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06React中使用react-player 播放視頻或直播的方法
這篇文章主要介紹了React中使用react-player 播放視頻或直播,本文教大家如何使用react框架及創(chuàng)建實例的代碼,本文內(nèi)容簡短給大家介紹的非常詳細,需要的朋友可以參考下2022-01-01聊聊ant?design?charts?獲取后端接口數(shù)據(jù)展示問題
今天在做項目的時候遇到幾個讓我很頭疼的問題,一個是通過后端接口成功訪問并又返回數(shù)據(jù),但拿不到數(shù)據(jù)值。其二是直接修改state中的data,console中數(shù)組發(fā)生變化但任然數(shù)據(jù)未顯示,這篇文章主要介紹了ant?design?charts?獲取后端接口數(shù)據(jù)展示,需要的朋友可以參考下2022-05-05