@vrojs/use 0.0.2 → 0.0.4

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/dist/index.d.ts CHANGED
@@ -50,12 +50,35 @@ interface UseAsyncTaskResult<T, D> {
50
50
  declare function useAsyncTask<T extends AnyPromiseFn>(task: T, options: UseAsyncTaskOptions<T> & Required<Pick<UseAsyncTaskOptions<T>, 'initialValue'>>): UseAsyncTaskResult<T, Awaited<ReturnType<T>>>;
51
51
  declare function useAsyncTask<T extends AnyPromiseFn>(task: T, options?: UseAsyncTaskOptions<T>): UseAsyncTaskResult<T, Awaited<ReturnType<T>> | undefined>;
52
52
 
53
+ type MaybeRef<T> = T | Ref<T>;
54
+ interface UseBodyScrollLockReturn {
55
+ locked: Ref<boolean>;
56
+ lock: () => void;
57
+ unlock: () => void;
58
+ toggle: (value?: boolean) => void;
59
+ }
60
+ /**
61
+ * 锁定/解锁 body 滚动
62
+ * @param initialLocked - 初始锁定状态,或外部响应式布尔值
63
+ */
64
+ declare function useBodyScrollLock(initialLocked?: MaybeRef<boolean>): UseBodyScrollLockReturn;
65
+
53
66
  declare function useComposition(): {
54
67
  handleCompositionStart: (event: CompositionEvent) => void;
55
68
  handleCompositionEnd: (event: CompositionEvent) => void;
56
69
  };
57
70
 
58
- declare function useCountDown(start?: number): readonly [vue.Ref<number, number>, (st?: number) => void];
71
+ interface UseCountDownOptions {
72
+ /**
73
+ * 每次倒计时值变化时触发,包括初始值和最终的 0
74
+ */
75
+ onChange?: (count: number) => void;
76
+ /**
77
+ * 倒计时结束时触发
78
+ */
79
+ onFinish?: () => void;
80
+ }
81
+ declare function useCountDown(start?: number, options?: UseCountDownOptions): readonly [vue.Ref<number, number>, (st?: number) => void];
59
82
 
60
83
  type Loose<T> = T & {
61
84
  [key: string]: any;
@@ -260,4 +283,4 @@ declare function useVisible<T extends Record<string, any>, R = any>(options?: Us
260
283
  hide: (reason?: any) => Promise<void>;
261
284
  };
262
285
 
263
- export { type TransferOptions, type UseAsyncTaskOptions, type UseAsyncTaskResult, type UseEventListenerOptions, type UseKeepAliveOptions, type UseKeepAliveState$1 as UseKeepAliveState, type UseKeepPositionOptions, type UseKeepPositionState, type UsePagingOptions, type UsePagingStatus, type UsePagingTask, type UsePagingTaskOptions, type UsePagingTaskResult, type UsePagingTriggerOptions, type UseSetState, type UseSetStateAction, type UseTransitionNameOptions, type UseTransitionNameState, type UseVisibleOptions, type UseVisibleShowOptions, debounceRef, useAsyncTask, useComposition, useCountDown, useEventListener, useId, useKeepAlive, useKeepAliveByPosition, useKeepPosition, usePaging, useParams, useQuery, useState, useToggle, useTransfer, useTransitionName, useVisible };
286
+ export { type TransferOptions, type UseAsyncTaskOptions, type UseAsyncTaskResult, type UseBodyScrollLockReturn, type UseCountDownOptions, type UseEventListenerOptions, type UseKeepAliveOptions, type UseKeepAliveState$1 as UseKeepAliveState, type UseKeepPositionOptions, type UseKeepPositionState, type UsePagingOptions, type UsePagingStatus, type UsePagingTask, type UsePagingTaskOptions, type UsePagingTaskResult, type UsePagingTriggerOptions, type UseSetState, type UseSetStateAction, type UseTransitionNameOptions, type UseTransitionNameState, type UseVisibleOptions, type UseVisibleShowOptions, debounceRef, useAsyncTask, useBodyScrollLock, useComposition, useCountDown, useEventListener, useId, useKeepAlive, useKeepAliveByPosition, useKeepPosition, usePaging, useParams, useQuery, useState, useToggle, useTransfer, useTransitionName, useVisible };
package/dist/index.js CHANGED
@@ -117,6 +117,66 @@ function useAsyncTask(task, options) {
117
117
  };
118
118
  }
119
119
 
120
+ // src/useBodyScrollLock/index.ts
121
+ import { getCurrentScope, isRef, onScopeDispose, ref as ref2, unref, watch } from "vue";
122
+ var lockCount = 0;
123
+ var originalOverflow = "";
124
+ function lockBody() {
125
+ if (typeof document === "undefined") return;
126
+ if (lockCount === 0) {
127
+ originalOverflow = document.body.style.overflow;
128
+ document.body.style.overflow = "hidden";
129
+ }
130
+ lockCount++;
131
+ }
132
+ function unlockBody() {
133
+ if (typeof document === "undefined" || lockCount <= 0) return;
134
+ lockCount--;
135
+ if (lockCount === 0) {
136
+ document.body.style.overflow = originalOverflow;
137
+ originalOverflow = "";
138
+ }
139
+ }
140
+ function useBodyScrollLock(initialLocked = false) {
141
+ const locked = isRef(initialLocked) ? initialLocked : ref2(unref(initialLocked));
142
+ let currentLocked = false;
143
+ const sync = (value) => {
144
+ if (value === currentLocked) return;
145
+ if (value) {
146
+ lockBody();
147
+ } else {
148
+ unlockBody();
149
+ }
150
+ currentLocked = value;
151
+ };
152
+ const stop = watch(locked, sync, { immediate: true });
153
+ const cleanup = () => {
154
+ stop();
155
+ if (currentLocked) {
156
+ unlockBody();
157
+ currentLocked = false;
158
+ }
159
+ };
160
+ if (getCurrentScope()) {
161
+ onScopeDispose(cleanup);
162
+ }
163
+ const lock = () => {
164
+ locked.value = true;
165
+ };
166
+ const unlock = () => {
167
+ locked.value = false;
168
+ };
169
+ const toggle = (value = !locked.value) => {
170
+ locked.value = value;
171
+ };
172
+ return {
173
+ locked,
174
+ lock,
175
+ unlock,
176
+ toggle
177
+ };
178
+ }
179
+
120
180
  // src/useComposition/index.ts
121
181
  function useComposition() {
122
182
  const handleCompositionStart = (event) => {
@@ -136,21 +196,23 @@ function useComposition() {
136
196
  }
137
197
 
138
198
  // src/useCountDown/index.ts
139
- import { ref as ref2 } from "vue";
140
- function useCountDown(start = 60) {
141
- const countRef = ref2(0);
199
+ import { ref as ref3 } from "vue";
200
+ function useCountDown(start = 60, options = {}) {
201
+ const countRef = ref3(0);
142
202
  let timer;
143
203
  const trigger = (st = start) => {
144
204
  clearTimeout(timer);
145
205
  let loop;
146
206
  (loop = (count) => {
207
+ var _a, _b;
208
+ count = Math.max(count, 0);
147
209
  countRef.value = count;
210
+ (_a = options.onChange) == null ? void 0 : _a.call(options, count);
148
211
  if (count <= 0) {
149
- countRef.value = 0;
212
+ (_b = options.onFinish) == null ? void 0 : _b.call(options);
150
213
  return;
151
214
  }
152
- count--;
153
- timer = setTimeout(loop, 1e3, count);
215
+ timer = setTimeout(loop, 1e3, count - 1);
154
216
  })(st);
155
217
  };
156
218
  return [countRef, trigger];
@@ -158,19 +220,19 @@ function useCountDown(start = 60) {
158
220
 
159
221
  // src/useEventListener/index.ts
160
222
  import {
161
- watch,
162
- isRef,
163
- unref,
164
- onUnmounted,
223
+ isRef as isRef2,
224
+ onActivated as onActivated2,
165
225
  onDeactivated,
166
226
  onMounted,
167
- onActivated as onActivated2
227
+ onUnmounted,
228
+ unref as unref2,
229
+ watch as watch2
168
230
  } from "vue";
169
231
  function useEventListener(type, listener, options = {}) {
170
232
  const { target = window, passive = false, capture = false } = options;
171
233
  let attached;
172
234
  const add = (target2) => {
173
- let element = unref(target2);
235
+ let element = unref2(target2);
174
236
  if (element == null ? void 0 : element.$el) {
175
237
  element = element.$el;
176
238
  }
@@ -183,7 +245,7 @@ function useEventListener(type, listener, options = {}) {
183
245
  }
184
246
  };
185
247
  const remove = (target2) => {
186
- let element = unref(target2);
248
+ let element = unref2(target2);
187
249
  if (element == null ? void 0 : element.$el) {
188
250
  element = element.$el;
189
251
  }
@@ -196,8 +258,8 @@ function useEventListener(type, listener, options = {}) {
196
258
  onDeactivated(() => remove(target));
197
259
  onActivated2(() => add(target));
198
260
  onMounted(() => add(target));
199
- if (isRef(target)) {
200
- watch(target, (val, oldVal) => {
261
+ if (isRef2(target)) {
262
+ watch2(target, (val, oldVal) => {
201
263
  remove(oldVal);
202
264
  add(val);
203
265
  });
@@ -215,10 +277,10 @@ function useId() {
215
277
 
216
278
  // src/useKeepAlive/index.ts
217
279
  import { isNumber, isUndefined } from "@daysnap/utils";
218
- import { ref as ref3, watch as watch2 } from "vue";
280
+ import { ref as ref4, watch as watch3 } from "vue";
219
281
  import { useRoute } from "vue-router";
220
- var keepAliveList = ref3();
221
- var includes = ref3([]);
282
+ var keepAliveList = ref4();
283
+ var includes = ref4([]);
222
284
  var getHistoryState = () => {
223
285
  var _a;
224
286
  return (_a = history.state) != null ? _a : {};
@@ -228,7 +290,7 @@ function useKeepAlive(options) {
228
290
  if (isUndefined(keepAliveList.value)) {
229
291
  keepAliveList.value = [];
230
292
  const route = useRoute();
231
- watch2(
293
+ watch3(
232
294
  () => __spreadValues({}, route),
233
295
  (to, from) => {
234
296
  var _a2;
@@ -253,7 +315,7 @@ function useKeepAlive(options) {
253
315
  }
254
316
  );
255
317
  const ms = isNumber(options) ? options : 0;
256
- watch2(
318
+ watch3(
257
319
  () => keepAliveList.value,
258
320
  (nv, ov) => {
259
321
  if (JSON.stringify(nv) !== JSON.stringify(ov)) {
@@ -286,10 +348,10 @@ function useKeepAlive(options) {
286
348
  }
287
349
 
288
350
  // src/useKeepAliveByPosition/index.ts
289
- import { ref as ref4, watch as watch3, computed } from "vue";
351
+ import { isBoolean, isUndefined as isUndefined2 } from "@daysnap/utils";
352
+ import { computed, ref as ref5, watch as watch4 } from "vue";
290
353
  import { useRoute as useRoute2 } from "vue-router";
291
- import { isUndefined as isUndefined2, isBoolean } from "@daysnap/utils";
292
- var keepAliveList2 = ref4();
354
+ var keepAliveList2 = ref5();
293
355
  var setState = (state, ms = 0) => {
294
356
  if (JSON.stringify(state) !== JSON.stringify(keepAliveList2.value)) {
295
357
  setTimeout(() => keepAliveList2.value = state, ms);
@@ -310,7 +372,7 @@ function useKeepAliveByPosition(options) {
310
372
  if (isUndefined2(keepAliveList2.value)) {
311
373
  keepAliveList2.value = [];
312
374
  const route = useRoute2();
313
- watch3(
375
+ watch4(
314
376
  () => __spreadValues({}, route),
315
377
  (_, from) => {
316
378
  var _a2;
@@ -346,11 +408,11 @@ function useKeepAliveByPosition(options) {
346
408
 
347
409
  // src/useKeepPosition/index.ts
348
410
  import { pick } from "@daysnap/utils";
349
- import { onActivated as onActivated3, nextTick, ref as ref5 } from "vue";
411
+ import { nextTick, onActivated as onActivated3, ref as ref6 } from "vue";
350
412
  import { onBeforeRouteLeave } from "vue-router";
351
413
  function useKeepPosition(options = {}) {
352
414
  const { selectors } = options;
353
- const state = ref5();
415
+ const state = ref6();
354
416
  const getTarget = () => {
355
417
  var _a, _b;
356
418
  return (_b = (_a = options.getTarget) == null ? void 0 : _a.call(options)) != null ? _b : selectors ? document.querySelector(selectors) : document.documentElement;
@@ -383,7 +445,7 @@ function useKeepPosition(options = {}) {
383
445
 
384
446
  // src/usePaging/index.ts
385
447
  import { isArray, isBoolean as isBoolean2, isFunction as isFunction2, isObject, isString } from "@daysnap/utils";
386
- import { computed as computed2, onBeforeMount as onBeforeMount2, reactive, ref as ref6 } from "vue";
448
+ import { computed as computed2, onBeforeMount as onBeforeMount2, reactive, ref as ref7 } from "vue";
387
449
  function usePaging(task, options = {}) {
388
450
  const { initialStatus, immediate, scrollSelector = ".hor-scroll" } = options;
389
451
  const pagingStatus = reactive(
@@ -407,7 +469,7 @@ function usePaging(task, options = {}) {
407
469
  }
408
470
  }
409
471
  };
410
- const pagingData = ref6([]);
472
+ const pagingData = ref7([]);
411
473
  const pagingReset = () => {
412
474
  pagingStatus.pagingIndex = 0;
413
475
  pagingStatus.pagingTotal = -1;
@@ -473,7 +535,10 @@ function usePaging(task, options = {}) {
473
535
  if (isFunction2(options2)) {
474
536
  options2();
475
537
  }
476
- return console.log("\u6CA1\u6709\u66F4\u591A\u4E86");
538
+ return console.log("\u6CA1\u6709\u66F4\u591A\u4E86...");
539
+ }
540
+ if (pagingStatus.pagingLoading) {
541
+ return console.log("\u6B63\u5728\u52A0\u8F7D\u4E2D...");
477
542
  }
478
543
  pagingTrigger(pagingStatus.pagingIndex + 1, options2);
479
544
  };
@@ -512,9 +577,9 @@ function useQuery() {
512
577
 
513
578
  // src/useState/index.ts
514
579
  import { isFunction as isFunction3 } from "@daysnap/utils";
515
- import { ref as ref7 } from "vue";
580
+ import { ref as ref8 } from "vue";
516
581
  function useState(initialState) {
517
- const state = ref7(initialState);
582
+ const state = ref8(initialState);
518
583
  const setState2 = (value) => {
519
584
  state.value = isFunction3(value) ? value(state.value) : value;
520
585
  };
@@ -522,9 +587,9 @@ function useState(initialState) {
522
587
  }
523
588
 
524
589
  // src/useToggle/index.ts
525
- import { ref as ref8 } from "vue";
590
+ import { ref as ref9 } from "vue";
526
591
  function useToggle(initialValue = false) {
527
- const visible = ref8(initialValue);
592
+ const visible = ref9(initialValue);
528
593
  const toggle = (value = !visible.value) => {
529
594
  visible.value = value;
530
595
  };
@@ -559,7 +624,7 @@ function useTransfer() {
559
624
  }
560
625
 
561
626
  // src/useTransitionName/index.ts
562
- import { ref as ref9, watch as watch4 } from "vue";
627
+ import { ref as ref10, watch as watch5 } from "vue";
563
628
  import { useRoute as useRoute5 } from "vue-router";
564
629
  var KEY = "$$useTransitionName__state";
565
630
  var get = () => {
@@ -575,9 +640,9 @@ var set = (state) => {
575
640
  };
576
641
  function useTransitionName(options) {
577
642
  const { deep } = options;
578
- const name = ref9("");
643
+ const name = ref10("");
579
644
  const route = useRoute5();
580
- watch4(
645
+ watch5(
581
646
  () => __spreadValues({}, route),
582
647
  (to, from) => {
583
648
  const { position } = history.state || {};
@@ -603,10 +668,10 @@ function useTransitionName(options) {
603
668
  }
604
669
 
605
670
  // src/useVisible/index.ts
606
- import { ref as ref10 } from "vue";
671
+ import { ref as ref11 } from "vue";
607
672
  function useVisible(options = {}) {
608
673
  const { showCallback, hideCallback, confirmCallback } = options;
609
- const visible = ref10(false);
674
+ const visible = ref11(false);
610
675
  let resolve;
611
676
  let reject;
612
677
  let beforeClose;
@@ -650,6 +715,7 @@ function useVisible(options = {}) {
650
715
  export {
651
716
  debounceRef,
652
717
  useAsyncTask,
718
+ useBodyScrollLock,
653
719
  useComposition,
654
720
  useCountDown,
655
721
  useEventListener,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vrojs/use",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "type": "module",
5
5
  "description": "vrojs composables",
6
6
  "main": "dist/index.js",
@@ -28,7 +28,7 @@
28
28
  "vue-router": ">=4.2.4"
29
29
  },
30
30
  "devDependencies": {
31
- "@daysnap/utils": "^0.1.34",
31
+ "@daysnap/utils": "^0.1.36",
32
32
  "rimraf": "^6.0.1",
33
33
  "tsup": "^8.5.1",
34
34
  "typescript": "^5.9.3",