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