@studiometa/js-toolkit 2.2.2 → 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Base/index.cjs +29 -37
- package/Base/index.d.ts +0 -8
- package/Base/index.js +18 -26
- package/Base/managers/EventsManager.cjs +35 -36
- package/Base/managers/EventsManager.js +35 -36
- package/Base/managers/OptionsManager.cjs +19 -13
- package/Base/managers/OptionsManager.js +19 -13
- package/Base/managers/RefsManager.cjs +8 -5
- package/Base/managers/RefsManager.js +8 -5
- package/Base/managers/ResponsiveOptionsManager.cjs +4 -1
- package/Base/managers/ResponsiveOptionsManager.js +4 -1
- package/Base/managers/ServicesManager.cjs +9 -7
- package/Base/managers/ServicesManager.js +9 -7
- package/Base/utils.cjs +22 -4
- package/Base/utils.d.ts +25 -8
- package/Base/utils.js +22 -4
- package/README.md +3 -3
- package/decorators/withBreakpointManager.cjs +15 -5
- package/decorators/withBreakpointManager.js +15 -5
- package/decorators/withBreakpointObserver.cjs +8 -2
- package/decorators/withBreakpointObserver.js +8 -2
- package/decorators/withVue2.cjs +10 -3
- package/decorators/withVue2.js +10 -3
- package/helpers/getDirectChildren.cjs +46 -0
- package/helpers/getDirectChildren.d.ts +24 -0
- package/helpers/getDirectChildren.js +24 -0
- package/helpers/importOnInteraction.cjs +2 -1
- package/helpers/importOnInteraction.js +2 -1
- package/helpers/importWhenVisible.cjs +2 -1
- package/helpers/importWhenVisible.js +2 -1
- package/helpers/index.cjs +2 -0
- package/helpers/index.d.ts +1 -0
- package/helpers/index.js +1 -0
- package/helpers/utils.cjs +3 -2
- package/helpers/utils.js +3 -2
- package/package.json +4 -1
- package/services/drag.cjs +126 -148
- package/services/drag.d.ts +10 -10
- package/services/drag.js +125 -148
- package/services/index.cjs +3 -1
- package/services/index.d.ts +1 -0
- package/services/index.js +3 -1
- package/services/key.cjs +48 -63
- package/services/key.d.ts +0 -8
- package/services/key.js +47 -63
- package/services/load.cjs +24 -40
- package/services/load.d.ts +0 -8
- package/services/load.js +23 -37
- package/services/pointer.cjs +73 -103
- package/services/pointer.d.ts +1 -9
- package/services/pointer.js +72 -100
- package/services/raf.cjs +38 -58
- package/services/raf.d.ts +0 -8
- package/services/raf.js +37 -55
- package/services/resize.cjs +59 -68
- package/services/resize.d.ts +0 -7
- package/services/resize.js +58 -68
- package/services/scroll.cjs +49 -71
- package/services/scroll.js +48 -71
- package/services/service.cjs +68 -0
- package/services/service.d.ts +18 -0
- package/services/service.js +46 -0
- package/utils/css/animate.cjs +31 -96
- package/utils/css/animate.d.ts +4 -16
- package/utils/css/animate.js +28 -93
- package/utils/css/classes.cjs +2 -1
- package/utils/css/classes.js +2 -1
- package/utils/css/styles.cjs +2 -5
- package/utils/css/styles.js +1 -1
- package/utils/css/transform.cjs +13 -16
- package/utils/css/transform.js +1 -1
- package/utils/css/transition.cjs +5 -3
- package/utils/css/transition.js +5 -3
- package/utils/focusTrap.cjs +2 -1
- package/utils/focusTrap.js +2 -1
- package/utils/{isFunction.cjs → has.cjs} +7 -7
- package/utils/has.d.ts +6 -0
- package/utils/has.js +6 -0
- package/utils/history.cjs +5 -7
- package/utils/history.js +4 -3
- package/utils/index.cjs +6 -5
- package/utils/index.d.ts +4 -2
- package/utils/index.js +6 -5
- package/utils/{object/isObject.cjs → is.cjs} +20 -8
- package/utils/is.d.ts +8 -0
- package/utils/is.js +18 -0
- package/utils/math/inertiaFinalValue.cjs +5 -3
- package/utils/math/inertiaFinalValue.js +2 -3
- package/utils/nextFrame.cjs +5 -3
- package/utils/nextFrame.js +5 -3
- package/utils/nextMicrotask.cjs +2 -1
- package/utils/nextMicrotask.js +2 -1
- package/utils/nextTick.cjs +2 -1
- package/utils/nextTick.js +2 -1
- package/utils/{isDefined.cjs → noop.cjs} +10 -7
- package/utils/noop.d.ts +12 -0
- package/utils/noop.js +9 -0
- package/utils/object/autoBind.cjs +3 -2
- package/utils/object/autoBind.js +3 -2
- package/utils/object/getAllProperties.cjs +2 -1
- package/utils/object/getAllProperties.js +2 -1
- package/utils/object/index.cjs +1 -3
- package/utils/object/index.d.ts +0 -1
- package/utils/object/index.js +1 -3
- package/utils/tween.cjs +114 -0
- package/utils/tween.d.ts +25 -0
- package/utils/tween.js +89 -0
- package/services/Service.cjs +0 -76
- package/services/Service.d.ts +0 -76
- package/services/Service.js +0 -55
- package/utils/isDefined.d.ts +0 -7
- package/utils/isDefined.js +0 -6
- package/utils/isFunction.d.ts +0 -7
- package/utils/isFunction.js +0 -6
- package/utils/object/isObject.d.ts +0 -7
- package/utils/object/isObject.js +0 -6
package/services/scroll.cjs
CHANGED
|
@@ -4,7 +4,6 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
5
|
var __getProtoOf = Object.getPrototypeOf;
|
|
6
6
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
7
|
var __export = (target, all) => {
|
|
9
8
|
for (var name in all)
|
|
10
9
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -19,10 +18,6 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
19
18
|
};
|
|
20
19
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
|
|
21
20
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
22
|
-
var __publicField = (obj, key, value) => {
|
|
23
|
-
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
24
|
-
return value;
|
|
25
|
-
};
|
|
26
21
|
|
|
27
22
|
// packages/js-toolkit/services/scroll.js
|
|
28
23
|
var scroll_exports = {};
|
|
@@ -30,12 +25,41 @@ __export(scroll_exports, {
|
|
|
30
25
|
default: () => useScroll
|
|
31
26
|
});
|
|
32
27
|
module.exports = __toCommonJS(scroll_exports);
|
|
33
|
-
var
|
|
28
|
+
var import_service = require("./service.cjs");
|
|
34
29
|
var import_debounce = __toESM(require("../utils/debounce.cjs"), 1);
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
30
|
+
function createScrollService() {
|
|
31
|
+
function updateProps() {
|
|
32
|
+
const yLast = props.y;
|
|
33
|
+
const xLast = props.x;
|
|
34
|
+
if (window.pageYOffset !== props.y) {
|
|
35
|
+
props.y = window.pageYOffset;
|
|
36
|
+
}
|
|
37
|
+
if (window.pageXOffset !== props.x) {
|
|
38
|
+
props.x = window.pageXOffset;
|
|
39
|
+
}
|
|
40
|
+
props.changed.x = props.x !== xLast;
|
|
41
|
+
props.changed.y = props.y !== yLast;
|
|
42
|
+
props.last.x = xLast;
|
|
43
|
+
props.last.y = yLast;
|
|
44
|
+
props.delta.x = props.x - xLast;
|
|
45
|
+
props.delta.y = props.y - yLast;
|
|
46
|
+
props.max.x = (document.scrollingElement || document.body).scrollWidth - window.innerWidth;
|
|
47
|
+
props.max.y = (document.scrollingElement || document.body).scrollHeight - window.innerHeight;
|
|
48
|
+
props.progress.x = props.max.x === 0 ? 1 : props.x / props.max.x;
|
|
49
|
+
props.progress.y = props.max.y === 0 ? 1 : props.y / props.max.y;
|
|
50
|
+
props.direction.x = props.x > xLast ? "RIGHT" : props.x < xLast ? "LEFT" : "NONE";
|
|
51
|
+
props.direction.y = props.y > yLast ? "DOWN" : props.y < yLast ? "UP" : "NONE";
|
|
52
|
+
return props;
|
|
53
|
+
}
|
|
54
|
+
const onScrollDebounced = (0, import_debounce.default)(() => {
|
|
55
|
+
trigger(updateProps());
|
|
56
|
+
}, 100);
|
|
57
|
+
function onScroll() {
|
|
58
|
+
trigger(updateProps());
|
|
59
|
+
onScrollDebounced();
|
|
60
|
+
}
|
|
61
|
+
const { add, remove, has, props, trigger } = (0, import_service.useService)({
|
|
62
|
+
props: {
|
|
39
63
|
x: window.pageXOffset,
|
|
40
64
|
y: window.pageYOffset,
|
|
41
65
|
changed: {
|
|
@@ -62,71 +86,25 @@ var Scroll = class extends import_Service.default {
|
|
|
62
86
|
x: "NONE",
|
|
63
87
|
y: "NONE"
|
|
64
88
|
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}
|
|
72
|
-
get debouncedHandler() {
|
|
73
|
-
const debounced = (0, import_debounce.default)(() => {
|
|
74
|
-
this.updateProps();
|
|
75
|
-
this.trigger(this.props);
|
|
76
|
-
}, 100);
|
|
77
|
-
Object.defineProperty(this, "debouncedHandler", {
|
|
78
|
-
value: debounced
|
|
79
|
-
});
|
|
80
|
-
return debounced;
|
|
81
|
-
}
|
|
82
|
-
handleEvent() {
|
|
83
|
-
this.updateProps();
|
|
84
|
-
this.trigger(this.props);
|
|
85
|
-
this.debouncedHandler();
|
|
86
|
-
}
|
|
87
|
-
kill() {
|
|
88
|
-
document.removeEventListener("scroll", this);
|
|
89
|
-
return this;
|
|
90
|
-
}
|
|
91
|
-
updateProps() {
|
|
92
|
-
const yLast = this.props.y;
|
|
93
|
-
const xLast = this.props.x;
|
|
94
|
-
if (window.pageYOffset !== this.props.y) {
|
|
95
|
-
this.props.y = window.pageYOffset;
|
|
89
|
+
},
|
|
90
|
+
init() {
|
|
91
|
+
document.addEventListener("scroll", onScroll, { passive: true, capture: true });
|
|
92
|
+
},
|
|
93
|
+
kill() {
|
|
94
|
+
document.removeEventListener("scroll", onScroll);
|
|
96
95
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
this.props.delta.y = this.props.y - yLast;
|
|
106
|
-
this.props.max.x = (document.scrollingElement || document.body).scrollWidth - window.innerWidth;
|
|
107
|
-
this.props.max.y = (document.scrollingElement || document.body).scrollHeight - window.innerHeight;
|
|
108
|
-
this.props.progress.x = this.props.max.x === 0 ? 1 : this.props.x / this.props.max.x;
|
|
109
|
-
this.props.progress.y = this.props.max.y === 0 ? 1 : this.props.y / this.props.max.y;
|
|
110
|
-
this.props.direction.x = this.props.x > xLast ? "RIGHT" : this.props.x < xLast ? "LEFT" : "NONE";
|
|
111
|
-
this.props.direction.y = this.props.y > yLast ? "DOWN" : this.props.y < yLast ? "UP" : "NONE";
|
|
112
|
-
return this.props;
|
|
113
|
-
}
|
|
114
|
-
};
|
|
115
|
-
var instance;
|
|
96
|
+
});
|
|
97
|
+
return {
|
|
98
|
+
add,
|
|
99
|
+
remove,
|
|
100
|
+
has,
|
|
101
|
+
props: () => props
|
|
102
|
+
};
|
|
103
|
+
}
|
|
116
104
|
var scroll;
|
|
117
105
|
function useScroll() {
|
|
118
106
|
if (!scroll) {
|
|
119
|
-
|
|
120
|
-
instance = new Scroll();
|
|
121
|
-
}
|
|
122
|
-
scroll = {
|
|
123
|
-
add: instance.add.bind(instance),
|
|
124
|
-
remove: instance.remove.bind(instance),
|
|
125
|
-
has: instance.has.bind(instance),
|
|
126
|
-
props() {
|
|
127
|
-
return instance.props;
|
|
128
|
-
}
|
|
129
|
-
};
|
|
107
|
+
scroll = createScrollService();
|
|
130
108
|
}
|
|
131
109
|
return scroll;
|
|
132
110
|
}
|
package/services/scroll.js
CHANGED
|
@@ -1,15 +1,38 @@
|
|
|
1
|
-
|
|
2
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
-
var __publicField = (obj, key, value) => {
|
|
4
|
-
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
|
-
return value;
|
|
6
|
-
};
|
|
7
|
-
import Service from "./Service.js";
|
|
1
|
+
import { useService } from "./service.js";
|
|
8
2
|
import debounce from "../utils/debounce.js";
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
3
|
+
function createScrollService() {
|
|
4
|
+
function updateProps() {
|
|
5
|
+
const yLast = props.y;
|
|
6
|
+
const xLast = props.x;
|
|
7
|
+
if (window.pageYOffset !== props.y) {
|
|
8
|
+
props.y = window.pageYOffset;
|
|
9
|
+
}
|
|
10
|
+
if (window.pageXOffset !== props.x) {
|
|
11
|
+
props.x = window.pageXOffset;
|
|
12
|
+
}
|
|
13
|
+
props.changed.x = props.x !== xLast;
|
|
14
|
+
props.changed.y = props.y !== yLast;
|
|
15
|
+
props.last.x = xLast;
|
|
16
|
+
props.last.y = yLast;
|
|
17
|
+
props.delta.x = props.x - xLast;
|
|
18
|
+
props.delta.y = props.y - yLast;
|
|
19
|
+
props.max.x = (document.scrollingElement || document.body).scrollWidth - window.innerWidth;
|
|
20
|
+
props.max.y = (document.scrollingElement || document.body).scrollHeight - window.innerHeight;
|
|
21
|
+
props.progress.x = props.max.x === 0 ? 1 : props.x / props.max.x;
|
|
22
|
+
props.progress.y = props.max.y === 0 ? 1 : props.y / props.max.y;
|
|
23
|
+
props.direction.x = props.x > xLast ? "RIGHT" : props.x < xLast ? "LEFT" : "NONE";
|
|
24
|
+
props.direction.y = props.y > yLast ? "DOWN" : props.y < yLast ? "UP" : "NONE";
|
|
25
|
+
return props;
|
|
26
|
+
}
|
|
27
|
+
const onScrollDebounced = debounce(() => {
|
|
28
|
+
trigger(updateProps());
|
|
29
|
+
}, 100);
|
|
30
|
+
function onScroll() {
|
|
31
|
+
trigger(updateProps());
|
|
32
|
+
onScrollDebounced();
|
|
33
|
+
}
|
|
34
|
+
const { add, remove, has, props, trigger } = useService({
|
|
35
|
+
props: {
|
|
13
36
|
x: window.pageXOffset,
|
|
14
37
|
y: window.pageYOffset,
|
|
15
38
|
changed: {
|
|
@@ -36,71 +59,25 @@ class Scroll extends Service {
|
|
|
36
59
|
x: "NONE",
|
|
37
60
|
y: "NONE"
|
|
38
61
|
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}
|
|
46
|
-
get debouncedHandler() {
|
|
47
|
-
const debounced = debounce(() => {
|
|
48
|
-
this.updateProps();
|
|
49
|
-
this.trigger(this.props);
|
|
50
|
-
}, 100);
|
|
51
|
-
Object.defineProperty(this, "debouncedHandler", {
|
|
52
|
-
value: debounced
|
|
53
|
-
});
|
|
54
|
-
return debounced;
|
|
55
|
-
}
|
|
56
|
-
handleEvent() {
|
|
57
|
-
this.updateProps();
|
|
58
|
-
this.trigger(this.props);
|
|
59
|
-
this.debouncedHandler();
|
|
60
|
-
}
|
|
61
|
-
kill() {
|
|
62
|
-
document.removeEventListener("scroll", this);
|
|
63
|
-
return this;
|
|
64
|
-
}
|
|
65
|
-
updateProps() {
|
|
66
|
-
const yLast = this.props.y;
|
|
67
|
-
const xLast = this.props.x;
|
|
68
|
-
if (window.pageYOffset !== this.props.y) {
|
|
69
|
-
this.props.y = window.pageYOffset;
|
|
70
|
-
}
|
|
71
|
-
if (window.pageXOffset !== this.props.x) {
|
|
72
|
-
this.props.x = window.pageXOffset;
|
|
62
|
+
},
|
|
63
|
+
init() {
|
|
64
|
+
document.addEventListener("scroll", onScroll, { passive: true, capture: true });
|
|
65
|
+
},
|
|
66
|
+
kill() {
|
|
67
|
+
document.removeEventListener("scroll", onScroll);
|
|
73
68
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
this.props.max.y = (document.scrollingElement || document.body).scrollHeight - window.innerHeight;
|
|
82
|
-
this.props.progress.x = this.props.max.x === 0 ? 1 : this.props.x / this.props.max.x;
|
|
83
|
-
this.props.progress.y = this.props.max.y === 0 ? 1 : this.props.y / this.props.max.y;
|
|
84
|
-
this.props.direction.x = this.props.x > xLast ? "RIGHT" : this.props.x < xLast ? "LEFT" : "NONE";
|
|
85
|
-
this.props.direction.y = this.props.y > yLast ? "DOWN" : this.props.y < yLast ? "UP" : "NONE";
|
|
86
|
-
return this.props;
|
|
87
|
-
}
|
|
69
|
+
});
|
|
70
|
+
return {
|
|
71
|
+
add,
|
|
72
|
+
remove,
|
|
73
|
+
has,
|
|
74
|
+
props: () => props
|
|
75
|
+
};
|
|
88
76
|
}
|
|
89
|
-
let instance;
|
|
90
77
|
let scroll;
|
|
91
78
|
function useScroll() {
|
|
92
79
|
if (!scroll) {
|
|
93
|
-
|
|
94
|
-
instance = new Scroll();
|
|
95
|
-
}
|
|
96
|
-
scroll = {
|
|
97
|
-
add: instance.add.bind(instance),
|
|
98
|
-
remove: instance.remove.bind(instance),
|
|
99
|
-
has: instance.has.bind(instance),
|
|
100
|
-
props() {
|
|
101
|
-
return instance.props;
|
|
102
|
-
}
|
|
103
|
-
};
|
|
80
|
+
scroll = createScrollService();
|
|
104
81
|
}
|
|
105
82
|
return scroll;
|
|
106
83
|
}
|
|
@@ -0,0 +1,68 @@
|
|
|
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
|
+
// packages/js-toolkit/services/service.js
|
|
20
|
+
var service_exports = {};
|
|
21
|
+
__export(service_exports, {
|
|
22
|
+
useService: () => useService
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(service_exports);
|
|
25
|
+
function useService(options) {
|
|
26
|
+
const callbacks = /* @__PURE__ */ new Map();
|
|
27
|
+
let isInit = false;
|
|
28
|
+
const { init, kill, props } = options;
|
|
29
|
+
function has(key) {
|
|
30
|
+
return callbacks.has(key);
|
|
31
|
+
}
|
|
32
|
+
function get(key) {
|
|
33
|
+
return callbacks.get(key);
|
|
34
|
+
}
|
|
35
|
+
function add(key, callback) {
|
|
36
|
+
if (has(key)) {
|
|
37
|
+
console.warn(`The key \`${key}\` has already been added.`);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
if (callbacks.size === 0 && !isInit) {
|
|
41
|
+
init();
|
|
42
|
+
isInit = true;
|
|
43
|
+
}
|
|
44
|
+
callbacks.set(key, callback);
|
|
45
|
+
}
|
|
46
|
+
function remove(key) {
|
|
47
|
+
callbacks.delete(key);
|
|
48
|
+
if (callbacks.size === 0 && isInit) {
|
|
49
|
+
kill();
|
|
50
|
+
isInit = false;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function trigger(p) {
|
|
54
|
+
callbacks.forEach(function forEachCallback(callback) {
|
|
55
|
+
callback(p);
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
callbacks,
|
|
60
|
+
props,
|
|
61
|
+
add,
|
|
62
|
+
remove,
|
|
63
|
+
has,
|
|
64
|
+
get,
|
|
65
|
+
trigger
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
if (module.exports.default) module.exports = module.exports.default;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Use a service.
|
|
3
|
+
* @template {Record<string, any>} T
|
|
4
|
+
* @param {{ init: () => void, kill: () => void, props: T }} options
|
|
5
|
+
*/
|
|
6
|
+
export function useService<T extends Record<string, any>>(options: {
|
|
7
|
+
init: () => void;
|
|
8
|
+
kill: () => void;
|
|
9
|
+
props: T;
|
|
10
|
+
}): {
|
|
11
|
+
callbacks: Map<string, (props: T) => any>;
|
|
12
|
+
props: T;
|
|
13
|
+
add: (key: string, callback: (props: T) => any) => void;
|
|
14
|
+
remove: (key: string) => void;
|
|
15
|
+
has: (key: string) => boolean;
|
|
16
|
+
get: (key: string) => (props: T) => any;
|
|
17
|
+
trigger: (p: T) => void;
|
|
18
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
function useService(options) {
|
|
2
|
+
const callbacks = /* @__PURE__ */ new Map();
|
|
3
|
+
let isInit = false;
|
|
4
|
+
const { init, kill, props } = options;
|
|
5
|
+
function has(key) {
|
|
6
|
+
return callbacks.has(key);
|
|
7
|
+
}
|
|
8
|
+
function get(key) {
|
|
9
|
+
return callbacks.get(key);
|
|
10
|
+
}
|
|
11
|
+
function add(key, callback) {
|
|
12
|
+
if (has(key)) {
|
|
13
|
+
console.warn(`The key \`${key}\` has already been added.`);
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
if (callbacks.size === 0 && !isInit) {
|
|
17
|
+
init();
|
|
18
|
+
isInit = true;
|
|
19
|
+
}
|
|
20
|
+
callbacks.set(key, callback);
|
|
21
|
+
}
|
|
22
|
+
function remove(key) {
|
|
23
|
+
callbacks.delete(key);
|
|
24
|
+
if (callbacks.size === 0 && isInit) {
|
|
25
|
+
kill();
|
|
26
|
+
isInit = false;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function trigger(p) {
|
|
30
|
+
callbacks.forEach(function forEachCallback(callback) {
|
|
31
|
+
callback(p);
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
callbacks,
|
|
36
|
+
props,
|
|
37
|
+
add,
|
|
38
|
+
remove,
|
|
39
|
+
has,
|
|
40
|
+
get,
|
|
41
|
+
trigger
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
export {
|
|
45
|
+
useService
|
|
46
|
+
};
|
package/utils/css/animate.cjs
CHANGED
|
@@ -25,17 +25,13 @@ __export(animate_exports, {
|
|
|
25
25
|
animate: () => animate
|
|
26
26
|
});
|
|
27
27
|
module.exports = __toCommonJS(animate_exports);
|
|
28
|
-
var import_easing = require("@motionone/easing");
|
|
29
28
|
var import_math = require("../math/index.cjs");
|
|
30
|
-
var
|
|
29
|
+
var import_is = require("../is.cjs");
|
|
31
30
|
var import_transform = __toESM(require("./transform.cjs"), 1);
|
|
32
|
-
var import_raf = __toESM(require("../../services/raf.cjs"), 1);
|
|
33
31
|
var import_scheduler = require("../scheduler.cjs");
|
|
32
|
+
var import_tween = require("../tween.cjs");
|
|
34
33
|
var id = 0;
|
|
35
34
|
var running = /* @__PURE__ */ new WeakMap();
|
|
36
|
-
var noop = () => {
|
|
37
|
-
};
|
|
38
|
-
var PROGRESS_PRECISION = 1e-4;
|
|
39
35
|
var CSSUnitConverter = {
|
|
40
36
|
"%": (value, getSizeRef) => value * getSizeRef() / 100,
|
|
41
37
|
vh: (value) => value * window.innerHeight / 100,
|
|
@@ -44,7 +40,7 @@ var CSSUnitConverter = {
|
|
|
44
40
|
vmax: (value) => value * Math.max(window.innerWidth, window.innerHeight) / 100
|
|
45
41
|
};
|
|
46
42
|
function getAnimationStepValue(val, getSizeRef) {
|
|
47
|
-
if (
|
|
43
|
+
if ((0, import_is.isNumber)(val)) {
|
|
48
44
|
return val;
|
|
49
45
|
}
|
|
50
46
|
if (!val[1] || !CSSUnitConverter[val[1]]) {
|
|
@@ -52,18 +48,6 @@ function getAnimationStepValue(val, getSizeRef) {
|
|
|
52
48
|
}
|
|
53
49
|
return CSSUnitConverter[val[1]](val[0], getSizeRef);
|
|
54
50
|
}
|
|
55
|
-
function linear(value) {
|
|
56
|
-
return value;
|
|
57
|
-
}
|
|
58
|
-
function normalizeEase(ease) {
|
|
59
|
-
if (!(0, import_isDefined.default)(ease)) {
|
|
60
|
-
return linear;
|
|
61
|
-
}
|
|
62
|
-
if (Array.isArray(ease)) {
|
|
63
|
-
return (0, import_easing.cubicBezier)(...ease);
|
|
64
|
-
}
|
|
65
|
-
return ease;
|
|
66
|
-
}
|
|
67
51
|
var generateTranslateRenderStrategy = (sizeRef) => (element, fromValue, toValue, progress) => (0, import_math.lerp)(getAnimationStepValue(fromValue != null ? fromValue : 0, () => element[sizeRef]), getAnimationStepValue(toValue != null ? toValue : 0, () => element[sizeRef]), progress);
|
|
68
52
|
var widthBasedTranslateRenderStrategy = generateTranslateRenderStrategy("offsetWidth");
|
|
69
53
|
var heightBasedTranslateRenderStrategy = generateTranslateRenderStrategy("offsetHeight");
|
|
@@ -91,18 +75,18 @@ function render(element, from, to, progress) {
|
|
|
91
75
|
import_scheduler.domScheduler.read(function renderRead() {
|
|
92
76
|
var _a, _b;
|
|
93
77
|
let opacity = false;
|
|
94
|
-
if ((0,
|
|
78
|
+
if ((0, import_is.isDefined)(from.opacity) || (0, import_is.isDefined)(to.opacity)) {
|
|
95
79
|
opacity = (0, import_math.map)(stepProgress, 0, 1, (_a = from.opacity) != null ? _a : 1, (_b = to.opacity) != null ? _b : 1);
|
|
96
80
|
} else if (element.style.opacity) {
|
|
97
81
|
opacity = "";
|
|
98
82
|
}
|
|
99
83
|
let transformOrigin = false;
|
|
100
|
-
if ((0,
|
|
84
|
+
if ((0, import_is.isDefined)(to.transformOrigin)) {
|
|
101
85
|
transformOrigin = to.transformOrigin;
|
|
102
86
|
} else if (element.style.transformOrigin) {
|
|
103
87
|
transformOrigin = "";
|
|
104
88
|
}
|
|
105
|
-
const props = Object.fromEntries(import_transform.TRANSFORM_PROPS.filter((name) => (0,
|
|
89
|
+
const props = Object.fromEntries(import_transform.TRANSFORM_PROPS.filter((name) => (0, import_is.isDefined)(from[name]) || (0, import_is.isDefined)(to[name])).map((name) => {
|
|
106
90
|
return [name, transformRenderStrategies[name](element, from[name], to[name], stepProgress)];
|
|
107
91
|
}));
|
|
108
92
|
import_scheduler.domScheduler.write(function renderWrite() {
|
|
@@ -117,91 +101,42 @@ function render(element, from, to, progress) {
|
|
|
117
101
|
});
|
|
118
102
|
}
|
|
119
103
|
function animate(element, keyframes, options = {}) {
|
|
120
|
-
var _a;
|
|
121
|
-
const raf = (0, import_raf.default)();
|
|
122
|
-
let progressValue = 0;
|
|
123
|
-
let easedProgress = 0;
|
|
124
|
-
if (!running.has(element)) {
|
|
125
|
-
running.set(element, /* @__PURE__ */ new Map());
|
|
126
|
-
}
|
|
127
|
-
const ease = normalizeEase(options.easing);
|
|
128
|
-
let duration = (_a = options.duration) != null ? _a : 1;
|
|
129
|
-
duration *= 1e3;
|
|
130
|
-
let startTime = performance.now();
|
|
131
|
-
let endTime = startTime + duration;
|
|
132
|
-
const key = `animate-${id}`;
|
|
133
|
-
id += 1;
|
|
134
|
-
const { onProgress = noop, onFinish = noop } = options;
|
|
135
|
-
let isRunning = false;
|
|
136
104
|
const keyframesCount = keyframes.length - 1;
|
|
137
105
|
const normalizedKeyframes = keyframes.map((keyframe, index) => {
|
|
138
|
-
var
|
|
106
|
+
var _a;
|
|
139
107
|
return {
|
|
140
108
|
...keyframe,
|
|
141
|
-
offset: (
|
|
142
|
-
easing: normalizeEase(keyframe.easing)
|
|
109
|
+
offset: (_a = keyframe.offset) != null ? _a : index / keyframesCount,
|
|
110
|
+
easing: (0, import_tween.normalizeEase)(keyframe.easing)
|
|
143
111
|
};
|
|
144
112
|
});
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
raf.remove(key);
|
|
113
|
+
if (!running.has(element)) {
|
|
114
|
+
running.set(element, /* @__PURE__ */ new Map());
|
|
148
115
|
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
}
|
|
153
|
-
progressValue = newProgress;
|
|
154
|
-
if (Math.abs(1 - progressValue) < PROGRESS_PRECISION) {
|
|
155
|
-
progressValue = 1;
|
|
156
|
-
pause();
|
|
157
|
-
import_scheduler.domScheduler.afterWrite(() => onFinish(progressValue, easedProgress));
|
|
158
|
-
}
|
|
159
|
-
easedProgress = ease(progressValue);
|
|
116
|
+
const key = `animate-${id}`;
|
|
117
|
+
id += 1;
|
|
118
|
+
function callback(progress) {
|
|
160
119
|
let toIndex = 0;
|
|
161
|
-
while (normalizedKeyframes[toIndex] && normalizedKeyframes[toIndex].offset <=
|
|
120
|
+
while (normalizedKeyframes[toIndex] && normalizedKeyframes[toIndex].offset <= progress && normalizedKeyframes[toIndex].offset !== 1) {
|
|
162
121
|
toIndex += 1;
|
|
163
122
|
}
|
|
164
|
-
render(element, normalizedKeyframes[toIndex - 1], normalizedKeyframes[toIndex],
|
|
165
|
-
onProgress(progressValue, easedProgress);
|
|
166
|
-
return progressValue;
|
|
167
|
-
}
|
|
168
|
-
function tick(props) {
|
|
169
|
-
if (!isRunning) {
|
|
170
|
-
raf.remove(key);
|
|
171
|
-
return;
|
|
172
|
-
}
|
|
173
|
-
progress((0, import_math.clamp01)((0, import_math.map)(props.time, startTime, endTime, 0, 1)));
|
|
123
|
+
render(element, normalizedKeyframes[toIndex - 1], normalizedKeyframes[toIndex], progress);
|
|
174
124
|
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
raf.add(key, tick);
|
|
189
|
-
}
|
|
190
|
-
function play() {
|
|
191
|
-
if (isRunning) {
|
|
192
|
-
return;
|
|
125
|
+
const controls = (0, import_tween.tween)(callback, {
|
|
126
|
+
...options,
|
|
127
|
+
onStart() {
|
|
128
|
+
if ((0, import_is.isFunction)(options.onStart)) {
|
|
129
|
+
options.onStart();
|
|
130
|
+
}
|
|
131
|
+
const runningKeys = running.get(element);
|
|
132
|
+
runningKeys.forEach((runningPause, runningKey) => {
|
|
133
|
+
runningPause();
|
|
134
|
+
runningKeys.delete(runningKey);
|
|
135
|
+
});
|
|
136
|
+
runningKeys.set(key, controls.pause);
|
|
137
|
+
running.set(element, runningKeys);
|
|
193
138
|
}
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
isRunning = true;
|
|
197
|
-
raf.add(key, tick);
|
|
198
|
-
}
|
|
199
|
-
return {
|
|
200
|
-
start,
|
|
201
|
-
finish: () => progress(1),
|
|
202
|
-
pause,
|
|
203
|
-
play,
|
|
204
|
-
progress
|
|
205
|
-
};
|
|
139
|
+
});
|
|
140
|
+
return controls;
|
|
206
141
|
}
|
|
207
142
|
if (module.exports.default) module.exports = module.exports.default;
|
package/utils/css/animate.d.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @typedef {import('./transform.js').TransformProps} TransformProps
|
|
3
|
-
* @typedef {[number, number, number, number]} BezierCurve
|
|
4
3
|
* @typedef {TransformProps & {
|
|
5
4
|
* opacity?: number;
|
|
6
5
|
* transformOrigin?: string;
|
|
7
|
-
* easing?: import('../math/createEases.js').EasingFunction|BezierCurve;
|
|
6
|
+
* easing?: import('../math/createEases.js').EasingFunction|import('../tween.js').BezierCurve;
|
|
8
7
|
* offset?: number;
|
|
9
8
|
* }} Keyframe
|
|
10
9
|
* @typedef {Keyframe & {
|
|
@@ -23,26 +22,15 @@
|
|
|
23
22
|
* Animate an element.
|
|
24
23
|
* @param {HTMLElement} element
|
|
25
24
|
* @param {Keyframe[]} keyframes
|
|
26
|
-
* @param {
|
|
27
|
-
* duration?: number;
|
|
28
|
-
* easing?: import('../math/createEases.js').EasingFunction|BezierCurve;
|
|
29
|
-
* onProgress?: (progress: number, easedProgress: number) => void;
|
|
30
|
-
* onFinish?: (progress: number, easedProgress: number) => void;
|
|
31
|
-
* }} [options]
|
|
25
|
+
* @param {import('../tween.js').TweenOptions} [options]
|
|
32
26
|
* @returns {Animate}
|
|
33
27
|
*/
|
|
34
|
-
export function animate(element: HTMLElement, keyframes: Keyframe[], options?:
|
|
35
|
-
duration?: number;
|
|
36
|
-
easing?: import('../math/createEases.js').EasingFunction | BezierCurve;
|
|
37
|
-
onProgress?: (progress: number, easedProgress: number) => void;
|
|
38
|
-
onFinish?: (progress: number, easedProgress: number) => void;
|
|
39
|
-
}): Animate;
|
|
28
|
+
export function animate(element: HTMLElement, keyframes: Keyframe[], options?: import('../tween.js').TweenOptions): Animate;
|
|
40
29
|
export type TransformProps = import('./transform.js').TransformProps;
|
|
41
|
-
export type BezierCurve = [number, number, number, number];
|
|
42
30
|
export type Keyframe = TransformProps & {
|
|
43
31
|
opacity?: number;
|
|
44
32
|
transformOrigin?: string;
|
|
45
|
-
easing?: import('../math/createEases.js').EasingFunction |
|
|
33
|
+
easing?: import('../math/createEases.js').EasingFunction | import('../tween.js').BezierCurve;
|
|
46
34
|
offset?: number;
|
|
47
35
|
};
|
|
48
36
|
export type NormalizedKeyframe = Keyframe & {
|