欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

React Native Popup實現(xiàn)示例

 更新時間:2022年05月18日 11:19:46   作者:KidLau  
本文主要介紹了React Native Popup實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

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的函數(shù)組件

    一文帶你搞懂React的函數(shù)組件

    React中函數(shù)式組件的基本意義是,組件實際上是一個函數(shù),不是類,下面就來給大家介紹一下關(guān)于React中函數(shù)組件的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-06-06
  • React組件三大屬性之state,props,refs

    React組件三大屬性之state,props,refs

    這篇文章主要介紹了React組件三大屬性之state,props,refs,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-07-07
  • React通過hook實現(xiàn)封裝表格常用功能

    React通過hook實現(xiàn)封裝表格常用功能

    這篇文章主要為大家詳細介紹了React通過hook封裝表格常用功能的使用,文中的示例代碼講解詳細,具有一定的借鑒價值,有需要的小伙伴可以參考下
    2023-12-12
  • React中的JSX??{?}的使用詳解

    React中的JSX??{?}的使用詳解

    這篇文章主要介紹了React中的JSX{?}的使用,React使用JSX來替代常規(guī)的JavaScript,JSX可以理解為的JavaScript語法擴展,它里面的標(biāo)簽申明要符合XML規(guī)范要求,對React?JSX使用感興趣的朋友一起看看吧
    2022-08-08
  • 基于react框架使用的一些細節(jié)要點的思考

    基于react框架使用的一些細節(jié)要點的思考

    下面小編就為大家?guī)硪黄趓eact框架使用的一些細節(jié)要點的思考。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • React-View-UI組件庫封裝Loading加載中源碼

    React-View-UI組件庫封裝Loading加載中源碼

    這篇文章主要介紹了React-View-UI組件庫封裝Loading加載樣式,主要包括組件介紹,組件源碼及組件測試源碼,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • React中使用react-player 播放視頻或直播的方法

    React中使用react-player 播放視頻或直播的方法

    這篇文章主要介紹了React中使用react-player 播放視頻或直播,本文教大家如何使用react框架及創(chuàng)建實例的代碼,本文內(nèi)容簡短給大家介紹的非常詳細,需要的朋友可以參考下
    2022-01-01
  • Input標(biāo)簽自動校驗功能去除實現(xiàn)

    Input標(biāo)簽自動校驗功能去除實現(xiàn)

    這篇文章主要為大家介紹了Input標(biāo)簽的自動拼寫檢查功能去除實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-07-07
  • react 可拖拽進度條的實現(xiàn)

    react 可拖拽進度條的實現(xiàn)

    本文主要介紹了react 可拖拽進度條的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • 聊聊ant?design?charts?獲取后端接口數(shù)據(jù)展示問題

    聊聊ant?design?charts?獲取后端接口數(shù)據(jù)展示問題

    今天在做項目的時候遇到幾個讓我很頭疼的問題,一個是通過后端接口成功訪問并又返回數(shù)據(jù),但拿不到數(shù)據(jù)值。其二是直接修改state中的data,console中數(shù)組發(fā)生變化但任然數(shù)據(jù)未顯示,這篇文章主要介紹了ant?design?charts?獲取后端接口數(shù)據(jù)展示,需要的朋友可以參考下
    2022-05-05

最新評論