網(wǎng)站首頁 編程語言 正文
React Native 官方提供了 Modal
組件,但 Modal
是屬于全屏的彈出層,當(dāng) Modal
顯示時(shí),操作區(qū)域只有 Modal
里的元素,而且焦點(diǎn)會(huì)被 Modal
劫持。雖然移動(dòng)端不常見,但有些場(chǎng)景還是希望可以用輕量級(jí)一點(diǎn)的 Popup
。
在 React Native 里,元素的層級(jí)是不可以被穿透的,子元素?zé)o論如何都不能遮擋父元素。所以選擇了在頂層添加 Popup
,設(shè)置絕對(duì)定位,顯示時(shí)根據(jù)指定元素來動(dòng)態(tài)調(diào)整 Popup
的位置的方案。
具體實(shí)現(xiàn)
Popup
會(huì)有顯示或隱藏兩種狀態(tài),使用一個(gè) state
來控制。
const Component = () => { const [visible, setVisible] = useState(false); return ( <> {visible && <></>} </> ); };
Popup
的 屬于視圖類組件,UI 結(jié)構(gòu)包括:
- 一個(gè)作為容器的
View
,由于 iOS 有劉海,所以在 iOS 上需要使用SafeAreaView
來避免被劉海遮擋。同時(shí)添加一個(gè)點(diǎn)擊事件監(jiān)聽當(dāng)點(diǎn)擊時(shí)關(guān)閉Popup
。 - 一個(gè)指向目標(biāo)對(duì)象的三角形。
- 一個(gè)包裹內(nèi)容的
View
。
由于 Popup
的位置和內(nèi)容是動(dòng)態(tài)的,所以需要兩個(gè) state
存儲(chǔ)相關(guān)數(shù)據(jù)。
- 一個(gè)存儲(chǔ)位置相關(guān)的 CSS。
- 一個(gè)存儲(chǔ)動(dòng)態(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', }, });
因?yàn)槭侨值?Popup
,所以選擇了一個(gè)全局變量來提供 Popup
相關(guān)的操作方法。
如果全局
Popup
不適用,可以改成在需要時(shí)插入Popup
并使用ref
來提供操作方法。
目標(biāo)元素,動(dòng)態(tài)內(nèi)容和一些相關(guān)的可選配置都是在調(diào)用 show
方法時(shí)通過參數(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();
原文鏈接:https://juejin.cn/post/7098603140982767624
相關(guān)推薦
- 2022-03-14 Springboot中遇到的問題——Failed to load ApplicationContex
- 2022-09-15 Go位集合相關(guān)操作bitset庫安裝使用_Golang
- 2022-04-21 Mybatis - tk.mybatis deleteByPrimaryKey無法正確識(shí)別主鍵
- 2022-09-18 Golang實(shí)現(xiàn)文件傳輸功能_Golang
- 2022-09-19 Tomcat配置HTTPS訪問的實(shí)現(xiàn)步驟_Tomcat
- 2022-04-02 C語言實(shí)現(xiàn)二叉樹層次遍歷介紹_C 語言
- 2022-11-06 Android?菜單欄DIY實(shí)現(xiàn)效果詳解_Android
- 2023-05-06 react中定義變量并使用方式_React
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支