@rc-component/util 1.0.0 → 1.0.1
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/es/Children/toArray.js +3 -4
- package/es/Dom/contains.js +1 -1
- package/es/Dom/dynamicCSS.js +50 -60
- package/es/Dom/findDOMNode.js +3 -5
- package/es/Dom/focus.js +13 -21
- package/es/Dom/isVisible.js +10 -8
- package/es/Dom/scrollLocker.js +63 -89
- package/es/Dom/shadow.js +1 -2
- package/es/Dom/styleChecker.js +9 -9
- package/es/KeyCode.js +4 -2
- package/es/Portal.js +15 -16
- package/es/PortalWrapper.js +150 -175
- package/es/React/isFragment.js +4 -5
- package/es/React/render.js +32 -63
- package/es/composeProps.js +9 -16
- package/es/getScrollBarSize.js +19 -15
- package/es/hooks/useEffect.js +3 -5
- package/es/hooks/useEvent.js +2 -8
- package/es/hooks/useId.js +10 -23
- package/es/hooks/useLayoutEffect.js +8 -8
- package/es/hooks/useMemo.js +1 -1
- package/es/hooks/useMergedState.js +23 -34
- package/es/hooks/useMobile.js +3 -12
- package/es/hooks/useState.js +4 -13
- package/es/hooks/useSyncState.js +4 -14
- package/es/isEqual.js +9 -14
- package/es/isMobile.js +3 -3
- package/es/omit.js +2 -2
- package/es/pickAttrs.js +29 -17
- package/es/proxyObject.js +2 -2
- package/es/raf.js +12 -23
- package/es/ref.js +16 -30
- package/es/setStyle.js +8 -8
- package/es/test/domHook.js +19 -25
- package/es/utils/get.js +2 -2
- package/es/utils/set.js +19 -38
- package/es/warning.js +7 -11
- package/lib/Children/toArray.js +3 -4
- package/lib/Dom/contains.js +1 -1
- package/lib/Dom/dynamicCSS.js +50 -60
- package/lib/Dom/findDOMNode.js +3 -5
- package/lib/Dom/focus.js +13 -21
- package/lib/Dom/isVisible.js +12 -9
- package/lib/Dom/scrollLocker.js +64 -88
- package/lib/Dom/shadow.js +1 -2
- package/lib/Dom/styleChecker.js +9 -9
- package/lib/KeyCode.js +4 -2
- package/lib/Portal.js +15 -16
- package/lib/PortalWrapper.js +154 -177
- package/lib/React/isFragment.js +4 -5
- package/lib/React/render.js +34 -65
- package/lib/composeProps.js +9 -16
- package/lib/getScrollBarSize.js +19 -15
- package/lib/hooks/useEffect.js +5 -8
- package/lib/hooks/useEvent.js +4 -11
- package/lib/hooks/useId.js +12 -25
- package/lib/hooks/useLayoutEffect.js +11 -11
- package/lib/hooks/useMemo.js +3 -4
- package/lib/hooks/useMergedState.js +24 -35
- package/lib/hooks/useMobile.js +3 -12
- package/lib/hooks/useState.js +6 -16
- package/lib/hooks/useSyncState.js +6 -17
- package/lib/index.js +8 -8
- package/lib/isEqual.js +9 -14
- package/lib/isMobile.js +5 -4
- package/lib/omit.js +2 -2
- package/lib/pickAttrs.js +29 -17
- package/lib/proxyObject.js +2 -2
- package/lib/raf.js +12 -23
- package/lib/ref.js +23 -31
- package/lib/setStyle.js +8 -8
- package/lib/test/domHook.js +19 -25
- package/lib/utils/get.js +2 -2
- package/lib/utils/set.js +19 -38
- package/lib/warning.js +8 -11
- package/package.json +3 -3
package/es/getScrollBarSize.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
/* eslint-disable no-param-reassign */
|
|
2
2
|
import { removeCSS, updateCSS } from "./Dom/dynamicCSS";
|
|
3
|
-
|
|
3
|
+
let cached;
|
|
4
4
|
function measureScrollbarSize(ele) {
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
const randomId = `rc-scrollbar-measure-${Math.random().toString(36).substring(7)}`;
|
|
6
|
+
const measureEle = document.createElement('div');
|
|
7
7
|
measureEle.id = randomId;
|
|
8
8
|
|
|
9
9
|
// Create Style
|
|
10
|
-
|
|
10
|
+
const measureStyle = measureEle.style;
|
|
11
11
|
measureStyle.position = 'absolute';
|
|
12
12
|
measureStyle.left = '0';
|
|
13
13
|
measureStyle.top = '0';
|
|
@@ -16,23 +16,27 @@ function measureScrollbarSize(ele) {
|
|
|
16
16
|
measureStyle.overflow = 'scroll';
|
|
17
17
|
|
|
18
18
|
// Clone Style if needed
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
let fallbackWidth;
|
|
20
|
+
let fallbackHeight;
|
|
21
21
|
if (ele) {
|
|
22
|
-
|
|
22
|
+
const targetStyle = getComputedStyle(ele);
|
|
23
23
|
measureStyle.scrollbarColor = targetStyle.scrollbarColor;
|
|
24
24
|
measureStyle.scrollbarWidth = targetStyle.scrollbarWidth;
|
|
25
25
|
|
|
26
26
|
// Set Webkit style
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
27
|
+
const webkitScrollbarStyle = getComputedStyle(ele, '::-webkit-scrollbar');
|
|
28
|
+
const width = parseInt(webkitScrollbarStyle.width, 10);
|
|
29
|
+
const height = parseInt(webkitScrollbarStyle.height, 10);
|
|
30
30
|
|
|
31
31
|
// Try wrap to handle CSP case
|
|
32
32
|
try {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
updateCSS(
|
|
33
|
+
const widthStyle = width ? `width: ${webkitScrollbarStyle.width};` : '';
|
|
34
|
+
const heightStyle = height ? `height: ${webkitScrollbarStyle.height};` : '';
|
|
35
|
+
updateCSS(`
|
|
36
|
+
#${randomId}::-webkit-scrollbar {
|
|
37
|
+
${widthStyle}
|
|
38
|
+
${heightStyle}
|
|
39
|
+
}`, randomId);
|
|
36
40
|
} catch (e) {
|
|
37
41
|
// Can't wrap, just log error
|
|
38
42
|
console.error(e);
|
|
@@ -45,8 +49,8 @@ function measureScrollbarSize(ele) {
|
|
|
45
49
|
document.body.appendChild(measureEle);
|
|
46
50
|
|
|
47
51
|
// Measure. Get fallback style if provided
|
|
48
|
-
|
|
49
|
-
|
|
52
|
+
const scrollWidth = ele && fallbackWidth && !isNaN(fallbackWidth) ? fallbackWidth : measureEle.offsetWidth - measureEle.clientWidth;
|
|
53
|
+
const scrollHeight = ele && fallbackHeight && !isNaN(fallbackHeight) ? fallbackHeight : measureEle.offsetHeight - measureEle.clientHeight;
|
|
50
54
|
|
|
51
55
|
// Clean up
|
|
52
56
|
document.body.removeChild(measureEle);
|
package/es/hooks/useEffect.js
CHANGED
|
@@ -2,11 +2,9 @@ import * as React from 'react';
|
|
|
2
2
|
|
|
3
3
|
/** As `React.useEffect` but pass origin value in callback and not need care deps length change. */
|
|
4
4
|
export default function useEffect(callback, deps) {
|
|
5
|
-
|
|
6
|
-
React.useEffect(
|
|
7
|
-
if (deps.length !== prevRef.current.length || deps.some(
|
|
8
|
-
return dep !== prevRef.current[index];
|
|
9
|
-
})) {
|
|
5
|
+
const prevRef = React.useRef(deps);
|
|
6
|
+
React.useEffect(() => {
|
|
7
|
+
if (deps.length !== prevRef.current.length || deps.some((dep, index) => dep !== prevRef.current[index])) {
|
|
10
8
|
callback(prevRef.current);
|
|
11
9
|
}
|
|
12
10
|
prevRef.current = deps;
|
package/es/hooks/useEvent.js
CHANGED
|
@@ -2,15 +2,9 @@
|
|
|
2
2
|
/* eslint-disable react-hooks/exhaustive-deps */
|
|
3
3
|
import * as React from 'react';
|
|
4
4
|
function useEvent(callback) {
|
|
5
|
-
|
|
5
|
+
const fnRef = React.useRef();
|
|
6
6
|
fnRef.current = callback;
|
|
7
|
-
|
|
8
|
-
var _fnRef$current;
|
|
9
|
-
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
10
|
-
args[_key] = arguments[_key];
|
|
11
|
-
}
|
|
12
|
-
return (_fnRef$current = fnRef.current) === null || _fnRef$current === void 0 ? void 0 : _fnRef$current.call.apply(_fnRef$current, [fnRef].concat(args));
|
|
13
|
-
}, []);
|
|
7
|
+
const memoFn = React.useCallback((...args) => fnRef.current?.(...args), []);
|
|
14
8
|
return memoFn;
|
|
15
9
|
}
|
|
16
10
|
export default useEvent;
|
package/es/hooks/useId.js
CHANGED
|
@@ -1,22 +1,12 @@
|
|
|
1
|
-
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
2
|
-
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
3
|
-
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
4
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
5
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
6
|
-
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
7
|
-
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
8
|
-
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
9
|
-
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
10
|
-
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
11
|
-
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
12
|
-
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
13
1
|
import * as React from 'react';
|
|
14
2
|
function getUseId() {
|
|
15
3
|
// We need fully clone React function here to avoid webpack warning React 17 do not export `useId`
|
|
16
|
-
|
|
4
|
+
const fullClone = {
|
|
5
|
+
...React
|
|
6
|
+
};
|
|
17
7
|
return fullClone.useId;
|
|
18
8
|
}
|
|
19
|
-
|
|
9
|
+
let uuid = 0;
|
|
20
10
|
|
|
21
11
|
/** @private Note only worked in develop env. Not work in production. */
|
|
22
12
|
export function resetUuid() {
|
|
@@ -24,11 +14,11 @@ export function resetUuid() {
|
|
|
24
14
|
uuid = 0;
|
|
25
15
|
}
|
|
26
16
|
}
|
|
27
|
-
|
|
17
|
+
const useOriginId = getUseId();
|
|
28
18
|
export default useOriginId ?
|
|
29
19
|
// Use React `useId`
|
|
30
20
|
function useId(id) {
|
|
31
|
-
|
|
21
|
+
const reactId = useOriginId();
|
|
32
22
|
|
|
33
23
|
// Developer passed id is single source of truth
|
|
34
24
|
if (id) {
|
|
@@ -44,14 +34,11 @@ function useId(id) {
|
|
|
44
34
|
// Use compatible of `useId`
|
|
45
35
|
function useCompatId(id) {
|
|
46
36
|
// Inner id for accessibility usage. Only work in client side
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
setInnerId = _React$useState2[1];
|
|
51
|
-
React.useEffect(function () {
|
|
52
|
-
var nextId = uuid;
|
|
37
|
+
const [innerId, setInnerId] = React.useState('ssr-id');
|
|
38
|
+
React.useEffect(() => {
|
|
39
|
+
const nextId = uuid;
|
|
53
40
|
uuid += 1;
|
|
54
|
-
setInnerId(
|
|
41
|
+
setInnerId(`rc_unique_${nextId}`);
|
|
55
42
|
}, []);
|
|
56
43
|
|
|
57
44
|
// Developer passed id is single source of truth
|
|
@@ -4,23 +4,23 @@ import canUseDom from "../Dom/canUseDom";
|
|
|
4
4
|
/**
|
|
5
5
|
* Wrap `React.useLayoutEffect` which will not throw warning message in test env
|
|
6
6
|
*/
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
useInternalLayoutEffect(
|
|
7
|
+
const useInternalLayoutEffect = process.env.NODE_ENV !== 'test' && canUseDom() ? React.useLayoutEffect : React.useEffect;
|
|
8
|
+
const useLayoutEffect = (callback, deps) => {
|
|
9
|
+
const firstMountRef = React.useRef(true);
|
|
10
|
+
useInternalLayoutEffect(() => {
|
|
11
11
|
return callback(firstMountRef.current);
|
|
12
12
|
}, deps);
|
|
13
13
|
|
|
14
14
|
// We tell react that first mount has passed
|
|
15
|
-
useInternalLayoutEffect(
|
|
15
|
+
useInternalLayoutEffect(() => {
|
|
16
16
|
firstMountRef.current = false;
|
|
17
|
-
return
|
|
17
|
+
return () => {
|
|
18
18
|
firstMountRef.current = true;
|
|
19
19
|
};
|
|
20
20
|
}, []);
|
|
21
21
|
};
|
|
22
|
-
export
|
|
23
|
-
useLayoutEffect(
|
|
22
|
+
export const useLayoutUpdateEffect = (callback, deps) => {
|
|
23
|
+
useLayoutEffect(firstMount => {
|
|
24
24
|
if (!firstMount) {
|
|
25
25
|
return callback();
|
|
26
26
|
}
|
package/es/hooks/useMemo.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
export default function useMemo(getValue, condition, shouldUpdate) {
|
|
3
|
-
|
|
3
|
+
const cacheRef = React.useRef({});
|
|
4
4
|
if (!('value' in cacheRef.current) || shouldUpdate(cacheRef.current.condition, condition)) {
|
|
5
5
|
cacheRef.current.value = getValue();
|
|
6
6
|
cacheRef.current.condition = condition;
|
|
@@ -1,9 +1,3 @@
|
|
|
1
|
-
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
2
|
-
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
3
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
4
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
5
|
-
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
6
|
-
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
7
1
|
import useEvent from "./useEvent";
|
|
8
2
|
import { useLayoutUpdateEffect } from "./useLayoutEffect";
|
|
9
3
|
import useState from "./useState";
|
|
@@ -17,50 +11,45 @@ function hasValue(value) {
|
|
|
17
11
|
* Note that internal use rc-util `useState` hook.
|
|
18
12
|
*/
|
|
19
13
|
export default function useMergedState(defaultStateValue, option) {
|
|
20
|
-
|
|
21
|
-
defaultValue
|
|
22
|
-
value
|
|
23
|
-
onChange
|
|
24
|
-
postState
|
|
14
|
+
const {
|
|
15
|
+
defaultValue,
|
|
16
|
+
value,
|
|
17
|
+
onChange,
|
|
18
|
+
postState
|
|
19
|
+
} = option || {};
|
|
25
20
|
|
|
26
21
|
// ======================= Init =======================
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
setInnerValue = _useState2[1];
|
|
39
|
-
var mergedValue = value !== undefined ? value : innerValue;
|
|
40
|
-
var postMergedValue = postState ? postState(mergedValue) : mergedValue;
|
|
22
|
+
const [innerValue, setInnerValue] = useState(() => {
|
|
23
|
+
if (hasValue(value)) {
|
|
24
|
+
return value;
|
|
25
|
+
} else if (hasValue(defaultValue)) {
|
|
26
|
+
return typeof defaultValue === 'function' ? defaultValue() : defaultValue;
|
|
27
|
+
} else {
|
|
28
|
+
return typeof defaultStateValue === 'function' ? defaultStateValue() : defaultStateValue;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
const mergedValue = value !== undefined ? value : innerValue;
|
|
32
|
+
const postMergedValue = postState ? postState(mergedValue) : mergedValue;
|
|
41
33
|
|
|
42
34
|
// ====================== Change ======================
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
setPrevValue = _useState4[1];
|
|
48
|
-
useLayoutUpdateEffect(function () {
|
|
49
|
-
var prev = prevValue[0];
|
|
35
|
+
const onChangeFn = useEvent(onChange);
|
|
36
|
+
const [prevValue, setPrevValue] = useState([mergedValue]);
|
|
37
|
+
useLayoutUpdateEffect(() => {
|
|
38
|
+
const prev = prevValue[0];
|
|
50
39
|
if (innerValue !== prev) {
|
|
51
40
|
onChangeFn(innerValue, prev);
|
|
52
41
|
}
|
|
53
42
|
}, [prevValue]);
|
|
54
43
|
|
|
55
44
|
// Sync value back to `undefined` when it from control to un-control
|
|
56
|
-
useLayoutUpdateEffect(
|
|
45
|
+
useLayoutUpdateEffect(() => {
|
|
57
46
|
if (!hasValue(value)) {
|
|
58
47
|
setInnerValue(value);
|
|
59
48
|
}
|
|
60
49
|
}, [value]);
|
|
61
50
|
|
|
62
51
|
// ====================== Update ======================
|
|
63
|
-
|
|
52
|
+
const triggerChange = useEvent((updater, ignoreDestroy) => {
|
|
64
53
|
setInnerValue(updater, ignoreDestroy);
|
|
65
54
|
setPrevValue([mergedValue], ignoreDestroy);
|
|
66
55
|
});
|
package/es/hooks/useMobile.js
CHANGED
|
@@ -1,9 +1,3 @@
|
|
|
1
|
-
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
2
|
-
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
3
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
4
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
5
|
-
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
6
|
-
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
7
1
|
import { useState } from 'react';
|
|
8
2
|
import isMobile from "../isMobile";
|
|
9
3
|
import useLayoutEffect from "./useLayoutEffect";
|
|
@@ -12,12 +6,9 @@ import useLayoutEffect from "./useLayoutEffect";
|
|
|
12
6
|
* Hook to detect if the user is on a mobile device
|
|
13
7
|
* Notice that this hook will only detect the device type in effect, so it will always be false in server side
|
|
14
8
|
*/
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
mobile = _useState2[0],
|
|
19
|
-
setMobile = _useState2[1];
|
|
20
|
-
useLayoutEffect(function () {
|
|
9
|
+
const useMobile = () => {
|
|
10
|
+
const [mobile, setMobile] = useState(false);
|
|
11
|
+
useLayoutEffect(() => {
|
|
21
12
|
setMobile(isMobile());
|
|
22
13
|
}, []);
|
|
23
14
|
return mobile;
|
package/es/hooks/useState.js
CHANGED
|
@@ -1,9 +1,3 @@
|
|
|
1
|
-
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
2
|
-
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
3
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
4
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
5
|
-
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
6
|
-
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
7
1
|
import * as React from 'react';
|
|
8
2
|
/**
|
|
9
3
|
* Same as React.useState but `setState` accept `ignoreDestroy` param to not to setState after destroyed.
|
|
@@ -11,14 +5,11 @@ import * as React from 'react';
|
|
|
11
5
|
* Developer should confirm it's safe to ignore themselves.
|
|
12
6
|
*/
|
|
13
7
|
export default function useSafeState(defaultValue) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
value = _React$useState2[0],
|
|
18
|
-
setValue = _React$useState2[1];
|
|
19
|
-
React.useEffect(function () {
|
|
8
|
+
const destroyRef = React.useRef(false);
|
|
9
|
+
const [value, setValue] = React.useState(defaultValue);
|
|
10
|
+
React.useEffect(() => {
|
|
20
11
|
destroyRef.current = false;
|
|
21
|
-
return
|
|
12
|
+
return () => {
|
|
22
13
|
destroyRef.current = true;
|
|
23
14
|
};
|
|
24
15
|
}, []);
|
package/es/hooks/useSyncState.js
CHANGED
|
@@ -1,9 +1,3 @@
|
|
|
1
|
-
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
2
|
-
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
3
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
4
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
5
|
-
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
6
|
-
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
7
1
|
import * as React from 'react';
|
|
8
2
|
import useEvent from "./useEvent";
|
|
9
3
|
/**
|
|
@@ -12,16 +6,12 @@ import useEvent from "./useEvent";
|
|
|
12
6
|
* e.g. onTransitionEnd trigger multiple event at once will be merged state update in React.
|
|
13
7
|
*/
|
|
14
8
|
export default function useSyncState(defaultValue) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
_React$useReducer2 = _slicedToArray(_React$useReducer, 2),
|
|
19
|
-
forceUpdate = _React$useReducer2[1];
|
|
20
|
-
var currentValueRef = React.useRef(defaultValue);
|
|
21
|
-
var getValue = useEvent(function () {
|
|
9
|
+
const [, forceUpdate] = React.useReducer(x => x + 1, 0);
|
|
10
|
+
const currentValueRef = React.useRef(defaultValue);
|
|
11
|
+
const getValue = useEvent(() => {
|
|
22
12
|
return currentValueRef.current;
|
|
23
13
|
});
|
|
24
|
-
|
|
14
|
+
const setValue = useEvent(updater => {
|
|
25
15
|
currentValueRef.current = typeof updater === 'function' ? updater(currentValueRef.current) : updater;
|
|
26
16
|
forceUpdate();
|
|
27
17
|
});
|
package/es/isEqual.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
2
1
|
import warning from "./warning";
|
|
3
2
|
|
|
4
3
|
/**
|
|
@@ -8,13 +7,11 @@ import warning from "./warning";
|
|
|
8
7
|
* @param shallow shallow compare
|
|
9
8
|
* @returns
|
|
10
9
|
*/
|
|
11
|
-
function isEqual(obj1, obj2) {
|
|
12
|
-
var shallow = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
10
|
+
function isEqual(obj1, obj2, shallow = false) {
|
|
13
11
|
// https://github.com/mapbox/mapbox-gl-js/pull/5979/files#diff-fde7145050c47cc3a306856efd5f9c3016e86e859de9afbd02c879be5067e58f
|
|
14
|
-
|
|
15
|
-
function deepEqual(a, b) {
|
|
16
|
-
|
|
17
|
-
var circular = refSet.has(a);
|
|
12
|
+
const refSet = new Set();
|
|
13
|
+
function deepEqual(a, b, level = 1) {
|
|
14
|
+
const circular = refSet.has(a);
|
|
18
15
|
warning(!circular, 'Warning: There may be circular references');
|
|
19
16
|
if (circular) {
|
|
20
17
|
return false;
|
|
@@ -26,26 +23,24 @@ function isEqual(obj1, obj2) {
|
|
|
26
23
|
return false;
|
|
27
24
|
}
|
|
28
25
|
refSet.add(a);
|
|
29
|
-
|
|
26
|
+
const newLevel = level + 1;
|
|
30
27
|
if (Array.isArray(a)) {
|
|
31
28
|
if (!Array.isArray(b) || a.length !== b.length) {
|
|
32
29
|
return false;
|
|
33
30
|
}
|
|
34
|
-
for (
|
|
31
|
+
for (let i = 0; i < a.length; i++) {
|
|
35
32
|
if (!deepEqual(a[i], b[i], newLevel)) {
|
|
36
33
|
return false;
|
|
37
34
|
}
|
|
38
35
|
}
|
|
39
36
|
return true;
|
|
40
37
|
}
|
|
41
|
-
if (a && b &&
|
|
42
|
-
|
|
38
|
+
if (a && b && typeof a === 'object' && typeof b === 'object') {
|
|
39
|
+
const keys = Object.keys(a);
|
|
43
40
|
if (keys.length !== Object.keys(b).length) {
|
|
44
41
|
return false;
|
|
45
42
|
}
|
|
46
|
-
return keys.every(
|
|
47
|
-
return deepEqual(a[key], b[key], newLevel);
|
|
48
|
-
});
|
|
43
|
+
return keys.every(key => deepEqual(a[key], b[key], newLevel));
|
|
49
44
|
}
|
|
50
45
|
// other
|
|
51
46
|
return false;
|
package/es/isMobile.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export default (
|
|
1
|
+
export default (() => {
|
|
2
2
|
if (typeof navigator === 'undefined' || typeof window === 'undefined') {
|
|
3
3
|
return false;
|
|
4
4
|
}
|
|
5
|
-
|
|
6
|
-
return /(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino|android|ipad|playbook|silk/i.test(agent) || /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55\/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk\/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(agent
|
|
5
|
+
const agent = navigator.userAgent || navigator.vendor || window.opera;
|
|
6
|
+
return /(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino|android|ipad|playbook|silk/i.test(agent) || /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55\/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk\/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(agent?.substr(0, 4));
|
|
7
7
|
});
|
package/es/omit.js
CHANGED
package/es/pickAttrs.js
CHANGED
|
@@ -1,16 +1,27 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
const attributes = `accept acceptCharset accessKey action allowFullScreen allowTransparency
|
|
2
|
+
alt async autoComplete autoFocus autoPlay capture cellPadding cellSpacing challenge
|
|
3
|
+
charSet checked classID className colSpan cols content contentEditable contextMenu
|
|
4
|
+
controls coords crossOrigin data dateTime default defer dir disabled download draggable
|
|
5
|
+
encType form formAction formEncType formMethod formNoValidate formTarget frameBorder
|
|
6
|
+
headers height hidden high href hrefLang htmlFor httpEquiv icon id inputMode integrity
|
|
7
|
+
is keyParams keyType kind label lang list loop low manifest marginHeight marginWidth max maxLength media
|
|
8
|
+
mediaGroup method min minLength multiple muted name noValidate nonce open
|
|
9
|
+
optimum pattern placeholder poster preload radioGroup readOnly rel required
|
|
10
|
+
reversed role rowSpan rows sandbox scope scoped scrolling seamless selected
|
|
11
|
+
shape size sizes span spellCheck src srcDoc srcLang srcSet start step style
|
|
12
|
+
summary tabIndex target title type useMap value width wmode wrap`;
|
|
13
|
+
const eventsName = `onCopy onCut onPaste onCompositionEnd onCompositionStart onCompositionUpdate onKeyDown
|
|
14
|
+
onKeyPress onKeyUp onFocus onBlur onChange onInput onSubmit onClick onContextMenu onDoubleClick
|
|
15
|
+
onDrag onDragEnd onDragEnter onDragExit onDragLeave onDragOver onDragStart onDrop onMouseDown
|
|
16
|
+
onMouseEnter onMouseLeave onMouseMove onMouseOut onMouseOver onMouseUp onSelect onTouchCancel
|
|
17
|
+
onTouchEnd onTouchMove onTouchStart onScroll onWheel onAbort onCanPlay onCanPlayThrough
|
|
18
|
+
onDurationChange onEmptied onEncrypted onEnded onError onLoadedData onLoadedMetadata
|
|
19
|
+
onLoadStart onPause onPlay onPlaying onProgress onRateChange onSeeked onSeeking onStalled onSuspend onTimeUpdate onVolumeChange onWaiting onLoad onError`;
|
|
20
|
+
const propList = `${attributes} ${eventsName}`.split(/[\s\n]+/);
|
|
10
21
|
|
|
11
22
|
/* eslint-enable max-len */
|
|
12
|
-
|
|
13
|
-
|
|
23
|
+
const ariaPrefix = 'aria-';
|
|
24
|
+
const dataPrefix = 'data-';
|
|
14
25
|
function match(key, prefix) {
|
|
15
26
|
return key.indexOf(prefix) === 0;
|
|
16
27
|
}
|
|
@@ -19,9 +30,8 @@ function match(key, prefix) {
|
|
|
19
30
|
* @param props Passed props
|
|
20
31
|
* @param ariaOnly boolean | { aria?: boolean; data?: boolean; attr?: boolean; } filter config
|
|
21
32
|
*/
|
|
22
|
-
export default function pickAttrs(props) {
|
|
23
|
-
|
|
24
|
-
var mergedConfig;
|
|
33
|
+
export default function pickAttrs(props, ariaOnly = false) {
|
|
34
|
+
let mergedConfig;
|
|
25
35
|
if (ariaOnly === false) {
|
|
26
36
|
mergedConfig = {
|
|
27
37
|
aria: true,
|
|
@@ -33,10 +43,12 @@ export default function pickAttrs(props) {
|
|
|
33
43
|
aria: true
|
|
34
44
|
};
|
|
35
45
|
} else {
|
|
36
|
-
mergedConfig =
|
|
46
|
+
mergedConfig = {
|
|
47
|
+
...ariaOnly
|
|
48
|
+
};
|
|
37
49
|
}
|
|
38
|
-
|
|
39
|
-
Object.keys(props).forEach(
|
|
50
|
+
const attrs = {};
|
|
51
|
+
Object.keys(props).forEach(key => {
|
|
40
52
|
if (
|
|
41
53
|
// Aria
|
|
42
54
|
mergedConfig.aria && (key === 'role' || match(key, ariaPrefix)) ||
|
package/es/proxyObject.js
CHANGED
|
@@ -4,13 +4,13 @@
|
|
|
4
4
|
export default function proxyObject(obj, extendProps) {
|
|
5
5
|
if (typeof Proxy !== 'undefined' && obj) {
|
|
6
6
|
return new Proxy(obj, {
|
|
7
|
-
get
|
|
7
|
+
get(target, prop) {
|
|
8
8
|
if (extendProps[prop]) {
|
|
9
9
|
return extendProps[prop];
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
// Proxy origin property
|
|
13
|
-
|
|
13
|
+
const originProp = target[prop];
|
|
14
14
|
return typeof originProp === 'function' ? originProp.bind(target) : originProp;
|
|
15
15
|
}
|
|
16
16
|
});
|
package/es/raf.js
CHANGED
|
@@ -1,26 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
};
|
|
4
|
-
var caf = function caf(num) {
|
|
5
|
-
return clearTimeout(num);
|
|
6
|
-
};
|
|
1
|
+
let raf = callback => +setTimeout(callback, 16);
|
|
2
|
+
let caf = num => clearTimeout(num);
|
|
7
3
|
if (typeof window !== 'undefined' && 'requestAnimationFrame' in window) {
|
|
8
|
-
raf =
|
|
9
|
-
|
|
10
|
-
};
|
|
11
|
-
caf = function caf(handle) {
|
|
12
|
-
return window.cancelAnimationFrame(handle);
|
|
13
|
-
};
|
|
4
|
+
raf = callback => window.requestAnimationFrame(callback);
|
|
5
|
+
caf = handle => window.cancelAnimationFrame(handle);
|
|
14
6
|
}
|
|
15
|
-
|
|
16
|
-
|
|
7
|
+
let rafUUID = 0;
|
|
8
|
+
const rafIds = new Map();
|
|
17
9
|
function cleanup(id) {
|
|
18
10
|
rafIds.delete(id);
|
|
19
11
|
}
|
|
20
|
-
|
|
21
|
-
var times = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
|
|
12
|
+
const wrapperRaf = (callback, times = 1) => {
|
|
22
13
|
rafUUID += 1;
|
|
23
|
-
|
|
14
|
+
const id = rafUUID;
|
|
24
15
|
function callRef(leftTimes) {
|
|
25
16
|
if (leftTimes === 0) {
|
|
26
17
|
// Clean up
|
|
@@ -30,7 +21,7 @@ var wrapperRaf = function wrapperRaf(callback) {
|
|
|
30
21
|
callback();
|
|
31
22
|
} else {
|
|
32
23
|
// Next raf
|
|
33
|
-
|
|
24
|
+
const realId = raf(() => {
|
|
34
25
|
callRef(leftTimes - 1);
|
|
35
26
|
});
|
|
36
27
|
|
|
@@ -41,14 +32,12 @@ var wrapperRaf = function wrapperRaf(callback) {
|
|
|
41
32
|
callRef(times);
|
|
42
33
|
return id;
|
|
43
34
|
};
|
|
44
|
-
wrapperRaf.cancel =
|
|
45
|
-
|
|
35
|
+
wrapperRaf.cancel = id => {
|
|
36
|
+
const realId = rafIds.get(id);
|
|
46
37
|
cleanup(id);
|
|
47
38
|
return caf(realId);
|
|
48
39
|
};
|
|
49
40
|
if (process.env.NODE_ENV !== 'production') {
|
|
50
|
-
wrapperRaf.ids =
|
|
51
|
-
return rafIds;
|
|
52
|
-
};
|
|
41
|
+
wrapperRaf.ids = () => rafIds;
|
|
53
42
|
}
|
|
54
43
|
export default wrapperRaf;
|