• 全屏组件封装(react18+antd)


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

    1、封装组件

    在components下面构建FullScreenButton文件:

    FullScreenButton/index.tsx

    1. import React, { useState, useCallback, useEffect } from "react";
    2. import { FullscreenOutlined, FullscreenExitOutlined } from "@ant-design/icons";
    3. import { Tooltip } from "antd";
    4. const FullScreenButton = ({ targetRef }) => {
    5. const [isFullScreen, setIsFullScreen] = useState(false);
    6. const toggleFullScreen = useCallback(() => {
    7. const parentElement = targetRef.current?.parentElement;
    8. if (!parentElement) return;
    9. if (isFullScreen) {
    10. exitFullScreen();
    11. } else {
    12. enterFullScreen(parentElement);
    13. }
    14. setIsFullScreen(!isFullScreen);
    15. }, [isFullScreen, targetRef]);
    16. const enterFullScreen = (element) => {
    17. if (element.requestFullscreen) {
    18. element.requestFullscreen();
    19. } else if (element.mozRequestFullScreen) {
    20. element.mozRequestFullScreen();
    21. } else if (element.webkitRequestFullscreen) {
    22. element.webkitRequestFullscreen();
    23. } else if (element.msRequestFullscreen) {
    24. element.msRequestFullscreen();
    25. }
    26. };
    27. const exitFullScreen = () => {
    28. if (document.exitFullscreen) {
    29. document.exitFullscreen();
    30. } else if (document.mozCancelFullScreen) {
    31. document.mozCancelFullScreen();
    32. } else if (document.webkitExitFullscreen) {
    33. document.webkitExitFullscreen();
    34. } else if (document.msExitFullscreen) {
    35. document.msExitFullscreen();
    36. }
    37. };
    38. useEffect(() => {
    39. const handleFullScreenChange = () => {
    40. const parentElement = targetRef.current?.parentElement;
    41. setIsFullScreen(document.fullscreenElement === parentElement);
    42. };
    43. document.addEventListener("fullscreenchange", handleFullScreenChange);
    44. document.addEventListener("webkitfullscreenchange", handleFullScreenChange);
    45. document.addEventListener("mozfullscreenchange", handleFullScreenChange);
    46. document.addEventListener("MSFullscreenChange", handleFullScreenChange);
    47. return () => {
    48. document.removeEventListener("fullscreenchange", handleFullScreenChange);
    49. document.removeEventListener(
    50. "webkitfullscreenchange",
    51. handleFullScreenChange,
    52. );
    53. document.removeEventListener(
    54. "mozfullscreenchange",
    55. handleFullScreenChange,
    56. );
    57. document.removeEventListener(
    58. "MSFullscreenChange",
    59. handleFullScreenChange,
    60. );
    61. };
    62. }, [targetRef]);
    63. return (
    64. <span
    65. style={{ cursor: "pointer", fontSize: "16px" }}
    66. onClick={toggleFullScreen}
    67. >
    68. <Tooltip placement="bottom" title={isFullScreen ? "退出全屏" : "全屏"}>
    69. {isFullScreen ? <FullscreenExitOutlined /> : <FullscreenOutlined />}
    70. </Tooltip>
    71. </span>
    72. );
    73. };
    74. export default FullScreenButton;

    2、使用组件

    在指定的组件中使用

    1. import "./index.less";
    2. import React, { useRef } from "react";
    3. import FullScreenButton from "@/components/FullScreenButton";
    4. const settingPage: React.FC = () => {
    5. const targenRef = useRef(null);
    6. return (
    7. <div ref={targenRef}>
    8. <div className="settings">
    9. <div className="bread">面包屑</div>
    10. <div className="settings-right">
    11. <FullScreenButton targetRef={targenRef} />
    12. {/* <span>导航+设置</span> */}
    13. </div>
    14. </div>
    15. </div>
    16. );
    17. };
    18. export default settingPage;

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

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

    1. import React, { useRef, useState, useEffect } from "react";
    2. import { Layout } from "antd";
    3. import { Outlet } from "react-router-dom";
    4. import "./index.less";
    5. import SettingPage from "@/components/setting";
    6. import isFullScreen from "@/utils/isFullScreen";
    7. const { Content } = Layout;
    8. const MainPage: React.FC = () => {
    9. const [isFullScreenState, setIsFullScreenState] = useState(isFullScreen());
    10. useEffect(() => {
    11. const handleFullScreenChange = () => {
    12. setIsFullScreenState(isFullScreen());
    13. };
    14. document.addEventListener("fullscreenchange", handleFullScreenChange);
    15. document.addEventListener("webkitfullscreenchange", handleFullScreenChange);
    16. document.addEventListener("mozfullscreenchange", handleFullScreenChange);
    17. document.addEventListener("MSFullscreenChange", handleFullScreenChange);
    18. // 清理函数
    19. return () => {
    20. document.removeEventListener("fullscreenchange", handleFullScreenChange);
    21. document.removeEventListener(
    22. "webkitfullscreenchange",
    23. handleFullScreenChange,
    24. );
    25. document.removeEventListener(
    26. "mozfullscreenchange",
    27. handleFullScreenChange,
    28. );
    29. document.removeEventListener(
    30. "MSFullscreenChange",
    31. handleFullScreenChange,
    32. );
    33. };
    34. }, []);
    35. return (
    36. <>
    37. <Content
    38. className="custom-scrollbar"
    39. style={{
    40. height: isFullScreenState ? "100vh" : `calc(100vh - 64px)`,
    41. overflow: "auto",
    42. backgroundColor: "#eee",
    43. }}
    44. >
    45. <SettingPage />
    46. <div
    47. style={{
    48. padding: 24,
    49. minHeight: 360,
    50. margin: "16px 8px",
    51. overflow: "auto",
    52. }}
    53. >
    54. <Outlet />
    55. </div>
    56. </Content>
    57. </>
    58. );
    59. };
    60. export default MainPage;

    注意事项:

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

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

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

     

  • 相关阅读:
    C语言从入门到进阶教程文章分享汇总~持续更新
    springboot校园疫情智慧防控微信小程序毕业设计源码011133
    时序预测 | MATLAB实现SSA-GRU(麻雀算法优化门控循环单元)时间序列预测
    十四天学会C++之第一天(入门和基本语法)
    C# 反射(Reflection)详解-Assembly
    Python - Wave2lip 环境配置与 Wave2lip x GFP-GAN 实战 [超详细!]
    【第56篇】GhostNet:廉价操作得到更多的特征
    深搜&回溯&剪枝-全排列
    卷积操作的不同类型
    【解刊】IEEE(Trans)系列,CCF-A类顶刊,国人友好,无需版面费!
  • 原文地址:https://blog.csdn.net/qq_47040462/article/details/141057397