@mantine-vue/hooks 3.6.0 → 3.7.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/esm/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { computed, getCurrentInstance, isRef, nextTick, onBeforeUnmount, onMounted, onUpdated, reactive, readonly, ref, shallowRef, toValue, triggerRef, watch, watchEffect } from "vue";
1
+ import { computed, getCurrentInstance, isRef, nextTick, onBeforeUnmount, onMounted, onUpdated, reactive, readonly, ref, shallowRef, toValue, triggerRef, useId as useId$1, watch, watchEffect } from "vue";
2
2
  //#region src/utils/clamp/clamp.ts
3
3
  function clamp(value, min, max) {
4
4
  if (min === void 0 && max === void 0) return value;
@@ -117,6 +117,10 @@ function useDebouncedCallback(callback, waitOrOptions) {
117
117
  }
118
118
  //#endregion
119
119
  //#region src/use-click-outside/use-click-outside.ts
120
+ function resolveElement(value) {
121
+ if (!value) return null;
122
+ return "$el" in value ? value.$el ?? null : value;
123
+ }
120
124
  function useClickOutside(callback, events = ["mousedown", "touchstart"], nodes, enabled = true, capture = false) {
121
125
  const element = ref(null);
122
126
  let activeEvents = [];
@@ -128,7 +132,10 @@ function useClickOutside(callback, events = ["mousedown", "touchstart"], nodes,
128
132
  const resolvedNodes = nodes ? toValue(nodes) : void 0;
129
133
  if (resolvedNodes) {
130
134
  if (resolvedNodes.every((node) => !!node && !path.includes(node))) callback(event);
131
- } else if (element.value && !path.includes(element.value)) callback(event);
135
+ } else {
136
+ const targetElement = resolveElement(element.value);
137
+ if (targetElement && !path.includes(targetElement)) callback(event);
138
+ }
132
139
  };
133
140
  const cleanup = () => {
134
141
  activeEvents.forEach((event) => document.removeEventListener(event, listener, activeCapture));
@@ -517,12 +524,8 @@ function useForceUpdate() {
517
524
  //#region src/use-id/use-id.ts
518
525
  var idCounter = 0;
519
526
  function useId(staticId) {
520
- const id = ref(staticId || "");
521
- onMounted(() => {
522
- if (!id.value) id.value = `mantine-vue-${++idCounter}`;
523
- });
524
- if (staticId) id.value = staticId;
525
- return id;
527
+ if (staticId) return ref(staticId);
528
+ return ref(`mantine-vue-${getCurrentInstance() ? useId$1() : `fallback-${++idCounter}`}`);
526
529
  }
527
530
  //#endregion
528
531
  //#region src/use-idle/use-idle.ts
@@ -1709,20 +1712,25 @@ function useLogger(componentName, props) {
1709
1712
  function useHover() {
1710
1713
  const refTarget = ref(null);
1711
1714
  const hovered = ref(false);
1712
- let cleanup;
1713
- onMounted(() => {
1714
- const node = refTarget.value;
1715
+ watch(refTarget, (node, _, onCleanup) => {
1716
+ hovered.value = false;
1715
1717
  if (!node) return;
1716
- const onEnter = () => hovered.value = true;
1717
- const onLeave = () => hovered.value = false;
1718
+ const onEnter = () => {
1719
+ hovered.value = true;
1720
+ };
1721
+ const onLeave = () => {
1722
+ hovered.value = false;
1723
+ };
1718
1724
  node.addEventListener("mouseenter", onEnter);
1719
1725
  node.addEventListener("mouseleave", onLeave);
1720
- cleanup = () => {
1726
+ onCleanup(() => {
1721
1727
  node.removeEventListener("mouseenter", onEnter);
1722
1728
  node.removeEventListener("mouseleave", onLeave);
1723
- };
1729
+ });
1730
+ }, {
1731
+ immediate: true,
1732
+ flush: "sync"
1724
1733
  });
1725
- onBeforeUnmount(() => cleanup?.());
1726
1734
  return {
1727
1735
  ref: refTarget,
1728
1736
  hovered
@@ -1848,27 +1856,35 @@ function useDisclosure(initialState = false, options = {}) {
1848
1856
  function useFocusWithin(options = {}) {
1849
1857
  const refTarget = ref(null);
1850
1858
  const focused = ref(false);
1851
- let cleanup;
1852
- onMounted(() => {
1853
- const node = refTarget.value;
1859
+ watch(refTarget, (node, _, onCleanup) => {
1860
+ focused.value = false;
1854
1861
  if (!node) return;
1862
+ let active = true;
1855
1863
  const onFocusIn = (event) => {
1856
- focused.value = true;
1857
- options.onFocus?.(event);
1864
+ if (!focused.value) {
1865
+ focused.value = true;
1866
+ options.onFocus?.(event);
1867
+ }
1868
+ };
1869
+ const onFocusOut = (event) => {
1870
+ nextTick(() => {
1871
+ if (!active) return;
1872
+ const isFocused = node.matches(":focus-within");
1873
+ if (!isFocused && focused.value) options.onBlur?.(event);
1874
+ focused.value = isFocused;
1875
+ });
1858
1876
  };
1859
- const onFocusOut = (event) => nextTick(() => {
1860
- const isFocused = node.matches(":focus-within");
1861
- if (!isFocused && focused.value) options.onBlur?.(event);
1862
- focused.value = isFocused;
1863
- });
1864
1877
  node.addEventListener("focusin", onFocusIn);
1865
1878
  node.addEventListener("focusout", onFocusOut);
1866
- cleanup = () => {
1879
+ onCleanup(() => {
1880
+ active = false;
1867
1881
  node.removeEventListener("focusin", onFocusIn);
1868
1882
  node.removeEventListener("focusout", onFocusOut);
1869
- };
1883
+ });
1884
+ }, {
1885
+ immediate: true,
1886
+ flush: "sync"
1870
1887
  });
1871
- onBeforeUnmount(() => cleanup?.());
1872
1888
  return {
1873
1889
  ref: refTarget,
1874
1890
  focused
@@ -4185,6 +4201,7 @@ function useDrag(handler, options = {}) {
4185
4201
  resetDrag();
4186
4202
  };
4187
4203
  const refCallback = (newNode) => {
4204
+ if (newNode === node) return;
4188
4205
  cleanupNode?.();
4189
4206
  cleanupNode = void 0;
4190
4207
  node = newNode;