@bifrostui/utils 0.0.5
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 +21 -0
- package/README.md +13 -0
- package/dist/debounce.d.ts +1 -0
- package/dist/debounce.js +37 -0
- package/dist/domTarget.d.ts +7 -0
- package/dist/domTarget.js +42 -0
- package/dist/getBoundingClientRect/index.d.ts +1 -0
- package/dist/getBoundingClientRect/index.js +27 -0
- package/dist/getBoundingClientRect/index.miniapp.d.ts +2 -0
- package/dist/getBoundingClientRect/index.miniapp.js +43 -0
- package/dist/hex2rgba.d.ts +2 -0
- package/dist/hex2rgba.js +39 -0
- package/dist/hooks/index.d.ts +9 -0
- package/dist/hooks/index.js +60 -0
- package/dist/hooks/useDidMountEffect.d.ts +5 -0
- package/dist/hooks/useDidMountEffect.js +35 -0
- package/dist/hooks/useDomCss/index.d.ts +3 -0
- package/dist/hooks/useDomCss/index.js +48 -0
- package/dist/hooks/useDomCss/index.miniapp.d.ts +3 -0
- package/dist/hooks/useDomCss/index.miniapp.js +51 -0
- package/dist/hooks/useDomReady/index.d.ts +2 -0
- package/dist/hooks/useDomReady/index.js +41 -0
- package/dist/hooks/useDomReady/index.miniapp.d.ts +2 -0
- package/dist/hooks/useDomReady/index.miniapp.js +43 -0
- package/dist/hooks/useEventCallback.d.ts +6 -0
- package/dist/hooks/useEventCallback.js +42 -0
- package/dist/hooks/useForkRef.d.ts +2 -0
- package/dist/hooks/useForkRef.js +48 -0
- package/dist/hooks/useMemoizedFn.d.ts +3 -0
- package/dist/hooks/useMemoizedFn.js +55 -0
- package/dist/hooks/useSize.d.ts +7 -0
- package/dist/hooks/useSize.js +51 -0
- package/dist/hooks/useTouchEmulator.d.ts +1 -0
- package/dist/hooks/useTouchEmulator.js +111 -0
- package/dist/hooks/useValue.d.ts +12 -0
- package/dist/hooks/useValue.js +49 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +96 -0
- package/dist/isDev.d.ts +2 -0
- package/dist/isDev.js +26 -0
- package/dist/isMini.d.ts +4 -0
- package/dist/isMini.js +35 -0
- package/dist/isType.d.ts +1 -0
- package/dist/isType.js +31 -0
- package/dist/setRef.d.ts +2 -0
- package/dist/setRef.js +31 -0
- package/dist/throttle.d.ts +6 -0
- package/dist/throttle.js +52 -0
- package/dist/toArray.d.ts +6 -0
- package/dist/toArray.js +52 -0
- package/dist/touchBlocker.d.ts +11 -0
- package/dist/touchBlocker.js +108 -0
- package/dist/transitions.d.ts +72 -0
- package/dist/transitions.js +91 -0
- package/es/debounce.d.ts +1 -0
- package/es/debounce.js +15 -0
- package/es/domTarget.d.ts +7 -0
- package/es/domTarget.js +14 -0
- package/es/getBoundingClientRect/index.d.ts +1 -0
- package/es/getBoundingClientRect/index.js +3 -0
- package/es/getBoundingClientRect/index.miniapp.d.ts +2 -0
- package/es/getBoundingClientRect/index.miniapp.js +17 -0
- package/es/hex2rgba.d.ts +2 -0
- package/es/hex2rgba.js +25 -0
- package/es/hooks/index.d.ts +9 -0
- package/es/hooks/index.js +9 -0
- package/es/hooks/useDidMountEffect.d.ts +5 -0
- package/es/hooks/useDidMountEffect.js +11 -0
- package/es/hooks/useDomCss/index.d.ts +3 -0
- package/es/hooks/useDomCss/index.js +14 -0
- package/es/hooks/useDomCss/index.miniapp.d.ts +3 -0
- package/es/hooks/useDomCss/index.miniapp.js +17 -0
- package/es/hooks/useDomReady/index.d.ts +2 -0
- package/es/hooks/useDomReady/index.js +7 -0
- package/es/hooks/useDomReady/index.miniapp.d.ts +2 -0
- package/es/hooks/useDomReady/index.miniapp.js +9 -0
- package/es/hooks/useEventCallback.d.ts +6 -0
- package/es/hooks/useEventCallback.js +18 -0
- package/es/hooks/useForkRef.d.ts +2 -0
- package/es/hooks/useForkRef.js +25 -0
- package/es/hooks/useMemoizedFn.d.ts +3 -0
- package/es/hooks/useMemoizedFn.js +25 -0
- package/es/hooks/useSize.d.ts +7 -0
- package/es/hooks/useSize.js +29 -0
- package/es/hooks/useTouchEmulator.d.ts +1 -0
- package/es/hooks/useTouchEmulator.js +146 -0
- package/es/hooks/useValue.d.ts +12 -0
- package/es/hooks/useValue.js +36 -0
- package/es/index.d.ts +12 -0
- package/es/index.js +12 -0
- package/es/isDev.d.ts +2 -0
- package/es/isDev.js +2 -0
- package/es/isMini.d.ts +4 -0
- package/es/isMini.js +4 -0
- package/es/isType.d.ts +1 -0
- package/es/isType.js +4 -0
- package/es/setRef.d.ts +2 -0
- package/es/setRef.js +7 -0
- package/es/throttle.d.ts +6 -0
- package/es/throttle.js +49 -0
- package/es/toArray.d.ts +6 -0
- package/es/toArray.js +19 -0
- package/es/touchBlocker.d.ts +11 -0
- package/es/touchBlocker.js +90 -0
- package/es/transitions.d.ts +72 -0
- package/es/transitions.js +65 -0
- package/package.json +32 -0
@@ -0,0 +1,29 @@
|
|
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
|
+
import React from 'react';
|
8
|
+
import useDomReady from "./useDomReady";
|
9
|
+
import getBoundingClientRect from "../getBoundingClientRect";
|
10
|
+
import { getTargetElement } from "../domTarget";
|
11
|
+
function useSize(target) {
|
12
|
+
var _React$useState = React.useState({}),
|
13
|
+
_React$useState2 = _slicedToArray(_React$useState, 2),
|
14
|
+
state = _React$useState2[0],
|
15
|
+
setState = _React$useState2[1];
|
16
|
+
useDomReady(function () {
|
17
|
+
var el = getTargetElement(target);
|
18
|
+
if (el) {
|
19
|
+
getBoundingClientRect(el).then(function (res) {
|
20
|
+
setState({
|
21
|
+
width: res.width,
|
22
|
+
height: res.height
|
23
|
+
});
|
24
|
+
});
|
25
|
+
}
|
26
|
+
});
|
27
|
+
return state;
|
28
|
+
}
|
29
|
+
export default useSize;
|
@@ -0,0 +1 @@
|
|
1
|
+
export default function useTouchEmulator(dom?: HTMLElement | Window): void;
|
@@ -0,0 +1,146 @@
|
|
1
|
+
/**
|
2
|
+
* Emulate touch event
|
3
|
+
* Source:https://github.com/hammerjs/touchemulator
|
4
|
+
*/
|
5
|
+
import { useEffect } from 'react';
|
6
|
+
var eventTarget;
|
7
|
+
|
8
|
+
/**
|
9
|
+
* create an touch point
|
10
|
+
* @constructor
|
11
|
+
* @param target
|
12
|
+
* @param identifier
|
13
|
+
* @param pos
|
14
|
+
* @param deltaX
|
15
|
+
* @param deltaY
|
16
|
+
* @returns {Object} touchPoint
|
17
|
+
*/
|
18
|
+
var Touch = function Touch(target, identifier, pos, deltaX, deltaY) {
|
19
|
+
deltaX = deltaX || 0;
|
20
|
+
deltaY = deltaY || 0;
|
21
|
+
this.identifier = identifier;
|
22
|
+
this.target = target;
|
23
|
+
this.clientX = pos.clientX + deltaX;
|
24
|
+
this.clientY = pos.clientY + deltaY;
|
25
|
+
this.screenX = pos.screenX + deltaX;
|
26
|
+
this.screenY = pos.screenY + deltaY;
|
27
|
+
this.pageX = pos.pageX + deltaX;
|
28
|
+
this.pageY = pos.pageY + deltaY;
|
29
|
+
};
|
30
|
+
|
31
|
+
/**
|
32
|
+
* create empty touchlist with the methods
|
33
|
+
* @constructor
|
34
|
+
* @returns touchList
|
35
|
+
*/
|
36
|
+
function TouchList() {
|
37
|
+
var _this = this;
|
38
|
+
var touchList = [];
|
39
|
+
touchList.item = function (index) {
|
40
|
+
return _this[index] || null;
|
41
|
+
};
|
42
|
+
|
43
|
+
// specified by Mozilla
|
44
|
+
touchList.identifiedTouch = function (id) {
|
45
|
+
return _this[id + 1] || null;
|
46
|
+
};
|
47
|
+
return touchList;
|
48
|
+
}
|
49
|
+
|
50
|
+
/**
|
51
|
+
* only trigger touches when the left mousebutton has been pressed
|
52
|
+
* @param touchType
|
53
|
+
* @returns {Function}
|
54
|
+
*/
|
55
|
+
|
56
|
+
var initiated = false;
|
57
|
+
function onMouse(touchType) {
|
58
|
+
return function (ev) {
|
59
|
+
// prevent mouse events
|
60
|
+
|
61
|
+
if (ev.type === 'mousedown') {
|
62
|
+
initiated = true;
|
63
|
+
}
|
64
|
+
if (ev.type === 'mouseup') {
|
65
|
+
initiated = false;
|
66
|
+
}
|
67
|
+
if (ev.type === 'mousemove' && !initiated) {
|
68
|
+
return;
|
69
|
+
}
|
70
|
+
|
71
|
+
// The EventTarget on which the touch point started when it was first placed on the surface,
|
72
|
+
// even if the touch point has since moved outside the interactive area of that element.
|
73
|
+
// also, when the target doesnt exist anymore, we update it
|
74
|
+
if (ev.type === 'mousedown' || !eventTarget || eventTarget && !eventTarget.dispatchEvent) {
|
75
|
+
eventTarget = ev.target;
|
76
|
+
}
|
77
|
+
triggerTouch(touchType, ev);
|
78
|
+
|
79
|
+
// reset
|
80
|
+
if (ev.type === 'mouseup') {
|
81
|
+
eventTarget = null;
|
82
|
+
}
|
83
|
+
};
|
84
|
+
}
|
85
|
+
|
86
|
+
/**
|
87
|
+
* trigger a touch event
|
88
|
+
* @param eventName
|
89
|
+
* @param mouseEv
|
90
|
+
*/
|
91
|
+
function triggerTouch(eventName, mouseEv) {
|
92
|
+
var touchEvent = document.createEvent('Event');
|
93
|
+
touchEvent.initEvent(eventName, true, false);
|
94
|
+
touchEvent.altKey = mouseEv.altKey;
|
95
|
+
touchEvent.ctrlKey = mouseEv.ctrlKey;
|
96
|
+
touchEvent.metaKey = mouseEv.metaKey;
|
97
|
+
touchEvent.shiftKey = mouseEv.shiftKey;
|
98
|
+
touchEvent.touches = getActiveTouches(mouseEv);
|
99
|
+
touchEvent.targetTouches = getActiveTouches(mouseEv);
|
100
|
+
touchEvent.changedTouches = createTouchList(mouseEv);
|
101
|
+
eventTarget.dispatchEvent(touchEvent);
|
102
|
+
}
|
103
|
+
|
104
|
+
/**
|
105
|
+
* create a touchList based on the mouse event
|
106
|
+
* @param mouseEv
|
107
|
+
* @returns {TouchList}
|
108
|
+
*/
|
109
|
+
function createTouchList(mouseEv) {
|
110
|
+
var touchList = TouchList();
|
111
|
+
touchList.push(new Touch(eventTarget, 1, mouseEv, 0, 0));
|
112
|
+
return touchList;
|
113
|
+
}
|
114
|
+
|
115
|
+
/**
|
116
|
+
* receive all active touches
|
117
|
+
* @param mouseEv
|
118
|
+
* @returns {TouchList}
|
119
|
+
*/
|
120
|
+
function getActiveTouches(mouseEv) {
|
121
|
+
// empty list
|
122
|
+
if (mouseEv.type === 'mouseup') {
|
123
|
+
return TouchList();
|
124
|
+
}
|
125
|
+
return createTouchList(mouseEv);
|
126
|
+
}
|
127
|
+
export default function useTouchEmulator() {
|
128
|
+
var dom = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : window;
|
129
|
+
var touchStart = onMouse('touchstart');
|
130
|
+
var touchMove = onMouse('touchmove');
|
131
|
+
var touchEnd = onMouse('touchend');
|
132
|
+
useEffect(function () {
|
133
|
+
if (dom) {
|
134
|
+
dom.addEventListener('mousedown', touchStart, true);
|
135
|
+
dom.addEventListener('mousemove', touchMove, true);
|
136
|
+
dom.addEventListener('mouseup', touchEnd, true);
|
137
|
+
}
|
138
|
+
return function () {
|
139
|
+
if (dom) {
|
140
|
+
dom.removeEventListener('mousedown', touchStart, true);
|
141
|
+
dom.removeEventListener('mousemove', touchMove, true);
|
142
|
+
dom.removeEventListener('mouseup', touchEnd, true);
|
143
|
+
}
|
144
|
+
};
|
145
|
+
}, [dom]);
|
146
|
+
}
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import { ChangeEvent } from 'react';
|
2
|
+
type Options<T> = {
|
3
|
+
value?: T;
|
4
|
+
defaultValue: T;
|
5
|
+
onChange?: (e: ChangeEvent<HTMLElement>, data: Record<string, any>) => void;
|
6
|
+
config?: {
|
7
|
+
name?: string;
|
8
|
+
state?: string;
|
9
|
+
};
|
10
|
+
};
|
11
|
+
export default function useValue<T>(options: Options<T>): readonly [T, (e: any, val: any) => void];
|
12
|
+
export {};
|
@@ -0,0 +1,36 @@
|
|
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
|
+
import { useCallback, useRef, useState, useEffect } from 'react';
|
8
|
+
export default function useValue(options) {
|
9
|
+
var value = options.value,
|
10
|
+
defaultValue = options.defaultValue,
|
11
|
+
onChange = options.onChange,
|
12
|
+
_options$config = options.config,
|
13
|
+
config = _options$config === void 0 ? {} : _options$config;
|
14
|
+
var state = config.state,
|
15
|
+
name = config.name;
|
16
|
+
var _useState = useState(defaultValue),
|
17
|
+
_useState2 = _slicedToArray(_useState, 2),
|
18
|
+
internalValue = _useState2[0],
|
19
|
+
setInternalValue = _useState2[1];
|
20
|
+
var initialDefaultValue = useRef(defaultValue);
|
21
|
+
var isControlled = value !== undefined;
|
22
|
+
|
23
|
+
// 异常情况
|
24
|
+
useEffect(function () {
|
25
|
+
if (!isControlled && JSON.stringify(defaultValue) !== JSON.stringify(initialDefaultValue.current) && state && name) {
|
26
|
+
console.error(["BUI: A component is changing the default ".concat(state, " state of an uncontrolled ").concat(name, " after being initialized. ") + "To suppress this warning opt to use a controlled ".concat(name, ".")].join('\n'));
|
27
|
+
}
|
28
|
+
}, [defaultValue]);
|
29
|
+
var triggerChange = useCallback(function (e, val) {
|
30
|
+
setInternalValue(val);
|
31
|
+
onChange === null || onChange === void 0 || onChange(e, {
|
32
|
+
value: val
|
33
|
+
});
|
34
|
+
}, [onChange]);
|
35
|
+
return [isControlled ? value : internalValue, triggerChange];
|
36
|
+
}
|
package/es/index.d.ts
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
export { default as debounce } from './debounce';
|
2
|
+
export { default as convertHexToRGBA } from './hex2rgba';
|
3
|
+
export { useDidMountEffect, useEventCallback, useForkRef, useTouchEmulator, useValue, useDomReady, useSize, useDomCss, } from './hooks';
|
4
|
+
export { default as isDev } from './isDev';
|
5
|
+
export { isMini, isWeapp, isAlipay } from './isMini';
|
6
|
+
export { default as setRef } from './setRef';
|
7
|
+
export { default as throttle } from './throttle';
|
8
|
+
export { default as toArray } from './toArray';
|
9
|
+
export { default as blockTouch } from './touchBlocker';
|
10
|
+
export { easing, duration, getTransitionProps, createTransitions, } from './transitions';
|
11
|
+
export { default as getBoundingClientRect } from './getBoundingClientRect';
|
12
|
+
export * from './isType';
|
package/es/index.js
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
export { default as debounce } from "./debounce";
|
2
|
+
export { default as convertHexToRGBA } from "./hex2rgba";
|
3
|
+
export { useDidMountEffect, useEventCallback, useForkRef, useTouchEmulator, useValue, useDomReady, useSize, useDomCss } from "./hooks";
|
4
|
+
export { default as isDev } from "./isDev";
|
5
|
+
export { isMini, isWeapp, isAlipay } from "./isMini";
|
6
|
+
export { default as setRef } from "./setRef";
|
7
|
+
export { default as throttle } from "./throttle";
|
8
|
+
export { default as toArray } from "./toArray";
|
9
|
+
export { default as blockTouch } from "./touchBlocker";
|
10
|
+
export { easing, duration, getTransitionProps, createTransitions } from "./transitions";
|
11
|
+
export { default as getBoundingClientRect } from "./getBoundingClientRect";
|
12
|
+
export * from "./isType";
|
package/es/isDev.d.ts
ADDED
package/es/isDev.js
ADDED
package/es/isMini.d.ts
ADDED
package/es/isMini.js
ADDED
package/es/isType.d.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
export declare const isFunction: (fn: any) => boolean;
|
package/es/isType.js
ADDED
package/es/setRef.d.ts
ADDED
package/es/setRef.js
ADDED
package/es/throttle.d.ts
ADDED
package/es/throttle.js
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
export default function throttle(fn) {
|
2
|
+
var interval = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 200;
|
3
|
+
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {
|
4
|
+
leading: true,
|
5
|
+
trailing: false
|
6
|
+
};
|
7
|
+
// 1.记录上一次的开始时间
|
8
|
+
var _options$leading = options.leading,
|
9
|
+
leading = _options$leading === void 0 ? true : _options$leading,
|
10
|
+
_options$trailing = options.trailing,
|
11
|
+
trailing = _options$trailing === void 0 ? false : _options$trailing;
|
12
|
+
var lastTime = 0;
|
13
|
+
var timer = null;
|
14
|
+
|
15
|
+
// 2.事件触发时, 真正执行的函数
|
16
|
+
/* eslint no-underscore-dangle: 0 */
|
17
|
+
var _throttle = function _throttle() {
|
18
|
+
var _this = this;
|
19
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
20
|
+
args[_key] = arguments[_key];
|
21
|
+
}
|
22
|
+
// 2.1.获取当前事件触发时的时间
|
23
|
+
var nowTime = new Date().getTime();
|
24
|
+
if (!lastTime && !leading) lastTime = nowTime;
|
25
|
+
|
26
|
+
// 2.2.使用当前触发的时间和之前的时间间隔以及上一次开始的时间, 计算出还剩余多长事件需要去触发函数
|
27
|
+
var remainTime = interval - (nowTime - lastTime);
|
28
|
+
if (remainTime <= 0) {
|
29
|
+
if (timer) {
|
30
|
+
clearTimeout(timer);
|
31
|
+
timer = null;
|
32
|
+
}
|
33
|
+
|
34
|
+
// 2.3.真正触发函数
|
35
|
+
fn.apply(this, args);
|
36
|
+
// 2.4.保留上次触发的时间
|
37
|
+
lastTime = nowTime;
|
38
|
+
return;
|
39
|
+
}
|
40
|
+
if (trailing && !timer) {
|
41
|
+
timer = setTimeout(function () {
|
42
|
+
timer = null;
|
43
|
+
lastTime = !leading ? 0 : new Date().getTime();
|
44
|
+
fn.apply(_this, args);
|
45
|
+
}, remainTime);
|
46
|
+
}
|
47
|
+
};
|
48
|
+
return _throttle;
|
49
|
+
}
|
package/es/toArray.d.ts
ADDED
package/es/toArray.js
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
import React from 'react';
|
2
|
+
import { isFragment } from 'react-is';
|
3
|
+
export default function toArray(children) {
|
4
|
+
var option = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
5
|
+
var ret = [];
|
6
|
+
React.Children.forEach(children, function (child) {
|
7
|
+
if ((child === undefined || child === null) && !option.keepEmpty) {
|
8
|
+
return;
|
9
|
+
}
|
10
|
+
if (Array.isArray(child)) {
|
11
|
+
ret = ret.concat(toArray(child));
|
12
|
+
} else if (isFragment(child) && child.props) {
|
13
|
+
ret = ret.concat(toArray(child.props.children, option));
|
14
|
+
} else {
|
15
|
+
ret.push(child);
|
16
|
+
}
|
17
|
+
});
|
18
|
+
return ret;
|
19
|
+
}
|
@@ -0,0 +1,90 @@
|
|
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
|
+
/**
|
8
|
+
* author: 辉宇
|
9
|
+
* 为一个DOM元素添加滑动穿透防护
|
10
|
+
* 返回一个函数,调用可解除滑动穿透防护
|
11
|
+
*
|
12
|
+
* **这里滑动穿透防护的定义为不允许root外部的元素被touch类事件滑动(overflow)**
|
13
|
+
*
|
14
|
+
* @param root: HTMLElement
|
15
|
+
* @returns {(function(): void)|*}
|
16
|
+
*/
|
17
|
+
export default function blockTouch(root) {
|
18
|
+
var lastTouch;
|
19
|
+
var moveDirection;
|
20
|
+
var moveAxis;
|
21
|
+
var onTouchStart = function onTouchStart(e) {
|
22
|
+
var _e$touches = _slicedToArray(e.touches, 1);
|
23
|
+
lastTouch = _e$touches[0];
|
24
|
+
};
|
25
|
+
var shouldPrevent = function shouldPrevent(targetElement, axis, delta) {
|
26
|
+
var el = targetElement;
|
27
|
+
var needPrevent = true;
|
28
|
+
while (el) {
|
29
|
+
var _window$getComputedSt = window.getComputedStyle(el),
|
30
|
+
overflowY = _window$getComputedSt.overflowY,
|
31
|
+
overflowX = _window$getComputedSt.overflowX;
|
32
|
+
if (axis === 'y' && (overflowY === 'scroll' || overflowY === 'auto')) {
|
33
|
+
var dir = moveDirection || delta;
|
34
|
+
if (moveDirection === undefined) moveDirection = dir;
|
35
|
+
if (el.scrollTop !== 0 && dir < 0) {
|
36
|
+
needPrevent = false;
|
37
|
+
break;
|
38
|
+
}
|
39
|
+
// 当内部滚动元素高度有小数时,可能会出现小于号左右差1的情况导致算法失效
|
40
|
+
if (el.scrollTop + el.offsetHeight + 1 < el.scrollHeight && dir > 0) {
|
41
|
+
needPrevent = false;
|
42
|
+
break;
|
43
|
+
}
|
44
|
+
}
|
45
|
+
if (axis === 'x' && (overflowX === 'scroll' || overflowX === 'auto')) {
|
46
|
+
var _dir = moveDirection || delta;
|
47
|
+
if (moveDirection === undefined) moveDirection = _dir;
|
48
|
+
if (el.scrollLeft !== 0 && _dir < 0) {
|
49
|
+
needPrevent = false;
|
50
|
+
break;
|
51
|
+
}
|
52
|
+
if (el.scrollLeft + el.offsetWidth < el.scrollWidth && _dir > 0) {
|
53
|
+
needPrevent = false;
|
54
|
+
break;
|
55
|
+
}
|
56
|
+
}
|
57
|
+
if (el === root) break;
|
58
|
+
el = el.parentNode;
|
59
|
+
}
|
60
|
+
return needPrevent;
|
61
|
+
};
|
62
|
+
var onTouchMove = function onTouchMove(e) {
|
63
|
+
var currentTouch = e.touches[0];
|
64
|
+
if (!moveAxis) {
|
65
|
+
if (Math.abs(lastTouch.screenY - currentTouch.screenY) > Math.abs(lastTouch.screenX - currentTouch.screenX)) moveAxis = 'y';
|
66
|
+
if (Math.abs(lastTouch.screenY - currentTouch.screenY) < Math.abs(lastTouch.screenX - currentTouch.screenX)) moveAxis = 'x';
|
67
|
+
}
|
68
|
+
if (shouldPrevent(e.target, moveAxis, moveAxis === 'x' ? lastTouch.screenX - currentTouch.screenX : lastTouch.screenY - currentTouch.screenY) && !e.cancelable === false) e.preventDefault();
|
69
|
+
// setLastTouch(e.touches[0]);
|
70
|
+
};
|
71
|
+
var onTouchEnd = function onTouchEnd() {
|
72
|
+
lastTouch = undefined;
|
73
|
+
moveDirection = undefined;
|
74
|
+
moveAxis = undefined;
|
75
|
+
};
|
76
|
+
var onScroll = function onScroll(e) {
|
77
|
+
if (shouldPrevent(e.target, Math.abs(e.deltaX) > Math.abs(e.deltaY) ? 'x' : 'y', Math.abs(e.deltaX) > Math.abs(e.deltaY) ? e.deltaX : e.deltaY) && !e.cancelable === false) e.preventDefault();
|
78
|
+
moveDirection = undefined;
|
79
|
+
};
|
80
|
+
root.addEventListener('touchstart', onTouchStart);
|
81
|
+
root.addEventListener('touchmove', onTouchMove);
|
82
|
+
root.addEventListener('touchend', onTouchEnd);
|
83
|
+
root.addEventListener('wheel', onScroll);
|
84
|
+
return function () {
|
85
|
+
root.removeEventListener('touchstart', onTouchStart);
|
86
|
+
root.removeEventListener('touchmove', onTouchMove);
|
87
|
+
root.removeEventListener('touchend', onTouchEnd);
|
88
|
+
root.removeEventListener('wheel', onScroll);
|
89
|
+
};
|
90
|
+
}
|
@@ -0,0 +1,72 @@
|
|
1
|
+
import React from 'react';
|
2
|
+
interface IEasing {
|
3
|
+
easeInOut: string;
|
4
|
+
easeIn: string;
|
5
|
+
easeOut: string;
|
6
|
+
sharp: string;
|
7
|
+
}
|
8
|
+
interface IDuration {
|
9
|
+
shortest: number;
|
10
|
+
shorter: number;
|
11
|
+
short: number;
|
12
|
+
standard: number;
|
13
|
+
complex: number;
|
14
|
+
enteringScreen: number;
|
15
|
+
leavingScreen: number;
|
16
|
+
}
|
17
|
+
export declare const easing: IEasing;
|
18
|
+
export declare const duration: IDuration;
|
19
|
+
interface ComponentProps {
|
20
|
+
easing: string | {
|
21
|
+
enter?: string;
|
22
|
+
exit?: string;
|
23
|
+
} | undefined;
|
24
|
+
style: React.CSSProperties | undefined;
|
25
|
+
timeout: number | {
|
26
|
+
enter?: number;
|
27
|
+
exit?: number;
|
28
|
+
};
|
29
|
+
delay: number | {
|
30
|
+
enter?: number;
|
31
|
+
exit?: number;
|
32
|
+
};
|
33
|
+
}
|
34
|
+
interface Options {
|
35
|
+
mode: 'enter' | 'exit' | 'unmounted' | 'exited' | 'entering' | 'entered' | 'exiting';
|
36
|
+
}
|
37
|
+
interface TransitionOptions {
|
38
|
+
duration: string | number;
|
39
|
+
easing: string | undefined;
|
40
|
+
delay: string | number;
|
41
|
+
}
|
42
|
+
export declare function getTransitionProps(props: ComponentProps, options: Options): TransitionOptions;
|
43
|
+
interface IInputTransition {
|
44
|
+
easing: IEasing;
|
45
|
+
duration: IDuration;
|
46
|
+
delay: number;
|
47
|
+
}
|
48
|
+
type ITransitionProps = string | string[];
|
49
|
+
export declare function createTransitions(inputTransitions?: Partial<IInputTransition>): {
|
50
|
+
easing: {
|
51
|
+
easeInOut: string;
|
52
|
+
easeIn: string;
|
53
|
+
easeOut: string;
|
54
|
+
sharp: string;
|
55
|
+
};
|
56
|
+
duration: {
|
57
|
+
shortest: number;
|
58
|
+
shorter: number;
|
59
|
+
short: number;
|
60
|
+
standard: number;
|
61
|
+
complex: number;
|
62
|
+
enteringScreen: number;
|
63
|
+
leavingScreen: number;
|
64
|
+
};
|
65
|
+
delay?: number;
|
66
|
+
create: (props?: ITransitionProps, options?: Partial<{
|
67
|
+
duration: number | string;
|
68
|
+
easing: string;
|
69
|
+
delay: number | string;
|
70
|
+
}>) => string;
|
71
|
+
};
|
72
|
+
export {};
|
@@ -0,0 +1,65 @@
|
|
1
|
+
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; }
|
2
|
+
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; }
|
3
|
+
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; }
|
4
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
5
|
+
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); }
|
6
|
+
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); }
|
7
|
+
export var easing = {
|
8
|
+
easeInOut: 'cubic-bezier(0.4, 0, 0.2, 1)',
|
9
|
+
easeOut: 'cubic-bezier(0.0, 0, 0.2, 1)',
|
10
|
+
easeIn: 'cubic-bezier(0.4, 0, 1, 1)',
|
11
|
+
sharp: 'cubic-bezier(0.4, 0, 0.6, 1)'
|
12
|
+
};
|
13
|
+
export var duration = {
|
14
|
+
shortest: 150,
|
15
|
+
shorter: 200,
|
16
|
+
short: 250,
|
17
|
+
/** 标准 */
|
18
|
+
standard: 300,
|
19
|
+
/** 用在复杂动画场景 */
|
20
|
+
complex: 375,
|
21
|
+
enteringScreen: 225,
|
22
|
+
leavingScreen: 195
|
23
|
+
};
|
24
|
+
export function getTransitionProps(props, options) {
|
25
|
+
var _style$transitionDura, _style$transitionTimi, _style$transitionDela;
|
26
|
+
var timeout = props.timeout,
|
27
|
+
easingProps = props.easing,
|
28
|
+
_props$style = props.style,
|
29
|
+
style = _props$style === void 0 ? {} : _props$style,
|
30
|
+
delay = props.delay;
|
31
|
+
var mode = options.mode;
|
32
|
+
if (mode !== 'enter' && mode !== 'exit') mode = mode === 'entering' || mode === 'entered' ? 'enter' : 'exit';
|
33
|
+
return {
|
34
|
+
duration: (_style$transitionDura = style.transitionDuration) !== null && _style$transitionDura !== void 0 ? _style$transitionDura : typeof timeout === 'number' ? timeout : timeout[mode] || 0,
|
35
|
+
easing: (_style$transitionTimi = style.transitionTimingFunction) !== null && _style$transitionTimi !== void 0 ? _style$transitionTimi : _typeof(easingProps) === 'object' ? easingProps[mode] : easingProps,
|
36
|
+
delay: (_style$transitionDela = style.transitionDelay) !== null && _style$transitionDela !== void 0 ? _style$transitionDela : typeof delay === 'number' ? delay : delay[mode] || 0
|
37
|
+
};
|
38
|
+
}
|
39
|
+
function formatMs(milliseconds) {
|
40
|
+
return "".concat(Math.round(milliseconds), "ms");
|
41
|
+
}
|
42
|
+
export function createTransitions() {
|
43
|
+
var inputTransitions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
44
|
+
var mergedEasing = _objectSpread(_objectSpread({}, easing), inputTransitions.easing);
|
45
|
+
var mergedDuration = _objectSpread(_objectSpread({}, duration), inputTransitions.duration);
|
46
|
+
var create = function create() {
|
47
|
+
var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ['all'];
|
48
|
+
var options = arguments.length > 1 ? arguments[1] : undefined;
|
49
|
+
var _options$duration = options.duration,
|
50
|
+
durationOption = _options$duration === void 0 ? mergedDuration.standard : _options$duration,
|
51
|
+
_options$easing = options.easing,
|
52
|
+
easingOption = _options$easing === void 0 ? mergedEasing.easeInOut : _options$easing,
|
53
|
+
_options$delay = options.delay,
|
54
|
+
delay = _options$delay === void 0 ? 0 : _options$delay;
|
55
|
+
return (Array.isArray(props) ? props : [props]).map(function (animatedProp) {
|
56
|
+
return "".concat(animatedProp, " ").concat(typeof durationOption === 'string' ? durationOption : formatMs(durationOption), " ").concat(easingOption, " ").concat(typeof delay === 'string' ? delay : formatMs(delay));
|
57
|
+
}).join(',');
|
58
|
+
};
|
59
|
+
return _objectSpread(_objectSpread({
|
60
|
+
create: create
|
61
|
+
}, inputTransitions), {}, {
|
62
|
+
easing: mergedEasing,
|
63
|
+
duration: mergedDuration
|
64
|
+
});
|
65
|
+
}
|
package/package.json
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
{
|
2
|
+
"name": "@bifrostui/utils",
|
3
|
+
"version": "0.0.5",
|
4
|
+
"description": "BUI React utilities for building components.",
|
5
|
+
"main": "dist/index.js",
|
6
|
+
"module": "es/index.js",
|
7
|
+
"sideEffects": false,
|
8
|
+
"files": [
|
9
|
+
"es",
|
10
|
+
"dist"
|
11
|
+
],
|
12
|
+
"scripts": {
|
13
|
+
"build": "father build",
|
14
|
+
"test": "echo \"Error: run tests from root\" && exit 1"
|
15
|
+
},
|
16
|
+
"devDependencies": {
|
17
|
+
"father": "^4.1.8"
|
18
|
+
},
|
19
|
+
"dependencies": {
|
20
|
+
"react-is": "^18.0.0"
|
21
|
+
},
|
22
|
+
"peerDependencies": {
|
23
|
+
"@tarojs/taro": "^3.2.0",
|
24
|
+
"react": "^17.0.0 || ^18.0.0"
|
25
|
+
},
|
26
|
+
"license": "MIT",
|
27
|
+
"publishConfig": {
|
28
|
+
"access": "public",
|
29
|
+
"registry": "https://registry.npmjs.org/"
|
30
|
+
},
|
31
|
+
"gitHead": "f24f431d8291f7838d943eb887c970c7bb0ce5ec"
|
32
|
+
}
|