@versini/ui-hooks 4.3.0 → 4.5.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.
@@ -0,0 +1,26 @@
1
+ import { useRef as f, useEffect as d } from "react";
2
+ const i = ["mousedown", "touchstart"];
3
+ function E(r, o, e) {
4
+ const c = f(null);
5
+ return d(() => {
6
+ const n = (t) => {
7
+ const s = t ? t.target : void 0;
8
+ if (Array.isArray(e)) {
9
+ const a = !document.body.contains(s) && s.tagName !== "HTML";
10
+ e.every(
11
+ (u) => !!u && !t.composedPath().includes(u)
12
+ ) && !a && r();
13
+ } else c.current && !c.current.contains(s) && r();
14
+ };
15
+ return (o || i).forEach(
16
+ (t) => document.addEventListener(t, n)
17
+ ), () => {
18
+ (o || i).forEach(
19
+ (t) => document.removeEventListener(t, n)
20
+ );
21
+ };
22
+ }, [r, e, o]), c;
23
+ }
24
+ export {
25
+ E as useClickOutside
26
+ };
@@ -0,0 +1,13 @@
1
+ import { useRef as f, useState as u, useCallback as l } from "react";
2
+ function b() {
3
+ const e = f(null), [i, t] = u(!1);
4
+ return { ref: l((r) => {
5
+ var n, s;
6
+ typeof IntersectionObserver < "u" && (r && !e.current ? e.current = new IntersectionObserver(
7
+ (o) => t(o.some((c) => c.isIntersecting))
8
+ ) : (n = e.current) == null || n.disconnect(), r ? (s = e.current) == null || s.observe(r) : t(!1));
9
+ }, []), inViewport: i };
10
+ }
11
+ export {
12
+ b as useInViewport
13
+ };
@@ -0,0 +1,12 @@
1
+ import { useState as i, useRef as o, useCallback as a, useEffect as v } from "react";
2
+ function p(u, c) {
3
+ const [t, s] = i(!1), e = o(null), l = o(null), r = a(() => {
4
+ s((f) => (!f && (!e.current || e.current === -1) && (e.current = window.setInterval(l.current, c)), !0));
5
+ }, [c]), n = a(() => {
6
+ s(!1), window.clearInterval(e.current || -1), e.current = -1;
7
+ }, []);
8
+ return v(() => (l.current = u, t && r(), n), [u, t, r, n]), { start: r, stop: n, active: t };
9
+ }
10
+ export {
11
+ p as useInterval
12
+ };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,23 @@
1
1
  import * as react from 'react';
2
2
 
3
+ /**
4
+ * Custom hooks that triggers a callback when a click is detected
5
+ * outside the target element.
6
+ *
7
+ * @param handler - Function to be called when clicked outside
8
+ * @param events - Array of events to listen to
9
+ * @param nodes - Array of nodes to check against
10
+ * @returns Ref to be attached to the target element
11
+ *
12
+ * @example
13
+ * const ref = useClickOutside(() => {
14
+ * console.log('Clicked outside!');
15
+ * });
16
+ * <div ref={ref}>Click me!</div>
17
+ *
18
+ */
19
+ declare function useClickOutside<T extends HTMLElement = any>(handler: () => void, events?: string[] | null, nodes?: (HTMLElement | null)[]): react.RefObject<T | null>;
20
+
3
21
  interface HotkeyItemOptions {
4
22
  preventDefault?: boolean;
5
23
  }
@@ -11,8 +29,42 @@ type HotkeyItem = [
11
29
  (event: KeyboardEvent) => void,
12
30
  HotkeyItemOptions?
13
31
  ];
32
+ declare function shouldFireEvent(event: KeyboardEvent, tagsToIgnore: string[], triggerOnContentEditable?: boolean): boolean;
14
33
  declare function useHotkeys(hotkeys: HotkeyItem[], tagsToIgnore?: string[], triggerOnContentEditable?: boolean): void;
15
34
 
35
+ /**
36
+ * Custom hook to call a function within a given interval.
37
+ *
38
+ * @param fn Callback function to be executed at each interval
39
+ * @param interval Interval time in milliseconds
40
+ * @returns An object containing start, stop, and active state
41
+ *
42
+ * @example
43
+ * const { start, stop, active } = useInterval(() => {
44
+ * console.log("Interval executed");
45
+ * }, 1000);
46
+ * start(); // To start the interval
47
+ * stop(); // To stop the interval
48
+ * console.log(active); // To check if the interval is active
49
+ *
50
+ */
51
+ declare function useInterval(fn: () => void, interval: number): {
52
+ start: () => void;
53
+ stop: () => void;
54
+ active: boolean;
55
+ };
56
+
57
+ /**
58
+ * Hook that checks if an element is visible in the viewport.
59
+ * @returns
60
+ * ref: React ref object to attach to the element you want to monitor.
61
+ * inViewport: Boolean indicating if the element is in the viewport.
62
+ */
63
+ declare function useInViewport<T extends HTMLElement = any>(): {
64
+ ref: (node: T | null) => void;
65
+ inViewport: boolean;
66
+ };
67
+
16
68
  /**
17
69
  * Custom hook that returns a function indicating whether the component
18
70
  * is mounted or not.
@@ -142,4 +194,4 @@ type UseUniqueIdOptions = string | number | {
142
194
  };
143
195
  declare function useUniqueId(options?: UseUniqueIdOptions): string | undefined;
144
196
 
145
- export { getHotkeyHandler, useHotkeys, useIsMounted, useLocalStorage, useMergeRefs, useResizeObserver, useUncontrolled, useUniqueId };
197
+ export { type HotkeyItem, type HotkeyItemOptions, type StorageProperties, type UseUniqueIdOptions, getHotkeyHandler, shouldFireEvent, useClickOutside, useHotkeys, useInViewport, useInterval, useIsMounted, useLocalStorage, useMergeRefs, useResizeObserver, useUncontrolled, useUniqueId };
package/dist/index.js CHANGED
@@ -1,21 +1,28 @@
1
- import { getHotkeyHandler as r, useHotkeys as t } from "./hooks/useHotkeys.js";
2
- import { useIsMounted as u } from "./hooks/useIsMounted.js";
3
- import { useLocalStorage as m } from "./hooks/useLocalStorage.js";
4
- import { useMergeRefs as x } from "./hooks/useMergeRefs.js";
5
- import { useResizeObserver as d } from "./hooks/useResizeObserver.js";
6
- import { useUncontrolled as a } from "./hooks/useUncontrolled.js";
7
- import { useUniqueId as H } from "./hooks/useUniqueId.js";
1
+ import { useClickOutside as r } from "./hooks/useClickOutside.js";
2
+ import { getHotkeyHandler as s, shouldFireEvent as u, useHotkeys as f } from "./hooks/useHotkeys.js";
3
+ import { useInterval as m } from "./hooks/useInterval.js";
4
+ import { useInViewport as n } from "./hooks/useInViewport.js";
5
+ import { useIsMounted as d } from "./hooks/useIsMounted.js";
6
+ import { useLocalStorage as a } from "./hooks/useLocalStorage.js";
7
+ import { useMergeRefs as c } from "./hooks/useMergeRefs.js";
8
+ import { useResizeObserver as k } from "./hooks/useResizeObserver.js";
9
+ import { useUncontrolled as H } from "./hooks/useUncontrolled.js";
10
+ import { useUniqueId as M } from "./hooks/useUniqueId.js";
8
11
  /*!
9
- @versini/ui-hooks v4.3.0
10
- © 2024 gizmette.com
12
+ @versini/ui-hooks v4.5.0
13
+ © 2025 gizmette.com
11
14
  */
12
15
  export {
13
- r as getHotkeyHandler,
14
- t as useHotkeys,
15
- u as useIsMounted,
16
- m as useLocalStorage,
17
- x as useMergeRefs,
18
- d as useResizeObserver,
19
- a as useUncontrolled,
20
- H as useUniqueId
16
+ s as getHotkeyHandler,
17
+ u as shouldFireEvent,
18
+ r as useClickOutside,
19
+ f as useHotkeys,
20
+ n as useInViewport,
21
+ m as useInterval,
22
+ d as useIsMounted,
23
+ a as useLocalStorage,
24
+ c as useMergeRefs,
25
+ k as useResizeObserver,
26
+ H as useUncontrolled,
27
+ M as useUniqueId
21
28
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@versini/ui-hooks",
3
- "version": "4.3.0",
3
+ "version": "4.5.0",
4
4
  "license": "MIT",
5
5
  "author": "Arno Versini",
6
6
  "publishConfig": {
@@ -37,5 +37,5 @@
37
37
  "react": "^18.3.1 || ^19.0.0",
38
38
  "react-dom": "^18.3.1 || ^19.0.0"
39
39
  },
40
- "gitHead": "413e5c7cd8228d2f135f54fa897101401207229b"
40
+ "gitHead": "9fe85d906eb117bb22c3b576f104b6f0203429d7"
41
41
  }