react中封装组件对三方库或者已有ui组件进行自动调整大小嵌入显示区域

原因:有些三方库的组件或者已有的组件ui部分无法调整大小,比如组件是全屏覆盖显示但是在新开发的迭代要求缩小到部分区域显示

解决方法:使用css的属性transform scale,通过绝对定位处理放大或者缩小的处理区域

ini 复制代码
import React, {
  FC, useEffect, useRef, useState,
} from 'react';
import { useSize } from 'ahooks';
import { isEmpty, min } from 'lodash';
import { css } from '@emotion/css';

interface Props {
  debug?: boolean
  lockMaxWidth?: boolean
  lockMaxHeight?: boolean
}

export const AutoScale: FC<React.PropsWithChildren & Props> = ({ children, debug, lockMaxHeight = true, lockMaxWidth = true }) => {
  const outsideWrapperRef = useRef<HTMLDivElement>(null);
  const insideWrapperRef = useRef<HTMLDivElement>(null);
  const outsideSize = useSize(outsideWrapperRef.current?.parentElement);
  const insideSize = useSize(insideWrapperRef);
  const [scale, setScale] = useState(1);
  const [height, setHeight] = useState(0);
  const [width, setWidth] = useState(0);

  useEffect(() => {
    if (isEmpty(insideSize) || isEmpty(outsideSize)) {
      return;
    }
    if (debug) {
      console.log(outsideSize);
    }

    const scaleWidth = outsideSize.width / insideSize.width;
    const scaleHeight = outsideSize.height / insideSize.height;
    let resultScale;

    switch (true) {
      case lockMaxWidth && !lockMaxHeight:
        resultScale = scaleWidth;
        break;
      case !lockMaxWidth && lockMaxHeight:
        resultScale = scaleHeight;
        break;
      case lockMaxWidth && lockMaxHeight:
        resultScale = min([scaleWidth, scaleHeight]);
        break;
      default:
        resultScale = scaleWidth;
        break;
    }

    setScale(resultScale);
    setHeight(insideSize.height * resultScale);
    setWidth(insideSize.width * resultScale);
  }, [insideSize, outsideSize]);

  return (
    <div
      ref={outsideWrapperRef}
      className={css`
          position: relative;
          width: ${`${width}px` || '100%'};
          height: ${`${height}px` || '100%'};
          overflow: ${width ? 'initial' : 'hidden'};
      `}
    >
      <div
        ref={insideWrapperRef}
        className={css`
          position: absolute;
          top: 0;
          left: 0;
          transform-origin: left top;
          transform: scale(${scale});
      `}
      >
        {children}
      </div>
    </div>
  );
};
相关推荐
Ruihong2 小时前
🚀 Vue 一键转 React!企业后台 VuReact 混写迁移实战
vue.js·react.js
@大迁世界3 小时前
17.在 React 中如何根据条件决定渲染哪个组件?
前端·javascript·react.js·前端框架·ecmascript
lihaozecq11 小时前
我用 1 天的时间 vibe coding 了一个多人德州扑克游戏
前端·react.js·ai编程
Highcharts.js14 小时前
高级可视化图表的暗色模式与主题|Highcharts 自适应主题配色全解
前端·react.js·实时图表
ISkp3V8b421 小时前
从 ReAct 到 Workflow:基于云端 API 构建事件驱动的智能体
前端·react.js·前端框架
谢尔登1 天前
【React】setState 触发渲染的流程
前端·react.js·前端框架
弓.长.1 天前
ReactNative for OpenHarmony项目鸿蒙化三方库:lottie-react-native — Lottie动画组件
react native·react.js·harmonyos
freewlt1 天前
React Server Components 深度解析:从原理到实战的完整指南
前端·javascript·react.js
弓.长.1 天前
ReactNative for OpenHarmony项目鸿蒙化三方库:react-native-flash-message — 闪现消息组件
react native·react.js·harmonyos
弓.长.1 天前
ReactNative for OpenHarmony项目鸿蒙化三方库:react-native-snap-carousel — 轮播组件
react native·react.js·harmonyos