@vueuse/components 10.2.1 → 10.3.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/index.cjs CHANGED
@@ -84,6 +84,7 @@ function onClickOutside(target, handler, options = {}) {
84
84
  if (shared.isIOS && !_iOSWorkaround) {
85
85
  _iOSWorkaround = true;
86
86
  Array.from(window.document.body.children).forEach((el) => el.addEventListener("click", shared.noop));
87
+ window.document.documentElement.addEventListener("click", shared.noop);
87
88
  }
88
89
  let shouldListen = true;
89
90
  const shouldIgnore = (event) => {
@@ -252,8 +253,7 @@ function onLongPress(target, handler, options) {
252
253
  once: (_b = options == null ? void 0 : options.modifiers) == null ? void 0 : _b.once
253
254
  };
254
255
  useEventListener(elementRef, "pointerdown", onDown, listenerOptions);
255
- useEventListener(elementRef, "pointerup", clear, listenerOptions);
256
- useEventListener(elementRef, "pointerleave", clear, listenerOptions);
256
+ useEventListener(elementRef, ["pointerup", "pointerleave"], clear, listenerOptions);
257
257
  }
258
258
 
259
259
  const OnLongPress = /* @__PURE__ */ /* #__PURE__ */ vueDemi.defineComponent({
@@ -522,29 +522,33 @@ function useMediaQuery(query, options = {}) {
522
522
  const isSupported = useSupported(() => window && "matchMedia" in window && typeof window.matchMedia === "function");
523
523
  let mediaQuery;
524
524
  const matches = vueDemi.ref(false);
525
+ const handler = (event) => {
526
+ matches.value = event.matches;
527
+ };
525
528
  const cleanup = () => {
526
529
  if (!mediaQuery)
527
530
  return;
528
531
  if ("removeEventListener" in mediaQuery)
529
- mediaQuery.removeEventListener("change", update);
532
+ mediaQuery.removeEventListener("change", handler);
530
533
  else
531
- mediaQuery.removeListener(update);
534
+ mediaQuery.removeListener(handler);
532
535
  };
533
- const update = () => {
536
+ const stopWatch = vueDemi.watchEffect(() => {
534
537
  if (!isSupported.value)
535
538
  return;
536
539
  cleanup();
537
- mediaQuery = window.matchMedia(shared.toRef(query).value);
538
- matches.value = !!(mediaQuery == null ? void 0 : mediaQuery.matches);
539
- if (!mediaQuery)
540
- return;
540
+ mediaQuery = window.matchMedia(shared.toValue(query));
541
541
  if ("addEventListener" in mediaQuery)
542
- mediaQuery.addEventListener("change", update);
542
+ mediaQuery.addEventListener("change", handler);
543
543
  else
544
- mediaQuery.addListener(update);
545
- };
546
- vueDemi.watchEffect(update);
547
- shared.tryOnScopeDispose(() => cleanup());
544
+ mediaQuery.addListener(handler);
545
+ matches.value = mediaQuery.matches;
546
+ });
547
+ shared.tryOnScopeDispose(() => {
548
+ stopWatch();
549
+ cleanup();
550
+ mediaQuery = void 0;
551
+ });
548
552
  return matches;
549
553
  }
550
554
 
@@ -1306,7 +1310,8 @@ function useScroll(element, options = {}) {
1306
1310
  capture: false,
1307
1311
  passive: true
1308
1312
  },
1309
- behavior = "auto"
1313
+ behavior = "auto",
1314
+ window = defaultWindow
1310
1315
  } = options;
1311
1316
  const internalX = vueDemi.ref(0);
1312
1317
  const internalY = vueDemi.ref(0);
@@ -1328,10 +1333,12 @@ function useScroll(element, options = {}) {
1328
1333
  });
1329
1334
  function scrollTo(_x, _y) {
1330
1335
  var _a, _b, _c;
1336
+ if (!window)
1337
+ return;
1331
1338
  const _element = shared.toValue(element);
1332
1339
  if (!_element)
1333
1340
  return;
1334
- (_c = _element instanceof Document ? document.body : _element) == null ? void 0 : _c.scrollTo({
1341
+ (_c = _element instanceof Document ? window.document.body : _element) == null ? void 0 : _c.scrollTo({
1335
1342
  top: (_a = shared.toValue(_y)) != null ? _a : y.value,
1336
1343
  left: (_b = shared.toValue(_x)) != null ? _b : x.value,
1337
1344
  behavior: shared.toValue(behavior)
@@ -1362,7 +1369,9 @@ function useScroll(element, options = {}) {
1362
1369
  };
1363
1370
  const onScrollEndDebounced = shared.useDebounceFn(onScrollEnd, throttle + idle);
1364
1371
  const setArrivedState = (target) => {
1365
- const el = target === window ? target.document.documentElement : target === document ? target.documentElement : target;
1372
+ if (!window)
1373
+ return;
1374
+ const el = target === window ? target.document.documentElement : target === window.document ? target.documentElement : target;
1366
1375
  const { display, flexDirection } = getComputedStyle(el);
1367
1376
  const scrollLeft = el.scrollLeft;
1368
1377
  directions.left = scrollLeft < internalX.value;
@@ -1378,8 +1387,8 @@ function useScroll(element, options = {}) {
1378
1387
  }
1379
1388
  internalX.value = scrollLeft;
1380
1389
  let scrollTop = el.scrollTop;
1381
- if (target === document && !scrollTop)
1382
- scrollTop = document.body.scrollTop;
1390
+ if (target === window.document && !scrollTop)
1391
+ scrollTop = window.document.body.scrollTop;
1383
1392
  directions.top = scrollTop < internalY.value;
1384
1393
  directions.bottom = scrollTop > internalY.value;
1385
1394
  const top = Math.abs(scrollTop) <= 0 + (offset.top || 0);
@@ -1394,7 +1403,9 @@ function useScroll(element, options = {}) {
1394
1403
  internalY.value = scrollTop;
1395
1404
  };
1396
1405
  const onScrollHandler = (e) => {
1397
- const eventTarget = e.target === document ? e.target.documentElement : e.target;
1406
+ if (!window)
1407
+ return;
1408
+ const eventTarget = e.target === window.document ? e.target.documentElement : e.target;
1398
1409
  setArrivedState(eventTarget);
1399
1410
  isScrolling.value = true;
1400
1411
  onScrollEndDebounced(e);
@@ -1420,7 +1431,7 @@ function useScroll(element, options = {}) {
1420
1431
  directions,
1421
1432
  measure() {
1422
1433
  const _element = shared.toValue(element);
1423
- if (_element)
1434
+ if (window && _element)
1424
1435
  setArrivedState(_element);
1425
1436
  }
1426
1437
  };
@@ -1461,12 +1472,21 @@ function useInfiniteScroll(element, onLoadMore, options = {}) {
1461
1472
  ));
1462
1473
  const promise = vueDemi.ref();
1463
1474
  const isLoading = vueDemi.computed(() => !!promise.value);
1475
+ const observedElement = vueDemi.computed(() => {
1476
+ const el = shared.toValue(element);
1477
+ if (el instanceof Window)
1478
+ return window.document.documentElement;
1479
+ if (el instanceof Document)
1480
+ return document.documentElement;
1481
+ return el;
1482
+ });
1483
+ const isElementVisible = useElementVisibility(observedElement);
1464
1484
  function checkAndLoad() {
1465
1485
  state.measure();
1466
- const el = shared.toValue(element);
1467
- if (!el || !el.offsetParent)
1486
+ if (!observedElement.value || !isElementVisible.value)
1468
1487
  return;
1469
- const isNarrower = direction === "bottom" || direction === "top" ? el.scrollHeight <= el.clientHeight : el.scrollWidth <= el.clientWidth;
1488
+ const { scrollHeight, clientHeight, scrollWidth, clientWidth } = observedElement.value;
1489
+ const isNarrower = direction === "bottom" || direction === "top" ? scrollHeight <= clientHeight : scrollWidth <= clientWidth;
1470
1490
  if (state.arrivedState[direction] || isNarrower) {
1471
1491
  if (!promise.value) {
1472
1492
  promise.value = Promise.all([
@@ -1480,7 +1500,7 @@ function useInfiniteScroll(element, onLoadMore, options = {}) {
1480
1500
  }
1481
1501
  }
1482
1502
  vueDemi.watch(
1483
- () => [state.arrivedState[direction], shared.toValue(element)],
1503
+ () => [state.arrivedState[direction], isElementVisible.value],
1484
1504
  checkAndLoad,
1485
1505
  { immediate: true }
1486
1506
  );