@arcblock/react-hooks 2.13.70 → 3.0.0

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/lib/index.js CHANGED
@@ -1,4 +1,10 @@
1
- export { default as useInterval } from "./useInterval.js";
2
- export { default as useStorage } from "./useStorage.js";
3
- export { default as useBrowser } from "./useBrowser.js";
4
- export { default as useLineClamp } from "./useLineClamp.js";
1
+ import { default as a } from "./useInterval.js";
2
+ import { default as t } from "./useStorage.js";
3
+ import { default as f } from "./useBrowser.js";
4
+ import { default as l } from "./useLineClamp.js";
5
+ export {
6
+ f as useBrowser,
7
+ a as useInterval,
8
+ l as useLineClamp,
9
+ t as useStorage
10
+ };
package/lib/useBrowser.js CHANGED
@@ -1,63 +1,60 @@
1
- import { useState } from 'react';
2
- import isMobile from 'ismobilejs';
3
-
4
- const DID_WALLET_TAG = 'ABTWallet';
5
- const ARC_SPHERE_TAG = 'ArcSphere';
6
-
7
- function getUaVersion(userAgent, tag) {
8
- const reg = new RegExp(`${tag}/(?<version>[0-9.]+)`, 'g');
9
- const result = reg.exec(userAgent);
10
- const version = result?.groups?.version;
11
- return version;
1
+ import { useState as f } from "react";
2
+ import i from "ismobilejs";
3
+ const n = "ABTWallet", r = "ArcSphere";
4
+ function t(e, s) {
5
+ var l;
6
+ const o = new RegExp(`${s}/(?<version>[0-9.]+)`, "g").exec(e);
7
+ return (l = o == null ? void 0 : o.groups) == null ? void 0 : l.version;
12
8
  }
13
-
14
- export default function useBrowser() {
15
- const userAgent = window?.navigator?.userAgent;
16
- const [browser] = useState({
9
+ function b() {
10
+ var a;
11
+ const e = (a = window == null ? void 0 : window.navigator) == null ? void 0 : a.userAgent, [s] = f({
17
12
  // FIXME: @zhanghan 待应用中相关代码兼容 arcSphere 的判断后,才能移除 arcSphere 的判断
18
13
  // https://github.com/search?q=org%3AArcBlock%20browser.wallet&type=code
19
14
  // https://github.com/search?q=org%3Ablocklet+browser.wallet&type=code
20
- wallet: userAgent.indexOf(DID_WALLET_TAG) > -1 || userAgent.indexOf(ARC_SPHERE_TAG) > -1,
21
- walletVersion: getUaVersion(userAgent, DID_WALLET_TAG),
22
- arcSphere: userAgent.indexOf(ARC_SPHERE_TAG) > -1,
23
- arcSphereVersion: getUaVersion(userAgent, ARC_SPHERE_TAG),
24
- wechat: /MicroMessenger/i.test(userAgent),
15
+ wallet: e.indexOf(n) > -1 || e.indexOf(r) > -1,
16
+ walletVersion: t(e, n),
17
+ arcSphere: e.indexOf(r) > -1,
18
+ arcSphereVersion: t(e, r),
19
+ wechat: /MicroMessenger/i.test(e),
25
20
  mobile: {
26
21
  apple: {
27
- phone: false,
28
- ipod: false,
29
- tablet: false,
30
- device: false,
22
+ phone: !1,
23
+ ipod: !1,
24
+ tablet: !1,
25
+ device: !1
31
26
  },
32
27
  amazon: {
33
- phone: false,
34
- tablet: false,
35
- device: false,
28
+ phone: !1,
29
+ tablet: !1,
30
+ device: !1
36
31
  },
37
32
  android: {
38
- phone: false,
39
- tablet: false,
40
- device: false,
33
+ phone: !1,
34
+ tablet: !1,
35
+ device: !1
41
36
  },
42
37
  windows: {
43
- phone: false,
44
- tablet: false,
45
- device: false,
38
+ phone: !1,
39
+ tablet: !1,
40
+ device: !1
46
41
  },
47
42
  other: {
48
- blackberry: false,
49
- blackberry10: false,
50
- opera: false,
51
- firefox: false,
52
- chrome: false,
53
- device: false,
43
+ blackberry: !1,
44
+ blackberry10: !1,
45
+ opera: !1,
46
+ firefox: !1,
47
+ chrome: !1,
48
+ device: !1
54
49
  },
55
- phone: false,
56
- tablet: false,
57
- any: false,
58
- ...isMobile(userAgent),
59
- },
50
+ phone: !1,
51
+ tablet: !1,
52
+ any: !1,
53
+ ...i(e)
54
+ }
60
55
  });
61
-
62
- return browser;
56
+ return s;
63
57
  }
58
+ export {
59
+ b as default
60
+ };
@@ -1,22 +1,18 @@
1
- /* eslint-disable consistent-return */
2
- import { useEffect, useRef } from 'react';
3
-
4
- export default function useInterval(callback, delay) {
5
- const savedCallback = useRef();
6
-
7
- // Remember the latest callback.
8
- useEffect(() => {
9
- savedCallback.current = callback;
10
- }, [callback]);
11
-
12
- // Set up the interval.
13
- useEffect(() => {
14
- function tick() {
15
- savedCallback.current();
1
+ import { useRef as f, useEffect as r } from "react";
2
+ function o(e, t) {
3
+ const n = f();
4
+ r(() => {
5
+ n.current = e;
6
+ }, [e]), r(() => {
7
+ function u() {
8
+ n.current();
16
9
  }
17
- if (delay !== null) {
18
- const id = setInterval(tick, delay);
19
- return () => clearInterval(id);
10
+ if (t !== null) {
11
+ const c = setInterval(u, t);
12
+ return () => clearInterval(c);
20
13
  }
21
- }, [delay]);
14
+ }, [t]);
22
15
  }
16
+ export {
17
+ o as default
18
+ };
@@ -1,127 +1,95 @@
1
- import { useDebounceFn, useMemoizedFn, useMount, useReactive, useUpdateEffect } from "ahooks";
2
- import { useRef } from "react";
3
- const defaultEllipsis = "\u2026";
4
- let keyCount = 0;
5
- const getNewKey = () => `__clamp_text_key__${keyCount++}`;
6
- export default function useLineClamp({
7
- text,
8
- ellipsis = defaultEllipsis,
9
- lines = 3,
10
- expanded = false,
11
- debounceTime = 300,
12
- charWidth = 1.2,
13
- offset = 1
1
+ import { useReactive as z, useMemoizedFn as w, useDebounceFn as E, useMount as N, useUpdateEffect as F } from "ahooks";
2
+ import { useRef as k } from "react";
3
+ const B = "";
4
+ let D = 0;
5
+ const f = () => `__clamp_text_key__${D++}`;
6
+ function $({
7
+ text: o,
8
+ ellipsis: c = B,
9
+ lines: i = 3,
10
+ expanded: l = !1,
11
+ debounceTime: T = 300,
12
+ charWidth: u = 1.2,
13
+ offset: L = 1
14
14
  }) {
15
- const currentState = useReactive({
16
- needClamp: true,
17
- noClamp: false,
18
- clampedText: "\u2026",
19
- key: getNewKey()
20
- });
21
- const textRef = useRef();
22
- const buttonRef = useRef();
23
- const lineHeightRef = useRef(0);
24
- const clampLines = useMemoizedFn(({ lineHeight, originalText, expanded: expanded2, ellipsis: ellipsis2, lines: lines2 }) => {
25
- if (!textRef.current) {
15
+ const e = z({
16
+ needClamp: !0,
17
+ noClamp: !1,
18
+ clampedText: "",
19
+ key: f()
20
+ }), a = k(void 0), H = k(void 0), m = k(0), p = w(({ lineHeight: s, originalText: t, expanded: _, ellipsis: g, lines: x }) => {
21
+ if (!a.current)
26
22
  return;
27
- }
28
- const node = textRef.current;
29
- const button = buttonRef.current;
30
- if (!originalText || expanded2) {
31
- currentState.noClamp = true;
32
- currentState.clampedText = originalText;
33
- currentState.key = getNewKey();
23
+ const n = a.current, d = H.current;
24
+ if (!t || _) {
25
+ e.noClamp = !0, e.clampedText = t, e.key = f();
34
26
  return;
35
27
  }
36
- const maxHeight = lineHeight * lines2 + 1;
37
- let start = 0;
38
- let middle = 0;
39
- let end = originalText.length;
40
- if (!node.clientHeight) {
28
+ const M = s * x + 1;
29
+ let h = 0, r = 0, y = t.length;
30
+ if (!n.clientHeight)
41
31
  return;
32
+ function b() {
33
+ ((n == null ? void 0 : n.getBoundingClientRect().height) ?? 1) <= M ? h = r + 1 : y = r - 1;
42
34
  }
43
- function moveMarkers() {
44
- const clientHeight = node?.getBoundingClientRect().height ?? 1;
45
- if (clientHeight <= maxHeight) {
46
- start = middle + 1;
47
- } else {
48
- end = middle - 1;
49
- }
50
- }
51
- while (start <= end) {
52
- middle = Math.floor((start + end) / 2);
53
- node.innerText = originalText.slice(0, middle) + ellipsis2;
54
- if (button) {
55
- node.appendChild(button.cloneNode(true));
56
- }
57
- if (middle === originalText.length) {
58
- currentState.needClamp = false;
59
- currentState.noClamp = true;
60
- currentState.clampedText = originalText;
61
- currentState.key = getNewKey();
35
+ for (; h <= y; ) {
36
+ if (r = Math.floor((h + y) / 2), n.innerText = t.slice(0, r) + g, d && n.appendChild(d.cloneNode(!0)), r === t.length) {
37
+ e.needClamp = !1, e.noClamp = !0, e.clampedText = t, e.key = f();
62
38
  return;
63
39
  }
64
- moveMarkers();
40
+ b();
65
41
  }
66
- const clampedText = originalText.slice(0, Math.max(middle - offset, 0)).trim() + (typeof ellipsis2 === "string" ? ellipsis2 : "");
67
- node.innerText = clampedText;
68
- if (button)
69
- node.appendChild(button.cloneNode(true));
70
- currentState.needClamp = true;
71
- currentState.noClamp = false;
72
- currentState.clampedText = clampedText;
73
- currentState.key = getNewKey();
74
- });
75
- const { run: debouncedClampLines } = useDebounceFn(clampLines, {
76
- wait: debounceTime
77
- });
78
- const resizeListener = useMemoizedFn(() => {
79
- debouncedClampLines({
80
- lineHeight: lineHeightRef.current,
81
- originalText: text,
82
- expanded,
83
- ellipsis,
84
- lines,
85
- charWidth
42
+ const v = t.slice(0, Math.max(r - L, 0)).trim() + (typeof g == "string" ? g : "");
43
+ n.innerText = v, d && n.appendChild(d.cloneNode(!0)), e.needClamp = !0, e.noClamp = !1, e.clampedText = v, e.key = f();
44
+ }), { run: R } = E(p, {
45
+ wait: T
46
+ }), C = w(() => {
47
+ R({
48
+ lineHeight: m.current,
49
+ originalText: o,
50
+ expanded: l,
51
+ ellipsis: c,
52
+ lines: i,
53
+ charWidth: u
86
54
  });
87
55
  });
88
- useMount(() => {
89
- if (text && !lineHeightRef.current) {
90
- const lineHeight = (textRef.current?.clientHeight ?? 1) + 1;
91
- lineHeightRef.current = lineHeight;
92
- setTimeout(() => {
93
- clampLines({
94
- lineHeight,
95
- originalText: text,
96
- expanded,
97
- ellipsis,
98
- lines,
99
- charWidth
56
+ return N(() => {
57
+ var s;
58
+ if (o && !m.current) {
59
+ const t = (((s = a.current) == null ? void 0 : s.clientHeight) ?? 1) + 1;
60
+ m.current = t, setTimeout(() => {
61
+ p({
62
+ lineHeight: t,
63
+ originalText: o,
64
+ expanded: l,
65
+ ellipsis: c,
66
+ lines: i,
67
+ charWidth: u
100
68
  });
101
69
  });
102
70
  }
103
- window.addEventListener("resize", resizeListener);
104
- return () => {
105
- window.removeEventListener("resize", resizeListener);
71
+ return window.addEventListener("resize", C), () => {
72
+ window.removeEventListener("resize", C);
106
73
  };
107
- });
108
- useUpdateEffect(() => {
109
- clampLines({
110
- lineHeight: lineHeightRef.current,
111
- originalText: text,
112
- expanded,
113
- ellipsis,
114
- lines,
115
- charWidth
74
+ }), F(() => {
75
+ p({
76
+ lineHeight: m.current,
77
+ originalText: o,
78
+ expanded: l,
79
+ ellipsis: c,
80
+ lines: i,
81
+ charWidth: u
116
82
  });
117
- }, [expanded, text, charWidth, ellipsis, lines]);
118
- return {
119
- textRef,
120
- buttonRef,
121
- needClamp: currentState.needClamp,
122
- noClamp: currentState.noClamp,
123
- clampedText: currentState.clampedText,
124
- key: currentState.key,
125
- run: resizeListener
83
+ }, [l, o, u, c, i]), {
84
+ textRef: a,
85
+ buttonRef: H,
86
+ needClamp: e.needClamp,
87
+ noClamp: e.noClamp,
88
+ clampedText: e.clampedText,
89
+ key: e.key,
90
+ run: C
126
91
  };
127
92
  }
93
+ export {
94
+ $ as default
95
+ };
package/lib/useStorage.js CHANGED
@@ -1,39 +1,22 @@
1
- /* eslint-disable react-hooks/rules-of-hooks */
2
- import { useState, useEffect } from 'react';
3
- import { EventTarget } from 'event-target-shim';
4
-
5
- const evtTarget = new EventTarget();
6
- export default function useStorage(storage, keyPrefix) {
7
- return (key, defaultValue) => {
8
- const storeKey = `${keyPrefix}.${key}`;
9
- const raw = storage.getItem(storeKey);
10
- const [value, setValue] = useState(raw ? JSON.parse(raw) : defaultValue);
11
-
12
- const updater = (updatedValue) => {
13
- setValue(updatedValue);
14
- storage.setItem(storeKey, JSON.stringify(updatedValue));
15
- evtTarget.dispatchEvent(new CustomEvent('storage_change', { detail: { key } }));
1
+ import { useState as u, useEffect as p } from "react";
2
+ import { EventTarget as E } from "event-target-shim";
3
+ const a = new E();
4
+ function w(n, m) {
5
+ return (r, s) => {
6
+ const o = `${m}.${r}`, t = n.getItem(o), [f, c] = u(t ? JSON.parse(t) : s), i = (e) => {
7
+ c(e), n.setItem(o, JSON.stringify(e)), a.dispatchEvent(new CustomEvent("storage_change", { detail: { key: r } }));
16
8
  };
17
-
18
- if (defaultValue != null && !raw) {
19
- updater(defaultValue);
20
- }
21
-
22
- useEffect(() => {
23
- const listener = ({ detail }) => {
24
- if (detail.key === key) {
25
- const _raw = storage.getItem(storeKey);
26
-
27
- if (_raw !== raw) {
28
- setValue(JSON.parse(_raw));
29
- }
9
+ return s != null && !t && i(s), p(() => {
10
+ const e = ({ detail: v }) => {
11
+ if (v.key === r) {
12
+ const g = n.getItem(o);
13
+ g !== t && c(JSON.parse(g));
30
14
  }
31
15
  };
32
-
33
- evtTarget.addEventListener('storage_change', listener);
34
- return () => evtTarget.removeEventListener('storage_change', listener);
35
- });
36
-
37
- return [value, updater];
16
+ return a.addEventListener("storage_change", e), () => a.removeEventListener("storage_change", e);
17
+ }), [f, i];
38
18
  };
39
19
  }
20
+ export {
21
+ w as default
22
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arcblock/react-hooks",
3
- "version": "2.13.70",
3
+ "version": "3.0.0",
4
4
  "description": "React hooks used by arcblock products",
5
5
  "keywords": [
6
6
  "react",
@@ -18,8 +18,8 @@
18
18
  "scripts": {
19
19
  "lint": "eslint src tests --ext js --ext jsx",
20
20
  "lint:fix": "npm run lint -- --fix",
21
- "build": "unbuild",
22
- "watch": "CONSOLA_LEVEL=1 nodemon -e .jsx,.js,.ts,.tsx -w src -x 'unbuild'",
21
+ "build": "vite build",
22
+ "watch": "vite build --watch",
23
23
  "precommit": "CI=1 npm run lint",
24
24
  "prepush": "CI=1 npm run lint",
25
25
  "prepublish": "npm run build",
@@ -30,7 +30,6 @@
30
30
  "url": "https://github.com/ArcBlock/ux/issues"
31
31
  },
32
32
  "dependencies": {
33
- "core-js": "^3.25.5",
34
33
  "event-target-shim": "^5.0.1",
35
34
  "ismobilejs": "^1.1.1"
36
35
  },
@@ -38,13 +37,8 @@
38
37
  "access": "public"
39
38
  },
40
39
  "devDependencies": {
41
- "@babel/cli": "^7.19.3",
42
- "@babel/core": "^7.19.3",
43
- "@babel/preset-env": "^7.19.3",
44
- "@babel/preset-react": "^7.18.6",
45
- "eslint-plugin-react-hooks": "^4.6.0",
46
- "jest": "^29.7.0",
47
- "unbuild": "^2.0.0"
40
+ "eslint-plugin-react-hooks": "^4.6.2",
41
+ "jest": "^29.7.0"
48
42
  },
49
- "gitHead": "b38a83a2c7d9a22e90bab369d8f61340db2abbd0"
43
+ "gitHead": "b001c32ca85d40d8a0784ca581bfd54d90550b24"
50
44
  }
@@ -34,8 +34,8 @@ export default function useLineClamp({
34
34
  key: getNewKey(),
35
35
  });
36
36
 
37
- const textRef = useRef<HTMLElement | null>();
38
- const buttonRef = useRef<HTMLElement | null>();
37
+ const textRef = useRef<HTMLElement | null>(undefined);
38
+ const buttonRef = useRef<HTMLElement | null>(undefined);
39
39
  const lineHeightRef = useRef(0);
40
40
 
41
41
  const clampLines = useMemoizedFn(({ lineHeight, originalText, expanded, ellipsis, lines }) => {
@@ -0,0 +1,25 @@
1
+ import { defineConfig } from 'vite';
2
+ import react from '@vitejs/plugin-react';
3
+ import noBundlePlugin from 'vite-plugin-no-bundle';
4
+ import fg from 'fast-glob';
5
+
6
+ export default defineConfig({
7
+ plugins: [
8
+ react({ jsxRuntime: 'automatic' }),
9
+ noBundlePlugin({
10
+ root: 'src',
11
+ copy: ['**/*.png', '**/*.gif', '**/*.jpg', '**/*.jpeg', '**/*.d.ts'],
12
+ }),
13
+ ],
14
+ build: {
15
+ lib: {
16
+ entry: fg.sync('src/**/*.{tsx,ts,jsx,js}', {
17
+ ignore: ['**/stories/**', '**/demo/**', '**/*.d.ts', '**/*.stories.*'],
18
+ }),
19
+ formats: ['es'],
20
+ fileName: (format, entryName) => `${entryName}.js`,
21
+ },
22
+ outDir: 'lib',
23
+ emptyOutDir: true,
24
+ },
25
+ });
package/lib/index.d.ts DELETED
@@ -1,4 +0,0 @@
1
- export { default as useInterval } from './useInterval';
2
- export { default as useStorage } from './useStorage';
3
- export { default as useBrowser } from './useBrowser';
4
- export { default as useLineClamp } from './useLineClamp';
@@ -1,42 +0,0 @@
1
- export default function useBrowser(): {
2
- wallet: boolean;
3
- walletVersion: string | undefined;
4
- arcSphere: boolean;
5
- arcSphereVersion: string | undefined;
6
- wechat: boolean;
7
- mobile: {
8
- apple: {
9
- phone: boolean;
10
- ipod: boolean;
11
- tablet: boolean;
12
- universal: boolean;
13
- device: boolean;
14
- };
15
- amazon: {
16
- phone: boolean;
17
- tablet: boolean;
18
- device: boolean;
19
- };
20
- android: {
21
- phone: boolean;
22
- tablet: boolean;
23
- device: boolean;
24
- };
25
- windows: {
26
- phone: boolean;
27
- tablet: boolean;
28
- device: boolean;
29
- };
30
- other: {
31
- blackberry: boolean;
32
- blackberry10: boolean;
33
- opera: boolean;
34
- firefox: boolean;
35
- chrome: boolean;
36
- device: boolean;
37
- };
38
- phone: boolean;
39
- tablet: boolean;
40
- any: boolean;
41
- };
42
- };
@@ -1 +0,0 @@
1
- export default function useInterval(callback: any, delay: any): void;
@@ -1,17 +0,0 @@
1
- export default function useLineClamp({ text, ellipsis, lines, expanded, debounceTime, charWidth, offset, }: {
2
- text: string;
3
- ellipsis?: string;
4
- lines?: number;
5
- expanded?: boolean;
6
- debounceTime?: number;
7
- charWidth?: number;
8
- offset?: number;
9
- }): {
10
- textRef: import("react").MutableRefObject<HTMLElement | null | undefined>;
11
- buttonRef: import("react").MutableRefObject<HTMLElement | null | undefined>;
12
- needClamp: boolean;
13
- noClamp: boolean;
14
- clampedText: string;
15
- key: string;
16
- run: (this: unknown) => void;
17
- };
@@ -1 +0,0 @@
1
- export default function useStorage(storage: any, keyPrefix: any): (key: any, defaultValue: any) => any[];