@arcblock/react-hooks 1.15.35 → 1.16.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.
- package/LICENSE +1 -1
- package/package.json +4 -3
- package/src/index.js +5 -0
- package/src/useBrowser.js +46 -0
- package/src/useInterval.js +22 -0
- package/src/useStorage.js +38 -0
package/LICENSE
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arcblock/react-hooks",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.16.2",
|
|
4
4
|
"description": "React hooks used by arcblock products",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -12,7 +12,8 @@
|
|
|
12
12
|
"license": "Apache-2.0",
|
|
13
13
|
"main": "lib/index.js",
|
|
14
14
|
"files": [
|
|
15
|
-
"lib"
|
|
15
|
+
"lib",
|
|
16
|
+
"src"
|
|
16
17
|
],
|
|
17
18
|
"repository": {
|
|
18
19
|
"type": "git",
|
|
@@ -48,5 +49,5 @@
|
|
|
48
49
|
"eslint-plugin-react-hooks": "^4.2.0",
|
|
49
50
|
"jest": "^24.1.0"
|
|
50
51
|
},
|
|
51
|
-
"gitHead": "
|
|
52
|
+
"gitHead": "b869c847f922a9e89158e7d3ba4219789e6d20d0"
|
|
52
53
|
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { useState } from 'react';
|
|
2
|
+
import isMobile from 'ismobilejs';
|
|
3
|
+
|
|
4
|
+
export default function useBrowser() {
|
|
5
|
+
const [browser] = useState({
|
|
6
|
+
wallet: navigator.userAgent.indexOf('ABTWallet') > -1,
|
|
7
|
+
wechat: /MicroMessenger/i.test(navigator.userAgent),
|
|
8
|
+
mobile: {
|
|
9
|
+
apple: {
|
|
10
|
+
phone: false,
|
|
11
|
+
ipod: false,
|
|
12
|
+
tablet: false,
|
|
13
|
+
device: false,
|
|
14
|
+
},
|
|
15
|
+
amazon: {
|
|
16
|
+
phone: false,
|
|
17
|
+
tablet: false,
|
|
18
|
+
device: false,
|
|
19
|
+
},
|
|
20
|
+
android: {
|
|
21
|
+
phone: false,
|
|
22
|
+
tablet: false,
|
|
23
|
+
device: false,
|
|
24
|
+
},
|
|
25
|
+
windows: {
|
|
26
|
+
phone: false,
|
|
27
|
+
tablet: false,
|
|
28
|
+
device: false,
|
|
29
|
+
},
|
|
30
|
+
other: {
|
|
31
|
+
blackberry: false,
|
|
32
|
+
blackberry10: false,
|
|
33
|
+
opera: false,
|
|
34
|
+
firefox: false,
|
|
35
|
+
chrome: false,
|
|
36
|
+
device: false,
|
|
37
|
+
},
|
|
38
|
+
phone: false,
|
|
39
|
+
tablet: false,
|
|
40
|
+
any: false,
|
|
41
|
+
...isMobile(navigator.userAgent),
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
return browser;
|
|
46
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
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();
|
|
16
|
+
}
|
|
17
|
+
if (delay !== null) {
|
|
18
|
+
const id = setInterval(tick, delay);
|
|
19
|
+
return () => clearInterval(id);
|
|
20
|
+
}
|
|
21
|
+
}, [delay]);
|
|
22
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { useState, useEffect } from 'react';
|
|
2
|
+
import { EventTarget } from 'event-target-shim';
|
|
3
|
+
|
|
4
|
+
const evtTarget = new EventTarget();
|
|
5
|
+
export default function useStorage(storage, keyPrefix) {
|
|
6
|
+
return (key, defaultValue) => {
|
|
7
|
+
const storeKey = `${keyPrefix}.${key}`;
|
|
8
|
+
const raw = storage.getItem(storeKey);
|
|
9
|
+
const [value, setValue] = useState(raw ? JSON.parse(raw) : defaultValue);
|
|
10
|
+
|
|
11
|
+
const updater = updatedValue => {
|
|
12
|
+
setValue(updatedValue);
|
|
13
|
+
storage.setItem(storeKey, JSON.stringify(updatedValue));
|
|
14
|
+
evtTarget.dispatchEvent(new CustomEvent('storage_change', { detail: { key } }));
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
if (defaultValue != null && !raw) {
|
|
18
|
+
updater(defaultValue);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
useEffect(() => {
|
|
22
|
+
const listener = ({ detail }) => {
|
|
23
|
+
if (detail.key === key) {
|
|
24
|
+
const _raw = storage.getItem(storeKey);
|
|
25
|
+
|
|
26
|
+
if (_raw !== raw) {
|
|
27
|
+
setValue(JSON.parse(_raw));
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
evtTarget.addEventListener('storage_change', listener);
|
|
33
|
+
return () => evtTarget.removeEventListener('storage_change', listener);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
return [value, updater];
|
|
37
|
+
};
|
|
38
|
+
}
|