@arcblock/ux 2.12.1 → 2.12.2

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.
@@ -8,7 +8,6 @@ import { HTMLDidAddressElement, IDidAddressProps } from './did-address';
8
8
  * - DidAddress 本身以 inline 形式渲染 (方便探测 did-address 的 full-width)
9
9
  * - 组件 mounted 时记录 did address 的 full-width (非 compact 模式的宽度)
10
10
  * - 监听容器宽度变化, 当容器宽度变化时, 对比容器宽度和 did address full-width, => 切换 compact 模式
11
- * - TODO: 初始化时, 在确定是否应该以 compact 模式渲染前, 隐藏显示, 避免闪烁问题
12
11
  */
13
12
  declare const ResponsiveDidAddress: React.ForwardRefExoticComponent<Omit<IResponsiveDidAddressProps, "ref"> & React.RefAttributes<HTMLDidAddressElement>>;
14
13
  export default ResponsiveDidAddress;
@@ -1,5 +1,5 @@
1
- import { jsx as _jsx } from "react/jsx-runtime";
2
- import { useLatest, useSize } from 'ahooks';
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { useDebounceFn, useLatest } from 'ahooks';
3
3
  import React, { forwardRef, useEffect, useImperativeHandle, useRef, useState } from 'react';
4
4
  import { Box } from '@mui/material';
5
5
  import { styled } from '../Theme';
@@ -13,22 +13,27 @@ import DidAddress from './did-address';
13
13
  * - DidAddress 本身以 inline 形式渲染 (方便探测 did-address 的 full-width)
14
14
  * - 组件 mounted 时记录 did address 的 full-width (非 compact 模式的宽度)
15
15
  * - 监听容器宽度变化, 当容器宽度变化时, 对比容器宽度和 did address full-width, => 切换 compact 模式
16
- * - TODO: 初始化时, 在确定是否应该以 compact 模式渲染前, 隐藏显示, 避免闪烁问题
17
16
  */
18
17
  const ResponsiveDidAddress = /*#__PURE__*/forwardRef(({
19
18
  style,
20
19
  className,
21
20
  component = 'span',
21
+ size,
22
22
  ...rest
23
23
  }, ref) => {
24
24
  const [compact, setCompact] = useState(false);
25
25
  const isCompact = useLatest(compact);
26
26
  // did address 完整显示时的宽度
27
27
  const [addressFullWidth, setAddressFullWidth] = useState(null);
28
+ const [containerWidth, setContainerWidth] = useState(null);
29
+ const [loading, setLoading] = useState(true);
30
+ const {
31
+ run: delaySetLoadingFalse
32
+ } = useDebounceFn(() => setLoading(false), {
33
+ wait: 50
34
+ });
28
35
  const containerRef = useRef(null);
29
36
  const innerRef = useRef(null);
30
- const size = useSize(containerRef);
31
- const containerWidth = size?.width || 0;
32
37
  useImperativeHandle(ref, () => new Proxy({}, {
33
38
  get(target, key) {
34
39
  return innerRef.current?.[key];
@@ -38,19 +43,30 @@ const ResponsiveDidAddress = /*#__PURE__*/forwardRef(({
38
43
  // 存储完整显示时 address 组件的宽度
39
44
  useEffect(() => {
40
45
  const innerWidth = innerRef.current?.offsetWidth || 0;
41
- let resizeObserver = null;
46
+ let innerObserver = null;
47
+ let containerObserver = null;
48
+
49
+ // 初始化宽度
42
50
  setAddressFullWidth(innerWidth);
51
+ setContainerWidth(containerRef.current?.offsetWidth);
43
52
 
44
53
  // 由于自定义字体的缘故,innerRef 的初始宽度可能发生二次改变,使用 observer 监听捕获
45
- resizeObserver = new ResizeObserver(() => {
54
+ innerObserver = new ResizeObserver(() => {
46
55
  if (!isCompact.current && innerRef.current?.offsetWidth !== innerWidth) {
47
56
  setAddressFullWidth(innerRef.current?.offsetWidth);
57
+ delaySetLoadingFalse();
48
58
  }
49
59
  });
50
- if (innerRef.current) {
51
- resizeObserver.observe(innerRef.current.getEl());
52
- }
53
- return () => resizeObserver.disconnect();
60
+ containerObserver = new ResizeObserver(() => {
61
+ setContainerWidth(containerRef.current?.offsetWidth);
62
+ });
63
+ innerObserver.observe(innerRef.current.getEl());
64
+ containerObserver.observe(containerRef.current);
65
+ delaySetLoadingFalse();
66
+ return () => {
67
+ innerObserver?.disconnect();
68
+ containerObserver?.disconnect();
69
+ };
54
70
  // eslint-disable-next-line react-hooks/exhaustive-deps
55
71
  }, []);
56
72
  useEffect(() => {
@@ -58,23 +74,35 @@ const ResponsiveDidAddress = /*#__PURE__*/forwardRef(({
58
74
  setCompact(containerWidth < addressFullWidth);
59
75
  }
60
76
  }, [containerWidth, addressFullWidth]);
61
- return /*#__PURE__*/_jsx(Root, {
77
+ return /*#__PURE__*/_jsxs(Root, {
62
78
  as: component,
63
79
  ref: containerRef,
64
80
  style: style,
65
81
  className: className,
66
- children: /*#__PURE__*/_jsx(StyledDidAddress, {
82
+ children: [/*#__PURE__*/_jsx(StyledDidAddress, {
83
+ style: {
84
+ position: loading ? 'absolute' : 'static',
85
+ left: loading ? '-99999px' : 'auto'
86
+ },
67
87
  ...rest,
88
+ size: size,
68
89
  component: component,
69
90
  inline: true,
70
91
  compact: compact,
71
92
  ref: innerRef
72
- })
93
+ }), loading && /*#__PURE__*/_jsx("div", {
94
+ style: {
95
+ width: '100%',
96
+ height: `${(size || 16) * 1.5}px`
97
+ }
98
+ })]
73
99
  });
74
100
  });
75
101
  export default ResponsiveDidAddress;
76
102
  const Root = styled(Box)`
77
103
  display: block;
104
+ width: 100%; /* flex & column & align-items: flex-start 时, 块级元素不会自动铺满容器 */
105
+ font-size: 0;
78
106
  overflow: hidden;
79
107
  ${({
80
108
  inline
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arcblock/ux",
3
- "version": "2.12.1",
3
+ "version": "2.12.2",
4
4
  "description": "Common used react components for arcblock products",
5
5
  "keywords": [
6
6
  "react",
@@ -68,12 +68,12 @@
68
68
  "react": ">=18.2.0",
69
69
  "react-router-dom": ">=6.22.3"
70
70
  },
71
- "gitHead": "f354d93f7db5e2e0547b75a05b5e47fd9a9587ac",
71
+ "gitHead": "4dc132cab82765eef5194cf00075a13bd5d8e458",
72
72
  "dependencies": {
73
73
  "@arcblock/did-motif": "^1.1.13",
74
- "@arcblock/icons": "^2.12.1",
75
- "@arcblock/nft-display": "^2.12.1",
76
- "@arcblock/react-hooks": "^2.12.1",
74
+ "@arcblock/icons": "^2.12.2",
75
+ "@arcblock/nft-display": "^2.12.2",
76
+ "@arcblock/react-hooks": "^2.12.2",
77
77
  "@babel/plugin-syntax-dynamic-import": "^7.8.3",
78
78
  "@fontsource/inter": "^5.0.16",
79
79
  "@fontsource/ubuntu-mono": "^5.0.18",
@@ -1,4 +1,4 @@
1
- import { useLatest, useSize } from 'ahooks';
1
+ import { useDebounceFn, useLatest } from 'ahooks';
2
2
  import React, { forwardRef, useEffect, useImperativeHandle, useRef, useState } from 'react';
3
3
  import { Box } from '@mui/material';
4
4
  import { styled } from '../Theme';
@@ -12,18 +12,20 @@ import DidAddress, { HTMLDidAddressElement, IDidAddressProps } from './did-addre
12
12
  * - DidAddress 本身以 inline 形式渲染 (方便探测 did-address 的 full-width)
13
13
  * - 组件 mounted 时记录 did address 的 full-width (非 compact 模式的宽度)
14
14
  * - 监听容器宽度变化, 当容器宽度变化时, 对比容器宽度和 did address full-width, => 切换 compact 模式
15
- * - TODO: 初始化时, 在确定是否应该以 compact 模式渲染前, 隐藏显示, 避免闪烁问题
16
15
  */
17
16
  const ResponsiveDidAddress = forwardRef<HTMLDidAddressElement, IResponsiveDidAddressProps>(
18
- ({ style, className, component = 'span', ...rest }, ref) => {
17
+ ({ style, className, component = 'span', size, ...rest }, ref) => {
19
18
  const [compact, setCompact] = useState(false);
20
19
  const isCompact = useLatest(compact);
21
20
  // did address 完整显示时的宽度
22
21
  const [addressFullWidth, setAddressFullWidth] = useState<number | null | undefined>(null);
22
+ const [containerWidth, setContainerWidth] = useState<number | null | undefined>(null);
23
+ const [loading, setLoading] = useState(true);
24
+ const { run: delaySetLoadingFalse } = useDebounceFn(() => setLoading(false), {
25
+ wait: 50,
26
+ });
23
27
  const containerRef = useRef<HTMLDivElement>(null);
24
28
  const innerRef = useRef<HTMLDidAddressElement>(null);
25
- const size = useSize(containerRef);
26
- const containerWidth = size?.width || 0;
27
29
 
28
30
  useImperativeHandle(
29
31
  ref,
@@ -41,22 +43,32 @@ const ResponsiveDidAddress = forwardRef<HTMLDidAddressElement, IResponsiveDidAdd
41
43
  // 存储完整显示时 address 组件的宽度
42
44
  useEffect(() => {
43
45
  const innerWidth = innerRef.current?.offsetWidth || 0;
44
- let resizeObserver: ResizeObserver | null = null;
46
+ let innerObserver: ResizeObserver | null = null;
47
+ let containerObserver: ResizeObserver | null = null;
45
48
 
49
+ // 初始化宽度
46
50
  setAddressFullWidth(innerWidth);
51
+ setContainerWidth(containerRef.current?.offsetWidth);
47
52
 
48
53
  // 由于自定义字体的缘故,innerRef 的初始宽度可能发生二次改变,使用 observer 监听捕获
49
- resizeObserver = new ResizeObserver(() => {
54
+ innerObserver = new ResizeObserver(() => {
50
55
  if (!isCompact.current && innerRef.current?.offsetWidth !== innerWidth) {
51
56
  setAddressFullWidth(innerRef.current?.offsetWidth);
57
+ delaySetLoadingFalse();
52
58
  }
53
59
  });
60
+ containerObserver = new ResizeObserver(() => {
61
+ setContainerWidth(containerRef.current?.offsetWidth);
62
+ });
54
63
 
55
- if (innerRef.current) {
56
- resizeObserver.observe(innerRef.current.getEl());
57
- }
64
+ innerObserver.observe(innerRef.current!.getEl());
65
+ containerObserver.observe(containerRef.current!);
66
+ delaySetLoadingFalse();
58
67
 
59
- return () => resizeObserver.disconnect();
68
+ return () => {
69
+ innerObserver?.disconnect();
70
+ containerObserver?.disconnect();
71
+ };
60
72
  // eslint-disable-next-line react-hooks/exhaustive-deps
61
73
  }, []);
62
74
 
@@ -65,9 +77,23 @@ const ResponsiveDidAddress = forwardRef<HTMLDidAddressElement, IResponsiveDidAdd
65
77
  setCompact(containerWidth < addressFullWidth);
66
78
  }
67
79
  }, [containerWidth, addressFullWidth]);
80
+
68
81
  return (
69
82
  <Root as={component} ref={containerRef} style={style} className={className}>
70
- <StyledDidAddress {...rest} component={component} inline compact={compact} ref={innerRef} />
83
+ <StyledDidAddress
84
+ style={{
85
+ position: loading ? 'absolute' : 'static',
86
+ left: loading ? '-99999px' : 'auto',
87
+ }}
88
+ {...rest}
89
+ size={size}
90
+ component={component}
91
+ inline
92
+ compact={compact}
93
+ ref={innerRef}
94
+ />
95
+ {/* placeholder */}
96
+ {loading && <div style={{ width: '100%', height: `${(size || 16) * 1.5}px` }} />}
71
97
  </Root>
72
98
  );
73
99
  }
@@ -83,6 +109,8 @@ export interface IResponsiveDidAddressProps extends IDidAddressProps {
83
109
 
84
110
  const Root = styled(Box)`
85
111
  display: block;
112
+ width: 100%; /* flex & column & align-items: flex-start 时, 块级元素不会自动铺满容器 */
113
+ font-size: 0;
86
114
  overflow: hidden;
87
115
  ${({ inline }: any) =>
88
116
  inline &&