@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.mjs CHANGED
@@ -1,6 +1,6 @@
1
- import { defineComponent, ref, h, unref, watch, computed, reactive, shallowRef, toRef, toRefs } from 'vue-demi';
1
+ import { defineComponent, ref, h, unref, watch, computed, reactive, shallowRef, nextTick, toRef, toRefs } from 'vue-demi';
2
2
  import { onClickOutside as onClickOutside$1, useActiveElement, useBattery, useBrowserLocation, useDark, useDeviceMotion, useDeviceOrientation, useDevicePixelRatio, useDevicesList, useDocumentVisibility, useStorage as useStorage$1, isClient as isClient$1, useDraggable, useElementBounding, useElementSize as useElementSize$1, useElementVisibility as useElementVisibility$1, useEyeDropper, useFullscreen, useGeolocation, useIdle, useMouse, useMouseInElement, useMousePressed, useNetwork, useNow, useOffsetPagination, useOnline, usePageLeave, usePointer, usePreferredColorScheme, usePreferredDark as usePreferredDark$1, usePreferredLanguages, useTimeAgo, useTimestamp, useVirtualList, useWindowFocus, useWindowSize } from '@vueuse/core';
3
- import { isClient, isString, noop, tryOnScopeDispose, directiveHooks, pausableWatch, tryOnBeforeMount, tryOnMounted, useToggle, useDebounceFn, useThrottleFn, isIOS } from '@vueuse/shared';
3
+ import { isClient, isString, noop, tryOnScopeDispose, directiveHooks, pausableWatch, tryOnBeforeMount, tryOnMounted, useToggle, promiseTimeout, useDebounceFn, useThrottleFn, isIOS } from '@vueuse/shared';
4
4
 
5
5
  const OnClickOutside = defineComponent({
6
6
  name: "OnClickOutside",
@@ -19,9 +19,8 @@ const OnClickOutside = defineComponent({
19
19
  });
20
20
 
21
21
  function unrefElement(elRef) {
22
- var _a;
23
22
  const plain = unref(elRef);
24
- return (_a = plain == null ? void 0 : plain.$el) != null ? _a : plain;
23
+ return plain != null && "$el" in plain ? plain.$el : plain;
25
24
  }
26
25
 
27
26
  const defaultWindow = isClient ? window : void 0;
@@ -86,7 +85,11 @@ function onClickOutside(target, handler, options = {}) {
86
85
  shouldListen.value = !!el && !e.composedPath().includes(el);
87
86
  }, { passive: true }),
88
87
  useEventListener(window, "pointerup", (e) => {
89
- fallback = window.setTimeout(() => listener(e), 50);
88
+ if (e.button === 0) {
89
+ const path = e.composedPath();
90
+ e.composedPath = () => path;
91
+ fallback = window.setTimeout(() => listener(e), 50);
92
+ }
90
93
  }, { passive: true })
91
94
  ];
92
95
  const stop = () => cleanup.forEach((fn) => fn());
@@ -128,19 +131,19 @@ function onKeyStroke(key, handler, options = {}) {
128
131
  return useEventListener(target, eventName, listener, passive);
129
132
  }
130
133
 
131
- var __defProp$b = Object.defineProperty;
132
- var __getOwnPropSymbols$c = Object.getOwnPropertySymbols;
133
- var __hasOwnProp$c = Object.prototype.hasOwnProperty;
134
- var __propIsEnum$c = Object.prototype.propertyIsEnumerable;
135
- var __defNormalProp$b = (obj, key, value) => key in obj ? __defProp$b(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
136
- var __spreadValues$b = (a, b) => {
134
+ var __defProp$c = Object.defineProperty;
135
+ var __getOwnPropSymbols$d = Object.getOwnPropertySymbols;
136
+ var __hasOwnProp$d = Object.prototype.hasOwnProperty;
137
+ var __propIsEnum$d = Object.prototype.propertyIsEnumerable;
138
+ var __defNormalProp$c = (obj, key, value) => key in obj ? __defProp$c(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
139
+ var __spreadValues$c = (a, b) => {
137
140
  for (var prop in b || (b = {}))
138
- if (__hasOwnProp$c.call(b, prop))
139
- __defNormalProp$b(a, prop, b[prop]);
140
- if (__getOwnPropSymbols$c)
141
- for (var prop of __getOwnPropSymbols$c(b)) {
142
- if (__propIsEnum$c.call(b, prop))
143
- __defNormalProp$b(a, prop, b[prop]);
141
+ if (__hasOwnProp$d.call(b, prop))
142
+ __defNormalProp$c(a, prop, b[prop]);
143
+ if (__getOwnPropSymbols$d)
144
+ for (var prop of __getOwnPropSymbols$d(b)) {
145
+ if (__propIsEnum$d.call(b, prop))
146
+ __defNormalProp$c(a, prop, b[prop]);
144
147
  }
145
148
  return a;
146
149
  };
@@ -154,7 +157,7 @@ const vOnKeyStroke = {
154
157
  });
155
158
  } else {
156
159
  const [handler, options] = binding.value;
157
- onKeyStroke(keys, handler, __spreadValues$b({
160
+ onKeyStroke(keys, handler, __spreadValues$c({
158
161
  target: el
159
162
  }, options));
160
163
  }
@@ -394,19 +397,19 @@ function usePreferredDark(options) {
394
397
  return useMediaQuery("(prefers-color-scheme: dark)", options);
395
398
  }
396
399
 
397
- var __defProp$a = Object.defineProperty;
398
- var __getOwnPropSymbols$b = Object.getOwnPropertySymbols;
399
- var __hasOwnProp$b = Object.prototype.hasOwnProperty;
400
- var __propIsEnum$b = Object.prototype.propertyIsEnumerable;
401
- var __defNormalProp$a = (obj, key, value) => key in obj ? __defProp$a(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
402
- var __spreadValues$a = (a, b) => {
400
+ var __defProp$b = Object.defineProperty;
401
+ var __getOwnPropSymbols$c = Object.getOwnPropertySymbols;
402
+ var __hasOwnProp$c = Object.prototype.hasOwnProperty;
403
+ var __propIsEnum$c = Object.prototype.propertyIsEnumerable;
404
+ var __defNormalProp$b = (obj, key, value) => key in obj ? __defProp$b(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
405
+ var __spreadValues$b = (a, b) => {
403
406
  for (var prop in b || (b = {}))
404
- if (__hasOwnProp$b.call(b, prop))
405
- __defNormalProp$a(a, prop, b[prop]);
406
- if (__getOwnPropSymbols$b)
407
- for (var prop of __getOwnPropSymbols$b(b)) {
408
- if (__propIsEnum$b.call(b, prop))
409
- __defNormalProp$a(a, prop, b[prop]);
407
+ if (__hasOwnProp$c.call(b, prop))
408
+ __defNormalProp$b(a, prop, b[prop]);
409
+ if (__getOwnPropSymbols$c)
410
+ for (var prop of __getOwnPropSymbols$c(b)) {
411
+ if (__propIsEnum$c.call(b, prop))
412
+ __defNormalProp$b(a, prop, b[prop]);
410
413
  }
411
414
  return a;
412
415
  };
@@ -420,7 +423,7 @@ function useColorMode(options = {}) {
420
423
  listenToStorageChanges = true,
421
424
  storageRef
422
425
  } = options;
423
- const modes = __spreadValues$a({
426
+ const modes = __spreadValues$b({
424
427
  auto: "",
425
428
  light: "light",
426
429
  dark: "dark"
@@ -558,21 +561,21 @@ const UseDocumentVisibility = defineComponent({
558
561
  }
559
562
  });
560
563
 
561
- var __defProp$9 = Object.defineProperty;
564
+ var __defProp$a = Object.defineProperty;
562
565
  var __defProps$8 = Object.defineProperties;
563
566
  var __getOwnPropDescs$8 = Object.getOwnPropertyDescriptors;
564
- var __getOwnPropSymbols$a = Object.getOwnPropertySymbols;
565
- var __hasOwnProp$a = Object.prototype.hasOwnProperty;
566
- var __propIsEnum$a = Object.prototype.propertyIsEnumerable;
567
- var __defNormalProp$9 = (obj, key, value) => key in obj ? __defProp$9(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
568
- var __spreadValues$9 = (a, b) => {
567
+ var __getOwnPropSymbols$b = Object.getOwnPropertySymbols;
568
+ var __hasOwnProp$b = Object.prototype.hasOwnProperty;
569
+ var __propIsEnum$b = Object.prototype.propertyIsEnumerable;
570
+ var __defNormalProp$a = (obj, key, value) => key in obj ? __defProp$a(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
571
+ var __spreadValues$a = (a, b) => {
569
572
  for (var prop in b || (b = {}))
570
- if (__hasOwnProp$a.call(b, prop))
571
- __defNormalProp$9(a, prop, b[prop]);
572
- if (__getOwnPropSymbols$a)
573
- for (var prop of __getOwnPropSymbols$a(b)) {
574
- if (__propIsEnum$a.call(b, prop))
575
- __defNormalProp$9(a, prop, b[prop]);
573
+ if (__hasOwnProp$b.call(b, prop))
574
+ __defNormalProp$a(a, prop, b[prop]);
575
+ if (__getOwnPropSymbols$b)
576
+ for (var prop of __getOwnPropSymbols$b(b)) {
577
+ if (__propIsEnum$b.call(b, prop))
578
+ __defNormalProp$a(a, prop, b[prop]);
576
579
  }
577
580
  return a;
578
581
  };
@@ -585,13 +588,14 @@ const UseDraggable = defineComponent({
585
588
  "initialValue",
586
589
  "exact",
587
590
  "preventDefault",
591
+ "stopPropagation",
588
592
  "pointerTypes",
589
593
  "as"
590
594
  ],
591
595
  setup(props, { slots }) {
592
596
  const target = ref();
593
597
  const initialValue = props.storageKey ? useStorage$1(props.storageKey, unref(props.initialValue) || { x: 0, y: 0 }, isClient$1 ? props.storageType === "session" ? sessionStorage : localStorage : void 0) : props.initialValue || { x: 0, y: 0 };
594
- const data = reactive(useDraggable(target, __spreadProps$8(__spreadValues$9({}, props), {
598
+ const data = reactive(useDraggable(target, __spreadProps$8(__spreadValues$a({}, props), {
595
599
  initialValue
596
600
  })));
597
601
  return () => {
@@ -643,17 +647,17 @@ const UseElementSize = defineComponent({
643
647
  }
644
648
  });
645
649
 
646
- var __getOwnPropSymbols$9 = Object.getOwnPropertySymbols;
647
- var __hasOwnProp$9 = Object.prototype.hasOwnProperty;
648
- var __propIsEnum$9 = Object.prototype.propertyIsEnumerable;
650
+ var __getOwnPropSymbols$a = Object.getOwnPropertySymbols;
651
+ var __hasOwnProp$a = Object.prototype.hasOwnProperty;
652
+ var __propIsEnum$a = Object.prototype.propertyIsEnumerable;
649
653
  var __objRest = (source, exclude) => {
650
654
  var target = {};
651
655
  for (var prop in source)
652
- if (__hasOwnProp$9.call(source, prop) && exclude.indexOf(prop) < 0)
656
+ if (__hasOwnProp$a.call(source, prop) && exclude.indexOf(prop) < 0)
653
657
  target[prop] = source[prop];
654
- if (source != null && __getOwnPropSymbols$9)
655
- for (var prop of __getOwnPropSymbols$9(source)) {
656
- if (exclude.indexOf(prop) < 0 && __propIsEnum$9.call(source, prop))
658
+ if (source != null && __getOwnPropSymbols$a)
659
+ for (var prop of __getOwnPropSymbols$a(source)) {
660
+ if (exclude.indexOf(prop) < 0 && __propIsEnum$a.call(source, prop))
657
661
  target[prop] = source[prop];
658
662
  }
659
663
  return target;
@@ -812,6 +816,109 @@ const UseIdle = defineComponent({
812
816
  }
813
817
  });
814
818
 
819
+ function useAsyncState(promise, initialState, options) {
820
+ const {
821
+ immediate = true,
822
+ delay = 0,
823
+ onError = noop,
824
+ resetOnExecute = true,
825
+ shallow = true
826
+ } = options != null ? options : {};
827
+ const state = shallow ? shallowRef(initialState) : ref(initialState);
828
+ const isReady = ref(false);
829
+ const isLoading = ref(false);
830
+ const error = ref(void 0);
831
+ async function execute(delay2 = 0, ...args) {
832
+ if (resetOnExecute)
833
+ state.value = initialState;
834
+ error.value = void 0;
835
+ isReady.value = false;
836
+ isLoading.value = true;
837
+ if (delay2 > 0)
838
+ await promiseTimeout(delay2);
839
+ const _promise = typeof promise === "function" ? promise(...args) : promise;
840
+ try {
841
+ const data = await _promise;
842
+ state.value = data;
843
+ isReady.value = true;
844
+ } catch (e) {
845
+ error.value = e;
846
+ onError(e);
847
+ } finally {
848
+ isLoading.value = false;
849
+ }
850
+ return state.value;
851
+ }
852
+ if (immediate)
853
+ execute(delay);
854
+ return {
855
+ state,
856
+ isReady,
857
+ isLoading,
858
+ error,
859
+ execute
860
+ };
861
+ }
862
+
863
+ var __defProp$9 = Object.defineProperty;
864
+ var __getOwnPropSymbols$9 = Object.getOwnPropertySymbols;
865
+ var __hasOwnProp$9 = Object.prototype.hasOwnProperty;
866
+ var __propIsEnum$9 = Object.prototype.propertyIsEnumerable;
867
+ var __defNormalProp$9 = (obj, key, value) => key in obj ? __defProp$9(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
868
+ var __spreadValues$9 = (a, b) => {
869
+ for (var prop in b || (b = {}))
870
+ if (__hasOwnProp$9.call(b, prop))
871
+ __defNormalProp$9(a, prop, b[prop]);
872
+ if (__getOwnPropSymbols$9)
873
+ for (var prop of __getOwnPropSymbols$9(b)) {
874
+ if (__propIsEnum$9.call(b, prop))
875
+ __defNormalProp$9(a, prop, b[prop]);
876
+ }
877
+ return a;
878
+ };
879
+ async function loadImage(options) {
880
+ return new Promise((resolve, reject) => {
881
+ const img = new Image();
882
+ const { src, srcset, sizes } = options;
883
+ img.src = src;
884
+ if (srcset)
885
+ img.srcset = srcset;
886
+ if (sizes)
887
+ img.sizes = sizes;
888
+ img.onload = () => resolve(img);
889
+ img.onerror = reject;
890
+ });
891
+ }
892
+ const useImage = (options, asyncStateOptions = {}) => {
893
+ const state = useAsyncState(() => loadImage(unref(options)), void 0, __spreadValues$9({
894
+ resetOnExecute: true
895
+ }, asyncStateOptions));
896
+ watch(() => unref(options), () => state.execute(asyncStateOptions.delay), { deep: true });
897
+ return state;
898
+ };
899
+
900
+ const UseImage = defineComponent({
901
+ name: "UseImage",
902
+ props: [
903
+ "src",
904
+ "srcset",
905
+ "sizes",
906
+ "as"
907
+ ],
908
+ setup(props, { slots }) {
909
+ const data = reactive(useImage(props));
910
+ return () => {
911
+ if (data.isLoading && slots.loading)
912
+ return slots.loading(data);
913
+ else if (data.error && slots.error)
914
+ return slots.error(data.error);
915
+ if (slots.default)
916
+ return slots.default(data);
917
+ return h(props.as || "img", props);
918
+ };
919
+ }
920
+ });
921
+
815
922
  function useScroll(element, options = {}) {
816
923
  const {
817
924
  throttle = 0,
@@ -861,7 +968,9 @@ function useScroll(element, options = {}) {
861
968
  arrivedState.left = scrollLeft <= 0 + (offset.left || 0);
862
969
  arrivedState.right = scrollLeft + eventTarget.clientWidth >= eventTarget.scrollWidth - (offset.right || 0);
863
970
  x.value = scrollLeft;
864
- const scrollTop = eventTarget.scrollTop;
971
+ let scrollTop = eventTarget.scrollTop;
972
+ if (e.target === document && !scrollTop)
973
+ scrollTop = document.body.scrollTop;
865
974
  directions.top = scrollTop < y.value;
866
975
  directions.bottom = scrollTop > y.value;
867
976
  arrivedState.top = scrollTop <= 0 + (offset.top || 0);
@@ -902,15 +1011,31 @@ var __spreadValues$8 = (a, b) => {
902
1011
  };
903
1012
  var __spreadProps$7 = (a, b) => __defProps$7(a, __getOwnPropDescs$7(b));
904
1013
  function useInfiniteScroll(element, onLoadMore, options = {}) {
905
- var _a;
1014
+ var _a, _b;
1015
+ const direction = (_a = options.direction) != null ? _a : "bottom";
906
1016
  const state = reactive(useScroll(element, __spreadProps$7(__spreadValues$8({}, options), {
907
1017
  offset: __spreadValues$8({
908
- bottom: (_a = options.distance) != null ? _a : 0
1018
+ [direction]: (_b = options.distance) != null ? _b : 0
909
1019
  }, options.offset)
910
1020
  })));
911
- watch(() => state.arrivedState.bottom, (v) => {
912
- if (v)
913
- onLoadMore(state);
1021
+ watch(() => state.arrivedState[direction], async (v) => {
1022
+ var _a2, _b2;
1023
+ if (v) {
1024
+ const elem = unref(element);
1025
+ const previous = {
1026
+ height: (_a2 = elem == null ? void 0 : elem.scrollHeight) != null ? _a2 : 0,
1027
+ width: (_b2 = elem == null ? void 0 : elem.scrollWidth) != null ? _b2 : 0
1028
+ };
1029
+ await onLoadMore(state);
1030
+ if (options.preserveScrollPosition && elem) {
1031
+ nextTick(() => {
1032
+ elem.scrollTo({
1033
+ top: elem.scrollHeight - previous.height,
1034
+ left: elem.scrollWidth - previous.width
1035
+ });
1036
+ });
1037
+ }
1038
+ }
914
1039
  });
915
1040
  }
916
1041
 
@@ -1231,15 +1356,17 @@ const UsePreferredLanguages = defineComponent({
1231
1356
  }
1232
1357
  });
1233
1358
 
1234
- function useCssVar(prop, target, { window = defaultWindow } = {}) {
1235
- const variable = ref("");
1359
+ function useCssVar(prop, target, { window = defaultWindow, initialValue = "" } = {}) {
1360
+ const variable = ref(initialValue);
1236
1361
  const elRef = computed(() => {
1237
1362
  var _a;
1238
1363
  return unrefElement(target) || ((_a = window == null ? void 0 : window.document) == null ? void 0 : _a.documentElement);
1239
1364
  });
1240
1365
  watch([elRef, () => unref(prop)], ([el, prop2]) => {
1241
- if (el && window)
1242
- variable.value = window.getComputedStyle(el).getPropertyValue(prop2);
1366
+ if (el && window) {
1367
+ const value = window.getComputedStyle(el).getPropertyValue(prop2);
1368
+ variable.value = value || initialValue;
1369
+ }
1243
1370
  }, { immediate: true });
1244
1371
  watch(variable, (val) => {
1245
1372
  var _a;
@@ -1560,4 +1687,4 @@ const UseWindowSize = defineComponent({
1560
1687
  }
1561
1688
  });
1562
1689
 
1563
- export { OnClickOutside, OnLongPress, UseActiveElement, UseBattery, UseBrowserLocation, UseColorMode, UseDark, UseDeviceMotion, UseDeviceOrientation, UseDevicePixelRatio, UseDevicesList, UseDocumentVisibility, UseDraggable, UseElementBounding, UseElementSize, UseElementVisibility, UseEyeDropper, UseFullscreen, UseGeolocation, UseIdle, UseMouse, UseMouseInElement, UseMousePressed, UseNetwork, UseNow, UseOffsetPagination, UseOnline, UsePageLeave, UsePointer, UsePreferredColorScheme, UsePreferredDark, UsePreferredLanguages, UseScreenSafeArea, UseTimeAgo, UseTimestamp, UseVirtualList, UseWindowFocus, UseWindowSize, vOnClickOutside as VOnClickOutside, vOnLongPress as VOnLongPress, vElementHover, vElementSize, vElementVisibility, vInfiniteScroll, vIntersectionObserver, vOnClickOutside, vOnKeyStroke, vOnLongPress, vScroll, vScrollLock };
1690
+ export { OnClickOutside, OnLongPress, UseActiveElement, UseBattery, UseBrowserLocation, UseColorMode, UseDark, UseDeviceMotion, UseDeviceOrientation, UseDevicePixelRatio, UseDevicesList, UseDocumentVisibility, UseDraggable, UseElementBounding, UseElementSize, UseElementVisibility, UseEyeDropper, UseFullscreen, UseGeolocation, UseIdle, UseImage, UseMouse, UseMouseInElement, UseMousePressed, UseNetwork, UseNow, UseOffsetPagination, UseOnline, UsePageLeave, UsePointer, UsePreferredColorScheme, UsePreferredDark, UsePreferredLanguages, UseScreenSafeArea, UseTimeAgo, UseTimestamp, UseVirtualList, UseWindowFocus, UseWindowSize, vOnClickOutside as VOnClickOutside, vOnLongPress as VOnLongPress, vElementHover, vElementSize, vElementVisibility, vInfiniteScroll, vIntersectionObserver, vOnClickOutside, vOnKeyStroke, vOnLongPress, vScroll, vScrollLock };
package/package.json CHANGED
@@ -1,40 +1,40 @@
1
1
  {
2
2
  "name": "@vueuse/components",
3
- "version": "8.5.0",
3
+ "version": "8.7.1",
4
4
  "description": "Renderless components for VueUse",
5
- "keywords": [
6
- "vue",
7
- "vue-use",
8
- "utils"
9
- ],
10
- "license": "MIT",
11
5
  "author": "Jacob Clevenger<https://github.com/wheatjs>",
6
+ "license": "MIT",
7
+ "homepage": "https://github.com/vueuse/vueuse/tree/main/packages/components#readme",
12
8
  "repository": {
13
9
  "type": "git",
14
10
  "url": "git+https://github.com/vueuse/vueuse.git",
15
11
  "directory": "packages/components"
16
12
  },
17
- "main": "./index.cjs",
18
- "module": "./index.mjs",
19
- "types": "./index.d.ts",
20
- "unpkg": "./index.iife.min.js",
21
- "jsdelivr": "./index.iife.min.js",
13
+ "bugs": {
14
+ "url": "https://github.com/vueuse/vueuse/issues"
15
+ },
16
+ "keywords": [
17
+ "vue",
18
+ "vue-use",
19
+ "utils"
20
+ ],
21
+ "sideEffects": false,
22
22
  "exports": {
23
23
  ".": {
24
- "import": "./index.mjs",
24
+ "types": "./index.d.ts",
25
25
  "require": "./index.cjs",
26
- "types": "./index.d.ts"
26
+ "import": "./index.mjs"
27
27
  },
28
28
  "./*": "./*"
29
29
  },
30
- "sideEffects": false,
31
- "bugs": {
32
- "url": "https://github.com/vueuse/vueuse/issues"
33
- },
34
- "homepage": "https://github.com/vueuse/vueuse/tree/main/packages/components#readme",
30
+ "main": "./index.cjs",
31
+ "module": "./index.mjs",
32
+ "unpkg": "./index.iife.min.js",
33
+ "jsdelivr": "./index.iife.min.js",
34
+ "types": "./index.d.ts",
35
35
  "dependencies": {
36
- "@vueuse/core": "8.5.0",
37
- "@vueuse/shared": "8.5.0",
36
+ "@vueuse/core": "8.7.1",
37
+ "@vueuse/shared": "8.7.1",
38
38
  "vue-demi": "*"
39
39
  }
40
40
  }