@maple-design/use 0.0.0-stage → 1.0.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/lib/index.cjs +621 -0
- package/lib/index.d.cts +109 -0
- package/lib/index.d.ts +109 -0
- package/lib/index.js +589 -0
- package/package.json +51 -3
- package/README.md +0 -3
package/lib/index.js
ADDED
|
@@ -0,0 +1,589 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
3
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
4
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __spreadValues = (a, b) => {
|
|
7
|
+
for (var prop in b || (b = {}))
|
|
8
|
+
if (__hasOwnProp.call(b, prop))
|
|
9
|
+
__defNormalProp(a, prop, b[prop]);
|
|
10
|
+
if (__getOwnPropSymbols)
|
|
11
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
12
|
+
if (__propIsEnum.call(b, prop))
|
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
|
14
|
+
}
|
|
15
|
+
return a;
|
|
16
|
+
};
|
|
17
|
+
var __objRest = (source, exclude) => {
|
|
18
|
+
var target = {};
|
|
19
|
+
for (var prop in source)
|
|
20
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
21
|
+
target[prop] = source[prop];
|
|
22
|
+
if (source != null && __getOwnPropSymbols)
|
|
23
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
24
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
25
|
+
target[prop] = source[prop];
|
|
26
|
+
}
|
|
27
|
+
return target;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// src/useEventListener.ts
|
|
31
|
+
import { isRef, onBeforeUnmount, onDeactivated, unref, watch } from "vue";
|
|
32
|
+
import { inBrowser, isFunction } from "@maple-design/shared";
|
|
33
|
+
|
|
34
|
+
// src/onSmartMounted.ts
|
|
35
|
+
import { nextTick, onActivated, onMounted } from "vue";
|
|
36
|
+
function onSmartMounted(hook) {
|
|
37
|
+
let isMounted = false;
|
|
38
|
+
onMounted(() => {
|
|
39
|
+
hook();
|
|
40
|
+
nextTick(() => {
|
|
41
|
+
isMounted = true;
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
onActivated(() => {
|
|
45
|
+
if (!isMounted) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
hook();
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// src/useEventListener.ts
|
|
53
|
+
function useEventListener(target, type, listener, options = {}) {
|
|
54
|
+
if (!inBrowser()) {
|
|
55
|
+
return () => {
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
const { passive = false, capture = false } = options;
|
|
59
|
+
let boundTarget;
|
|
60
|
+
let active = false;
|
|
61
|
+
let cleaned = false;
|
|
62
|
+
let stop;
|
|
63
|
+
const resolveTarget = () => {
|
|
64
|
+
var _a;
|
|
65
|
+
return (_a = isFunction(target) ? target() : unref(target)) != null ? _a : void 0;
|
|
66
|
+
};
|
|
67
|
+
const detach = () => {
|
|
68
|
+
boundTarget == null ? void 0 : boundTarget.removeEventListener(type, listener, { capture });
|
|
69
|
+
boundTarget = void 0;
|
|
70
|
+
};
|
|
71
|
+
const bind = () => {
|
|
72
|
+
if (!active || cleaned) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
const next = resolveTarget();
|
|
76
|
+
if (next === boundTarget) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
detach();
|
|
80
|
+
next == null ? void 0 : next.addEventListener(type, listener, { passive, capture });
|
|
81
|
+
boundTarget = next;
|
|
82
|
+
};
|
|
83
|
+
const cleanup = () => {
|
|
84
|
+
if (cleaned) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
cleaned = true;
|
|
88
|
+
active = false;
|
|
89
|
+
stop == null ? void 0 : stop();
|
|
90
|
+
detach();
|
|
91
|
+
};
|
|
92
|
+
if (isRef(target) || isFunction(target)) {
|
|
93
|
+
stop = watch(resolveTarget, bind);
|
|
94
|
+
}
|
|
95
|
+
onSmartMounted(() => {
|
|
96
|
+
active = true;
|
|
97
|
+
bind();
|
|
98
|
+
});
|
|
99
|
+
onDeactivated(() => {
|
|
100
|
+
active = false;
|
|
101
|
+
detach();
|
|
102
|
+
});
|
|
103
|
+
onBeforeUnmount(cleanup);
|
|
104
|
+
return cleanup;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// src/useClickOutside.ts
|
|
108
|
+
import { unref as unref2 } from "vue";
|
|
109
|
+
import { inBrowser as inBrowser2, isFunction as isFunction2 } from "@maple-design/shared";
|
|
110
|
+
function useClickOutside(target, type, listener) {
|
|
111
|
+
if (!inBrowser2()) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
const handler = (event) => {
|
|
115
|
+
const element = isFunction2(target) ? target() : unref2(target);
|
|
116
|
+
if (element && !event.composedPath().includes(element) && !element.contains(event.target)) {
|
|
117
|
+
listener(event);
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
useEventListener(document, type, handler);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// src/onSmartUnmounted.ts
|
|
124
|
+
import { onDeactivated as onDeactivated2, onUnmounted } from "vue";
|
|
125
|
+
function onSmartUnmounted(hook) {
|
|
126
|
+
let keepalive = false;
|
|
127
|
+
onDeactivated2(() => {
|
|
128
|
+
keepalive = true;
|
|
129
|
+
hook();
|
|
130
|
+
});
|
|
131
|
+
onUnmounted(() => {
|
|
132
|
+
if (keepalive) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
hook();
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// src/useParent.ts
|
|
140
|
+
import {
|
|
141
|
+
computed,
|
|
142
|
+
getCurrentInstance,
|
|
143
|
+
inject,
|
|
144
|
+
nextTick as nextTick2,
|
|
145
|
+
onActivated as onActivated2,
|
|
146
|
+
onBeforeUnmount as onBeforeUnmount2,
|
|
147
|
+
onDeactivated as onDeactivated3,
|
|
148
|
+
onMounted as onMounted2
|
|
149
|
+
} from "vue";
|
|
150
|
+
function keyInProvides(key) {
|
|
151
|
+
const instance = getCurrentInstance();
|
|
152
|
+
return key in instance.provides;
|
|
153
|
+
}
|
|
154
|
+
function useParent(key) {
|
|
155
|
+
if (!keyInProvides(key)) {
|
|
156
|
+
return {
|
|
157
|
+
index: null,
|
|
158
|
+
parentProvider: null,
|
|
159
|
+
bindParent: null
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
const provider = inject(key);
|
|
163
|
+
const _a = provider, { childInstances, collect, clear } = _a, parentProvider = __objRest(_a, ["childInstances", "collect", "clear"]);
|
|
164
|
+
const childInstance = getCurrentInstance();
|
|
165
|
+
const index = computed(() => childInstances.indexOf(childInstance));
|
|
166
|
+
const bindParent = (childProvider) => {
|
|
167
|
+
let active = false;
|
|
168
|
+
let registered = false;
|
|
169
|
+
const add = () => {
|
|
170
|
+
active = true;
|
|
171
|
+
nextTick2(() => {
|
|
172
|
+
if (active && !registered) {
|
|
173
|
+
collect(childInstance, childProvider);
|
|
174
|
+
registered = true;
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
};
|
|
178
|
+
const remove = () => {
|
|
179
|
+
active = false;
|
|
180
|
+
if (registered) {
|
|
181
|
+
clear(childInstance, childProvider);
|
|
182
|
+
registered = false;
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
onMounted2(add);
|
|
186
|
+
onActivated2(add);
|
|
187
|
+
onDeactivated3(remove);
|
|
188
|
+
onBeforeUnmount2(remove);
|
|
189
|
+
};
|
|
190
|
+
return {
|
|
191
|
+
index,
|
|
192
|
+
parentProvider,
|
|
193
|
+
bindParent
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// src/useChildren.ts
|
|
198
|
+
import {
|
|
199
|
+
computed as computed2,
|
|
200
|
+
getCurrentInstance as getCurrentInstance2,
|
|
201
|
+
isVNode,
|
|
202
|
+
onUpdated,
|
|
203
|
+
provide,
|
|
204
|
+
reactive
|
|
205
|
+
} from "vue";
|
|
206
|
+
import { isArray, removeItem } from "@maple-design/shared";
|
|
207
|
+
function flatVNodes(subTree) {
|
|
208
|
+
const vNodes = [];
|
|
209
|
+
const flat = (subTree2) => {
|
|
210
|
+
if (subTree2 == null ? void 0 : subTree2.component) {
|
|
211
|
+
flat(subTree2 == null ? void 0 : subTree2.component.subTree);
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
if (isArray(subTree2 == null ? void 0 : subTree2.children)) {
|
|
215
|
+
subTree2.children.forEach((child) => {
|
|
216
|
+
if (isVNode(child)) {
|
|
217
|
+
vNodes.push(child);
|
|
218
|
+
flat(child);
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
flat(subTree);
|
|
224
|
+
return vNodes;
|
|
225
|
+
}
|
|
226
|
+
function useChildren(key) {
|
|
227
|
+
const parentInstance = getCurrentInstance2();
|
|
228
|
+
const childInstances = reactive([]);
|
|
229
|
+
const childProviders = [];
|
|
230
|
+
const length = computed2(() => childInstances.length);
|
|
231
|
+
const sortInstances = () => {
|
|
232
|
+
const vNodes = flatVNodes(parentInstance.subTree);
|
|
233
|
+
const sorted = [...childInstances].sort((a, b) => vNodes.indexOf(a.vnode) - vNodes.indexOf(b.vnode));
|
|
234
|
+
if (sorted.every((instance, index) => instance === childInstances[index])) {
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
const providers = sorted.map((instance) => childProviders[childInstances.indexOf(instance)]);
|
|
238
|
+
childInstances.splice(0, childInstances.length, ...sorted);
|
|
239
|
+
childProviders.splice(0, childProviders.length, ...providers);
|
|
240
|
+
};
|
|
241
|
+
onUpdated(sortInstances);
|
|
242
|
+
const collect = (childInstance, childProvider) => {
|
|
243
|
+
if (childInstances.includes(childInstance)) {
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
childInstances.push(childInstance);
|
|
247
|
+
childProviders.push(childProvider);
|
|
248
|
+
sortInstances();
|
|
249
|
+
};
|
|
250
|
+
const clear = (childInstance, childProvider) => {
|
|
251
|
+
removeItem(childInstances, childInstance);
|
|
252
|
+
removeItem(childProviders, childProvider);
|
|
253
|
+
};
|
|
254
|
+
const bindChildren = (parentProvider) => {
|
|
255
|
+
provide(key, __spreadValues({
|
|
256
|
+
childInstances,
|
|
257
|
+
collect,
|
|
258
|
+
clear
|
|
259
|
+
}, parentProvider));
|
|
260
|
+
};
|
|
261
|
+
return {
|
|
262
|
+
length,
|
|
263
|
+
childProviders,
|
|
264
|
+
bindChildren
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// src/onWindowResize.ts
|
|
269
|
+
function onWindowResize(listener) {
|
|
270
|
+
useEventListener(() => window, "resize", listener, { passive: true });
|
|
271
|
+
useEventListener(() => window, "orientationchange", listener, { passive: true });
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// src/useInitialized.ts
|
|
275
|
+
import { ref, watch as watch2 } from "vue";
|
|
276
|
+
function useInitialized(source, value) {
|
|
277
|
+
const initialized = ref(false);
|
|
278
|
+
watch2(
|
|
279
|
+
source,
|
|
280
|
+
(newValue) => {
|
|
281
|
+
if (value === newValue) {
|
|
282
|
+
initialized.value = true;
|
|
283
|
+
}
|
|
284
|
+
},
|
|
285
|
+
{ immediate: true }
|
|
286
|
+
);
|
|
287
|
+
return initialized;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// src/useTouch.ts
|
|
291
|
+
import { onBeforeUnmount as onBeforeUnmount3, onDeactivated as onDeactivated4, ref as ref2 } from "vue";
|
|
292
|
+
import { getScrollTop } from "@maple-design/shared";
|
|
293
|
+
function getDirection(x, y) {
|
|
294
|
+
if (x > y) {
|
|
295
|
+
return "horizontal";
|
|
296
|
+
}
|
|
297
|
+
if (y > x) {
|
|
298
|
+
return "vertical";
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
function useTouch() {
|
|
302
|
+
const startX = ref2(0);
|
|
303
|
+
const startY = ref2(0);
|
|
304
|
+
const deltaX = ref2(0);
|
|
305
|
+
const deltaY = ref2(0);
|
|
306
|
+
const offsetX = ref2(0);
|
|
307
|
+
const offsetY = ref2(0);
|
|
308
|
+
const prevX = ref2(0);
|
|
309
|
+
const prevY = ref2(0);
|
|
310
|
+
const moveX = ref2(0);
|
|
311
|
+
const moveY = ref2(0);
|
|
312
|
+
const direction = ref2();
|
|
313
|
+
const touching = ref2(false);
|
|
314
|
+
const dragging = ref2(false);
|
|
315
|
+
const startTime = ref2(0);
|
|
316
|
+
const distance = ref2(0);
|
|
317
|
+
let draggingAnimationFrame = null;
|
|
318
|
+
const cancelFrame = () => {
|
|
319
|
+
if (draggingAnimationFrame !== null && typeof window !== "undefined") {
|
|
320
|
+
window.cancelAnimationFrame(draggingAnimationFrame);
|
|
321
|
+
}
|
|
322
|
+
draggingAnimationFrame = null;
|
|
323
|
+
};
|
|
324
|
+
const resetTouch = () => {
|
|
325
|
+
cancelFrame();
|
|
326
|
+
startX.value = 0;
|
|
327
|
+
startY.value = 0;
|
|
328
|
+
deltaX.value = 0;
|
|
329
|
+
deltaY.value = 0;
|
|
330
|
+
offsetX.value = 0;
|
|
331
|
+
offsetY.value = 0;
|
|
332
|
+
prevX.value = 0;
|
|
333
|
+
prevY.value = 0;
|
|
334
|
+
moveX.value = 0;
|
|
335
|
+
moveY.value = 0;
|
|
336
|
+
direction.value = void 0;
|
|
337
|
+
touching.value = false;
|
|
338
|
+
dragging.value = false;
|
|
339
|
+
startTime.value = 0;
|
|
340
|
+
distance.value = 0;
|
|
341
|
+
};
|
|
342
|
+
const startTouch = (event) => {
|
|
343
|
+
resetTouch();
|
|
344
|
+
if (!event.touches.length) {
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
const { clientX: x, clientY: y } = event.touches[0];
|
|
348
|
+
startX.value = x;
|
|
349
|
+
startY.value = y;
|
|
350
|
+
prevX.value = x;
|
|
351
|
+
prevY.value = y;
|
|
352
|
+
touching.value = true;
|
|
353
|
+
startTime.value = performance.now();
|
|
354
|
+
dragging.value = false;
|
|
355
|
+
if (draggingAnimationFrame) {
|
|
356
|
+
window.cancelAnimationFrame(draggingAnimationFrame);
|
|
357
|
+
}
|
|
358
|
+
};
|
|
359
|
+
const moveTouch = (event) => {
|
|
360
|
+
if (!event.touches.length) {
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
const { clientX: x, clientY: y } = event.touches[0];
|
|
364
|
+
dragging.value = true;
|
|
365
|
+
deltaX.value = x - startX.value;
|
|
366
|
+
deltaY.value = y - startY.value;
|
|
367
|
+
offsetX.value = Math.abs(deltaX.value);
|
|
368
|
+
offsetY.value = Math.abs(deltaY.value);
|
|
369
|
+
distance.value = Math.sqrt(offsetX.value ** 2 + offsetY.value ** 2);
|
|
370
|
+
moveX.value = x - prevX.value;
|
|
371
|
+
moveY.value = y - prevY.value;
|
|
372
|
+
if (!direction.value) {
|
|
373
|
+
direction.value = getDirection(offsetX.value, offsetY.value);
|
|
374
|
+
}
|
|
375
|
+
prevX.value = x;
|
|
376
|
+
prevY.value = y;
|
|
377
|
+
};
|
|
378
|
+
const endTouch = () => {
|
|
379
|
+
touching.value = false;
|
|
380
|
+
cancelFrame();
|
|
381
|
+
if (typeof window === "undefined") {
|
|
382
|
+
dragging.value = false;
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
draggingAnimationFrame = window.requestAnimationFrame(() => {
|
|
386
|
+
draggingAnimationFrame = null;
|
|
387
|
+
dragging.value = false;
|
|
388
|
+
});
|
|
389
|
+
};
|
|
390
|
+
const isReachTop = (element) => {
|
|
391
|
+
const scrollTop = getScrollTop(element);
|
|
392
|
+
return scrollTop === 0 && deltaY.value > 0;
|
|
393
|
+
};
|
|
394
|
+
const isReachBottom = (element, offset = 1) => {
|
|
395
|
+
const { scrollHeight, clientHeight, scrollTop } = element;
|
|
396
|
+
const offsetBottom = Math.abs(scrollHeight - scrollTop - clientHeight);
|
|
397
|
+
return deltaY.value < 0 && offsetBottom <= offset;
|
|
398
|
+
};
|
|
399
|
+
onBeforeUnmount3(resetTouch);
|
|
400
|
+
onDeactivated4(resetTouch);
|
|
401
|
+
return {
|
|
402
|
+
startX,
|
|
403
|
+
startY,
|
|
404
|
+
deltaX,
|
|
405
|
+
deltaY,
|
|
406
|
+
offsetX,
|
|
407
|
+
offsetY,
|
|
408
|
+
prevX,
|
|
409
|
+
prevY,
|
|
410
|
+
moveX,
|
|
411
|
+
moveY,
|
|
412
|
+
direction,
|
|
413
|
+
touching,
|
|
414
|
+
dragging,
|
|
415
|
+
startTime,
|
|
416
|
+
distance,
|
|
417
|
+
resetTouch,
|
|
418
|
+
startTouch,
|
|
419
|
+
moveTouch,
|
|
420
|
+
endTouch,
|
|
421
|
+
isReachTop,
|
|
422
|
+
isReachBottom
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
// src/useId.ts
|
|
427
|
+
import * as Vue from "vue";
|
|
428
|
+
import { kebabCase } from "@maple-design/shared";
|
|
429
|
+
function useId2() {
|
|
430
|
+
var _a, _b;
|
|
431
|
+
const instance = Vue.getCurrentInstance();
|
|
432
|
+
const name = kebabCase((_a = instance.type.name) != null ? _a : "component");
|
|
433
|
+
const nativeId = (_b = Vue.useId) == null ? void 0 : _b();
|
|
434
|
+
const id = Vue.ref();
|
|
435
|
+
id.value = process.env.NODE_ENV === "test" ? `${name}-mock-id` : nativeId ? `${name}-${nativeId}` : void 0;
|
|
436
|
+
Vue.onMounted(() => {
|
|
437
|
+
if (id.value === void 0) {
|
|
438
|
+
id.value = `${name}-${instance.uid}`;
|
|
439
|
+
}
|
|
440
|
+
});
|
|
441
|
+
return id;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
// src/useClientId.ts
|
|
445
|
+
import { getCurrentInstance as getCurrentInstance4, onMounted as onMounted4, ref as ref4 } from "vue";
|
|
446
|
+
import { kebabCase as kebabCase2 } from "@maple-design/shared";
|
|
447
|
+
function useClientId() {
|
|
448
|
+
const instance = getCurrentInstance4();
|
|
449
|
+
const name = kebabCase2(instance.type.name);
|
|
450
|
+
const id = ref4(process.env.NODE_ENV === "test" ? `${name}-mock-id` : void 0);
|
|
451
|
+
onMounted4(() => {
|
|
452
|
+
if (process.env.NODE_ENV !== "test") {
|
|
453
|
+
id.value = `${name}-${instance.uid}`;
|
|
454
|
+
}
|
|
455
|
+
});
|
|
456
|
+
return id;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
// src/useWindowSize.ts
|
|
460
|
+
import { ref as ref5 } from "vue";
|
|
461
|
+
import { inBrowser as inBrowser3 } from "@maple-design/shared";
|
|
462
|
+
function useWindowSize(options = {}) {
|
|
463
|
+
const { initialWidth = 0, initialHeight = 0 } = options;
|
|
464
|
+
const width = ref5(initialWidth);
|
|
465
|
+
const height = ref5(initialHeight);
|
|
466
|
+
const update = () => {
|
|
467
|
+
if (!inBrowser3()) {
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
width.value = window.innerWidth;
|
|
471
|
+
height.value = window.innerHeight;
|
|
472
|
+
};
|
|
473
|
+
onSmartMounted(update);
|
|
474
|
+
onWindowResize(update);
|
|
475
|
+
return {
|
|
476
|
+
width,
|
|
477
|
+
height
|
|
478
|
+
};
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
// src/useVModel.ts
|
|
482
|
+
import { computed as computed3, nextTick as nextTick3, ref as ref6, watch as watch3 } from "vue";
|
|
483
|
+
import { call } from "@maple-design/shared";
|
|
484
|
+
function useVModel(props, key, options = {}) {
|
|
485
|
+
const { passive = true, eventName, defaultValue, emit } = options;
|
|
486
|
+
const event = eventName != null ? eventName : `${emit ? "update" : "onUpdate"}:${key.toString()}`;
|
|
487
|
+
const getValue = () => {
|
|
488
|
+
var _a;
|
|
489
|
+
return (_a = props[key]) != null ? _a : defaultValue;
|
|
490
|
+
};
|
|
491
|
+
if (!passive) {
|
|
492
|
+
return computed3({
|
|
493
|
+
get() {
|
|
494
|
+
return getValue();
|
|
495
|
+
},
|
|
496
|
+
set(value) {
|
|
497
|
+
emit ? emit(event, value) : call(props[event], value);
|
|
498
|
+
}
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
const proxy = ref6(getValue());
|
|
502
|
+
let shouldEmit = true;
|
|
503
|
+
watch3(
|
|
504
|
+
() => props[key],
|
|
505
|
+
() => {
|
|
506
|
+
shouldEmit = false;
|
|
507
|
+
proxy.value = getValue();
|
|
508
|
+
nextTick3(() => {
|
|
509
|
+
shouldEmit = true;
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
);
|
|
513
|
+
watch3(
|
|
514
|
+
() => proxy.value,
|
|
515
|
+
(newValue) => {
|
|
516
|
+
if (!shouldEmit) {
|
|
517
|
+
return;
|
|
518
|
+
}
|
|
519
|
+
emit ? emit(event, newValue) : call(props[event], newValue);
|
|
520
|
+
}
|
|
521
|
+
);
|
|
522
|
+
return proxy;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
// src/useMotion.ts
|
|
526
|
+
import { ref as ref7 } from "vue";
|
|
527
|
+
import { isFunction as isFunction3, motion } from "@maple-design/shared";
|
|
528
|
+
function useMotion(options) {
|
|
529
|
+
const value = ref7(getter(options.from));
|
|
530
|
+
const state = ref7("pending");
|
|
531
|
+
let ctx = createMotionContext();
|
|
532
|
+
function getter(value2) {
|
|
533
|
+
return isFunction3(value2) ? value2() : value2;
|
|
534
|
+
}
|
|
535
|
+
function reset() {
|
|
536
|
+
ctx.reset();
|
|
537
|
+
value.value = getter(options.from);
|
|
538
|
+
state.value = "pending";
|
|
539
|
+
ctx = createMotionContext();
|
|
540
|
+
}
|
|
541
|
+
function start() {
|
|
542
|
+
ctx.start();
|
|
543
|
+
}
|
|
544
|
+
function pause() {
|
|
545
|
+
ctx.pause();
|
|
546
|
+
}
|
|
547
|
+
function createMotionContext() {
|
|
548
|
+
return motion({
|
|
549
|
+
from: getter(options.from),
|
|
550
|
+
to: getter(options.to),
|
|
551
|
+
duration: options.duration ? getter(options.duration) : 300,
|
|
552
|
+
timingFunction: options.timingFunction,
|
|
553
|
+
onStateChange(newState) {
|
|
554
|
+
state.value = newState;
|
|
555
|
+
},
|
|
556
|
+
frame({ value: newValue, done }) {
|
|
557
|
+
var _a;
|
|
558
|
+
value.value = newValue;
|
|
559
|
+
if (done) {
|
|
560
|
+
(_a = options.onFinished) == null ? void 0 : _a.call(options, value.value);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
});
|
|
564
|
+
}
|
|
565
|
+
return {
|
|
566
|
+
value,
|
|
567
|
+
state,
|
|
568
|
+
start,
|
|
569
|
+
pause,
|
|
570
|
+
reset
|
|
571
|
+
};
|
|
572
|
+
}
|
|
573
|
+
export {
|
|
574
|
+
keyInProvides,
|
|
575
|
+
onSmartMounted,
|
|
576
|
+
onSmartUnmounted,
|
|
577
|
+
onWindowResize,
|
|
578
|
+
useChildren,
|
|
579
|
+
useClickOutside,
|
|
580
|
+
useClientId,
|
|
581
|
+
useEventListener,
|
|
582
|
+
useId2 as useId,
|
|
583
|
+
useInitialized,
|
|
584
|
+
useMotion,
|
|
585
|
+
useParent,
|
|
586
|
+
useTouch,
|
|
587
|
+
useVModel,
|
|
588
|
+
useWindowSize
|
|
589
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,54 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@maple-design/use",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "composable utils of maple",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"composable",
|
|
7
|
+
"utils",
|
|
8
|
+
"maple"
|
|
9
|
+
],
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://xxx/pe.frontend/materials/maplejs/maple-design-mobile/-/issues"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "https://xxx/pe.frontend/materials/maplejs/maple-design-mobile"
|
|
16
|
+
},
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"author": "shuai.meng",
|
|
19
|
+
"sideEffects": false,
|
|
20
|
+
"type": "module",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"import": "./lib/index.js",
|
|
24
|
+
"require": "./lib/index.cjs",
|
|
25
|
+
"types": "./lib/index.d.ts"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"main": "lib/index.cjs",
|
|
29
|
+
"module": "lib/index.js",
|
|
30
|
+
"types": "lib/index.d.ts",
|
|
31
|
+
"files": [
|
|
32
|
+
"lib"
|
|
33
|
+
],
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@maple-design/shared": "1.0.0"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@types/node": "^18.7.20",
|
|
39
|
+
"tsup": "7.2.0",
|
|
40
|
+
"typescript": "5.3.3",
|
|
41
|
+
"vue": "3.5.13"
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"vue": "^3.2.0"
|
|
45
|
+
},
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"access": "public",
|
|
48
|
+
"registry": "https://registry.npmjs.org/"
|
|
49
|
+
},
|
|
50
|
+
"scripts": {
|
|
51
|
+
"build": "tsup src/index.ts --format esm,cjs --out-dir=lib --dts --clean",
|
|
52
|
+
"dev": "tsup src/index.ts --format esm --out-dir=lib --watch --dts"
|
|
53
|
+
}
|
|
6
54
|
}
|
package/README.md
DELETED