全屏组件封装(react18+antd)

基于react+ts封装的公用全屏组件

1、封装组件

在components下面构建FullScreenButton文件:

FullScreenButton/index.tsx

import React, { useState, useCallback, useEffect } from "react";
import { FullscreenOutlined, FullscreenExitOutlined } from "@ant-design/icons";
import { Tooltip } from "antd";
const FullScreenButton = ({ targetRef }) => {
  const [isFullScreen, setIsFullScreen] = useState(false);

  const toggleFullScreen = useCallback(() => {
    const parentElement = targetRef.current?.parentElement;

    if (!parentElement) return;

    if (isFullScreen) {
      exitFullScreen();
    } else {
      enterFullScreen(parentElement);
    }

    setIsFullScreen(!isFullScreen);
  }, [isFullScreen, targetRef]);

  const enterFullScreen = (element) => {
    if (element.requestFullscreen) {
      element.requestFullscreen();
    } else if (element.mozRequestFullScreen) {
      element.mozRequestFullScreen();
    } else if (element.webkitRequestFullscreen) {
      element.webkitRequestFullscreen();
    } else if (element.msRequestFullscreen) {
      element.msRequestFullscreen();
    }
  };

  const exitFullScreen = () => {
    if (document.exitFullscreen) {
      document.exitFullscreen();
    } else if (document.mozCancelFullScreen) {
      document.mozCancelFullScreen();
    } else if (document.webkitExitFullscreen) {
      document.webkitExitFullscreen();
    } else if (document.msExitFullscreen) {
      document.msExitFullscreen();
    }
  };

  useEffect(() => {
    const handleFullScreenChange = () => {
      const parentElement = targetRef.current?.parentElement;
      setIsFullScreen(document.fullscreenElement === parentElement);
    };

    document.addEventListener("fullscreenchange", handleFullScreenChange);
    document.addEventListener("webkitfullscreenchange", handleFullScreenChange);
    document.addEventListener("mozfullscreenchange", handleFullScreenChange);
    document.addEventListener("MSFullscreenChange", handleFullScreenChange);

    return () => {
      document.removeEventListener("fullscreenchange", handleFullScreenChange);
      document.removeEventListener(
        "webkitfullscreenchange",
        handleFullScreenChange,
      );
      document.removeEventListener(
        "mozfullscreenchange",
        handleFullScreenChange,
      );
      document.removeEventListener(
        "MSFullscreenChange",
        handleFullScreenChange,
      );
    };
  }, [targetRef]);

  return (
    <span
      style={{ cursor: "pointer", fontSize: "16px" }}
      onClick={toggleFullScreen}
    >
      <Tooltip placement="bottom" title={isFullScreen ? "退出全屏" : "全屏"}>
        {isFullScreen ? <FullscreenExitOutlined /> : <FullscreenOutlined />}
      </Tooltip>
    </span>
  );
};

export default FullScreenButton;

2、使用组件

在指定的组件中使用

import "./index.less";
import React, { useRef } from "react";
import FullScreenButton from "@/components/FullScreenButton";
const settingPage: React.FC = () => {
  const targenRef = useRef(null);
  return (
    <div ref={targenRef}>
      <div className="settings">
        <div className="bread">面包屑</div>
        <div className="settings-right">
          <FullScreenButton targetRef={targenRef} />
          {/* <span>导航+设置</span> */}
        </div>
      </div>
    </div>
  );
};

export default settingPage;

3、监听操作(根据需要设定)

在父组件中监听是否放大(因为涉及到高度问题,最好是监听下根据是否放大来修改)

import React, { useRef, useState, useEffect } from "react";
import { Layout } from "antd";
import { Outlet } from "react-router-dom";
import "./index.less";
import SettingPage from "@/components/setting";
import isFullScreen from "@/utils/isFullScreen";
const { Content } = Layout;
const MainPage: React.FC = () => {
  const [isFullScreenState, setIsFullScreenState] = useState(isFullScreen());
  useEffect(() => {
    const handleFullScreenChange = () => {
      setIsFullScreenState(isFullScreen());
    };

    document.addEventListener("fullscreenchange", handleFullScreenChange);
    document.addEventListener("webkitfullscreenchange", handleFullScreenChange);
    document.addEventListener("mozfullscreenchange", handleFullScreenChange);
    document.addEventListener("MSFullscreenChange", handleFullScreenChange);

    // 清理函数
    return () => {
      document.removeEventListener("fullscreenchange", handleFullScreenChange);
      document.removeEventListener(
        "webkitfullscreenchange",
        handleFullScreenChange,
      );
      document.removeEventListener(
        "mozfullscreenchange",
        handleFullScreenChange,
      );
      document.removeEventListener(
        "MSFullscreenChange",
        handleFullScreenChange,
      );
    };
  }, []);
  return (
    <>
      <Content
        className="custom-scrollbar"
        style={{
          height: isFullScreenState ? "100vh" : `calc(100vh - 64px)`,
          overflow: "auto",
          backgroundColor: "#eee",
        }}
      >
        <SettingPage />
        <div
          style={{
            padding: 24,
            minHeight: 360,
            margin: "16px 8px",
            overflow: "auto",
          }}
        >
          <Outlet />
        </div>
      </Content>
    </>
  );
};

export default MainPage;

注意事项:

1、封装的组件中放大的是父组件,可以根据具体需要修改parentElement

2、使用的时候通过ref获取dom并且通过父传子的方式传给封装组件

3、监听是否放大了,修改父盒子的高度

相关推荐
jessezappy几秒前
jQuery-Word-Export 使用记录及完整修正文件下载 jquery.wordexport.js
前端·word·jquery·filesaver·word-export
旧林84328 分钟前
第八章 利用CSS制作导航菜单
前端·css
yngsqq40 分钟前
c#使用高版本8.0步骤
java·前端·c#
Myli_ing1 小时前
考研倒计时-配色+1
前端·javascript·考研
余道各努力,千里自同风1 小时前
前端 vue 如何区分开发环境
前端·javascript·vue.js
软件小伟1 小时前
Vue3+element-plus 实现中英文切换(Vue-i18n组件的使用)
前端·javascript·vue.js
醉の虾2 小时前
Vue3 使用v-for 渲染列表数据后更新
前端·javascript·vue.js
张小小大智慧2 小时前
TypeScript 的发展与基本语法
前端·javascript·typescript
hummhumm2 小时前
第 22 章 - Go语言 测试与基准测试
java·大数据·开发语言·前端·python·golang·log4j
asleep7012 小时前
第8章利用CSS制作导航菜单
前端·css