deepsea-components 5.15.22 → 5.15.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +23 -23
- package/dist/components/AutoFit.cjs +7 -7
- package/dist/components/AutoFit.js +30 -30
- package/dist/components/AutoScroll.cjs +7 -7
- package/dist/components/AutoScroll.js +30 -30
- package/dist/components/AutoSizeTextarea.cjs +4 -4
- package/dist/components/AutoSizeTextarea.js +17 -17
- package/dist/components/CircleText.js +4 -4
- package/dist/components/CopyButton.cjs +2 -2
- package/dist/components/CopyButton.js +13 -14
- package/dist/components/Echart.cjs +2 -2
- package/dist/components/Echart.js +12 -12
- package/dist/components/Flow.cjs +19 -19
- package/dist/components/Flow.js +44 -44
- package/dist/components/FormLabel.cjs +3 -3
- package/dist/components/FormLabel.js +9 -9
- package/dist/components/HlsPlayer.js +10 -10
- package/dist/components/IconFileType.cjs +4 -4
- package/dist/components/IconFileType.js +9 -9
- package/dist/components/InfiniteScroll.cjs +13 -13
- package/dist/components/InfiniteScroll.js +32 -32
- package/dist/components/InputFile.js +4 -4
- package/dist/components/InputFileButton.js +11 -11
- package/dist/components/LoopSwiper.cjs +33 -33
- package/dist/components/LoopSwiper.js +47 -47
- package/dist/components/ReadExcel.js +2 -2
- package/dist/components/ReadSheet.js +5 -5
- package/dist/components/Ring.js +4 -4
- package/dist/components/Scroll.cjs +15 -15
- package/dist/components/Scroll.js +32 -33
- package/dist/components/ScrollMask.js +13 -13
- package/dist/components/ScrollMask.module.cjs +2 -2
- package/dist/components/ScrollMask.module.js +2 -2
- package/dist/components/SectionRing.js +6 -6
- package/dist/components/Skeleton.cjs +17 -17
- package/dist/components/Skeleton.js +23 -23
- package/dist/components/Title.js +12 -12
- package/dist/components/TransitionBox.js +10 -10
- package/dist/components/TransitionNum.cjs +1 -1
- package/dist/components/TransitionNum.js +12 -12
- package/dist/components/Trapezium.cjs +4 -4
- package/dist/components/Trapezium.js +14 -14
- package/dist/components/Unify.js +10 -10
- package/dist/components/WriteExcel.js +2 -2
- package/dist/components/WriteSheet.js +4 -4
- package/dist/utils/getReactVersion.js +2 -2
- package/package.json +6 -5
- package/src/components/AutoFit.tsx +104 -104
- package/src/components/AutoScroll.tsx +150 -150
- package/src/components/AutoSizeTextarea.tsx +50 -50
- package/src/components/CircleText.tsx +81 -81
- package/src/components/CopyButton.tsx +31 -31
- package/src/components/Echart.tsx +69 -69
- package/src/components/Flow.tsx +271 -271
- package/src/components/FormLabel.tsx +41 -41
- package/src/components/HlsPlayer.tsx +34 -34
- package/src/components/IconFileType.tsx +162 -162
- package/src/components/InfiniteScroll.tsx +163 -163
- package/src/components/InputFile.tsx +93 -93
- package/src/components/InputFileButton.tsx +122 -122
- package/src/components/LoopSwiper.tsx +125 -125
- package/src/components/ReadExcel.tsx +13 -13
- package/src/components/ReadSheet.tsx +17 -17
- package/src/components/Ring.tsx +30 -30
- package/src/components/Scroll.tsx +97 -97
- package/src/components/ScrollMask.module.css +87 -87
- package/src/components/ScrollMask.tsx +68 -68
- package/src/components/SectionRing.tsx +40 -40
- package/src/components/Skeleton.tsx +45 -45
- package/src/components/Title.tsx +27 -27
- package/src/components/TransitionBox.tsx +44 -44
- package/src/components/TransitionNum.tsx +54 -54
- package/src/components/Trapezium.tsx +60 -60
- package/src/components/Unify.tsx +38 -38
- package/src/components/WriteExcel.tsx +13 -13
- package/src/components/WriteSheet.tsx +25 -25
- package/src/utils/getReactVersion.ts +7 -7
- package/src/utils/index.ts +33 -33
- package/tsconfig.json +16 -16
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
"use client"
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
const TransitionNum = /*#__PURE__*/
|
|
2
|
+
import { jsx } from "react/jsx-runtime";
|
|
3
|
+
import { setFrameInterval } from "deepsea-tools";
|
|
4
|
+
import { forwardRef, useEffect, useImperativeHandle, useRef } from "react";
|
|
5
|
+
const TransitionNum = /*#__PURE__*/ forwardRef((props, ref)=>{
|
|
6
6
|
const { children: num, period, numToStr, ins, ...rest } = props;
|
|
7
|
-
if (!Number.isInteger(num) || !Number.isInteger(period) || period <= 0) throw new RangeError("
|
|
8
|
-
const ele =
|
|
9
|
-
const cache =
|
|
7
|
+
if (!Number.isInteger(num) || !Number.isInteger(period) || period <= 0) throw new RangeError("\u76EE\u6807\u6570\u5B57\u5FC5\u987B\u662F\u6574\u6570\uFF0C\u5468\u671F\u5FC5\u987B\u662F\u6B63\u6574\u6570");
|
|
8
|
+
const ele = useRef(null);
|
|
9
|
+
const cache = useRef({
|
|
10
10
|
num,
|
|
11
11
|
period,
|
|
12
12
|
numToStr,
|
|
@@ -18,17 +18,17 @@ const TransitionNum = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.forwar
|
|
|
18
18
|
period,
|
|
19
19
|
numToStr
|
|
20
20
|
};
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
useImperativeHandle(ref, ()=>ele.current, []);
|
|
22
|
+
useImperativeHandle(ins, ()=>({
|
|
23
23
|
get: ()=>cache.current.show
|
|
24
24
|
}), []);
|
|
25
|
-
|
|
25
|
+
useEffect(()=>{
|
|
26
26
|
const { num, period, show, numToStr } = cache.current;
|
|
27
27
|
ele.current.innerText = (numToStr || String)(show);
|
|
28
28
|
if (num === show) return;
|
|
29
29
|
const div = ele.current;
|
|
30
30
|
const speed = (num - show) / period;
|
|
31
|
-
const cancel =
|
|
31
|
+
const cancel = setFrameInterval(()=>{
|
|
32
32
|
const { num, numToStr } = cache.current;
|
|
33
33
|
cache.current.show += speed;
|
|
34
34
|
if (speed > 0 && cache.current.show > num || speed < 0 && cache.current.show < num) {
|
|
@@ -41,7 +41,7 @@ const TransitionNum = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.forwar
|
|
|
41
41
|
}, [
|
|
42
42
|
num
|
|
43
43
|
]);
|
|
44
|
-
return /*#__PURE__*/
|
|
44
|
+
return /*#__PURE__*/ jsx("div", {
|
|
45
45
|
ref: ele,
|
|
46
46
|
...rest
|
|
47
47
|
});
|
|
@@ -45,10 +45,10 @@ const Trapezium = /*#__PURE__*/ (0, external_react_namespaceObject.forwardRef)((
|
|
|
45
45
|
const h = f * Math.sin(2 * a);
|
|
46
46
|
return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
|
|
47
47
|
ref: ref,
|
|
48
|
-
className: (0, external_deepsea_tools_namespaceObject.clsx)((0, css_namespaceObject.css)`
|
|
49
|
-
width: var(--width);
|
|
50
|
-
height: var(--height);
|
|
51
|
-
clip-path: var(--clip-path);
|
|
48
|
+
className: (0, external_deepsea_tools_namespaceObject.clsx)((0, css_namespaceObject.css)`
|
|
49
|
+
width: var(--width);
|
|
50
|
+
height: var(--height);
|
|
51
|
+
clip-path: var(--clip-path);
|
|
52
52
|
`, className),
|
|
53
53
|
style: (0, index_cjs_namespaceObject.transformCSSVariable)({
|
|
54
54
|
width: (0, index_cjs_namespaceObject.px)(bottom),
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
"use client"
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
const Trapezium = /*#__PURE__*/
|
|
2
|
+
import { jsx } from "react/jsx-runtime";
|
|
3
|
+
import { css } from "@emotion/css";
|
|
4
|
+
import { clsx } from "deepsea-tools";
|
|
5
|
+
import { forwardRef } from "react";
|
|
6
|
+
import { px, transformCSSVariable } from "../utils/index.js";
|
|
7
|
+
const Trapezium = /*#__PURE__*/ forwardRef((props, ref)=>{
|
|
8
8
|
const { top, bottom, height, borderRadius, className, style, ...rest } = props;
|
|
9
9
|
const diff = (bottom - top) / 2;
|
|
10
10
|
const a = Math.atan(height / diff) / 2;
|
|
@@ -15,16 +15,16 @@ const Trapezium = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.forwardRef
|
|
|
15
15
|
const f = borderRadius / Math.tan(e);
|
|
16
16
|
const g = f * Math.cos(2 * a);
|
|
17
17
|
const h = f * Math.sin(2 * a);
|
|
18
|
-
return /*#__PURE__*/
|
|
18
|
+
return /*#__PURE__*/ jsx("div", {
|
|
19
19
|
ref: ref,
|
|
20
|
-
className:
|
|
21
|
-
width: var(--width);
|
|
22
|
-
height: var(--height);
|
|
23
|
-
clip-path: var(--clip-path);
|
|
20
|
+
className: clsx(css`
|
|
21
|
+
width: var(--width);
|
|
22
|
+
height: var(--height);
|
|
23
|
+
clip-path: var(--clip-path);
|
|
24
24
|
`, className),
|
|
25
|
-
style:
|
|
26
|
-
width:
|
|
27
|
-
height:
|
|
25
|
+
style: transformCSSVariable({
|
|
26
|
+
width: px(bottom),
|
|
27
|
+
height: px(height),
|
|
28
28
|
clipath: `path("M ${diff + f} 0 A ${borderRadius} ${borderRadius} 0 0 0 ${diff - g} ${h} L ${c} ${height - d} A ${borderRadius} ${borderRadius} 0 0 0 ${b} ${height} L ${bottom - b} ${height} A ${borderRadius} ${borderRadius} 0 0 0 ${bottom - c} ${height - d} L ${top + diff + g} ${h} A ${borderRadius} ${borderRadius} 0 0 0 ${top + diff - f} 0 Z")`
|
|
29
29
|
}, style),
|
|
30
30
|
...rest
|
package/dist/components/Unify.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
"use client"
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
const UnifyConfigContext = /*#__PURE__*/
|
|
2
|
+
import { jsx } from "react/jsx-runtime";
|
|
3
|
+
import { clsx } from "deepsea-tools";
|
|
4
|
+
import { createContext, createElement, useContext } from "react";
|
|
5
|
+
const UnifyConfigContext = /*#__PURE__*/ createContext({});
|
|
6
6
|
const UnifyConfigProvider = ({ className, style, children })=>{
|
|
7
|
-
const { className: _className, style: _style } =
|
|
8
|
-
return /*#__PURE__*/
|
|
7
|
+
const { className: _className, style: _style } = useContext(UnifyConfigContext);
|
|
8
|
+
return /*#__PURE__*/ jsx(UnifyConfigContext.Provider, {
|
|
9
9
|
value: {
|
|
10
|
-
className:
|
|
10
|
+
className: clsx(_className, className),
|
|
11
11
|
style: {
|
|
12
12
|
..._style,
|
|
13
13
|
...style
|
|
@@ -18,9 +18,9 @@ const UnifyConfigProvider = ({ className, style, children })=>{
|
|
|
18
18
|
};
|
|
19
19
|
function Unify(props) {
|
|
20
20
|
const { as = "div", className, style, ...rest } = props;
|
|
21
|
-
const { className: _className, style: _style } =
|
|
22
|
-
return /*#__PURE__*/
|
|
23
|
-
className:
|
|
21
|
+
const { className: _className, style: _style } = useContext(UnifyConfigContext);
|
|
22
|
+
return /*#__PURE__*/ createElement(as, {
|
|
23
|
+
className: clsx(_className, className),
|
|
24
24
|
style: {
|
|
25
25
|
..._style,
|
|
26
26
|
...style
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
"use client"
|
|
2
|
-
import
|
|
3
|
-
const WriteExcel =
|
|
2
|
+
import { WriteSheet } from "./WriteSheet.js";
|
|
3
|
+
const WriteExcel = WriteSheet;
|
|
4
4
|
export { WriteExcel };
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
"use client"
|
|
2
|
-
import
|
|
3
|
-
import
|
|
2
|
+
import { writeSheet } from "deepsea-tools";
|
|
3
|
+
import { createElement } from "react";
|
|
4
4
|
function WriteSheet(props) {
|
|
5
5
|
const { as, excel, onClick: _onClick, ...rest } = props;
|
|
6
6
|
function onClick(e) {
|
|
7
7
|
_onClick?.(e);
|
|
8
8
|
if (!excel) return;
|
|
9
|
-
|
|
9
|
+
writeSheet(excel);
|
|
10
10
|
}
|
|
11
|
-
return /*#__PURE__*/
|
|
11
|
+
return /*#__PURE__*/ createElement(as ?? "button", {
|
|
12
12
|
onClick,
|
|
13
13
|
...rest
|
|
14
14
|
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { version } from "react";
|
|
2
2
|
const reg = /(\d+)\.(\d+)\.(\d+)/;
|
|
3
3
|
function getReactVersion() {
|
|
4
|
-
return
|
|
4
|
+
return version.match(reg).slice(1).map(Number);
|
|
5
5
|
}
|
|
6
6
|
export { getReactVersion };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "deepsea-components",
|
|
3
|
-
"version": "5.15.
|
|
3
|
+
"version": "5.15.24",
|
|
4
4
|
"description": "格数科技自用组件库",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -38,15 +38,16 @@
|
|
|
38
38
|
"@emotion/css": "^11.13.5",
|
|
39
39
|
"clipboard": "^2.0.11",
|
|
40
40
|
"echarts": "^5.6.0",
|
|
41
|
-
"hls.js": "^1.6.
|
|
41
|
+
"hls.js": "^1.6.9",
|
|
42
42
|
"smooth-scrollbar": "^8.8.4",
|
|
43
|
-
"
|
|
44
|
-
"
|
|
43
|
+
"soda-hooks": "6.12.7",
|
|
44
|
+
"deepsea-tools": "5.40.4"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
|
-
"typescript": "^5.
|
|
47
|
+
"typescript": "^5.9.2"
|
|
48
48
|
},
|
|
49
49
|
"peerDependencies": {
|
|
50
|
+
"@types/node": ">=20",
|
|
50
51
|
"@types/react": ">=18.3.18",
|
|
51
52
|
"@types/react-dom": ">=18.3.5",
|
|
52
53
|
"react": ">=18.3.1",
|
|
@@ -1,104 +1,104 @@
|
|
|
1
|
-
"use client"
|
|
2
|
-
|
|
3
|
-
import { css } from "@emotion/css"
|
|
4
|
-
import { clsx } from "deepsea-tools"
|
|
5
|
-
import { HTMLAttributes, forwardRef, useEffect, useImperativeHandle, useLayoutEffect, useRef, useState } from "react"
|
|
6
|
-
|
|
7
|
-
import { px, transformCSSVariable } from "@/utils"
|
|
8
|
-
|
|
9
|
-
export interface AutoFitProps extends HTMLAttributes<HTMLDivElement> {
|
|
10
|
-
/** 设计稿宽度,默认 1920 */
|
|
11
|
-
width?: number
|
|
12
|
-
/** 设计稿高度,默认 1080 */
|
|
13
|
-
height?: number
|
|
14
|
-
/**
|
|
15
|
-
* 在哪些方向进行缩放,默认缩放所有方向
|
|
16
|
-
* 1. 水平方向是指,宽度按照设计稿进行占满,高度反向缩放
|
|
17
|
-
* 2. 垂直方向是指,高度按照设计稿进行占满,宽度反向缩放
|
|
18
|
-
* 3. 默认是水平和垂直方向都进行缩放,类似于 background-size: contain 的效果
|
|
19
|
-
*/
|
|
20
|
-
direction?: "horizontal" | "vertical" | "both"
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* 自适应缩放组件
|
|
25
|
-
*
|
|
26
|
-
* 注意:
|
|
27
|
-
* 1. 父元素必须设置非 static 定位方式
|
|
28
|
-
* 2. 父元素只能有且仅有一个子元素,那就是 AutoFit
|
|
29
|
-
* 3. 不要设置 AutoFit 的 position、left、top、transform、width、height 属性
|
|
30
|
-
* 4. 在第一次完成缩放前,无论 props 是什么,返回的都是 <div style={{ display: "none" }} />
|
|
31
|
-
* 5. 元素的属性、事件、资源并不是立即加载的,会有一帧的延迟,在第一次完成缩放后才会显示
|
|
32
|
-
*/
|
|
33
|
-
export const AutoFit = forwardRef<HTMLDivElement, AutoFitProps>((props, ref) => {
|
|
34
|
-
const { width: designWidth = 1920, height: designHeight = 1080, direction, className, style, ...rest } = props
|
|
35
|
-
const ele = useRef<HTMLDivElement>(null)
|
|
36
|
-
const [show, setShow] = useState(false)
|
|
37
|
-
const [transform, setTransform] = useState<string | undefined>(undefined)
|
|
38
|
-
const [width, setWidth] = useState<string | undefined>(undefined)
|
|
39
|
-
const [height, setHeight] = useState<string | undefined>(undefined)
|
|
40
|
-
|
|
41
|
-
useImperativeHandle(ref, () => ele.current!, [ele.current])
|
|
42
|
-
useLayoutEffect(() => {
|
|
43
|
-
const element = ele.current
|
|
44
|
-
const parent = element?.parentElement
|
|
45
|
-
if (!element || !parent) return
|
|
46
|
-
function listener(entries: ResizeObserverEntry[]) {
|
|
47
|
-
const entry = entries[0]
|
|
48
|
-
const { contentRect } = entry
|
|
49
|
-
if (direction === "horizontal") {
|
|
50
|
-
const scale = contentRect.width / designWidth
|
|
51
|
-
setTransform(`scale(${scale})`)
|
|
52
|
-
setWidth(px(designWidth))
|
|
53
|
-
setHeight(px(contentRect.height / scale))
|
|
54
|
-
} else if (direction === "vertical") {
|
|
55
|
-
const scale = contentRect.height / designHeight
|
|
56
|
-
setTransform(`scale(${scale})`)
|
|
57
|
-
setWidth(px(contentRect.width / scale))
|
|
58
|
-
setHeight(px(designHeight))
|
|
59
|
-
} else {
|
|
60
|
-
const scale = Math.min(contentRect.width / designWidth, contentRect.height / designHeight)
|
|
61
|
-
setTransform(
|
|
62
|
-
`translateX(${(contentRect.width - designWidth * scale) / 2}px) translateY(${(contentRect.height - designHeight * scale) / 2}px) scale(${scale})`,
|
|
63
|
-
)
|
|
64
|
-
setWidth(px(designWidth))
|
|
65
|
-
setHeight(px(designHeight))
|
|
66
|
-
}
|
|
67
|
-
setShow(true)
|
|
68
|
-
}
|
|
69
|
-
const observer = new ResizeObserver(listener)
|
|
70
|
-
observer.observe(parent)
|
|
71
|
-
return () => observer.disconnect()
|
|
72
|
-
}, [ele.current?.parentElement, designWidth, designHeight, direction])
|
|
73
|
-
|
|
74
|
-
if (process.env.NODE_ENV === "development") {
|
|
75
|
-
useEffect(() => {
|
|
76
|
-
const parent = ele.current?.parentElement
|
|
77
|
-
if (!parent) return
|
|
78
|
-
const style = getComputedStyle(parent)
|
|
79
|
-
if (style.position === "static") {
|
|
80
|
-
console.warn("AutoFit 组件的父元素的 position 属性不应该是 static")
|
|
81
|
-
}
|
|
82
|
-
}, [ele.current?.parentElement])
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if (!show) return <div ref={ele} style={{ display: "none" }} />
|
|
86
|
-
|
|
87
|
-
return (
|
|
88
|
-
<div
|
|
89
|
-
ref={ele}
|
|
90
|
-
className={clsx(
|
|
91
|
-
css`
|
|
92
|
-
position: absolute;
|
|
93
|
-
transform: var(--transform);
|
|
94
|
-
transform-origin: top left;
|
|
95
|
-
width: var(--width);
|
|
96
|
-
height: var(--height);
|
|
97
|
-
`,
|
|
98
|
-
className,
|
|
99
|
-
)}
|
|
100
|
-
style={transformCSSVariable({ transform, width, height }, style)}
|
|
101
|
-
{...rest}
|
|
102
|
-
/>
|
|
103
|
-
)
|
|
104
|
-
})
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
import { css } from "@emotion/css"
|
|
4
|
+
import { clsx } from "deepsea-tools"
|
|
5
|
+
import { HTMLAttributes, forwardRef, useEffect, useImperativeHandle, useLayoutEffect, useRef, useState } from "react"
|
|
6
|
+
|
|
7
|
+
import { px, transformCSSVariable } from "@/utils"
|
|
8
|
+
|
|
9
|
+
export interface AutoFitProps extends HTMLAttributes<HTMLDivElement> {
|
|
10
|
+
/** 设计稿宽度,默认 1920 */
|
|
11
|
+
width?: number
|
|
12
|
+
/** 设计稿高度,默认 1080 */
|
|
13
|
+
height?: number
|
|
14
|
+
/**
|
|
15
|
+
* 在哪些方向进行缩放,默认缩放所有方向
|
|
16
|
+
* 1. 水平方向是指,宽度按照设计稿进行占满,高度反向缩放
|
|
17
|
+
* 2. 垂直方向是指,高度按照设计稿进行占满,宽度反向缩放
|
|
18
|
+
* 3. 默认是水平和垂直方向都进行缩放,类似于 background-size: contain 的效果
|
|
19
|
+
*/
|
|
20
|
+
direction?: "horizontal" | "vertical" | "both"
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 自适应缩放组件
|
|
25
|
+
*
|
|
26
|
+
* 注意:
|
|
27
|
+
* 1. 父元素必须设置非 static 定位方式
|
|
28
|
+
* 2. 父元素只能有且仅有一个子元素,那就是 AutoFit
|
|
29
|
+
* 3. 不要设置 AutoFit 的 position、left、top、transform、width、height 属性
|
|
30
|
+
* 4. 在第一次完成缩放前,无论 props 是什么,返回的都是 <div style={{ display: "none" }} />
|
|
31
|
+
* 5. 元素的属性、事件、资源并不是立即加载的,会有一帧的延迟,在第一次完成缩放后才会显示
|
|
32
|
+
*/
|
|
33
|
+
export const AutoFit = forwardRef<HTMLDivElement, AutoFitProps>((props, ref) => {
|
|
34
|
+
const { width: designWidth = 1920, height: designHeight = 1080, direction, className, style, ...rest } = props
|
|
35
|
+
const ele = useRef<HTMLDivElement>(null)
|
|
36
|
+
const [show, setShow] = useState(false)
|
|
37
|
+
const [transform, setTransform] = useState<string | undefined>(undefined)
|
|
38
|
+
const [width, setWidth] = useState<string | undefined>(undefined)
|
|
39
|
+
const [height, setHeight] = useState<string | undefined>(undefined)
|
|
40
|
+
|
|
41
|
+
useImperativeHandle(ref, () => ele.current!, [ele.current])
|
|
42
|
+
useLayoutEffect(() => {
|
|
43
|
+
const element = ele.current
|
|
44
|
+
const parent = element?.parentElement
|
|
45
|
+
if (!element || !parent) return
|
|
46
|
+
function listener(entries: ResizeObserverEntry[]) {
|
|
47
|
+
const entry = entries[0]
|
|
48
|
+
const { contentRect } = entry
|
|
49
|
+
if (direction === "horizontal") {
|
|
50
|
+
const scale = contentRect.width / designWidth
|
|
51
|
+
setTransform(`scale(${scale})`)
|
|
52
|
+
setWidth(px(designWidth))
|
|
53
|
+
setHeight(px(contentRect.height / scale))
|
|
54
|
+
} else if (direction === "vertical") {
|
|
55
|
+
const scale = contentRect.height / designHeight
|
|
56
|
+
setTransform(`scale(${scale})`)
|
|
57
|
+
setWidth(px(contentRect.width / scale))
|
|
58
|
+
setHeight(px(designHeight))
|
|
59
|
+
} else {
|
|
60
|
+
const scale = Math.min(contentRect.width / designWidth, contentRect.height / designHeight)
|
|
61
|
+
setTransform(
|
|
62
|
+
`translateX(${(contentRect.width - designWidth * scale) / 2}px) translateY(${(contentRect.height - designHeight * scale) / 2}px) scale(${scale})`,
|
|
63
|
+
)
|
|
64
|
+
setWidth(px(designWidth))
|
|
65
|
+
setHeight(px(designHeight))
|
|
66
|
+
}
|
|
67
|
+
setShow(true)
|
|
68
|
+
}
|
|
69
|
+
const observer = new ResizeObserver(listener)
|
|
70
|
+
observer.observe(parent)
|
|
71
|
+
return () => observer.disconnect()
|
|
72
|
+
}, [ele.current?.parentElement, designWidth, designHeight, direction])
|
|
73
|
+
|
|
74
|
+
if (process.env.NODE_ENV === "development") {
|
|
75
|
+
useEffect(() => {
|
|
76
|
+
const parent = ele.current?.parentElement
|
|
77
|
+
if (!parent) return
|
|
78
|
+
const style = getComputedStyle(parent)
|
|
79
|
+
if (style.position === "static") {
|
|
80
|
+
console.warn("AutoFit 组件的父元素的 position 属性不应该是 static")
|
|
81
|
+
}
|
|
82
|
+
}, [ele.current?.parentElement])
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (!show) return <div ref={ele} style={{ display: "none" }} />
|
|
86
|
+
|
|
87
|
+
return (
|
|
88
|
+
<div
|
|
89
|
+
ref={ele}
|
|
90
|
+
className={clsx(
|
|
91
|
+
css`
|
|
92
|
+
position: absolute;
|
|
93
|
+
transform: var(--transform);
|
|
94
|
+
transform-origin: top left;
|
|
95
|
+
width: var(--width);
|
|
96
|
+
height: var(--height);
|
|
97
|
+
`,
|
|
98
|
+
className,
|
|
99
|
+
)}
|
|
100
|
+
style={transformCSSVariable({ transform, width, height }, style)}
|
|
101
|
+
{...rest}
|
|
102
|
+
/>
|
|
103
|
+
)
|
|
104
|
+
})
|