@arcblock/ux 2.12.2 → 2.12.4

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.
@@ -0,0 +1,96 @@
1
+ import React, { useEffect, useRef, useState } from 'react';
2
+
3
+ interface SubContainerProps extends React.HTMLAttributes<HTMLDivElement> {
4
+ children: React.ReactNode;
5
+ }
6
+
7
+ const paddingTop = 8;
8
+
9
+ // 下拉子菜单容器
10
+ export function SubContainer({ children, ...props }: SubContainerProps) {
11
+ const rootRef = useRef<HTMLDivElement>(null);
12
+ const [position, setPosition] = useState(0); // 只需要保存水平偏移量
13
+
14
+ const updatePosition = () => {
15
+ if (!rootRef.current) return;
16
+ const anchor = rootRef.current.parentElement; // 以父容器作为参照
17
+ if (!anchor) return;
18
+
19
+ const anchorRect = anchor.getBoundingClientRect();
20
+ const containerRect = rootRef.current.getBoundingClientRect();
21
+ const windowWidth = window.innerWidth;
22
+ const padding = 32;
23
+
24
+ // 1. 计算相对于父元素的水平居中位置
25
+ let left = (anchorRect.width - containerRect.width) / 2;
26
+
27
+ // 2. 检查容器在视口中的绝对位置
28
+ const absoluteLeft = anchorRect.left + left;
29
+
30
+ // 3. 调整水平位置确保不超出窗口
31
+ if (absoluteLeft < 0) {
32
+ // 如果超出左边界,向右偏移
33
+ left = left - absoluteLeft + padding;
34
+ } else if (absoluteLeft + containerRect.width > windowWidth) {
35
+ // 如果超出右边界,向左偏移
36
+ left -= absoluteLeft + containerRect.width - windowWidth + padding;
37
+ }
38
+
39
+ setPosition(left);
40
+ };
41
+
42
+ // 监听自身大小变化
43
+ useEffect(() => {
44
+ const resizeObserver = new ResizeObserver(() => {
45
+ updatePosition();
46
+ });
47
+
48
+ resizeObserver.observe(rootRef.current!);
49
+
50
+ return () => resizeObserver.disconnect();
51
+ // eslint-disable-next-line react-hooks/exhaustive-deps
52
+ }, []);
53
+
54
+ // 监听 anchor 的大小变化
55
+ useEffect(() => {
56
+ const anchor = rootRef.current?.parentElement;
57
+ let resizeObserver: ResizeObserver | null = null;
58
+
59
+ if (anchor) {
60
+ resizeObserver = new ResizeObserver(() => {
61
+ updatePosition();
62
+ });
63
+
64
+ resizeObserver.observe(anchor);
65
+ }
66
+
67
+ return () => resizeObserver?.disconnect();
68
+ // eslint-disable-next-line react-hooks/exhaustive-deps
69
+ }, []);
70
+
71
+ // 监听窗口大小变化
72
+ useEffect(() => {
73
+ const handleResize = () => {
74
+ updatePosition();
75
+ };
76
+
77
+ window.addEventListener('resize', handleResize);
78
+ return () => window.removeEventListener('resize', handleResize);
79
+ // eslint-disable-next-line react-hooks/exhaustive-deps
80
+ }, []);
81
+
82
+ return (
83
+ <div
84
+ ref={rootRef}
85
+ className="navmenu-sub__container"
86
+ style={{
87
+ paddingTop: `${paddingTop}px`,
88
+ transform: `translateX(${position}px)`,
89
+ }}
90
+ {...props}>
91
+ {children}
92
+ </div>
93
+ );
94
+ }
95
+
96
+ export default SubContainer;
package/src/Util/index.ts CHANGED
@@ -313,7 +313,7 @@ export function openWebWallet({
313
313
  return { type: 'web' };
314
314
  }
315
315
 
316
- export const getFontSize = (size: string) => {
316
+ export const getFontSize = (size: string | number) => {
317
317
  // 12px 及以上的 size 有效, 否则返回 inherit
318
318
  if (size && Number(size) >= 12) {
319
319
  return `${Number(size)}px`;