@vueuse/components 8.5.0 → 8.7.1

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/index.cjs CHANGED
@@ -23,9 +23,8 @@ const OnClickOutside = vueDemi.defineComponent({
23
23
  });
24
24
 
25
25
  function unrefElement(elRef) {
26
- var _a;
27
26
  const plain = vueDemi.unref(elRef);
28
- return (_a = plain == null ? void 0 : plain.$el) != null ? _a : plain;
27
+ return plain != null && "$el" in plain ? plain.$el : plain;
29
28
  }
30
29
 
31
30
  const defaultWindow = shared.isClient ? window : void 0;
@@ -90,7 +89,11 @@ function onClickOutside(target, handler, options = {}) {
90
89
  shouldListen.value = !!el && !e.composedPath().includes(el);
91
90
  }, { passive: true }),
92
91
  useEventListener(window, "pointerup", (e) => {
93
- fallback = window.setTimeout(() => listener(e), 50);
92
+ if (e.button === 0) {
93
+ const path = e.composedPath();
94
+ e.composedPath = () => path;
95
+ fallback = window.setTimeout(() => listener(e), 50);
96
+ }
94
97
  }, { passive: true })
95
98
  ];
96
99
  const stop = () => cleanup.forEach((fn) => fn());
@@ -132,19 +135,19 @@ function onKeyStroke(key, handler, options = {}) {
132
135
  return useEventListener(target, eventName, listener, passive);
133
136
  }
134
137
 
135
- var __defProp$b = Object.defineProperty;
136
- var __getOwnPropSymbols$c = Object.getOwnPropertySymbols;
137
- var __hasOwnProp$c = Object.prototype.hasOwnProperty;
138
- var __propIsEnum$c = Object.prototype.propertyIsEnumerable;
139
- var __defNormalProp$b = (obj, key, value) => key in obj ? __defProp$b(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
140
- var __spreadValues$b = (a, b) => {
138
+ var __defProp$c = Object.defineProperty;
139
+ var __getOwnPropSymbols$d = Object.getOwnPropertySymbols;
140
+ var __hasOwnProp$d = Object.prototype.hasOwnProperty;
141
+ var __propIsEnum$d = Object.prototype.propertyIsEnumerable;
142
+ var __defNormalProp$c = (obj, key, value) => key in obj ? __defProp$c(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
143
+ var __spreadValues$c = (a, b) => {
141
144
  for (var prop in b || (b = {}))
142
- if (__hasOwnProp$c.call(b, prop))
143
- __defNormalProp$b(a, prop, b[prop]);
144
- if (__getOwnPropSymbols$c)
145
- for (var prop of __getOwnPropSymbols$c(b)) {
146
- if (__propIsEnum$c.call(b, prop))
147
- __defNormalProp$b(a, prop, b[prop]);
145
+ if (__hasOwnProp$d.call(b, prop))
146
+ __defNormalProp$c(a, prop, b[prop]);
147
+ if (__getOwnPropSymbols$d)
148
+ for (var prop of __getOwnPropSymbols$d(b)) {
149
+ if (__propIsEnum$d.call(b, prop))
150
+ __defNormalProp$c(a, prop, b[prop]);
148
151
  }
149
152
  return a;
150
153
  };
@@ -158,7 +161,7 @@ const vOnKeyStroke = {
158
161
  });
159
162
  } else {
160
163
  const [handler, options] = binding.value;
161
- onKeyStroke(keys, handler, __spreadValues$b({
164
+ onKeyStroke(keys, handler, __spreadValues$c({
162
165
  target: el
163
166
  }, options));
164
167
  }
@@ -398,19 +401,19 @@ function usePreferredDark(options) {
398
401
  return useMediaQuery("(prefers-color-scheme: dark)", options);
399
402
  }
400
403
 
401
- var __defProp$a = Object.defineProperty;
402
- var __getOwnPropSymbols$b = Object.getOwnPropertySymbols;
403
- var __hasOwnProp$b = Object.prototype.hasOwnProperty;
404
- var __propIsEnum$b = Object.prototype.propertyIsEnumerable;
405
- var __defNormalProp$a = (obj, key, value) => key in obj ? __defProp$a(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
406
- var __spreadValues$a = (a, b) => {
404
+ var __defProp$b = Object.defineProperty;
405
+ var __getOwnPropSymbols$c = Object.getOwnPropertySymbols;
406
+ var __hasOwnProp$c = Object.prototype.hasOwnProperty;
407
+ var __propIsEnum$c = Object.prototype.propertyIsEnumerable;
408
+ var __defNormalProp$b = (obj, key, value) => key in obj ? __defProp$b(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
409
+ var __spreadValues$b = (a, b) => {
407
410
  for (var prop in b || (b = {}))
408
- if (__hasOwnProp$b.call(b, prop))
409
- __defNormalProp$a(a, prop, b[prop]);
410
- if (__getOwnPropSymbols$b)
411
- for (var prop of __getOwnPropSymbols$b(b)) {
412
- if (__propIsEnum$b.call(b, prop))
413
- __defNormalProp$a(a, prop, b[prop]);
411
+ if (__hasOwnProp$c.call(b, prop))
412
+ __defNormalProp$b(a, prop, b[prop]);
413
+ if (__getOwnPropSymbols$c)
414
+ for (var prop of __getOwnPropSymbols$c(b)) {
415
+ if (__propIsEnum$c.call(b, prop))
416
+ __defNormalProp$b(a, prop, b[prop]);
414
417
  }
415
418
  return a;
416
419
  };
@@ -424,7 +427,7 @@ function useColorMode(options = {}) {
424
427
  listenToStorageChanges = true,
425
428
  storageRef
426
429
  } = options;
427
- const modes = __spreadValues$a({
430
+ const modes = __spreadValues$b({
428
431
  auto: "",
429
432
  light: "light",
430
433
  dark: "dark"
@@ -562,21 +565,21 @@ const UseDocumentVisibility = vueDemi.defineComponent({
562
565
  }
563
566
  });
564
567
 
565
- var __defProp$9 = Object.defineProperty;
568
+ var __defProp$a = Object.defineProperty;
566
569
  var __defProps$8 = Object.defineProperties;
567
570
  var __getOwnPropDescs$8 = Object.getOwnPropertyDescriptors;
568
- var __getOwnPropSymbols$a = Object.getOwnPropertySymbols;
569
- var __hasOwnProp$a = Object.prototype.hasOwnProperty;
570
- var __propIsEnum$a = Object.prototype.propertyIsEnumerable;
571
- var __defNormalProp$9 = (obj, key, value) => key in obj ? __defProp$9(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
572
- var __spreadValues$9 = (a, b) => {
571
+ var __getOwnPropSymbols$b = Object.getOwnPropertySymbols;
572
+ var __hasOwnProp$b = Object.prototype.hasOwnProperty;
573
+ var __propIsEnum$b = Object.prototype.propertyIsEnumerable;
574
+ var __defNormalProp$a = (obj, key, value) => key in obj ? __defProp$a(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
575
+ var __spreadValues$a = (a, b) => {
573
576
  for (var prop in b || (b = {}))
574
- if (__hasOwnProp$a.call(b, prop))
575
- __defNormalProp$9(a, prop, b[prop]);
576
- if (__getOwnPropSymbols$a)
577
- for (var prop of __getOwnPropSymbols$a(b)) {
578
- if (__propIsEnum$a.call(b, prop))
579
- __defNormalProp$9(a, prop, b[prop]);
577
+ if (__hasOwnProp$b.call(b, prop))
578
+ __defNormalProp$a(a, prop, b[prop]);
579
+ if (__getOwnPropSymbols$b)
580
+ for (var prop of __getOwnPropSymbols$b(b)) {
581
+ if (__propIsEnum$b.call(b, prop))
582
+ __defNormalProp$a(a, prop, b[prop]);
580
583
  }
581
584
  return a;
582
585
  };
@@ -589,13 +592,14 @@ const UseDraggable = vueDemi.defineComponent({
589
592
  "initialValue",
590
593
  "exact",
591
594
  "preventDefault",
595
+ "stopPropagation",
592
596
  "pointerTypes",
593
597
  "as"
594
598
  ],
595
599
  setup(props, { slots }) {
596
600
  const target = vueDemi.ref();
597
601
  const initialValue = props.storageKey ? core.useStorage(props.storageKey, vueDemi.unref(props.initialValue) || { x: 0, y: 0 }, core.isClient ? props.storageType === "session" ? sessionStorage : localStorage : void 0) : props.initialValue || { x: 0, y: 0 };
598
- const data = vueDemi.reactive(core.useDraggable(target, __spreadProps$8(__spreadValues$9({}, props), {
602
+ const data = vueDemi.reactive(core.useDraggable(target, __spreadProps$8(__spreadValues$a({}, props), {
599
603
  initialValue
600
604
  })));
601
605
  return () => {
@@ -647,17 +651,17 @@ const UseElementSize = vueDemi.defineComponent({
647
651
  }
648
652
  });
649
653
 
650
- var __getOwnPropSymbols$9 = Object.getOwnPropertySymbols;
651
- var __hasOwnProp$9 = Object.prototype.hasOwnProperty;
652
- var __propIsEnum$9 = Object.prototype.propertyIsEnumerable;
654
+ var __getOwnPropSymbols$a = Object.getOwnPropertySymbols;
655
+ var __hasOwnProp$a = Object.prototype.hasOwnProperty;
656
+ var __propIsEnum$a = Object.prototype.propertyIsEnumerable;
653
657
  var __objRest = (source, exclude) => {
654
658
  var target = {};
655
659
  for (var prop in source)
656
- if (__hasOwnProp$9.call(source, prop) && exclude.indexOf(prop) < 0)
660
+ if (__hasOwnProp$a.call(source, prop) && exclude.indexOf(prop) < 0)
657
661
  target[prop] = source[prop];
658
- if (source != null && __getOwnPropSymbols$9)
659
- for (var prop of __getOwnPropSymbols$9(source)) {
660
- if (exclude.indexOf(prop) < 0 && __propIsEnum$9.call(source, prop))
662
+ if (source != null && __getOwnPropSymbols$a)
663
+ for (var prop of __getOwnPropSymbols$a(source)) {
664
+ if (exclude.indexOf(prop) < 0 && __propIsEnum$a.call(source, prop))
661
665
  target[prop] = source[prop];
662
666
  }
663
667
  return target;
@@ -816,6 +820,109 @@ const UseIdle = vueDemi.defineComponent({
816
820
  }
817
821
  });
818
822
 
823
+ function useAsyncState(promise, initialState, options) {
824
+ const {
825
+ immediate = true,
826
+ delay = 0,
827
+ onError = shared.noop,
828
+ resetOnExecute = true,
829
+ shallow = true
830
+ } = options != null ? options : {};
831
+ const state = shallow ? vueDemi.shallowRef(initialState) : vueDemi.ref(initialState);
832
+ const isReady = vueDemi.ref(false);
833
+ const isLoading = vueDemi.ref(false);
834
+ const error = vueDemi.ref(void 0);
835
+ async function execute(delay2 = 0, ...args) {
836
+ if (resetOnExecute)
837
+ state.value = initialState;
838
+ error.value = void 0;
839
+ isReady.value = false;
840
+ isLoading.value = true;
841
+ if (delay2 > 0)
842
+ await shared.promiseTimeout(delay2);
843
+ const _promise = typeof promise === "function" ? promise(...args) : promise;
844
+ try {
845
+ const data = await _promise;
846
+ state.value = data;
847
+ isReady.value = true;
848
+ } catch (e) {
849
+ error.value = e;
850
+ onError(e);
851
+ } finally {
852
+ isLoading.value = false;
853
+ }
854
+ return state.value;
855
+ }
856
+ if (immediate)
857
+ execute(delay);
858
+ return {
859
+ state,
860
+ isReady,
861
+ isLoading,
862
+ error,
863
+ execute
864
+ };
865
+ }
866
+
867
+ var __defProp$9 = Object.defineProperty;
868
+ var __getOwnPropSymbols$9 = Object.getOwnPropertySymbols;
869
+ var __hasOwnProp$9 = Object.prototype.hasOwnProperty;
870
+ var __propIsEnum$9 = Object.prototype.propertyIsEnumerable;
871
+ var __defNormalProp$9 = (obj, key, value) => key in obj ? __defProp$9(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
872
+ var __spreadValues$9 = (a, b) => {
873
+ for (var prop in b || (b = {}))
874
+ if (__hasOwnProp$9.call(b, prop))
875
+ __defNormalProp$9(a, prop, b[prop]);
876
+ if (__getOwnPropSymbols$9)
877
+ for (var prop of __getOwnPropSymbols$9(b)) {
878
+ if (__propIsEnum$9.call(b, prop))
879
+ __defNormalProp$9(a, prop, b[prop]);
880
+ }
881
+ return a;
882
+ };
883
+ async function loadImage(options) {
884
+ return new Promise((resolve, reject) => {
885
+ const img = new Image();
886
+ const { src, srcset, sizes } = options;
887
+ img.src = src;
888
+ if (srcset)
889
+ img.srcset = srcset;
890
+ if (sizes)
891
+ img.sizes = sizes;
892
+ img.onload = () => resolve(img);
893
+ img.onerror = reject;
894
+ });
895
+ }
896
+ const useImage = (options, asyncStateOptions = {}) => {
897
+ const state = useAsyncState(() => loadImage(vueDemi.unref(options)), void 0, __spreadValues$9({
898
+ resetOnExecute: true
899
+ }, asyncStateOptions));
900
+ vueDemi.watch(() => vueDemi.unref(options), () => state.execute(asyncStateOptions.delay), { deep: true });
901
+ return state;
902
+ };
903
+
904
+ const UseImage = vueDemi.defineComponent({
905
+ name: "UseImage",
906
+ props: [
907
+ "src",
908
+ "srcset",
909
+ "sizes",
910
+ "as"
911
+ ],
912
+ setup(props, { slots }) {
913
+ const data = vueDemi.reactive(useImage(props));
914
+ return () => {
915
+ if (data.isLoading && slots.loading)
916
+ return slots.loading(data);
917
+ else if (data.error && slots.error)
918
+ return slots.error(data.error);
919
+ if (slots.default)
920
+ return slots.default(data);
921
+ return vueDemi.h(props.as || "img", props);
922
+ };
923
+ }
924
+ });
925
+
819
926
  function useScroll(element, options = {}) {
820
927
  const {
821
928
  throttle = 0,
@@ -865,7 +972,9 @@ function useScroll(element, options = {}) {
865
972
  arrivedState.left = scrollLeft <= 0 + (offset.left || 0);
866
973
  arrivedState.right = scrollLeft + eventTarget.clientWidth >= eventTarget.scrollWidth - (offset.right || 0);
867
974
  x.value = scrollLeft;
868
- const scrollTop = eventTarget.scrollTop;
975
+ let scrollTop = eventTarget.scrollTop;
976
+ if (e.target === document && !scrollTop)
977
+ scrollTop = document.body.scrollTop;
869
978
  directions.top = scrollTop < y.value;
870
979
  directions.bottom = scrollTop > y.value;
871
980
  arrivedState.top = scrollTop <= 0 + (offset.top || 0);
@@ -906,15 +1015,31 @@ var __spreadValues$8 = (a, b) => {
906
1015
  };
907
1016
  var __spreadProps$7 = (a, b) => __defProps$7(a, __getOwnPropDescs$7(b));
908
1017
  function useInfiniteScroll(element, onLoadMore, options = {}) {
909
- var _a;
1018
+ var _a, _b;
1019
+ const direction = (_a = options.direction) != null ? _a : "bottom";
910
1020
  const state = vueDemi.reactive(useScroll(element, __spreadProps$7(__spreadValues$8({}, options), {
911
1021
  offset: __spreadValues$8({
912
- bottom: (_a = options.distance) != null ? _a : 0
1022
+ [direction]: (_b = options.distance) != null ? _b : 0
913
1023
  }, options.offset)
914
1024
  })));
915
- vueDemi.watch(() => state.arrivedState.bottom, (v) => {
916
- if (v)
917
- onLoadMore(state);
1025
+ vueDemi.watch(() => state.arrivedState[direction], async (v) => {
1026
+ var _a2, _b2;
1027
+ if (v) {
1028
+ const elem = vueDemi.unref(element);
1029
+ const previous = {
1030
+ height: (_a2 = elem == null ? void 0 : elem.scrollHeight) != null ? _a2 : 0,
1031
+ width: (_b2 = elem == null ? void 0 : elem.scrollWidth) != null ? _b2 : 0
1032
+ };
1033
+ await onLoadMore(state);
1034
+ if (options.preserveScrollPosition && elem) {
1035
+ vueDemi.nextTick(() => {
1036
+ elem.scrollTo({
1037
+ top: elem.scrollHeight - previous.height,
1038
+ left: elem.scrollWidth - previous.width
1039
+ });
1040
+ });
1041
+ }
1042
+ }
918
1043
  });
919
1044
  }
920
1045
 
@@ -1235,15 +1360,17 @@ const UsePreferredLanguages = vueDemi.defineComponent({
1235
1360
  }
1236
1361
  });
1237
1362
 
1238
- function useCssVar(prop, target, { window = defaultWindow } = {}) {
1239
- const variable = vueDemi.ref("");
1363
+ function useCssVar(prop, target, { window = defaultWindow, initialValue = "" } = {}) {
1364
+ const variable = vueDemi.ref(initialValue);
1240
1365
  const elRef = vueDemi.computed(() => {
1241
1366
  var _a;
1242
1367
  return unrefElement(target) || ((_a = window == null ? void 0 : window.document) == null ? void 0 : _a.documentElement);
1243
1368
  });
1244
1369
  vueDemi.watch([elRef, () => vueDemi.unref(prop)], ([el, prop2]) => {
1245
- if (el && window)
1246
- variable.value = window.getComputedStyle(el).getPropertyValue(prop2);
1370
+ if (el && window) {
1371
+ const value = window.getComputedStyle(el).getPropertyValue(prop2);
1372
+ variable.value = value || initialValue;
1373
+ }
1247
1374
  }, { immediate: true });
1248
1375
  vueDemi.watch(variable, (val) => {
1249
1376
  var _a;
@@ -1584,6 +1711,7 @@ exports.UseEyeDropper = UseEyeDropper;
1584
1711
  exports.UseFullscreen = UseFullscreen;
1585
1712
  exports.UseGeolocation = UseGeolocation;
1586
1713
  exports.UseIdle = UseIdle;
1714
+ exports.UseImage = UseImage;
1587
1715
  exports.UseMouse = UseMouse;
1588
1716
  exports.UseMouseInElement = UseMouseInElement;
1589
1717
  exports.UseMousePressed = UseMousePressed;
package/index.d.ts CHANGED
@@ -266,6 +266,17 @@ declare const UseIdle: vue_demi.DefineComponent<IdleOptions & {
266
266
  timeout: number;
267
267
  }>, {}>;
268
268
 
269
+ interface UseImageOptions {
270
+ /** Address of the resource */
271
+ src: string;
272
+ /** Images to use in different situations, e.g., high-resolution displays, small monitors, etc. */
273
+ srcset?: string;
274
+ /** Image sizes for different page layouts */
275
+ sizes?: string;
276
+ }
277
+
278
+ declare const UseImage: vue_demi.DefineComponent<UseImageOptions & RenderableComponent, {}, {}, {}, {}, vue_demi.ComponentOptionsMixin, vue_demi.ComponentOptionsMixin, vue_demi.EmitsOptions, string, vue_demi.VNodeProps & vue_demi.AllowedComponentProps & vue_demi.ComponentCustomProps, Readonly<UseImageOptions & RenderableComponent>, {}>;
279
+
269
280
  interface UseScrollOptions {
270
281
  /**
271
282
  * Throttle time for scroll event, it’s disabled by default.
@@ -340,13 +351,25 @@ interface UseInfiniteScrollOptions extends UseScrollOptions {
340
351
  * @default 0
341
352
  */
342
353
  distance?: number;
354
+ /**
355
+ * The direction in which to listen the scroll.
356
+ *
357
+ * @default 'bottom'
358
+ */
359
+ direction?: 'top' | 'bottom';
360
+ /**
361
+ * Whether to preserve the current scroll position when loading more items.
362
+ *
363
+ * @default false
364
+ */
365
+ preserveScrollPosition?: boolean;
343
366
  }
344
367
  /**
345
368
  * Reactive infinite scroll.
346
369
  *
347
370
  * @see https://vueuse.org/useInfiniteScroll
348
371
  */
349
- declare function useInfiniteScroll(element: MaybeRef<HTMLElement | SVGElement | Window | Document | null | undefined>, onLoadMore: (state: UnwrapNestedRefs<ReturnType<typeof useScroll>>) => void, options?: UseInfiniteScrollOptions): void;
372
+ declare function useInfiniteScroll(element: MaybeRef<HTMLElement | SVGElement | Window | Document | null | undefined>, onLoadMore: (state: UnwrapNestedRefs<ReturnType<typeof useScroll>>) => void | Promise<void>, options?: UseInfiniteScrollOptions): void;
350
373
 
351
374
  declare type BindingValueFunction$2 = Parameters<typeof useInfiniteScroll>[1];
352
375
  declare type BindingValueArray$2 = [BindingValueFunction$2, UseInfiniteScrollOptions];
@@ -509,4 +532,4 @@ declare const UseWindowFocus: vue_demi.DefineComponent<{}, () => vue_demi.VNode<
509
532
 
510
533
  declare const UseWindowSize: vue_demi.DefineComponent<WindowSizeOptions, {}, {}, {}, {}, vue_demi.ComponentOptionsMixin, vue_demi.ComponentOptionsMixin, vue_demi.EmitsOptions, string, vue_demi.VNodeProps & vue_demi.AllowedComponentProps & vue_demi.ComponentCustomProps, Readonly<WindowSizeOptions>, {}>;
511
534
 
512
- export { OnClickOutside, OnLongPress, OnLongPressProps, UseActiveElement, UseBattery, UseBrowserLocation, UseColorMode, UseDark, UseDeviceMotion, UseDeviceOrientation, UseDevicePixelRatio, UseDevicesList, UseDocumentVisibility, UseDraggable, UseDraggableProps, UseElementBounding, UseElementSize, UseElementVisibility, UseEyeDropper, UseFullscreen, UseGeolocation, UseIdle, UseMouse, UseMouseInElement, UseMousePressed, UseNetwork, UseNow, UseOffsetPagination, UseOnline, UsePageLeave, UsePointer, UsePreferredColorScheme, UsePreferredDark, UsePreferredLanguages, UseScreenSafeArea, UseTimeAgo, UseTimestamp, UseVirtualList, UseVirtualListProps, UseWindowFocus, UseWindowSize, vOnClickOutside as VOnClickOutside, vOnLongPress as VOnLongPress, vElementHover, vElementSize, vElementVisibility, vInfiniteScroll, vIntersectionObserver, vOnClickOutside, vOnKeyStroke, vOnLongPress, vScroll, vScrollLock };
535
+ export { OnClickOutside, OnLongPress, OnLongPressProps, UseActiveElement, UseBattery, UseBrowserLocation, UseColorMode, UseDark, UseDeviceMotion, UseDeviceOrientation, UseDevicePixelRatio, UseDevicesList, UseDocumentVisibility, UseDraggable, UseDraggableProps, UseElementBounding, UseElementSize, UseElementVisibility, UseEyeDropper, UseFullscreen, UseGeolocation, UseIdle, UseImage, UseMouse, UseMouseInElement, UseMousePressed, UseNetwork, UseNow, UseOffsetPagination, UseOnline, UsePageLeave, UsePointer, UsePreferredColorScheme, UsePreferredDark, UsePreferredLanguages, UseScreenSafeArea, UseTimeAgo, UseTimestamp, UseVirtualList, UseVirtualListProps, UseWindowFocus, UseWindowSize, vOnClickOutside as VOnClickOutside, vOnLongPress as VOnLongPress, vElementHover, vElementSize, vElementVisibility, vInfiniteScroll, vIntersectionObserver, vOnClickOutside, vOnKeyStroke, vOnLongPress, vScroll, vScrollLock };