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