@varlet/use 2.16.2 → 2.16.3-alpha.1694361535255

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.d.ts CHANGED
@@ -1,9 +1,68 @@
1
- export * from './useEventListener.js';
2
- export * from './useClickOutside.js';
3
- export * from './onSmartMounted.js';
4
- export * from './onSmartUnmounted.js';
5
- export * from './useParent.js';
6
- export * from './useChildren.js';
7
- export * from './onWindowResize.js';
8
- export * from './useInitialized.js';
9
- export * from './useTouch.js';
1
+ import * as vue from 'vue';
2
+ import { Ref, ComponentInternalInstance, ComputedRef, WatchSource } from 'vue';
3
+
4
+ type UseEventListenerTarget = EventTarget | Ref<EventTarget | undefined | null> | (() => EventTarget);
5
+ interface UseEventListenerOptions {
6
+ capture?: boolean;
7
+ passive?: boolean;
8
+ }
9
+ declare function useEventListener<T extends keyof DocumentEventMap>(target: UseEventListenerTarget, type: T, listener: (event: DocumentEventMap[T]) => void, options?: UseEventListenerOptions): () => void;
10
+ declare function useEventListener(target: UseEventListenerTarget, type: string, listener: EventListener, options?: UseEventListenerOptions): () => void;
11
+
12
+ type UseClickOutsideTarget = Element | Ref<Element | undefined | null> | (() => Element);
13
+ declare function useClickOutside(target: UseClickOutsideTarget, type: string, listener: EventListener): void;
14
+
15
+ declare function onSmartMounted(hook: () => void): void;
16
+
17
+ declare function onSmartUnmounted(hook: () => void): void;
18
+
19
+ interface UseChildrenBaseProvider<C> {
20
+ childInstances: ComponentInternalInstance[];
21
+ collect(instance: ComponentInternalInstance, childProvider: C): void;
22
+ clear(instance: ComponentInternalInstance, childProvider: C): void;
23
+ }
24
+ declare function useChildren<P, C>(key: symbol | string): {
25
+ length: ComputedRef<number>;
26
+ childProviders: C[];
27
+ bindChildren: (parentProvider: P) => void;
28
+ };
29
+
30
+ declare function keyInProvides(key: symbol | string): boolean;
31
+ declare function useParent<P, C>(key: symbol | string): {
32
+ index: null;
33
+ parentProvider: null;
34
+ bindParent: null;
35
+ } | {
36
+ index: ComputedRef<number>;
37
+ parentProvider: Omit<P & UseChildrenBaseProvider<C>, "childInstances" | "collect" | "clear">;
38
+ bindParent: (childProvider: C) => void;
39
+ };
40
+
41
+ declare function onWindowResize(listener: EventListener): void;
42
+
43
+ declare function useInitialized<T>(source: WatchSource<T>, value: T): vue.Ref<boolean>;
44
+
45
+ type TouchDirection = 'horizontal' | 'vertical';
46
+ declare function useTouch(): {
47
+ startX: vue.Ref<number>;
48
+ startY: vue.Ref<number>;
49
+ deltaX: vue.Ref<number>;
50
+ deltaY: vue.Ref<number>;
51
+ offsetX: vue.Ref<number>;
52
+ offsetY: vue.Ref<number>;
53
+ prevX: vue.Ref<number>;
54
+ prevY: vue.Ref<number>;
55
+ moveX: vue.Ref<number>;
56
+ moveY: vue.Ref<number>;
57
+ direction: vue.Ref<TouchDirection | undefined>;
58
+ touching: vue.Ref<boolean>;
59
+ dragging: vue.Ref<boolean>;
60
+ startTime: vue.Ref<number>;
61
+ distance: vue.Ref<number>;
62
+ resetTouch: () => void;
63
+ startTouch: (event: TouchEvent) => void;
64
+ moveTouch: (event: TouchEvent) => void;
65
+ endTouch: () => void;
66
+ };
67
+
68
+ export { TouchDirection, UseChildrenBaseProvider, UseClickOutsideTarget, UseEventListenerOptions, UseEventListenerTarget, keyInProvides, onSmartMounted, onSmartUnmounted, onWindowResize, useChildren, useClickOutside, useEventListener, useInitialized, useParent, useTouch };
package/lib/index.js CHANGED
@@ -1,9 +1,379 @@
1
- export * from './useEventListener.js';
2
- export * from './useClickOutside.js';
3
- export * from './onSmartMounted.js';
4
- export * from './onSmartUnmounted.js';
5
- export * from './useParent.js';
6
- export * from './useChildren.js';
7
- export * from './onWindowResize.js';
8
- export * from './useInitialized.js';
9
- export * from './useTouch.js';
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 { inBrowser, isFunction } from "@varlet/shared";
32
+ import { isRef, onDeactivated, onBeforeUnmount, unref, watch } from "vue";
33
+
34
+ // src/onSmartMounted.ts
35
+ import { onMounted, nextTick, onActivated } 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
+ const { passive = false, capture = false } = options;
58
+ let listening = false;
59
+ let cleaned = false;
60
+ const getElement = (target2) => isFunction(target2) ? target2() : unref(target2);
61
+ const add = (target2) => {
62
+ if (listening || cleaned) {
63
+ return;
64
+ }
65
+ const element = getElement(target2);
66
+ if (element) {
67
+ element.addEventListener(type, listener, {
68
+ passive,
69
+ capture
70
+ });
71
+ listening = true;
72
+ }
73
+ };
74
+ const remove = (target2) => {
75
+ if (!listening || cleaned) {
76
+ return;
77
+ }
78
+ const element = getElement(target2);
79
+ if (element) {
80
+ element.removeEventListener(type, listener, {
81
+ capture
82
+ });
83
+ listening = false;
84
+ }
85
+ };
86
+ let watchStopHandle;
87
+ if (isRef(target)) {
88
+ watchStopHandle = watch(
89
+ () => target.value,
90
+ (newValue, oldValue) => {
91
+ remove(oldValue);
92
+ add(newValue);
93
+ }
94
+ );
95
+ }
96
+ const cleanup = () => {
97
+ watchStopHandle == null ? void 0 : watchStopHandle();
98
+ remove(target);
99
+ cleaned = true;
100
+ };
101
+ onSmartMounted(() => {
102
+ add(target);
103
+ });
104
+ onBeforeUnmount(() => {
105
+ remove(target);
106
+ });
107
+ onDeactivated(() => {
108
+ remove(target);
109
+ });
110
+ return cleanup;
111
+ }
112
+
113
+ // src/useClickOutside.ts
114
+ import { inBrowser as inBrowser2, isFunction as isFunction2 } from "@varlet/shared";
115
+ import { unref as unref2 } from "vue";
116
+ function useClickOutside(target, type, listener) {
117
+ if (!inBrowser2()) {
118
+ return;
119
+ }
120
+ const handler = (event) => {
121
+ const element = isFunction2(target) ? target() : unref2(target);
122
+ if (element && !element.contains(event.target)) {
123
+ listener(event);
124
+ }
125
+ };
126
+ useEventListener(document, type, handler);
127
+ }
128
+
129
+ // src/onSmartUnmounted.ts
130
+ import { onDeactivated as onDeactivated2, onUnmounted } from "vue";
131
+ function onSmartUnmounted(hook) {
132
+ onUnmounted(() => {
133
+ hook();
134
+ });
135
+ onDeactivated2(() => {
136
+ hook();
137
+ });
138
+ }
139
+
140
+ // src/useParent.ts
141
+ import {
142
+ getCurrentInstance,
143
+ inject,
144
+ onMounted as onMounted2,
145
+ onBeforeUnmount as onBeforeUnmount2,
146
+ nextTick as nextTick2,
147
+ computed
148
+ } from "vue";
149
+ function keyInProvides(key) {
150
+ const instance = getCurrentInstance();
151
+ return key in instance.provides;
152
+ }
153
+ function useParent(key) {
154
+ if (!keyInProvides(key)) {
155
+ return {
156
+ index: null,
157
+ parentProvider: null,
158
+ bindParent: null
159
+ };
160
+ }
161
+ const provider = inject(key);
162
+ const _a = provider, { childInstances, collect, clear } = _a, parentProvider = __objRest(_a, ["childInstances", "collect", "clear"]);
163
+ const childInstance = getCurrentInstance();
164
+ const index = computed(() => childInstances.indexOf(childInstance));
165
+ const bindParent = (childProvider) => {
166
+ onMounted2(() => {
167
+ nextTick2().then(() => {
168
+ collect(childInstance, childProvider);
169
+ });
170
+ });
171
+ onBeforeUnmount2(() => {
172
+ nextTick2().then(() => {
173
+ clear(childInstance, childProvider);
174
+ });
175
+ });
176
+ };
177
+ return {
178
+ index,
179
+ parentProvider,
180
+ bindParent
181
+ };
182
+ }
183
+
184
+ // src/useChildren.ts
185
+ import { removeItem } from "@varlet/shared";
186
+ import {
187
+ getCurrentInstance as getCurrentInstance2,
188
+ computed as computed2,
189
+ provide,
190
+ reactive,
191
+ isVNode
192
+ } from "vue";
193
+ function flatVNodes(subTree) {
194
+ const vNodes = [];
195
+ const flat = (subTree2) => {
196
+ if (subTree2 == null ? void 0 : subTree2.component) {
197
+ flat(subTree2 == null ? void 0 : subTree2.component.subTree);
198
+ return;
199
+ }
200
+ if (Array.isArray(subTree2 == null ? void 0 : subTree2.children)) {
201
+ subTree2.children.forEach((child) => {
202
+ if (isVNode(child)) {
203
+ vNodes.push(child);
204
+ flat(child);
205
+ }
206
+ });
207
+ }
208
+ };
209
+ flat(subTree);
210
+ return vNodes;
211
+ }
212
+ function useChildren(key) {
213
+ const parentInstance = getCurrentInstance2();
214
+ const childInstances = reactive([]);
215
+ const childProviders = [];
216
+ const length = computed2(() => childInstances.length);
217
+ const sortInstances = () => {
218
+ const vNodes = flatVNodes(parentInstance.subTree);
219
+ childInstances.sort((a, b) => vNodes.indexOf(a.vnode) - vNodes.indexOf(b.vnode));
220
+ };
221
+ const collect = (childInstance, childProvider) => {
222
+ childInstances.push(childInstance);
223
+ childProviders.push(childProvider);
224
+ sortInstances();
225
+ };
226
+ const clear = (childInstance, childProvider) => {
227
+ removeItem(childInstances, childInstance);
228
+ removeItem(childProviders, childProvider);
229
+ };
230
+ const bindChildren = (parentProvider) => {
231
+ provide(key, __spreadValues({
232
+ childInstances,
233
+ collect,
234
+ clear
235
+ }, parentProvider));
236
+ };
237
+ return {
238
+ length,
239
+ childProviders,
240
+ bindChildren
241
+ };
242
+ }
243
+
244
+ // src/onWindowResize.ts
245
+ function onWindowResize(listener) {
246
+ useEventListener(() => window, "resize", listener, { passive: true });
247
+ useEventListener(() => window, "orientationchange", listener, { passive: true });
248
+ }
249
+
250
+ // src/useInitialized.ts
251
+ import { watch as watch2, ref } from "vue";
252
+ function useInitialized(source, value) {
253
+ const initialized = ref(false);
254
+ watch2(
255
+ source,
256
+ (newValue) => {
257
+ if (value === newValue) {
258
+ initialized.value = true;
259
+ }
260
+ },
261
+ { immediate: true }
262
+ );
263
+ return initialized;
264
+ }
265
+
266
+ // src/useTouch.ts
267
+ import { ref as ref2 } from "vue";
268
+ function getDirection(x, y) {
269
+ if (x > y) {
270
+ return "horizontal";
271
+ }
272
+ if (y > x) {
273
+ return "vertical";
274
+ }
275
+ }
276
+ function useTouch() {
277
+ const startX = ref2(0);
278
+ const startY = ref2(0);
279
+ const deltaX = ref2(0);
280
+ const deltaY = ref2(0);
281
+ const offsetX = ref2(0);
282
+ const offsetY = ref2(0);
283
+ const prevX = ref2(0);
284
+ const prevY = ref2(0);
285
+ const moveX = ref2(0);
286
+ const moveY = ref2(0);
287
+ const direction = ref2();
288
+ const touching = ref2(false);
289
+ const dragging = ref2(false);
290
+ const startTime = ref2(0);
291
+ const distance = ref2(0);
292
+ let draggingAnimationFrame = null;
293
+ const resetTouch = () => {
294
+ startX.value = 0;
295
+ startY.value = 0;
296
+ deltaX.value = 0;
297
+ deltaY.value = 0;
298
+ offsetX.value = 0;
299
+ offsetY.value = 0;
300
+ prevX.value = 0;
301
+ prevY.value = 0;
302
+ moveX.value = 0;
303
+ moveY.value = 0;
304
+ direction.value = void 0;
305
+ touching.value = false;
306
+ dragging.value = false;
307
+ startTime.value = 0;
308
+ distance.value = 0;
309
+ };
310
+ const startTouch = (event) => {
311
+ resetTouch();
312
+ const { clientX: x, clientY: y } = event.touches[0];
313
+ startX.value = x;
314
+ startY.value = y;
315
+ prevX.value = x;
316
+ prevY.value = y;
317
+ touching.value = true;
318
+ startTime.value = performance.now();
319
+ dragging.value = false;
320
+ if (draggingAnimationFrame) {
321
+ window.cancelAnimationFrame(draggingAnimationFrame);
322
+ }
323
+ };
324
+ const moveTouch = (event) => {
325
+ const { clientX: x, clientY: y } = event.touches[0];
326
+ dragging.value = true;
327
+ deltaX.value = x - startX.value;
328
+ deltaY.value = y - startY.value;
329
+ offsetX.value = Math.abs(deltaX.value);
330
+ offsetY.value = Math.abs(deltaY.value);
331
+ distance.value = Math.sqrt(offsetX.value ** 2 + offsetY.value ** 2);
332
+ moveX.value = x - prevX.value;
333
+ moveY.value = y - prevY.value;
334
+ if (!direction.value) {
335
+ direction.value = getDirection(offsetX.value, offsetY.value);
336
+ }
337
+ prevX.value = x;
338
+ prevY.value = y;
339
+ };
340
+ const endTouch = () => {
341
+ touching.value = false;
342
+ draggingAnimationFrame = window.requestAnimationFrame(() => {
343
+ dragging.value = false;
344
+ });
345
+ };
346
+ return {
347
+ startX,
348
+ startY,
349
+ deltaX,
350
+ deltaY,
351
+ offsetX,
352
+ offsetY,
353
+ prevX,
354
+ prevY,
355
+ moveX,
356
+ moveY,
357
+ direction,
358
+ touching,
359
+ dragging,
360
+ startTime,
361
+ distance,
362
+ resetTouch,
363
+ startTouch,
364
+ moveTouch,
365
+ endTouch
366
+ };
367
+ }
368
+ export {
369
+ keyInProvides,
370
+ onSmartMounted,
371
+ onSmartUnmounted,
372
+ onWindowResize,
373
+ useChildren,
374
+ useClickOutside,
375
+ useEventListener,
376
+ useInitialized,
377
+ useParent,
378
+ useTouch
379
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@varlet/use",
3
- "version": "2.16.2",
3
+ "version": "2.16.3-alpha.1694361535255",
4
4
  "type": "module",
5
5
  "main": "lib/index.js",
6
6
  "module": "lib/index.js",
@@ -24,10 +24,10 @@
24
24
  "url": "https://github.com/varletjs/varlet/issues"
25
25
  },
26
26
  "dependencies": {
27
- "@varlet/shared": "2.16.2"
27
+ "@varlet/shared": "2.16.3-alpha.1694361535255"
28
28
  },
29
29
  "devDependencies": {
30
- "rimraf": "^5.0.1",
30
+ "tsup": "7.2.0",
31
31
  "typescript": "^5.1.5",
32
32
  "vue": "3.3.4"
33
33
  },
@@ -35,7 +35,7 @@
35
35
  "vue": "^3.2.0"
36
36
  },
37
37
  "scripts": {
38
- "dev": "tsc --watch",
39
- "build": "rimraf ./lib && tsc"
38
+ "dev": "tsup src/index.ts --format esm --out-dir=lib --watch --dts",
39
+ "build": "tsup src/index.ts --format esm --out-dir=lib --dts --clean"
40
40
  }
41
41
  }
@@ -1 +0,0 @@
1
- export declare function onSmartMounted(hook: () => void): void;
@@ -1,16 +0,0 @@
1
- import { onMounted, nextTick, onActivated } from 'vue';
2
- export function onSmartMounted(hook) {
3
- let isMounted = false;
4
- onMounted(() => {
5
- hook();
6
- nextTick(() => {
7
- isMounted = true;
8
- });
9
- });
10
- onActivated(() => {
11
- if (!isMounted) {
12
- return;
13
- }
14
- hook();
15
- });
16
- }
@@ -1 +0,0 @@
1
- export declare function onSmartUnmounted(hook: () => void): void;
@@ -1,9 +0,0 @@
1
- import { onDeactivated, onUnmounted } from 'vue';
2
- export function onSmartUnmounted(hook) {
3
- onUnmounted(() => {
4
- hook();
5
- });
6
- onDeactivated(() => {
7
- hook();
8
- });
9
- }
@@ -1 +0,0 @@
1
- export declare function onWindowResize(listener: EventListener): void;
@@ -1,5 +0,0 @@
1
- import { useEventListener } from './useEventListener.js';
2
- export function onWindowResize(listener) {
3
- useEventListener(() => window, 'resize', listener, { passive: true });
4
- useEventListener(() => window, 'orientationchange', listener, { passive: true });
5
- }
@@ -1,11 +0,0 @@
1
- import { type ComponentInternalInstance, type ComputedRef } from 'vue';
2
- export interface UseChildrenBaseProvider<C> {
3
- childInstances: ComponentInternalInstance[];
4
- collect(instance: ComponentInternalInstance, childProvider: C): void;
5
- clear(instance: ComponentInternalInstance, childProvider: C): void;
6
- }
7
- export declare function useChildren<P, C>(key: symbol | string): {
8
- length: ComputedRef<number>;
9
- childProviders: C[];
10
- bindChildren: (parentProvider: P) => void;
11
- };
@@ -1,50 +0,0 @@
1
- import { removeItem } from '@varlet/shared';
2
- import { getCurrentInstance, computed, provide, reactive, isVNode, } from 'vue';
3
- function flatVNodes(subTree) {
4
- const vNodes = [];
5
- const flat = (subTree) => {
6
- if (subTree === null || subTree === void 0 ? void 0 : subTree.component) {
7
- flat(subTree === null || subTree === void 0 ? void 0 : subTree.component.subTree);
8
- return;
9
- }
10
- if (Array.isArray(subTree === null || subTree === void 0 ? void 0 : subTree.children)) {
11
- subTree.children.forEach((child) => {
12
- if (isVNode(child)) {
13
- vNodes.push(child);
14
- flat(child);
15
- }
16
- });
17
- }
18
- };
19
- flat(subTree);
20
- return vNodes;
21
- }
22
- export function useChildren(key) {
23
- const parentInstance = getCurrentInstance();
24
- const childInstances = reactive([]);
25
- const childProviders = [];
26
- const length = computed(() => childInstances.length);
27
- const sortInstances = () => {
28
- const vNodes = flatVNodes(parentInstance.subTree);
29
- childInstances.sort((a, b) => vNodes.indexOf(a.vnode) - vNodes.indexOf(b.vnode));
30
- };
31
- const collect = (childInstance, childProvider) => {
32
- childInstances.push(childInstance);
33
- childProviders.push(childProvider);
34
- sortInstances();
35
- };
36
- const clear = (childInstance, childProvider) => {
37
- removeItem(childInstances, childInstance);
38
- removeItem(childProviders, childProvider);
39
- };
40
- const bindChildren = (parentProvider) => {
41
- provide(key, Object.assign({ childInstances,
42
- collect,
43
- clear }, parentProvider));
44
- };
45
- return {
46
- length,
47
- childProviders,
48
- bindChildren,
49
- };
50
- }
@@ -1,3 +0,0 @@
1
- import { type Ref } from 'vue';
2
- export type UseClickOutsideTarget = Element | Ref<Element | undefined | null> | (() => Element);
3
- export declare function useClickOutside(target: UseClickOutsideTarget, type: string, listener: EventListener): void;
@@ -1,15 +0,0 @@
1
- import { useEventListener } from './useEventListener.js';
2
- import { inBrowser, isFunction } from '@varlet/shared';
3
- import { unref } from 'vue';
4
- export function useClickOutside(target, type, listener) {
5
- if (!inBrowser()) {
6
- return;
7
- }
8
- const handler = (event) => {
9
- const element = isFunction(target) ? target() : unref(target);
10
- if (element && !element.contains(event.target)) {
11
- listener(event);
12
- }
13
- };
14
- useEventListener(document, type, handler);
15
- }
@@ -1,8 +0,0 @@
1
- import { type Ref } from 'vue';
2
- export type UseEventListenerTarget = EventTarget | Ref<EventTarget | undefined | null> | (() => EventTarget);
3
- export interface UseEventListenerOptions {
4
- capture?: boolean;
5
- passive?: boolean;
6
- }
7
- export declare function useEventListener<T extends keyof DocumentEventMap>(target: UseEventListenerTarget, type: T, listener: (event: DocumentEventMap[T]) => void, options?: UseEventListenerOptions): () => void;
8
- export declare function useEventListener(target: UseEventListenerTarget, type: string, listener: EventListener, options?: UseEventListenerOptions): () => void;
@@ -1,59 +0,0 @@
1
- import { inBrowser, isFunction } from '@varlet/shared';
2
- import { isRef, onDeactivated, onBeforeUnmount, unref, watch } from 'vue';
3
- import { onSmartMounted } from './onSmartMounted.js';
4
- export function useEventListener(target, type, listener, options = {}) {
5
- if (!inBrowser()) {
6
- return;
7
- }
8
- const { passive = false, capture = false } = options;
9
- let listening = false;
10
- let cleaned = false;
11
- const getElement = (target) => isFunction(target) ? target() : unref(target);
12
- const add = (target) => {
13
- if (listening || cleaned) {
14
- return;
15
- }
16
- const element = getElement(target);
17
- if (element) {
18
- element.addEventListener(type, listener, {
19
- passive,
20
- capture,
21
- });
22
- listening = true;
23
- }
24
- };
25
- const remove = (target) => {
26
- if (!listening || cleaned) {
27
- return;
28
- }
29
- const element = getElement(target);
30
- if (element) {
31
- element.removeEventListener(type, listener, {
32
- capture,
33
- });
34
- listening = false;
35
- }
36
- };
37
- let watchStopHandle;
38
- if (isRef(target)) {
39
- watchStopHandle = watch(() => target.value, (newValue, oldValue) => {
40
- remove(oldValue);
41
- add(newValue);
42
- });
43
- }
44
- const cleanup = () => {
45
- watchStopHandle === null || watchStopHandle === void 0 ? void 0 : watchStopHandle();
46
- remove(target);
47
- cleaned = true;
48
- };
49
- onSmartMounted(() => {
50
- add(target);
51
- });
52
- onBeforeUnmount(() => {
53
- remove(target);
54
- });
55
- onDeactivated(() => {
56
- remove(target);
57
- });
58
- return cleanup;
59
- }
@@ -1,2 +0,0 @@
1
- import { type WatchSource } from 'vue';
2
- export declare function useInitialized<T>(source: WatchSource<T>, value: T): import("vue").Ref<boolean>;
@@ -1,10 +0,0 @@
1
- import { watch, ref } from 'vue';
2
- export function useInitialized(source, value) {
3
- const initialized = ref(false);
4
- watch(source, (newValue) => {
5
- if (value === newValue) {
6
- initialized.value = true;
7
- }
8
- }, { immediate: true });
9
- return initialized;
10
- }
@@ -1,12 +0,0 @@
1
- import { type ComputedRef } from 'vue';
2
- import { type UseChildrenBaseProvider } from './useChildren.js';
3
- export declare function keyInProvides(key: symbol | string): boolean;
4
- export declare function useParent<P, C>(key: symbol | string): {
5
- index: null;
6
- parentProvider: null;
7
- bindParent: null;
8
- } | {
9
- index: ComputedRef<number>;
10
- parentProvider: Omit<P & UseChildrenBaseProvider<C>, "childInstances" | "collect" | "clear">;
11
- bindParent: (childProvider: C) => void;
12
- };
package/lib/useParent.js DELETED
@@ -1,46 +0,0 @@
1
- var __rest = (this && this.__rest) || function (s, e) {
2
- var t = {};
3
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
- t[p] = s[p];
5
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
- t[p[i]] = s[p[i]];
9
- }
10
- return t;
11
- };
12
- import { getCurrentInstance, inject, onMounted, onBeforeUnmount, nextTick, computed, } from 'vue';
13
- export function keyInProvides(key) {
14
- const instance = getCurrentInstance();
15
- return key in instance.provides;
16
- }
17
- export function useParent(key) {
18
- if (!keyInProvides(key)) {
19
- return {
20
- index: null,
21
- parentProvider: null,
22
- bindParent: null,
23
- };
24
- }
25
- const provider = inject(key);
26
- const { childInstances, collect, clear } = provider, parentProvider = __rest(provider, ["childInstances", "collect", "clear"]);
27
- const childInstance = getCurrentInstance();
28
- const index = computed(() => childInstances.indexOf(childInstance));
29
- const bindParent = (childProvider) => {
30
- onMounted(() => {
31
- nextTick().then(() => {
32
- collect(childInstance, childProvider);
33
- });
34
- });
35
- onBeforeUnmount(() => {
36
- nextTick().then(() => {
37
- clear(childInstance, childProvider);
38
- });
39
- });
40
- };
41
- return {
42
- index,
43
- parentProvider,
44
- bindParent,
45
- };
46
- }
package/lib/useTouch.d.ts DELETED
@@ -1,22 +0,0 @@
1
- export type TouchDirection = 'horizontal' | 'vertical';
2
- export declare function useTouch(): {
3
- startX: import("vue").Ref<number>;
4
- startY: import("vue").Ref<number>;
5
- deltaX: import("vue").Ref<number>;
6
- deltaY: import("vue").Ref<number>;
7
- offsetX: import("vue").Ref<number>;
8
- offsetY: import("vue").Ref<number>;
9
- prevX: import("vue").Ref<number>;
10
- prevY: import("vue").Ref<number>;
11
- moveX: import("vue").Ref<number>;
12
- moveY: import("vue").Ref<number>;
13
- direction: import("vue").Ref<TouchDirection | undefined>;
14
- touching: import("vue").Ref<boolean>;
15
- dragging: import("vue").Ref<boolean>;
16
- startTime: import("vue").Ref<number>;
17
- distance: import("vue").Ref<number>;
18
- resetTouch: () => void;
19
- startTouch: (event: TouchEvent) => void;
20
- moveTouch: (event: TouchEvent) => void;
21
- endTouch: () => void;
22
- };
package/lib/useTouch.js DELETED
@@ -1,101 +0,0 @@
1
- import { ref } from 'vue';
2
- function getDirection(x, y) {
3
- if (x > y) {
4
- return 'horizontal';
5
- }
6
- if (y > x) {
7
- return 'vertical';
8
- }
9
- }
10
- export function useTouch() {
11
- const startX = ref(0);
12
- const startY = ref(0);
13
- const deltaX = ref(0);
14
- const deltaY = ref(0);
15
- const offsetX = ref(0);
16
- const offsetY = ref(0);
17
- const prevX = ref(0);
18
- const prevY = ref(0);
19
- const moveX = ref(0);
20
- const moveY = ref(0);
21
- const direction = ref();
22
- const touching = ref(false);
23
- const dragging = ref(false);
24
- const startTime = ref(0);
25
- const distance = ref(0);
26
- let draggingAnimationFrame = null;
27
- const resetTouch = () => {
28
- startX.value = 0;
29
- startY.value = 0;
30
- deltaX.value = 0;
31
- deltaY.value = 0;
32
- offsetX.value = 0;
33
- offsetY.value = 0;
34
- prevX.value = 0;
35
- prevY.value = 0;
36
- moveX.value = 0;
37
- moveY.value = 0;
38
- direction.value = undefined;
39
- touching.value = false;
40
- dragging.value = false;
41
- startTime.value = 0;
42
- distance.value = 0;
43
- };
44
- const startTouch = (event) => {
45
- resetTouch();
46
- const { clientX: x, clientY: y } = event.touches[0];
47
- startX.value = x;
48
- startY.value = y;
49
- prevX.value = x;
50
- prevY.value = y;
51
- touching.value = true;
52
- startTime.value = performance.now();
53
- dragging.value = false;
54
- if (draggingAnimationFrame) {
55
- window.cancelAnimationFrame(draggingAnimationFrame);
56
- }
57
- };
58
- const moveTouch = (event) => {
59
- const { clientX: x, clientY: y } = event.touches[0];
60
- dragging.value = true;
61
- deltaX.value = x - startX.value;
62
- deltaY.value = y - startY.value;
63
- offsetX.value = Math.abs(deltaX.value);
64
- offsetY.value = Math.abs(deltaY.value);
65
- distance.value = Math.sqrt(offsetX.value ** 2 + offsetY.value ** 2);
66
- moveX.value = x - prevX.value;
67
- moveY.value = y - prevY.value;
68
- if (!direction.value) {
69
- direction.value = getDirection(offsetX.value, offsetY.value);
70
- }
71
- prevX.value = x;
72
- prevY.value = y;
73
- };
74
- const endTouch = () => {
75
- touching.value = false;
76
- draggingAnimationFrame = window.requestAnimationFrame(() => {
77
- dragging.value = false;
78
- });
79
- };
80
- return {
81
- startX,
82
- startY,
83
- deltaX,
84
- deltaY,
85
- offsetX,
86
- offsetY,
87
- prevX,
88
- prevY,
89
- moveX,
90
- moveY,
91
- direction,
92
- touching,
93
- dragging,
94
- startTime,
95
- distance,
96
- resetTouch,
97
- startTouch,
98
- moveTouch,
99
- endTouch,
100
- };
101
- }