最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

React Native 官方提供了 Modal 組件,但 Modal 是屬于全屏的彈出層,當(dāng) Modal 顯示時(shí),操作區(qū)域只有 Modal 里的元素,而且焦點(diǎn)會(huì)被 Modal 劫持。雖然移動(dòng)端不常見(jiàn),但有些場(chǎng)景還是希望可以用輕量級(jí)一點(diǎn)的 Popup。

在 React Native 里,元素的層級(jí)是不可以被穿透的,子元素?zé)o論如何都不能遮擋父元素。所以選擇了在頂層添加 Popup,設(shè)置絕對(duì)定位,顯示時(shí)根據(jù)指定元素來(lái)動(dòng)態(tài)調(diào)整 Popup 的位置的方案。

具體實(shí)現(xiàn)

Popup 會(huì)有顯示或隱藏兩種狀態(tài),使用一個(gè) state 來(lái)控制。

const Component = () => {
  const [visible, setVisible] = useState(false);
  
  return (
    <>
      {visible && <></>}
    </>
  );
};

Popup 的 屬于視圖類組件,UI 結(jié)構(gòu)包括:

  • 一個(gè)作為容器的 View,由于 iOS 有劉海,所以在 iOS 上需要使用 SafeAreaView 來(lái)避免被劉海遮擋。同時(shí)添加一個(gè)點(diǎn)擊事件監(jiān)聽(tīng)當(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è)全局變量來(lái)提供 Popup 相關(guān)的操作方法。

如果全局 Popup 不適用,可以改成在需要時(shí)插入 Popup 并使用 ref 來(lái)提供操作方法。

目標(biāo)元素,動(dòng)態(tài)內(nèi)容和一些相關(guān)的可選配置都是在調(diào)用 show 方法時(shí)通過(guò)參數(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);

使用方法

  • 在入口文件頁(yè)面內(nèi)容的末尾插入 Popup 元素。

    // App.jsx
    import Popup from './Popup';
    
    const App = () => {
      return (
        <>
          ...
          <Popup />
        </>
      );
    };
  • 使用全局變量控制。

    // 顯示
    $popup.show();
    // 隱藏
    $popup.hide();

到此這篇關(guān)于React Native Popup實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)React Native Popup內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • ReactQuery?渲染優(yōu)化示例詳解

    ReactQuery?渲染優(yōu)化示例詳解

    這篇文章主要為大家介紹了ReactQuery?渲染優(yōu)化示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • 詳解react應(yīng)用中的DOM DIFF算法

    詳解react應(yīng)用中的DOM DIFF算法

    這篇文章主要介紹了react應(yīng)用中的DOM DIFF算法,幫助大家更好的理解和學(xué)習(xí)使用react,感興趣的朋友可以了解下
    2021-04-04
  • React Native第三方平臺(tái)分享的實(shí)例(Android,IOS雙平臺(tái))

    React Native第三方平臺(tái)分享的實(shí)例(Android,IOS雙平臺(tái))

    本篇文章主要介紹了React Native第三方平臺(tái)分享的實(shí)例(Android,IOS雙平臺(tái)),具有一定的參考價(jià)值,有興趣的可以了解一下
    2017-08-08
  • React?TypeScript?應(yīng)用中便捷使用Redux?Toolkit方法詳解

    React?TypeScript?應(yīng)用中便捷使用Redux?Toolkit方法詳解

    這篇文章主要為大家介紹了React?TypeScript?應(yīng)用中便捷使用Redux?Toolkit方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • Electron+React進(jìn)行通信的方法

    Electron+React進(jìn)行通信的方法

    electron其實(shí)是一個(gè)桌面應(yīng)用程序,不是一個(gè)標(biāo)準(zhǔn)的前端web程序,所有沒(méi)有什么請(qǐng)求的發(fā)生,控制臺(tái)network看不到請(qǐng)求,而是只能通過(guò)console.log去打印查看,而且通信協(xié)議使用的不是http而是gRPC協(xié)議,這篇文章主要介紹了Electron+React如何進(jìn)行通信,需要的朋友可以參考下
    2022-06-06
  • 詳解使用React制作一個(gè)模態(tài)框

    詳解使用React制作一個(gè)模態(tài)框

    這篇文章主要介紹了詳解使用React制作一個(gè)模態(tài)框,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-03-03
  • 模塊化react-router配置方法詳解

    模塊化react-router配置方法詳解

    這篇文章主要介紹了模塊化react-router配置方法詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-06-06
  • react項(xiàng)目中如何引入國(guó)際化

    react項(xiàng)目中如何引入國(guó)際化

    在React項(xiàng)目中引入國(guó)際化可以使用第三方庫(kù)來(lái)實(shí)現(xiàn),本文主要介紹了react項(xiàng)目中如何引入國(guó)際化,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • react-intl實(shí)現(xiàn)React國(guó)際化多語(yǔ)言的方法

    react-intl實(shí)現(xiàn)React國(guó)際化多語(yǔ)言的方法

    這篇文章主要介紹了react-intl實(shí)現(xiàn)React國(guó)際化多語(yǔ)言的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • 簡(jiǎn)易的redux?createStore手寫(xiě)實(shí)現(xiàn)示例

    簡(jiǎn)易的redux?createStore手寫(xiě)實(shí)現(xiàn)示例

    這篇文章主要介紹了簡(jiǎn)易的redux?createStore手寫(xiě)實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10

最新評(píng)論

若羌县| 江北区| 敦煌市| 宣化县| 祁阳县| 道真| 荆门市| 湾仔区| 宜章县| 广南县| 乐安县| 烟台市| 丹寨县| 甘肃省| 辽宁省| 唐河县| 绥滨县| 平山县| 贵定县| 邹城市| 景德镇市| 慈利县| 大渡口区| 三原县| 赤水市| 古丈县| 依安县| 年辖:市辖区| 福安市| 车致| 中江县| 长子县| 荔浦县| 锦州市| 都江堰市| 镇平县| 汉阴县| 启东市| 荥阳市| 绿春县| 读书|