@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
package/dist/toArray.js
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
var __create = Object.create;
|
2
|
+
var __defProp = Object.defineProperty;
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
7
|
+
var __export = (target, all) => {
|
8
|
+
for (var name in all)
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
10
|
+
};
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
13
|
+
for (let key of __getOwnPropNames(from))
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
16
|
+
}
|
17
|
+
return to;
|
18
|
+
};
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
25
|
+
mod
|
26
|
+
));
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
28
|
+
|
29
|
+
// src/toArray.ts
|
30
|
+
var toArray_exports = {};
|
31
|
+
__export(toArray_exports, {
|
32
|
+
default: () => toArray
|
33
|
+
});
|
34
|
+
module.exports = __toCommonJS(toArray_exports);
|
35
|
+
var import_react = __toESM(require("react"));
|
36
|
+
var import_react_is = require("react-is");
|
37
|
+
function toArray(children, option = {}) {
|
38
|
+
let ret = [];
|
39
|
+
import_react.default.Children.forEach(children, (child) => {
|
40
|
+
if ((child === void 0 || child === null) && !option.keepEmpty) {
|
41
|
+
return;
|
42
|
+
}
|
43
|
+
if (Array.isArray(child)) {
|
44
|
+
ret = ret.concat(toArray(child));
|
45
|
+
} else if ((0, import_react_is.isFragment)(child) && child.props) {
|
46
|
+
ret = ret.concat(toArray(child.props.children, option));
|
47
|
+
} else {
|
48
|
+
ret.push(child);
|
49
|
+
}
|
50
|
+
});
|
51
|
+
return ret;
|
52
|
+
}
|
@@ -0,0 +1,108 @@
|
|
1
|
+
var __defProp = Object.defineProperty;
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
5
|
+
var __export = (target, all) => {
|
6
|
+
for (var name in all)
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
8
|
+
};
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
11
|
+
for (let key of __getOwnPropNames(from))
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
14
|
+
}
|
15
|
+
return to;
|
16
|
+
};
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
18
|
+
|
19
|
+
// src/touchBlocker.ts
|
20
|
+
var touchBlocker_exports = {};
|
21
|
+
__export(touchBlocker_exports, {
|
22
|
+
default: () => blockTouch
|
23
|
+
});
|
24
|
+
module.exports = __toCommonJS(touchBlocker_exports);
|
25
|
+
function blockTouch(root) {
|
26
|
+
let lastTouch;
|
27
|
+
let moveDirection;
|
28
|
+
let moveAxis;
|
29
|
+
const onTouchStart = (e) => {
|
30
|
+
[lastTouch] = e.touches;
|
31
|
+
};
|
32
|
+
const shouldPrevent = (targetElement, axis, delta) => {
|
33
|
+
let el = targetElement;
|
34
|
+
let needPrevent = true;
|
35
|
+
while (el) {
|
36
|
+
const { overflowY, overflowX } = window.getComputedStyle(el);
|
37
|
+
if (axis === "y" && (overflowY === "scroll" || overflowY === "auto")) {
|
38
|
+
const dir = moveDirection || delta;
|
39
|
+
if (moveDirection === void 0)
|
40
|
+
moveDirection = dir;
|
41
|
+
if (el.scrollTop !== 0 && dir < 0) {
|
42
|
+
needPrevent = false;
|
43
|
+
break;
|
44
|
+
}
|
45
|
+
if (el.scrollTop + el.offsetHeight + 1 < el.scrollHeight && dir > 0) {
|
46
|
+
needPrevent = false;
|
47
|
+
break;
|
48
|
+
}
|
49
|
+
}
|
50
|
+
if (axis === "x" && (overflowX === "scroll" || overflowX === "auto")) {
|
51
|
+
const dir = moveDirection || delta;
|
52
|
+
if (moveDirection === void 0)
|
53
|
+
moveDirection = dir;
|
54
|
+
if (el.scrollLeft !== 0 && dir < 0) {
|
55
|
+
needPrevent = false;
|
56
|
+
break;
|
57
|
+
}
|
58
|
+
if (el.scrollLeft + el.offsetWidth < el.scrollWidth && dir > 0) {
|
59
|
+
needPrevent = false;
|
60
|
+
break;
|
61
|
+
}
|
62
|
+
}
|
63
|
+
if (el === root)
|
64
|
+
break;
|
65
|
+
el = el.parentNode;
|
66
|
+
}
|
67
|
+
return needPrevent;
|
68
|
+
};
|
69
|
+
const onTouchMove = (e) => {
|
70
|
+
const currentTouch = e.touches[0];
|
71
|
+
if (!moveAxis) {
|
72
|
+
if (Math.abs(lastTouch.screenY - currentTouch.screenY) > Math.abs(lastTouch.screenX - currentTouch.screenX))
|
73
|
+
moveAxis = "y";
|
74
|
+
if (Math.abs(lastTouch.screenY - currentTouch.screenY) < Math.abs(lastTouch.screenX - currentTouch.screenX))
|
75
|
+
moveAxis = "x";
|
76
|
+
}
|
77
|
+
if (shouldPrevent(
|
78
|
+
e.target,
|
79
|
+
moveAxis,
|
80
|
+
moveAxis === "x" ? lastTouch.screenX - currentTouch.screenX : lastTouch.screenY - currentTouch.screenY
|
81
|
+
) && !e.cancelable === false)
|
82
|
+
e.preventDefault();
|
83
|
+
};
|
84
|
+
const onTouchEnd = () => {
|
85
|
+
lastTouch = void 0;
|
86
|
+
moveDirection = void 0;
|
87
|
+
moveAxis = void 0;
|
88
|
+
};
|
89
|
+
const onScroll = (e) => {
|
90
|
+
if (shouldPrevent(
|
91
|
+
e.target,
|
92
|
+
Math.abs(e.deltaX) > Math.abs(e.deltaY) ? "x" : "y",
|
93
|
+
Math.abs(e.deltaX) > Math.abs(e.deltaY) ? e.deltaX : e.deltaY
|
94
|
+
) && !e.cancelable === false)
|
95
|
+
e.preventDefault();
|
96
|
+
moveDirection = void 0;
|
97
|
+
};
|
98
|
+
root.addEventListener("touchstart", onTouchStart);
|
99
|
+
root.addEventListener("touchmove", onTouchMove);
|
100
|
+
root.addEventListener("touchend", onTouchEnd);
|
101
|
+
root.addEventListener("wheel", onScroll);
|
102
|
+
return () => {
|
103
|
+
root.removeEventListener("touchstart", onTouchStart);
|
104
|
+
root.removeEventListener("touchmove", onTouchMove);
|
105
|
+
root.removeEventListener("touchend", onTouchEnd);
|
106
|
+
root.removeEventListener("wheel", onScroll);
|
107
|
+
};
|
108
|
+
}
|
@@ -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,91 @@
|
|
1
|
+
var __defProp = Object.defineProperty;
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
5
|
+
var __export = (target, all) => {
|
6
|
+
for (var name in all)
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
8
|
+
};
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
11
|
+
for (let key of __getOwnPropNames(from))
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
14
|
+
}
|
15
|
+
return to;
|
16
|
+
};
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
18
|
+
|
19
|
+
// src/transitions.ts
|
20
|
+
var transitions_exports = {};
|
21
|
+
__export(transitions_exports, {
|
22
|
+
createTransitions: () => createTransitions,
|
23
|
+
duration: () => duration,
|
24
|
+
easing: () => easing,
|
25
|
+
getTransitionProps: () => getTransitionProps
|
26
|
+
});
|
27
|
+
module.exports = __toCommonJS(transitions_exports);
|
28
|
+
var easing = {
|
29
|
+
easeInOut: "cubic-bezier(0.4, 0, 0.2, 1)",
|
30
|
+
easeOut: "cubic-bezier(0.0, 0, 0.2, 1)",
|
31
|
+
easeIn: "cubic-bezier(0.4, 0, 1, 1)",
|
32
|
+
sharp: "cubic-bezier(0.4, 0, 0.6, 1)"
|
33
|
+
};
|
34
|
+
var duration = {
|
35
|
+
shortest: 150,
|
36
|
+
shorter: 200,
|
37
|
+
short: 250,
|
38
|
+
/** 标准 */
|
39
|
+
standard: 300,
|
40
|
+
/** 用在复杂动画场景 */
|
41
|
+
complex: 375,
|
42
|
+
enteringScreen: 225,
|
43
|
+
leavingScreen: 195
|
44
|
+
};
|
45
|
+
function getTransitionProps(props, options) {
|
46
|
+
const { timeout, easing: easingProps, style = {}, delay } = props;
|
47
|
+
let { mode } = options;
|
48
|
+
if (mode !== "enter" && mode !== "exit")
|
49
|
+
mode = mode === "entering" || mode === "entered" ? "enter" : "exit";
|
50
|
+
return {
|
51
|
+
duration: style.transitionDuration ?? (typeof timeout === "number" ? timeout : timeout[mode] || 0),
|
52
|
+
easing: style.transitionTimingFunction ?? (typeof easingProps === "object" ? easingProps[mode] : easingProps),
|
53
|
+
delay: style.transitionDelay ?? (typeof delay === "number" ? delay : delay[mode] || 0)
|
54
|
+
};
|
55
|
+
}
|
56
|
+
function formatMs(milliseconds) {
|
57
|
+
return `${Math.round(milliseconds)}ms`;
|
58
|
+
}
|
59
|
+
function createTransitions(inputTransitions = {}) {
|
60
|
+
const mergedEasing = {
|
61
|
+
...easing,
|
62
|
+
...inputTransitions.easing
|
63
|
+
};
|
64
|
+
const mergedDuration = {
|
65
|
+
...duration,
|
66
|
+
...inputTransitions.duration
|
67
|
+
};
|
68
|
+
const create = (props = ["all"], options) => {
|
69
|
+
const {
|
70
|
+
duration: durationOption = mergedDuration.standard,
|
71
|
+
easing: easingOption = mergedEasing.easeInOut,
|
72
|
+
delay = 0
|
73
|
+
} = options;
|
74
|
+
return (Array.isArray(props) ? props : [props]).map(
|
75
|
+
(animatedProp) => `${animatedProp} ${typeof durationOption === "string" ? durationOption : formatMs(durationOption)} ${easingOption} ${typeof delay === "string" ? delay : formatMs(delay)}`
|
76
|
+
).join(",");
|
77
|
+
};
|
78
|
+
return {
|
79
|
+
create,
|
80
|
+
...inputTransitions,
|
81
|
+
easing: mergedEasing,
|
82
|
+
duration: mergedDuration
|
83
|
+
};
|
84
|
+
}
|
85
|
+
// Annotate the CommonJS export names for ESM import in node:
|
86
|
+
0 && (module.exports = {
|
87
|
+
createTransitions,
|
88
|
+
duration,
|
89
|
+
easing,
|
90
|
+
getTransitionProps
|
91
|
+
});
|
package/es/debounce.d.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
export default function debounce(func: any, wait: number, immediate?: boolean): (...args: any[]) => void;
|
package/es/debounce.js
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
export default function debounce(func, wait, immediate) {
|
2
|
+
var timeout;
|
3
|
+
return function () {
|
4
|
+
var _this = this;
|
5
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
6
|
+
args[_key] = arguments[_key];
|
7
|
+
}
|
8
|
+
if (immediate && !timeout) func.apply(this, args);
|
9
|
+
clearTimeout(timeout);
|
10
|
+
timeout = setTimeout(function () {
|
11
|
+
timeout = null;
|
12
|
+
if (!immediate) func.apply(_this, args);
|
13
|
+
}, wait);
|
14
|
+
};
|
15
|
+
}
|
@@ -0,0 +1,7 @@
|
|
1
|
+
import type { MutableRefObject } from 'react';
|
2
|
+
import type { TaroElement } from '@tarojs/runtime';
|
3
|
+
type TargetValue<T> = T | undefined | null;
|
4
|
+
type TargetType = HTMLElement | Element | TaroElement | Window | Document;
|
5
|
+
export type BasicTarget<T extends TargetType = Element> = (() => TargetValue<T>) | TargetValue<T> | MutableRefObject<TargetValue<T>>;
|
6
|
+
export declare function getTargetElement<T extends TargetType>(target: BasicTarget<T>, defaultElement?: T): T;
|
7
|
+
export {};
|
package/es/domTarget.js
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
export function getTargetElement(target, defaultElement) {
|
2
|
+
if (!target) {
|
3
|
+
return defaultElement;
|
4
|
+
}
|
5
|
+
var targetElement;
|
6
|
+
if (typeof target === 'function') {
|
7
|
+
targetElement = target();
|
8
|
+
} else if ('current' in target) {
|
9
|
+
targetElement = target.current;
|
10
|
+
} else {
|
11
|
+
targetElement = target;
|
12
|
+
}
|
13
|
+
return targetElement;
|
14
|
+
}
|
@@ -0,0 +1 @@
|
|
1
|
+
export default function getBoundingClientRect(ele: Element): Promise<DOMRect>;
|
@@ -0,0 +1,17 @@
|
|
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 Taro from '@tarojs/taro';
|
8
|
+
export default function getBoundingClientRect(ele) {
|
9
|
+
return new Promise(function (resolve) {
|
10
|
+
var query = Taro.createSelectorQuery();
|
11
|
+
query.select("#".concat(ele.uid)).boundingClientRect().exec(function (_ref) {
|
12
|
+
var _ref2 = _slicedToArray(_ref, 1),
|
13
|
+
res = _ref2[0];
|
14
|
+
resolve(res);
|
15
|
+
});
|
16
|
+
});
|
17
|
+
}
|
package/es/hex2rgba.d.ts
ADDED
package/es/hex2rgba.js
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
/*
|
2
|
+
* @Author: @跃知
|
3
|
+
* @Date: 2021-12-30 20:30:48
|
4
|
+
* @LastEditors: @跃知
|
5
|
+
* @LastEditTime: 2021-12-30 20:35:26
|
6
|
+
* @Description: 颜色处理
|
7
|
+
*/
|
8
|
+
var convertHexToRGBA = function convertHexToRGBA(hexCode) {
|
9
|
+
var opacity = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
|
10
|
+
var hex = hexCode.replace('#', '');
|
11
|
+
if (hex.length === 3) {
|
12
|
+
hex = "".concat(hex[0]).concat(hex[0]).concat(hex[1]).concat(hex[1]).concat(hex[2]).concat(hex[2]);
|
13
|
+
}
|
14
|
+
var r = parseInt(hex.substring(0, 2), 16);
|
15
|
+
var g = parseInt(hex.substring(2, 4), 16);
|
16
|
+
var b = parseInt(hex.substring(4, 6), 16);
|
17
|
+
var a = opacity;
|
18
|
+
|
19
|
+
/* Backward compatibility for whole number based opacity values. */
|
20
|
+
if (opacity > 1 && opacity <= 100) {
|
21
|
+
a = opacity / 100;
|
22
|
+
}
|
23
|
+
return "rgba(".concat(r, ",").concat(g, ",").concat(b, ",").concat(a, ")");
|
24
|
+
};
|
25
|
+
export default convertHexToRGBA;
|
@@ -0,0 +1,9 @@
|
|
1
|
+
import useDidMountEffect from './useDidMountEffect';
|
2
|
+
import useEventCallback from './useEventCallback';
|
3
|
+
import useForkRef from './useForkRef';
|
4
|
+
import useTouchEmulator from './useTouchEmulator';
|
5
|
+
import useValue from './useValue';
|
6
|
+
import useDomReady from './useDomReady';
|
7
|
+
import useSize from './useSize';
|
8
|
+
import useDomCss from './useDomCss';
|
9
|
+
export { useValue, useForkRef, useEventCallback, useDidMountEffect, useTouchEmulator, useDomReady, useSize, useDomCss, };
|
@@ -0,0 +1,9 @@
|
|
1
|
+
import useDidMountEffect from "./useDidMountEffect";
|
2
|
+
import useEventCallback from "./useEventCallback";
|
3
|
+
import useForkRef from "./useForkRef";
|
4
|
+
import useTouchEmulator from "./useTouchEmulator";
|
5
|
+
import useValue from "./useValue";
|
6
|
+
import useDomReady from "./useDomReady";
|
7
|
+
import useSize from "./useSize";
|
8
|
+
import useDomCss from "./useDomCss";
|
9
|
+
export { useValue, useForkRef, useEventCallback, useDidMountEffect, useTouchEmulator, useDomReady, useSize, useDomCss };
|
@@ -0,0 +1,11 @@
|
|
1
|
+
import { useEffect, useRef } from 'react';
|
2
|
+
/**
|
3
|
+
* skip inital run useEffect
|
4
|
+
*/
|
5
|
+
var useDidMountEffect = function useDidMountEffect(func, deps) {
|
6
|
+
var didMount = useRef(false);
|
7
|
+
useEffect(function () {
|
8
|
+
if (didMount.current) func();else didMount.current = true;
|
9
|
+
}, deps);
|
10
|
+
};
|
11
|
+
export default useDidMountEffect;
|
@@ -0,0 +1,14 @@
|
|
1
|
+
import useDomReady from "../useDomReady";
|
2
|
+
import { getTargetElement } from "../../domTarget";
|
3
|
+
function useDomCss(target, computedStyle, cb) {
|
4
|
+
useDomReady(function () {
|
5
|
+
var ele = getTargetElement(target);
|
6
|
+
var style = window.getComputedStyle(ele, null);
|
7
|
+
var res = {};
|
8
|
+
computedStyle.forEach(function (key) {
|
9
|
+
res[key] = style === null || style === void 0 ? void 0 : style[key];
|
10
|
+
});
|
11
|
+
cb === null || cb === void 0 || cb(res);
|
12
|
+
});
|
13
|
+
}
|
14
|
+
export default useDomCss;
|
@@ -0,0 +1,17 @@
|
|
1
|
+
import Taro from '@tarojs/taro';
|
2
|
+
import useDomReady from "../useDomReady";
|
3
|
+
import { getTargetElement } from "../../domTarget";
|
4
|
+
function useDomCss(target, computedStyle, cb) {
|
5
|
+
useDomReady(function () {
|
6
|
+
var ele = getTargetElement(target);
|
7
|
+
if (ele) {
|
8
|
+
var query = Taro.createSelectorQuery();
|
9
|
+
query.select("#".concat(ele === null || ele === void 0 ? void 0 : ele.uid)).fields({
|
10
|
+
computedStyle: computedStyle
|
11
|
+
}).exec(function (res) {
|
12
|
+
cb === null || cb === void 0 || cb(res === null || res === void 0 ? void 0 : res[0]);
|
13
|
+
});
|
14
|
+
}
|
15
|
+
});
|
16
|
+
}
|
17
|
+
export default useDomCss;
|
@@ -0,0 +1,18 @@
|
|
1
|
+
import React, { useLayoutEffect } from 'react';
|
2
|
+
|
3
|
+
/**
|
4
|
+
* https://github.com/facebook/react/issues/14099#issuecomment-440013892
|
5
|
+
*
|
6
|
+
* @param {function} fn
|
7
|
+
*/
|
8
|
+
export default function useEventCallback(fn) {
|
9
|
+
var ref = React.useRef(fn);
|
10
|
+
useLayoutEffect(function () {
|
11
|
+
ref.current = fn;
|
12
|
+
});
|
13
|
+
|
14
|
+
// @ts-ignore
|
15
|
+
return React.useCallback(function () {
|
16
|
+
return (0, ref.current).apply(void 0, arguments);
|
17
|
+
}, []);
|
18
|
+
}
|
@@ -0,0 +1,25 @@
|
|
1
|
+
import * as React from 'react';
|
2
|
+
import setRef from "../setRef";
|
3
|
+
export default function useForkRef() {
|
4
|
+
for (var _len = arguments.length, refs = new Array(_len), _key = 0; _key < _len; _key++) {
|
5
|
+
refs[_key] = arguments[_key];
|
6
|
+
}
|
7
|
+
/**
|
8
|
+
* This will create a new function if the refs passed to this hook change and are all defined.
|
9
|
+
* This means react will call the old forkRef with `null` and the new forkRef
|
10
|
+
* with the ref. Cleanup naturally emerges from this behavior.
|
11
|
+
*/
|
12
|
+
return React.useMemo(function () {
|
13
|
+
if (refs.every(function (ref) {
|
14
|
+
return ref == null;
|
15
|
+
})) {
|
16
|
+
return null;
|
17
|
+
}
|
18
|
+
return function (instance) {
|
19
|
+
refs.forEach(function (ref) {
|
20
|
+
setRef(ref, instance);
|
21
|
+
});
|
22
|
+
};
|
23
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
24
|
+
}, refs);
|
25
|
+
}
|
@@ -0,0 +1,25 @@
|
|
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
|
+
import { useMemo, useRef } from 'react';
|
3
|
+
import isDev from "../isDev";
|
4
|
+
import { isFunction } from "../isType";
|
5
|
+
export default function useMemoizedFn(fn) {
|
6
|
+
if (isDev) {
|
7
|
+
if (!isFunction(fn)) {
|
8
|
+
console.error("useMemoizedFn expected parameter is a function, got ".concat(_typeof(fn)));
|
9
|
+
}
|
10
|
+
}
|
11
|
+
var fnRef = useRef(fn);
|
12
|
+
fnRef.current = useMemo(function () {
|
13
|
+
return fn;
|
14
|
+
}, [fn]);
|
15
|
+
var memoizedFn = useRef();
|
16
|
+
if (!memoizedFn.current) {
|
17
|
+
memoizedFn.current = function () {
|
18
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
19
|
+
args[_key] = arguments[_key];
|
20
|
+
}
|
21
|
+
return fnRef.current.apply(this, args);
|
22
|
+
};
|
23
|
+
}
|
24
|
+
return memoizedFn.current;
|
25
|
+
}
|