@w3ux/hooks 2.0.4 → 2.2.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.
Files changed (49) hide show
  1. package/README.md +63 -0
  2. package/cjs/index.d.ts +6 -6
  3. package/cjs/index.js.map +1 -1
  4. package/cjs/useEffectIgnoreInitial.js +1 -1
  5. package/cjs/useEffectIgnoreInitial.js.map +1 -1
  6. package/cjs/useOnResize.d.ts +1 -1
  7. package/cjs/useOnResize.js +2 -2
  8. package/cjs/useOnResize.js.map +1 -1
  9. package/cjs/useOutsideAlerter.d.ts +1 -1
  10. package/cjs/useOutsideAlerter.js +2 -2
  11. package/cjs/useOutsideAlerter.js.map +1 -1
  12. package/cjs/useSafeContext.d.ts +1 -1
  13. package/cjs/useSafeContext.js +1 -1
  14. package/cjs/useSafeContext.js.map +1 -1
  15. package/cjs/useSize.d.ts +1 -1
  16. package/cjs/useSize.js +2 -2
  17. package/cjs/useSize.js.map +1 -1
  18. package/cjs/useTimeLeft/defaults.d.ts +1 -1
  19. package/cjs/useTimeLeft/defaults.js.map +1 -1
  20. package/cjs/useTimeLeft/index.d.ts +1 -1
  21. package/cjs/useTimeLeft/index.js.map +1 -1
  22. package/cjs/util.d.ts +1 -1
  23. package/cjs/util.js.map +1 -1
  24. package/mjs/index.d.ts +6 -6
  25. package/mjs/index.js +6 -6
  26. package/mjs/index.js.map +1 -1
  27. package/mjs/useEffectIgnoreInitial.js +2 -2
  28. package/mjs/useEffectIgnoreInitial.js.map +1 -1
  29. package/mjs/useOnResize.d.ts +1 -1
  30. package/mjs/useOnResize.js +3 -3
  31. package/mjs/useOnResize.js.map +1 -1
  32. package/mjs/useOutsideAlerter.d.ts +1 -1
  33. package/mjs/useOutsideAlerter.js +3 -3
  34. package/mjs/useOutsideAlerter.js.map +1 -1
  35. package/mjs/useSafeContext.d.ts +1 -1
  36. package/mjs/useSafeContext.js +2 -2
  37. package/mjs/useSafeContext.js.map +1 -1
  38. package/mjs/useSize.d.ts +1 -1
  39. package/mjs/useSize.js +3 -3
  40. package/mjs/useSize.js.map +1 -1
  41. package/mjs/useTimeLeft/defaults.d.ts +1 -1
  42. package/mjs/useTimeLeft/defaults.js.map +1 -1
  43. package/mjs/useTimeLeft/index.d.ts +1 -1
  44. package/mjs/useTimeLeft/index.js +3 -3
  45. package/mjs/useTimeLeft/index.js.map +1 -1
  46. package/mjs/util.d.ts +1 -1
  47. package/mjs/util.js +2 -2
  48. package/mjs/util.js.map +1 -1
  49. package/package.json +23 -6
package/README.md ADDED
@@ -0,0 +1,63 @@
1
+ # Hooks
2
+
3
+ A collection of general purpose React hooks
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @w3ux/hooks
9
+ ```
10
+
11
+ or
12
+
13
+ ```bash
14
+ yarn add @w3ux/hooks
15
+ ```
16
+
17
+ or
18
+
19
+ ```bash
20
+ pnpm add @w3ux/hooks
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ ```typescript
26
+ import { /* your imports */ } from '@w3ux/hooks'
27
+ ```
28
+
29
+ ### React Usage
30
+
31
+ This package provides React hooks and components. Here's a basic example:
32
+
33
+ ```tsx
34
+ import React from 'react'
35
+ import { /* specific hook or component */ } from '@w3ux/hooks'
36
+
37
+ function MyComponent() {
38
+ // Use the imported hooks or components here
39
+ return <div>Your component content</div>
40
+ }
41
+ ```
42
+
43
+ ## Documentation
44
+
45
+ For comprehensive documentation and examples, visit the [w3ux documentation](https://w3ux.org/library/hooks/overview).
46
+
47
+ ## Keywords
48
+
49
+ `w3ux`, `polkadot`, `web3`, `react`, `hooks`, `typescript`
50
+
51
+ ## Repository
52
+
53
+ - **Source**: [GitHub](https://github.com/w3ux/w3ux-library)
54
+ - **Package**: [npm](https://www.npmjs.com/package/@w3ux/hooks)
55
+ - **Issues**: [GitHub Issues](https://github.com/w3ux/w3ux-library/issues)
56
+
57
+ ## License
58
+
59
+ This package is licensed under the GPL-3.0-only.
60
+
61
+ ---
62
+
63
+ Part of the [w3ux library](https://github.com/w3ux/w3ux-library) - A collection of packages for building Web3 applications.
package/cjs/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- export * from "./useEffectIgnoreInitial";
2
- export * from "./useOnResize";
3
- export * from "./useOutsideAlerter";
4
- export * from "./useSafeContext";
5
- export * from "./useSize";
6
- export * from "./useTimeLeft";
1
+ export * from './useEffectIgnoreInitial';
2
+ export * from './useOnResize';
3
+ export * from './useOutsideAlerter';
4
+ export * from './useSafeContext';
5
+ export * from './useSize';
6
+ export * from './useTimeLeft';
package/cjs/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAGA,2DAAyC;AACzC,gDAA8B;AAC9B,sDAAoC;AACpC,mDAAiC;AACjC,4CAA0B;AAC1B,gDAA8B","file":"index.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nexport * from \"./useEffectIgnoreInitial\";\nexport * from \"./useOnResize\";\nexport * from \"./useOutsideAlerter\";\nexport * from \"./useSafeContext\";\nexport * from \"./useSize\";\nexport * from \"./useTimeLeft\";\n"]}
1
+ {"version":3,"sources":["../src/index.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAGA,2DAAwC;AACxC,gDAA6B;AAC7B,sDAAmC;AACnC,mDAAgC;AAChC,4CAAyB;AACzB,gDAA6B","file":"index.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nexport * from './useEffectIgnoreInitial'\nexport * from './useOnResize'\nexport * from './useOutsideAlerter'\nexport * from './useSafeContext'\nexport * from './useSize'\nexport * from './useTimeLeft'\n"]}
@@ -6,7 +6,7 @@ const useEffectIgnoreInitial = (fn, deps) => {
6
6
  const isInitial = (0, react_1.useRef)(true);
7
7
  (0, react_1.useEffect)(() => {
8
8
  if (!isInitial.current) {
9
- if (typeof fn === "function") {
9
+ if (typeof fn === 'function') {
10
10
  fn();
11
11
  }
12
12
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/useEffectIgnoreInitial.tsx"],"names":[],"mappings":";;;AAGA,iCAA0C;AAEnC,MAAM,sBAAsB,GAAG,CAAO,EAAK,EAAE,IAAS,EAAE,EAAE;IAC/D,MAAM,SAAS,GAAG,IAAA,cAAM,EAAU,IAAI,CAAC,CAAC;IACxC,IAAA,iBAAS,EACP,GAAG,EAAE;QACH,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,CAAC;YACvB,IAAI,OAAO,EAAE,KAAK,UAAU,EAAE,CAAC;gBAC7B,EAAE,EAAE,CAAC;YACP,CAAC;QACH,CAAC;QACD,SAAS,CAAC,OAAO,GAAG,KAAK,CAAC;IAC5B,CAAC,EACD,IAAI,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,CAC7B,CAAC;AACJ,CAAC,CAAC;AAbW,QAAA,sBAAsB,0BAajC","file":"useEffectIgnoreInitial.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nimport { useEffect, useRef } from \"react\";\n\nexport const useEffectIgnoreInitial = <T, U>(fn: T, deps: U[]) => {\n const isInitial = useRef<boolean>(true);\n useEffect(\n () => {\n if (!isInitial.current) {\n if (typeof fn === \"function\") {\n fn();\n }\n }\n isInitial.current = false;\n },\n deps ? [...deps] : undefined\n );\n};\n"]}
1
+ {"version":3,"sources":["../src/useEffectIgnoreInitial.tsx"],"names":[],"mappings":";;;AAGA,iCAAyC;AAElC,MAAM,sBAAsB,GAAG,CAAO,EAAK,EAAE,IAAS,EAAE,EAAE;IAC/D,MAAM,SAAS,GAAG,IAAA,cAAM,EAAU,IAAI,CAAC,CAAA;IAEvC,IAAA,iBAAS,EACP,GAAG,EAAE;QACH,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,CAAC;YACvB,IAAI,OAAO,EAAE,KAAK,UAAU,EAAE,CAAC;gBAC7B,EAAE,EAAE,CAAA;YACN,CAAC;QACH,CAAC;QACD,SAAS,CAAC,OAAO,GAAG,KAAK,CAAA;IAC3B,CAAC,EACD,IAAI,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,CAC7B,CAAA;AACH,CAAC,CAAA;AAdY,QAAA,sBAAsB,0BAclC","file":"useEffectIgnoreInitial.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nimport { useEffect, useRef } from 'react'\n\nexport const useEffectIgnoreInitial = <T, U>(fn: T, deps: U[]) => {\n const isInitial = useRef<boolean>(true)\n\n useEffect(\n () => {\n if (!isInitial.current) {\n if (typeof fn === 'function') {\n fn()\n }\n }\n isInitial.current = false\n },\n deps ? [...deps] : undefined\n )\n}\n"]}
@@ -1,4 +1,4 @@
1
- import { RefObject } from "react";
1
+ import type { RefObject } from 'react';
2
2
  interface UseOnResizeOptions {
3
3
  outerElement?: RefObject<HTMLElement | null>;
4
4
  throttle?: number;
@@ -15,9 +15,9 @@ const useOnResize = (callback, options = {}) => {
15
15
  };
16
16
  (0, react_1.useEffect)(() => {
17
17
  const target = (outerElement === null || outerElement === void 0 ? void 0 : outerElement.current) || window;
18
- target.addEventListener("resize", handleResize);
18
+ target.addEventListener('resize', handleResize);
19
19
  return () => {
20
- target.removeEventListener("resize", handleResize);
20
+ target.removeEventListener('resize', handleResize);
21
21
  };
22
22
  }, [throttleDuration, callback]);
23
23
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/useOnResize.tsx"],"names":[],"mappings":";;;AAGA,iCAAqD;AAiB9C,MAAM,WAAW,GAAG,CACzB,QAAoB,EACpB,UAA8B,EAAE,EAChC,EAAE;IACF,MAAM,EAAE,YAAY,EAAE,QAAQ,EAAE,gBAAgB,GAAG,GAAG,EAAE,GAAG,OAAO,CAAC;IACnE,MAAM,eAAe,GAAG,IAAA,cAAM,EAAS,CAAC,CAAC,CAAC;IAG1C,MAAM,YAAY,GAAG,GAAG,EAAE;QACxB,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QAGvB,IAAI,GAAG,GAAG,eAAe,CAAC,OAAO,GAAG,gBAAgB,EAAE,CAAC;YACrD,OAAO;QACT,CAAC;QAED,eAAe,CAAC,OAAO,GAAG,GAAG,CAAC;QAC9B,QAAQ,EAAE,CAAC;IACb,CAAC,CAAC;IAEF,IAAA,iBAAS,EAAC,GAAG,EAAE;QAEb,MAAM,MAAM,GAAG,CAAA,YAAY,aAAZ,YAAY,uBAAZ,YAAY,CAAE,OAAO,KAAI,MAAM,CAAC;QAG/C,MAAM,CAAC,gBAAgB,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC;QAGhD,OAAO,GAAG,EAAE;YACV,MAAM,CAAC,mBAAmB,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC;QACrD,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,gBAAgB,EAAE,QAAQ,CAAC,CAAC,CAAC;AACnC,CAAC,CAAC;AAhCW,QAAA,WAAW,eAgCtB","file":"useOnResize.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nimport { RefObject, useEffect, useRef } from \"react\";\n\ninterface UseOnResizeOptions {\n outerElement?: RefObject<HTMLElement | null>;\n throttle?: number;\n}\n\n/**\n * Custom hook that triggers a callback function when the specified element\n * or the window is resized.\n *\n * @param callback - The function to be executed on resize.\n * @param options - Optional parameters to customize the behavior:\n * - outerElement: A ref to an HTMLElement to listen for resize events.\n * - throttle: Optional duration in milliseconds to throttle the callback execution.\n * Default is 100 milliseconds.\n */\nexport const useOnResize = (\n callback: () => void,\n options: UseOnResizeOptions = {}\n) => {\n const { outerElement, throttle: throttleDuration = 100 } = options;\n const lastExecutedRef = useRef<number>(0);\n\n // Throttled resize handler to limit the frequency of callback execution.\n const handleResize = () => {\n const now = Date.now();\n\n // Check if the callback can be executed based on the throttle duration.\n if (now - lastExecutedRef.current < throttleDuration) {\n return;\n }\n\n lastExecutedRef.current = now;\n callback();\n };\n\n useEffect(() => {\n // Determine the target for the resize event listener.\n const target = outerElement?.current || window;\n\n // Add the resize event listener when the component mounts.\n target.addEventListener(\"resize\", handleResize);\n\n // Clean up the event listener when the component unmounts.\n return () => {\n target.removeEventListener(\"resize\", handleResize);\n };\n }, [throttleDuration, callback]);\n};\n"]}
1
+ {"version":3,"sources":["../src/useOnResize.tsx"],"names":[],"mappings":";;;AAIA,iCAAyC;AAiBlC,MAAM,WAAW,GAAG,CACzB,QAAoB,EACpB,UAA8B,EAAE,EAChC,EAAE;IACF,MAAM,EAAE,YAAY,EAAE,QAAQ,EAAE,gBAAgB,GAAG,GAAG,EAAE,GAAG,OAAO,CAAA;IAClE,MAAM,eAAe,GAAG,IAAA,cAAM,EAAS,CAAC,CAAC,CAAA;IAGzC,MAAM,YAAY,GAAG,GAAG,EAAE;QACxB,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;QAGtB,IAAI,GAAG,GAAG,eAAe,CAAC,OAAO,GAAG,gBAAgB,EAAE,CAAC;YACrD,OAAM;QACR,CAAC;QAED,eAAe,CAAC,OAAO,GAAG,GAAG,CAAA;QAC7B,QAAQ,EAAE,CAAA;IACZ,CAAC,CAAA;IAED,IAAA,iBAAS,EAAC,GAAG,EAAE;QAEb,MAAM,MAAM,GAAG,CAAA,YAAY,aAAZ,YAAY,uBAAZ,YAAY,CAAE,OAAO,KAAI,MAAM,CAAA;QAG9C,MAAM,CAAC,gBAAgB,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAA;QAG/C,OAAO,GAAG,EAAE;YACV,MAAM,CAAC,mBAAmB,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAA;QACpD,CAAC,CAAA;IACH,CAAC,EAAE,CAAC,gBAAgB,EAAE,QAAQ,CAAC,CAAC,CAAA;AAClC,CAAC,CAAA;AAhCY,QAAA,WAAW,eAgCvB","file":"useOnResize.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nimport type { RefObject } from 'react'\nimport { useEffect, useRef } from 'react'\n\ninterface UseOnResizeOptions {\n outerElement?: RefObject<HTMLElement | null>\n throttle?: number\n}\n\n/**\n * Custom hook that triggers a callback function when the specified element\n * or the window is resized.\n *\n * @param callback - The function to be executed on resize.\n * @param options - Optional parameters to customize the behavior:\n * - outerElement: A ref to an HTMLElement to listen for resize events.\n * - throttle: Optional duration in milliseconds to throttle the callback execution.\n * Default is 100 milliseconds.\n */\nexport const useOnResize = (\n callback: () => void,\n options: UseOnResizeOptions = {}\n) => {\n const { outerElement, throttle: throttleDuration = 100 } = options\n const lastExecutedRef = useRef<number>(0)\n\n // Throttled resize handler to limit the frequency of callback execution.\n const handleResize = () => {\n const now = Date.now()\n\n // Check if the callback can be executed based on the throttle duration.\n if (now - lastExecutedRef.current < throttleDuration) {\n return\n }\n\n lastExecutedRef.current = now\n callback()\n }\n\n useEffect(() => {\n // Determine the target for the resize event listener.\n const target = outerElement?.current || window\n\n // Add the resize event listener when the component mounts.\n target.addEventListener('resize', handleResize)\n\n // Clean up the event listener when the component unmounts.\n return () => {\n target.removeEventListener('resize', handleResize)\n }\n }, [throttleDuration, callback])\n}\n"]}
@@ -1,2 +1,2 @@
1
- import { type RefObject } from "react";
1
+ import { type RefObject } from 'react';
2
2
  export declare const useOutsideAlerter: (ref: RefObject<HTMLElement | null>, callback: () => void, ignore?: string[]) => void;
@@ -16,9 +16,9 @@ const useOutsideAlerter = (ref, callback, ignore = []) => {
16
16
  }
17
17
  }
18
18
  };
19
- document.addEventListener("mousedown", handleClickOutside);
19
+ document.addEventListener('mousedown', handleClickOutside);
20
20
  return () => {
21
- document.removeEventListener("mousedown", handleClickOutside);
21
+ document.removeEventListener('mousedown', handleClickOutside);
22
22
  };
23
23
  }, [ref]);
24
24
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/useOutsideAlerter.tsx"],"names":[],"mappings":";;;AAGA,iCAAkD;AAG3C,MAAM,iBAAiB,GAAG,CAC/B,GAAkC,EAClC,QAAoB,EACpB,SAAmB,EAAE,EACrB,EAAE;IACF,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,MAAM,kBAAkB,GAAG,CAAC,EAAc,EAAE,EAAE;YAC5C,IAAI,EAAE,EAAE,CAAC;gBACP,IAAI,GAAG,CAAC,OAAO,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC,MAAc,CAAC,EAAE,CAAC;oBAC5D,MAAM,MAAM,GAAG,EAAE,CAAC,MAAqB,CAAC;oBAGxC,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,WAAW,EAAE,CAAC;oBAC7C,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,CACzB,CAAC,CAAC,EAAE,EAAE,CACJ,CAAC,CAAC,WAAW,EAAE,KAAK,OAAO,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC,KAAK,IAAI,CAClE,CAAC;oBAEF,IAAI,CAAC,OAAO,EAAE,CAAC;wBACb,QAAQ,EAAE,CAAC;oBACb,CAAC;gBACH,CAAC;YACH,CAAC;QACH,CAAC,CAAC;QACF,QAAQ,CAAC,gBAAgB,CAAC,WAAW,EAAE,kBAAkB,CAAC,CAAC;QAC3D,OAAO,GAAG,EAAE;YACV,QAAQ,CAAC,mBAAmB,CAAC,WAAW,EAAE,kBAAkB,CAAC,CAAC;QAChE,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;AACZ,CAAC,CAAC;AA7BW,QAAA,iBAAiB,qBA6B5B","file":"useOutsideAlerter.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nimport { useEffect, type RefObject } from \"react\";\n\n// A hook that alerts clicks outside of the passed ref.\nexport const useOutsideAlerter = (\n ref: RefObject<HTMLElement | null>,\n callback: () => void,\n ignore: string[] = []\n) => {\n useEffect(() => {\n const handleClickOutside = (ev: MouseEvent) => {\n if (ev) {\n if (ref.current && !ref.current.contains(ev.target as Node)) {\n const target = ev.target as HTMLElement;\n // Ignore tags with a name of `ignore`, or if there is a class of `ignore` in the parent\n // tree.\n const tagName = target.tagName.toLowerCase();\n const ignored = ignore.some(\n (i) =>\n i.toLowerCase() === tagName || target.closest(`.${i}`) !== null\n );\n\n if (!ignored) {\n callback();\n }\n }\n }\n };\n document.addEventListener(\"mousedown\", handleClickOutside);\n return () => {\n document.removeEventListener(\"mousedown\", handleClickOutside);\n };\n }, [ref]);\n};\n"]}
1
+ {"version":3,"sources":["../src/useOutsideAlerter.tsx"],"names":[],"mappings":";;;AAGA,iCAAiD;AAG1C,MAAM,iBAAiB,GAAG,CAC/B,GAAkC,EAClC,QAAoB,EACpB,SAAmB,EAAE,EACrB,EAAE;IACF,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,MAAM,kBAAkB,GAAG,CAAC,EAAc,EAAE,EAAE;YAC5C,IAAI,EAAE,EAAE,CAAC;gBACP,IAAI,GAAG,CAAC,OAAO,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC,MAAc,CAAC,EAAE,CAAC;oBAC5D,MAAM,MAAM,GAAG,EAAE,CAAC,MAAqB,CAAA;oBAGvC,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,WAAW,EAAE,CAAA;oBAC5C,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,CACzB,CAAC,CAAC,EAAE,EAAE,CACJ,CAAC,CAAC,WAAW,EAAE,KAAK,OAAO,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC,KAAK,IAAI,CAClE,CAAA;oBAED,IAAI,CAAC,OAAO,EAAE,CAAC;wBACb,QAAQ,EAAE,CAAA;oBACZ,CAAC;gBACH,CAAC;YACH,CAAC;QACH,CAAC,CAAA;QACD,QAAQ,CAAC,gBAAgB,CAAC,WAAW,EAAE,kBAAkB,CAAC,CAAA;QAC1D,OAAO,GAAG,EAAE;YACV,QAAQ,CAAC,mBAAmB,CAAC,WAAW,EAAE,kBAAkB,CAAC,CAAA;QAC/D,CAAC,CAAA;IACH,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAA;AACX,CAAC,CAAA;AA7BY,QAAA,iBAAiB,qBA6B7B","file":"useOutsideAlerter.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nimport { useEffect, type RefObject } from 'react'\n\n// A hook that alerts clicks outside of the passed ref.\nexport const useOutsideAlerter = (\n ref: RefObject<HTMLElement | null>,\n callback: () => void,\n ignore: string[] = []\n) => {\n useEffect(() => {\n const handleClickOutside = (ev: MouseEvent) => {\n if (ev) {\n if (ref.current && !ref.current.contains(ev.target as Node)) {\n const target = ev.target as HTMLElement\n // Ignore tags with a name of `ignore`, or if there is a class of `ignore` in the parent\n // tree.\n const tagName = target.tagName.toLowerCase()\n const ignored = ignore.some(\n (i) =>\n i.toLowerCase() === tagName || target.closest(`.${i}`) !== null\n )\n\n if (!ignored) {\n callback()\n }\n }\n }\n }\n document.addEventListener('mousedown', handleClickOutside)\n return () => {\n document.removeEventListener('mousedown', handleClickOutside)\n }\n }, [ref])\n}\n"]}
@@ -1,3 +1,3 @@
1
- import type { Context } from "react";
1
+ import type { Context } from 'react';
2
2
  export declare const useSafeContext: <T>(ctx: T | null | undefined) => T;
3
3
  export declare const createSafeContext: <T>() => [Context<T | null>, () => T];
@@ -4,7 +4,7 @@ exports.createSafeContext = exports.useSafeContext = void 0;
4
4
  const react_1 = require("react");
5
5
  const useSafeContext = (ctx) => {
6
6
  if (ctx === null || ctx === undefined) {
7
- throw new Error("Context value is null or undefined. Please ensure the context Provider is used correctly.");
7
+ throw new Error('Context value is null or undefined. Please ensure the context Provider is used correctly.');
8
8
  }
9
9
  return ctx;
10
10
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/useSafeContext.tsx"],"names":[],"mappings":";;;AAIA,iCAAkD;AAE3C,MAAM,cAAc,GAAG,CAAK,GAAyB,EAAK,EAAE;IACjE,IAAI,GAAG,KAAK,IAAI,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;QACtC,MAAM,IAAI,KAAK,CACb,2FAA2F,CAC5F,CAAC;IACJ,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC,CAAC;AAPW,QAAA,cAAc,kBAOzB;AAEK,MAAM,iBAAiB,GAAG,GAAqC,EAAE;IACtE,MAAM,OAAO,GAAG,IAAA,qBAAa,EAAW,IAAI,CAAC,CAAC;IAC9C,MAAM,OAAO,GAAG,GAAG,EAAE,CAAC,IAAA,sBAAc,EAAI,IAAA,kBAAU,EAAC,OAAO,CAAC,CAAC,CAAC;IAC7D,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;AAC5B,CAAC,CAAC;AAJW,QAAA,iBAAiB,qBAI5B","file":"useSafeContext.js","sourcesContent":["// Copyright 2025 @polkadot-cloud/polkadot-staking-dashboard authors & contributors\n// SPDX-License-Identifier: GPL-3.0-only\n\nimport type { Context } from \"react\";\nimport { createContext, useContext } from \"react\";\n\nexport const useSafeContext = <T,>(ctx: T | null | undefined): T => {\n if (ctx === null || ctx === undefined) {\n throw new Error(\n \"Context value is null or undefined. Please ensure the context Provider is used correctly.\"\n );\n }\n return ctx;\n};\n\nexport const createSafeContext = <T,>(): [Context<T | null>, () => T] => {\n const Context = createContext<T | null>(null);\n const useHook = () => useSafeContext<T>(useContext(Context));\n return [Context, useHook];\n};\n"]}
1
+ {"version":3,"sources":["../src/useSafeContext.tsx"],"names":[],"mappings":";;;AAIA,iCAAiD;AAE1C,MAAM,cAAc,GAAG,CAAK,GAAyB,EAAK,EAAE;IACjE,IAAI,GAAG,KAAK,IAAI,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;QACtC,MAAM,IAAI,KAAK,CACb,2FAA2F,CAC5F,CAAA;IACH,CAAC;IACD,OAAO,GAAG,CAAA;AACZ,CAAC,CAAA;AAPY,QAAA,cAAc,kBAO1B;AAEM,MAAM,iBAAiB,GAAG,GAAqC,EAAE;IACtE,MAAM,OAAO,GAAG,IAAA,qBAAa,EAAW,IAAI,CAAC,CAAA;IAC7C,MAAM,OAAO,GAAG,GAAG,EAAE,CAAC,IAAA,sBAAc,EAAI,IAAA,kBAAU,EAAC,OAAO,CAAC,CAAC,CAAA;IAC5D,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,CAAA;AAC3B,CAAC,CAAA;AAJY,QAAA,iBAAiB,qBAI7B","file":"useSafeContext.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nimport type { Context } from 'react'\nimport { createContext, useContext } from 'react'\n\nexport const useSafeContext = <T,>(ctx: T | null | undefined): T => {\n if (ctx === null || ctx === undefined) {\n throw new Error(\n 'Context value is null or undefined. Please ensure the context Provider is used correctly.'\n )\n }\n return ctx\n}\n\nexport const createSafeContext = <T,>(): [Context<T | null>, () => T] => {\n const Context = createContext<T | null>(null)\n const useHook = () => useSafeContext<T>(useContext(Context))\n return [Context, useHook]\n}\n"]}
package/cjs/useSize.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { RefObject } from "react";
1
+ import type { RefObject } from 'react';
2
2
  interface UseSizeOptions {
3
3
  outerElement?: RefObject<HTMLElement | null>;
4
4
  throttle?: number;
package/cjs/useSize.js CHANGED
@@ -21,9 +21,9 @@ const useSize = (element, options = {}) => {
21
21
  };
22
22
  (0, react_1.useEffect)(() => {
23
23
  const listenFor = (outerElement === null || outerElement === void 0 ? void 0 : outerElement.current) || window;
24
- listenFor.addEventListener("resize", handleResize);
24
+ listenFor.addEventListener('resize', handleResize);
25
25
  return () => {
26
- listenFor.removeEventListener("resize", handleResize);
26
+ listenFor.removeEventListener('resize', handleResize);
27
27
  };
28
28
  }, [outerElement === null || outerElement === void 0 ? void 0 : outerElement.current]);
29
29
  return size;
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/useSize.tsx"],"names":[],"mappings":";;;AAIA,iCAAoD;AAU7C,MAAM,OAAO,GAAG,CACrB,OAAsC,EACtC,UAA0B,EAAE,EAC5B,EAAE;IACF,MAAM,EAAE,YAAY,EAAE,QAAQ,EAAE,gBAAgB,GAAG,GAAG,EAAE,GAAG,OAAO,CAAC;IAInE,MAAM,OAAO,GAAG,CAAC,KAAyB,IAAI,EAAE,EAAE;QAChD,MAAM,KAAK,GAAG,CAAA,EAAE,aAAF,EAAE,uBAAF,EAAE,CAAE,WAAW,KAAI,CAAC,CAAC;QACnC,MAAM,MAAM,GAAG,CAAA,EAAE,aAAF,EAAE,uBAAF,EAAE,CAAE,YAAY,KAAI,CAAC,CAAC;QACrC,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC;IAC3B,CAAC,CAAC;IAGF,MAAM,eAAe,GAAG,IAAA,cAAM,EAAS,CAAC,CAAC,CAAC;IAG1C,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,IAAA,gBAAQ,EAC9B,OAAO,CAAC,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,OAAO,CAAC,CAC1B,CAAC;IAGF,MAAM,YAAY,GAAG,GAAG,EAAE;QACxB,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QACvB,IAAI,GAAG,GAAG,eAAe,CAAC,OAAO,GAAG,gBAAgB,EAAE,CAAC;YACrD,OAAO;QACT,CAAC;QAED,eAAe,CAAC,OAAO,GAAG,GAAG,CAAC;QAE9B,OAAO,CAAC,OAAO,CAAC,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,OAAO,CAAC,CAAC,CAAC;IACrC,CAAC,CAAC;IAGF,IAAA,iBAAS,EAAC,GAAG,EAAE;QAGb,MAAM,SAAS,GAAG,CAAA,YAAY,aAAZ,YAAY,uBAAZ,YAAY,CAAE,OAAO,KAAI,MAAM,CAAC;QAElD,SAAS,CAAC,gBAAgB,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC;QAGnD,OAAO,GAAG,EAAE;YACV,SAAS,CAAC,mBAAmB,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC;QACxD,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,YAAY,aAAZ,YAAY,uBAAZ,YAAY,CAAE,OAAO,CAAC,CAAC,CAAC;IAG5B,OAAO,IAAI,CAAC;AACd,CAAC,CAAC;AAlDW,QAAA,OAAO,WAkDlB","file":"useSize.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nimport type { RefObject } from \"react\";\nimport { useEffect, useRef, useState } from \"react\";\n\n// Define the type for the options parameter.\ninterface UseSizeOptions {\n outerElement?: RefObject<HTMLElement | null>;\n throttle?: number;\n}\n\n// Custom hook to get the width and height of a specified element. Updates the `size` state when the\n// specified \"outer element\" (or the window by default) resizes.\nexport const useSize = (\n element: RefObject<HTMLElement | null>,\n options: UseSizeOptions = {}\n) => {\n const { outerElement, throttle: throttleDuration = 100 } = options;\n\n // Helper function to retrieve the width and height of an element\n // If no element is found, default dimensions are set to 0.\n const getSize = (el: HTMLElement | null = null) => {\n const width = el?.offsetWidth || 0;\n const height = el?.offsetHeight || 0;\n return { width, height };\n };\n\n // Ref to store the last execution time of the `resizeThrottle` handler.\n const lastExecutedRef = useRef<number>(0);\n\n // State to store the current width and height of the specified element.\n const [size, setSize] = useState<{ width: number; height: number }>(\n getSize(element?.current)\n );\n\n // Throttle the resize event handler to limit how often size updates occur.\n const handleResize = () => {\n const now = Date.now();\n if (now - lastExecutedRef.current < throttleDuration) {\n return;\n } // Exit if `throttleDuration` has not passed.\n\n lastExecutedRef.current = now; // Update last execution time.\n\n setSize(getSize(element?.current));\n };\n\n // Set up the resize event listener on mount and clean it up on unmount.\n useEffect(() => {\n // Determine the target for the resize event listener.\n // If `outerElement` is provided, listen to its resize events; otherwise, listen to the window's.\n const listenFor = outerElement?.current || window;\n\n listenFor.addEventListener(\"resize\", handleResize);\n\n // Clean up event listener when the component unmounts to avoid memory leaks.\n return () => {\n listenFor.removeEventListener(\"resize\", handleResize);\n };\n }, [outerElement?.current]);\n\n // Return the current size of the element.\n return size;\n};\n"]}
1
+ {"version":3,"sources":["../src/useSize.tsx"],"names":[],"mappings":";;;AAIA,iCAAmD;AAU5C,MAAM,OAAO,GAAG,CACrB,OAAsC,EACtC,UAA0B,EAAE,EAC5B,EAAE;IACF,MAAM,EAAE,YAAY,EAAE,QAAQ,EAAE,gBAAgB,GAAG,GAAG,EAAE,GAAG,OAAO,CAAA;IAIlE,MAAM,OAAO,GAAG,CAAC,KAAyB,IAAI,EAAE,EAAE;QAChD,MAAM,KAAK,GAAG,CAAA,EAAE,aAAF,EAAE,uBAAF,EAAE,CAAE,WAAW,KAAI,CAAC,CAAA;QAClC,MAAM,MAAM,GAAG,CAAA,EAAE,aAAF,EAAE,uBAAF,EAAE,CAAE,YAAY,KAAI,CAAC,CAAA;QACpC,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,CAAA;IAC1B,CAAC,CAAA;IAGD,MAAM,eAAe,GAAG,IAAA,cAAM,EAAS,CAAC,CAAC,CAAA;IAGzC,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,IAAA,gBAAQ,EAC9B,OAAO,CAAC,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,OAAO,CAAC,CAC1B,CAAA;IAGD,MAAM,YAAY,GAAG,GAAG,EAAE;QACxB,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;QACtB,IAAI,GAAG,GAAG,eAAe,CAAC,OAAO,GAAG,gBAAgB,EAAE,CAAC;YACrD,OAAM;QACR,CAAC;QAED,eAAe,CAAC,OAAO,GAAG,GAAG,CAAA;QAE7B,OAAO,CAAC,OAAO,CAAC,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,OAAO,CAAC,CAAC,CAAA;IACpC,CAAC,CAAA;IAGD,IAAA,iBAAS,EAAC,GAAG,EAAE;QAGb,MAAM,SAAS,GAAG,CAAA,YAAY,aAAZ,YAAY,uBAAZ,YAAY,CAAE,OAAO,KAAI,MAAM,CAAA;QAEjD,SAAS,CAAC,gBAAgB,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAA;QAGlD,OAAO,GAAG,EAAE;YACV,SAAS,CAAC,mBAAmB,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAA;QACvD,CAAC,CAAA;IACH,CAAC,EAAE,CAAC,YAAY,aAAZ,YAAY,uBAAZ,YAAY,CAAE,OAAO,CAAC,CAAC,CAAA;IAG3B,OAAO,IAAI,CAAA;AACb,CAAC,CAAA;AAlDY,QAAA,OAAO,WAkDnB","file":"useSize.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nimport type { RefObject } from 'react'\nimport { useEffect, useRef, useState } from 'react'\n\n// Define the type for the options parameter.\ninterface UseSizeOptions {\n outerElement?: RefObject<HTMLElement | null>\n throttle?: number\n}\n\n// Custom hook to get the width and height of a specified element. Updates the `size` state when the\n// specified \"outer element\" (or the window by default) resizes.\nexport const useSize = (\n element: RefObject<HTMLElement | null>,\n options: UseSizeOptions = {}\n) => {\n const { outerElement, throttle: throttleDuration = 100 } = options\n\n // Helper function to retrieve the width and height of an element\n // If no element is found, default dimensions are set to 0.\n const getSize = (el: HTMLElement | null = null) => {\n const width = el?.offsetWidth || 0\n const height = el?.offsetHeight || 0\n return { width, height }\n }\n\n // Ref to store the last execution time of the `resizeThrottle` handler.\n const lastExecutedRef = useRef<number>(0)\n\n // State to store the current width and height of the specified element.\n const [size, setSize] = useState<{ width: number; height: number }>(\n getSize(element?.current)\n )\n\n // Throttle the resize event handler to limit how often size updates occur.\n const handleResize = () => {\n const now = Date.now()\n if (now - lastExecutedRef.current < throttleDuration) {\n return\n } // Exit if `throttleDuration` has not passed.\n\n lastExecutedRef.current = now // Update last execution time.\n\n setSize(getSize(element?.current))\n }\n\n // Set up the resize event listener on mount and clean it up on unmount.\n useEffect(() => {\n // Determine the target for the resize event listener.\n // If `outerElement` is provided, listen to its resize events; otherwise, listen to the window's.\n const listenFor = outerElement?.current || window\n\n listenFor.addEventListener('resize', handleResize)\n\n // Clean up event listener when the component unmounts to avoid memory leaks.\n return () => {\n listenFor.removeEventListener('resize', handleResize)\n }\n }, [outerElement?.current])\n\n // Return the current size of the element.\n return size\n}\n"]}
@@ -1,3 +1,3 @@
1
- import type { TimeleftDuration } from "@w3ux/types";
1
+ import type { TimeleftDuration } from '@w3ux/types';
2
2
  export declare const defaultDuration: TimeleftDuration;
3
3
  export declare const defaultRefreshInterval = 60;
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/useTimeLeft/defaults.ts"],"names":[],"mappings":";;;AAKa,QAAA,eAAe,GAAqB;IAC/C,IAAI,EAAE,CAAC;IACP,KAAK,EAAE,CAAC;IACR,OAAO,EAAE,CAAC;IACV,OAAO,EAAE,CAAC;IACV,UAAU,EAAE,KAAK;CAClB,CAAC;AAEW,QAAA,sBAAsB,GAAG,EAAE,CAAC","file":"defaults.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nimport type { TimeleftDuration } from \"@w3ux/types\";\n\nexport const defaultDuration: TimeleftDuration = {\n days: 0,\n hours: 0,\n minutes: 0,\n seconds: 0,\n lastMinute: false,\n};\n\nexport const defaultRefreshInterval = 60;\n"]}
1
+ {"version":3,"sources":["../src/useTimeLeft/defaults.ts"],"names":[],"mappings":";;;AAKa,QAAA,eAAe,GAAqB;IAC/C,IAAI,EAAE,CAAC;IACP,KAAK,EAAE,CAAC;IACR,OAAO,EAAE,CAAC;IACV,OAAO,EAAE,CAAC;IACV,UAAU,EAAE,KAAK;CAClB,CAAA;AAEY,QAAA,sBAAsB,GAAG,EAAE,CAAA","file":"defaults.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nimport type { TimeleftDuration } from '@w3ux/types'\n\nexport const defaultDuration: TimeleftDuration = {\n days: 0,\n hours: 0,\n minutes: 0,\n seconds: 0,\n lastMinute: false,\n}\n\nexport const defaultRefreshInterval = 60\n"]}
@@ -1,4 +1,4 @@
1
- import type { TimeLeftAll, UseTimeleftProps } from "@w3ux/types";
1
+ import type { TimeLeftAll, UseTimeleftProps } from '@w3ux/types';
2
2
  export declare const useTimeLeft: (props?: UseTimeleftProps) => {
3
3
  setFromNow: (dateFrom: Date, dateTo: Date) => void;
4
4
  timeleft: TimeLeftAll;
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/useTimeLeft/index.tsx"],"names":[],"mappings":";;;AAGA,uCAA8C;AAC9C,iCAAoD;AAOpD,kCAAsC;AAE/B,MAAM,WAAW,GAAG,CAAC,KAAwB,EAAE,EAAE;IACtD,MAAM,YAAY,GAAG,CAAA,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,YAAY,KAAI,EAAE,CAAC;IAC/C,MAAM,UAAU,GAAG,CAAA,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,UAAU,KAAI,EAAE,CAAC;IAG3C,MAAM,UAAU,GAAG,GAAG,EAAE;QACtB,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,IAAA,kBAAW,EAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QACnD,OAAO,CAAC,IAAI,IAAI,CAAC,KAAK,CAAC;IACzB,CAAC,CAAC;IAGF,MAAM,mBAAmB,GAAG,GAAG,EAAE;QAC/B,MAAM,EAAE,OAAO,EAAE,GAAG,IAAA,kBAAW,EAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QAC/C,IAAI,CAAC,UAAU,EAAE,EAAE,CAAC;YAClB,OAAO,EAAE,CAAC;QACZ,CAAC;QACD,OAAO,OAAO,CAAC;IACjB,CAAC,CAAC;IAGF,MAAM,WAAW,GAAG,CA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D,IAAA,uBAAe,EAAC,MAAM,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC;IACxC,CAAC,CAAC;IAEF,OAAO;QACL,UAAU;QACV,QAAQ;KACT,CAAC;AACJ,CAAC,CAAC;AA3GW,QAAA,WAAW,eA2GtB","file":"index.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nimport { setStateWithRef } from \"@w3ux/utils\";\nimport { useEffect, useRef, useState } from \"react\";\nimport type {\n TimeLeftAll,\n TimeLeftRaw,\n TimeleftDuration,\n UseTimeleftProps,\n} from \"@w3ux/types\";\nimport { getDuration } from \"../util\";\n\nexport const useTimeLeft = (props?: UseTimeleftProps) => {\n const depsTimeleft = props?.depsTimeleft || [];\n const depsFormat = props?.depsFormat || [];\n\n // check whether timeleft is within a minute of finishing.\n const inLastHour = () => {\n const { days, hours } = getDuration(toRef.current);\n return !days && !hours;\n };\n\n // get the amount of seconds left if timeleft is in the last minute.\n const lastMinuteCountdown = () => {\n const { seconds } = getDuration(toRef.current);\n if (!inLastHour()) {\n return 60;\n }\n return seconds;\n };\n\n // calculate resulting timeleft object from latest duration.\n const getTimeleft = (c?: TimeleftDuration): TimeLeftAll => {\n const { days, hours, minutes, seconds } = c || getDuration(toRef.current);\n const raw: TimeLeftRaw = {\n days,\n hours,\n minutes,\n };\n if (!days && !hours) {\n raw.seconds = seconds;\n }\n return {\n raw,\n };\n };\n\n // the end time as a date.\n const [to, setTo] = useState<Date | null>(null);\n const toRef = useRef(to);\n\n // resulting timeleft object to be returned.\n const [timeleft, setTimeleft] = useState<TimeLeftAll>(getTimeleft());\n\n // timeleft refresh intervals.\n const [minInterval, setMinInterval] = useState<\n ReturnType<typeof setInterval> | undefined\n >(undefined);\n const minIntervalRef = useRef(minInterval);\n\n const [secInterval, setSecInterval] = useState<\n ReturnType<typeof setInterval> | undefined\n >(undefined);\n const secIntervalRef = useRef(secInterval);\n\n // refresh effects.\n useEffect(() => {\n setTimeleft(getTimeleft());\n if (inLastHour()) {\n // refresh timeleft every second.\n if (!secIntervalRef.current) {\n const interval = setInterval(() => {\n if (!inLastHour()) {\n clearInterval(secIntervalRef.current);\n setStateWithRef(undefined, setSecInterval, secIntervalRef);\n }\n setTimeleft(getTimeleft());\n }, 1000);\n\n setStateWithRef(interval, setSecInterval, secIntervalRef);\n }\n }\n // refresh timeleft every minute.\n else if (!minIntervalRef.current) {\n const interval = setInterval(() => {\n if (inLastHour()) {\n clearInterval(minIntervalRef.current);\n setStateWithRef(undefined, setMinInterval, minIntervalRef);\n }\n setTimeleft(getTimeleft());\n }, 60000);\n setStateWithRef(interval, setMinInterval, minIntervalRef);\n }\n }, [to, inLastHour(), lastMinuteCountdown(), ...depsTimeleft]);\n\n // re-render the timeleft upon formatting changes.\n useEffect(() => {\n setTimeleft(getTimeleft());\n }, [...depsFormat]);\n\n // clear intervals on unmount\n useEffect(\n () => () => {\n clearInterval(minInterval);\n clearInterval(secInterval);\n },\n []\n );\n\n // Set the end time and calculate timeleft.\n const setFromNow = (dateFrom: Date, dateTo: Date) => {\n setTimeleft(getTimeleft(getDuration(dateFrom)));\n setStateWithRef(dateTo, setTo, toRef);\n };\n\n return {\n setFromNow,\n timeleft,\n };\n};\n"]}
1
+ {"version":3,"sources":["../src/useTimeLeft/index.tsx"],"names":[],"mappings":";;;AASA,uCAA6C;AAC7C,iCAAmD;AACnD,kCAAqC;AAE9B,MAAM,WAAW,GAAG,CAAC,KAAwB,EAAE,EAAE;IACtD,MAAM,YAAY,GAAG,CAAA,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,YAAY,KAAI,EAAE,CAAA;IAC9C,MAAM,UAAU,GAAG,CAAA,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,UAAU,KAAI,EAAE,CAAA;IAG1C,MAAM,UAAU,GAAG,GAAG,EAAE;QACtB,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,IAAA,kBAAW,EAAC,KAAK,CAAC,OAAO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C,EAAE,CAAC,GAAG,UAAU,CAAC,CAAC,CAAA;IAGnB,IAAA,iBAAS,EACP,GAAG,EAAE,CAAC,GAAG,EAAE;QACT,aAAa,CAAC,WAAW,CAAC,CAAA;QAC1B,aAAa,CAAC,WAAW,CAAC,CAAA;IAC5B,CAAC,EACD,EAAE,CACH,CAAA;IAGD,MAAM,UAAU,GAAG,CAAC,QAAc,EAAE,MAAY,EAAE,EAAE;QAClD,WAAW,CAAC,WAAW,CAAC,IAAA,kBAAW,EAAC,QAAQ,CAAC,CAAC,CAAC,CAAA;QAC/C,IAAA,uBAAe,EAAC,MAAM,EAAE,KAAK,EAAE,KAAK,CAAC,CAAA;IACvC,CAAC,CAAA;IAED,OAAO;QACL,UAAU;QACV,QAAQ;KACT,CAAA;AACH,CAAC,CAAA;AA3GY,QAAA,WAAW,eA2GvB","file":"index.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nimport type {\n TimeLeftAll,\n TimeLeftRaw,\n TimeleftDuration,\n UseTimeleftProps,\n} from '@w3ux/types'\nimport { setStateWithRef } from '@w3ux/utils'\nimport { useEffect, useRef, useState } from 'react'\nimport { getDuration } from '../util'\n\nexport const useTimeLeft = (props?: UseTimeleftProps) => {\n const depsTimeleft = props?.depsTimeleft || []\n const depsFormat = props?.depsFormat || []\n\n // check whether timeleft is within a minute of finishing.\n const inLastHour = () => {\n const { days, hours } = getDuration(toRef.current)\n return !days && !hours\n }\n\n // get the amount of seconds left if timeleft is in the last minute.\n const lastMinuteCountdown = () => {\n const { seconds } = getDuration(toRef.current)\n if (!inLastHour()) {\n return 60\n }\n return seconds\n }\n\n // calculate resulting timeleft object from latest duration.\n const getTimeleft = (c?: TimeleftDuration): TimeLeftAll => {\n const { days, hours, minutes, seconds } = c || getDuration(toRef.current)\n const raw: TimeLeftRaw = {\n days,\n hours,\n minutes,\n }\n if (!days && !hours) {\n raw.seconds = seconds\n }\n return {\n raw,\n }\n }\n\n // the end time as a date.\n const [to, setTo] = useState<Date | null>(null)\n const toRef = useRef(to)\n\n // resulting timeleft object to be returned.\n const [timeleft, setTimeleft] = useState<TimeLeftAll>(getTimeleft())\n\n // timeleft refresh intervals.\n const [minInterval, setMinInterval] = useState<\n ReturnType<typeof setInterval> | undefined\n >(undefined)\n const minIntervalRef = useRef(minInterval)\n\n const [secInterval, setSecInterval] = useState<\n ReturnType<typeof setInterval> | undefined\n >(undefined)\n const secIntervalRef = useRef(secInterval)\n\n // refresh effects.\n useEffect(() => {\n setTimeleft(getTimeleft())\n if (inLastHour()) {\n // refresh timeleft every second.\n if (!secIntervalRef.current) {\n const interval = setInterval(() => {\n if (!inLastHour()) {\n clearInterval(secIntervalRef.current)\n setStateWithRef(undefined, setSecInterval, secIntervalRef)\n }\n setTimeleft(getTimeleft())\n }, 1000)\n\n setStateWithRef(interval, setSecInterval, secIntervalRef)\n }\n }\n // refresh timeleft every minute.\n else if (!minIntervalRef.current) {\n const interval = setInterval(() => {\n if (inLastHour()) {\n clearInterval(minIntervalRef.current)\n setStateWithRef(undefined, setMinInterval, minIntervalRef)\n }\n setTimeleft(getTimeleft())\n }, 60000)\n setStateWithRef(interval, setMinInterval, minIntervalRef)\n }\n }, [to, inLastHour(), lastMinuteCountdown(), ...depsTimeleft])\n\n // re-render the timeleft upon formatting changes.\n useEffect(() => {\n setTimeleft(getTimeleft())\n }, [...depsFormat])\n\n // clear intervals on unmount\n useEffect(\n () => () => {\n clearInterval(minInterval)\n clearInterval(secInterval)\n },\n []\n )\n\n // Set the end time and calculate timeleft.\n const setFromNow = (dateFrom: Date, dateTo: Date) => {\n setTimeleft(getTimeleft(getDuration(dateFrom)))\n setStateWithRef(dateTo, setTo, toRef)\n }\n\n return {\n setFromNow,\n timeleft,\n }\n}\n"]}
package/cjs/util.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { TimeleftDuration } from "@w3ux/types";
1
+ import type { TimeleftDuration } from '@w3ux/types';
2
2
  export declare const getDuration: (toDate: Date | null) => TimeleftDuration;
3
3
  export declare const secondsFromNow: (seconds: number) => Date;
4
4
  export declare const getDurationFromNow: (toDate: Date | null) => TimeleftDuration;
package/cjs/util.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/util.ts"],"names":[],"mappings":";;;AAIA,uCAA6E;AAC7E,qDAAyD;AAGlD,MAAM,WAAW,GAAG,CAAC,MAAmB,EAAoB,EAAE;IACnE,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,OAAO,0BAAe,CAAC;IACzB,CAAC;IACD,IAAI,IAAA,sBAAW,EAAC,MAAM,CAAC,IAAI,IAAA,sBAAW,EAAC,IAAI,IAAI,EAAE,CAAC,EAAE,CAAC;QACnD,OAAO,0BAAe,CAAC;IACzB,CAAC;IAED,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,UAAU,EAAE,CAAC,CAAC;IACvC,MAAM,CAAC,GAAG,IAAA,6BAAkB,EAAC;QAC3B,KAAK,EAAE,IAAI,CAAC,GAAG,EAAE;QACjB,GAAG,EAAE,MAAM;KACZ,CAAC,CAAC;IAEH,MAAM,IAAI,GAAG,IAAA,2BAAgB,EAAC,MAAM,EAAE,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;IAClD,MAAM,KAAK,GAAG,CAAA,CAAC,aAAD,CAAC,uBAAD,CAAC,CAAE,KAAK,KAAI,CAAC,CAAC;IAC5B,MAAM,OAAO,GAAG,CAAA,CAAC,aAAD,CAAC,uBAAD,CAAC,CAAE,OAAO,KAAI,CAAC,CAAC;IAChC,MAAM,OAAO,GAAG,CAAA,CAAC,aAAD,CAAC,uBAAD,CAAC,CAAE,OAAO,KAAI,CAAC,CAAC;IAChC,MAAM,QAAQ,GAAG,IAAI,KAAK,CAAC,IAAI,KAAK,KAAK,CAAC,CAAC;IAC3C,MAAM,UAAU,GAAG,QAAQ,IAAI,OAAO,KAAK,CAAC,CAAC;IAE7C,OAAO;QACL,IAAI;QACJ,KAAK;QACL,OAAO;QACP,OA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,UAAU,GAAG,QAAQ,IAAI,OAAO,KAAK,CAAC,CAAC;IAE7C,OAAO;QACL,IAAI;QACJ,KAAK;QACL,OAAO;QACP,OAAO;QACP,UAAU;KACX,CAAC;AACJ,CAAC,CAAC;AA5BW,QAAA,kBAAkB,sBA4B7B","file":"util.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nimport { TimeleftDuration } from \"@w3ux/types\";\nimport { differenceInDays, getUnixTime, intervalToDuration } from \"date-fns\";\nimport { defaultDuration } from \"./useTimeLeft/defaults\";\n\n// calculates the current timeleft duration.\nexport const getDuration = (toDate: Date | null): TimeleftDuration => {\n if (!toDate) {\n return defaultDuration;\n }\n if (getUnixTime(toDate) <= getUnixTime(new Date())) {\n return defaultDuration;\n }\n\n toDate.setSeconds(toDate.getSeconds());\n const d = intervalToDuration({\n start: Date.now(),\n end: toDate,\n });\n\n const days = differenceInDays(toDate, Date.now());\n const hours = d?.hours || 0;\n const minutes = d?.minutes || 0;\n const seconds = d?.seconds || 0;\n const lastHour = days === 0 && hours === 0;\n const lastMinute = lastHour && minutes === 0;\n\n return {\n days,\n hours,\n minutes,\n seconds,\n lastMinute,\n };\n};\n\n// Helper: Adds `seconds` to the current time and returns the resulting date.\nexport const secondsFromNow = (seconds: number): Date => {\n const end = new Date();\n end.setSeconds(end.getSeconds() + seconds);\n return end;\n};\n\n// Helper: Calculates the duration between the current time and the provided date.\nexport const getDurationFromNow = (toDate: Date | null): TimeleftDuration => {\n if (!toDate) {\n return defaultDuration;\n }\n if (getUnixTime(toDate) <= getUnixTime(new Date())) {\n return defaultDuration;\n }\n\n toDate.setSeconds(toDate.getSeconds());\n const d = intervalToDuration({\n start: Date.now(),\n end: toDate,\n });\n\n const days = differenceInDays(toDate, Date.now());\n const hours = d?.hours || 0;\n const minutes = d?.minutes || 0;\n const seconds = d?.seconds || 0;\n const lastHour = days === 0 && hours === 0;\n const lastMinute = lastHour && minutes === 0;\n\n return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AK;QACL,OAAO;QACP,OAAO;QACP,UAAU;KACX,CAAA;AACH,CAAC,CAAA;AA5BY,QAAA,kBAAkB,sBA4B9B","file":"util.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nimport type { TimeleftDuration } from '@w3ux/types'\nimport { differenceInDays, getUnixTime, intervalToDuration } from 'date-fns'\nimport { defaultDuration } from './useTimeLeft/defaults'\n\n// calculates the current timeleft duration.\nexport const getDuration = (toDate: Date | null): TimeleftDuration => {\n if (!toDate) {\n return defaultDuration\n }\n if (getUnixTime(toDate) <= getUnixTime(new Date())) {\n return defaultDuration\n }\n\n toDate.setSeconds(toDate.getSeconds())\n const d = intervalToDuration({\n start: Date.now(),\n end: toDate,\n })\n\n const days = differenceInDays(toDate, Date.now())\n const hours = d?.hours || 0\n const minutes = d?.minutes || 0\n const seconds = d?.seconds || 0\n const lastHour = days === 0 && hours === 0\n const lastMinute = lastHour && minutes === 0\n\n return {\n days,\n hours,\n minutes,\n seconds,\n lastMinute,\n }\n}\n\n// Helper: Adds `seconds` to the current time and returns the resulting date.\nexport const secondsFromNow = (seconds: number): Date => {\n const end = new Date()\n end.setSeconds(end.getSeconds() + seconds)\n return end\n}\n\n// Helper: Calculates the duration between the current time and the provided date.\nexport const getDurationFromNow = (toDate: Date | null): TimeleftDuration => {\n if (!toDate) {\n return defaultDuration\n }\n if (getUnixTime(toDate) <= getUnixTime(new Date())) {\n return defaultDuration\n }\n\n toDate.setSeconds(toDate.getSeconds())\n const d = intervalToDuration({\n start: Date.now(),\n end: toDate,\n })\n\n const days = differenceInDays(toDate, Date.now())\n const hours = d?.hours || 0\n const minutes = d?.minutes || 0\n const seconds = d?.seconds || 0\n const lastHour = days === 0 && hours === 0\n const lastMinute = lastHour && minutes === 0\n\n return {\n days,\n hours,\n minutes,\n seconds,\n lastMinute,\n }\n}\n"]}
package/mjs/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- export * from "./useEffectIgnoreInitial";
2
- export * from "./useOnResize";
3
- export * from "./useOutsideAlerter";
4
- export * from "./useSafeContext";
5
- export * from "./useSize";
6
- export * from "./useTimeLeft";
1
+ export * from './useEffectIgnoreInitial';
2
+ export * from './useOnResize';
3
+ export * from './useOutsideAlerter';
4
+ export * from './useSafeContext';
5
+ export * from './useSize';
6
+ export * from './useTimeLeft';
package/mjs/index.js CHANGED
@@ -1,9 +1,9 @@
1
- export * from "./useEffectIgnoreInitial";
2
- export * from "./useOnResize";
3
- export * from "./useOutsideAlerter";
4
- export * from "./useSafeContext";
5
- export * from "./useSize";
6
- export * from "./useTimeLeft";
1
+ export * from './useEffectIgnoreInitial';
2
+ export * from './useOnResize';
3
+ export * from './useOutsideAlerter';
4
+ export * from './useSafeContext';
5
+ export * from './useSize';
6
+ export * from './useTimeLeft';
7
7
 
8
8
  //# sourceMappingURL=index.js.map
9
9
 
package/mjs/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.tsx"],"names":[],"mappings":"AAGA,cAAc,0BAA0B,CAAC;AACzC,cAAc,eAAe,CAAC;AAC9B,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC;AACjC,cAAc,WAAW,CAAC;AAC1B,cAAc,eAAe,CAAC","file":"index.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nexport * from \"./useEffectIgnoreInitial\";\nexport * from \"./useOnResize\";\nexport * from \"./useOutsideAlerter\";\nexport * from \"./useSafeContext\";\nexport * from \"./useSize\";\nexport * from \"./useTimeLeft\";\n"]}
1
+ {"version":3,"sources":["../src/index.tsx"],"names":[],"mappings":"AAGA,cAAc,0BAA0B,CAAA;AACxC,cAAc,eAAe,CAAA;AAC7B,cAAc,qBAAqB,CAAA;AACnC,cAAc,kBAAkB,CAAA;AAChC,cAAc,WAAW,CAAA;AACzB,cAAc,eAAe,CAAA","file":"index.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nexport * from './useEffectIgnoreInitial'\nexport * from './useOnResize'\nexport * from './useOutsideAlerter'\nexport * from './useSafeContext'\nexport * from './useSize'\nexport * from './useTimeLeft'\n"]}
@@ -1,9 +1,9 @@
1
- import { useEffect, useRef } from "react";
1
+ import { useEffect, useRef } from 'react';
2
2
  export const useEffectIgnoreInitial = (fn, deps) => {
3
3
  const isInitial = useRef(true);
4
4
  useEffect(() => {
5
5
  if (!isInitial.current) {
6
- if (typeof fn === "function") {
6
+ if (typeof fn === 'function') {
7
7
  fn();
8
8
  }
9
9
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/useEffectIgnoreInitial.tsx"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAE1C,MAAM,CAAC,MAAM,sBAAsB,GAAG,CAAO,EAAK,EAAE,IAAS,EAAE,EAAE;IAC/D,MAAM,SAAS,GAAG,MAAM,CAAU,IAAI,CAAC,CAAC;IACxC,SAAS,CACP,GAAG,EAAE;QACH,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,CAAC;YACvB,IAAI,OAAO,EAAE,KAAK,UAAU,EAAE,CAAC;gBAC7B,EAAE,EAAE,CAAC;YACP,CAAC;QACH,CAAC;QACD,SAAS,CAAC,OAAO,GAAG,KAAK,CAAC;IAC5B,CAAC,EACD,IAAI,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,CAC7B,CAAC;AACJ,CAAC,CAAC","file":"useEffectIgnoreInitial.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nimport { useEffect, useRef } from \"react\";\n\nexport const useEffectIgnoreInitial = <T, U>(fn: T, deps: U[]) => {\n const isInitial = useRef<boolean>(true);\n useEffect(\n () => {\n if (!isInitial.current) {\n if (typeof fn === \"function\") {\n fn();\n }\n }\n isInitial.current = false;\n },\n deps ? [...deps] : undefined\n );\n};\n"]}
1
+ {"version":3,"sources":["../src/useEffectIgnoreInitial.tsx"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,OAAO,CAAA;AAEzC,MAAM,CAAC,MAAM,sBAAsB,GAAG,CAAO,EAAK,EAAE,IAAS,EAAE,EAAE;IAC/D,MAAM,SAAS,GAAG,MAAM,CAAU,IAAI,CAAC,CAAA;IAEvC,SAAS,CACP,GAAG,EAAE;QACH,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,CAAC;YACvB,IAAI,OAAO,EAAE,KAAK,UAAU,EAAE,CAAC;gBAC7B,EAAE,EAAE,CAAA;YACN,CAAC;QACH,CAAC;QACD,SAAS,CAAC,OAAO,GAAG,KAAK,CAAA;IAC3B,CAAC,EACD,IAAI,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,CAC7B,CAAA;AACH,CAAC,CAAA","file":"useEffectIgnoreInitial.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nimport { useEffect, useRef } from 'react'\n\nexport const useEffectIgnoreInitial = <T, U>(fn: T, deps: U[]) => {\n const isInitial = useRef<boolean>(true)\n\n useEffect(\n () => {\n if (!isInitial.current) {\n if (typeof fn === 'function') {\n fn()\n }\n }\n isInitial.current = false\n },\n deps ? [...deps] : undefined\n )\n}\n"]}
@@ -1,4 +1,4 @@
1
- import { RefObject } from "react";
1
+ import type { RefObject } from 'react';
2
2
  interface UseOnResizeOptions {
3
3
  outerElement?: RefObject<HTMLElement | null>;
4
4
  throttle?: number;
@@ -1,4 +1,4 @@
1
- import { useEffect, useRef } from "react";
1
+ import { useEffect, useRef } from 'react';
2
2
  export const useOnResize = (callback, options = {}) => {
3
3
  const { outerElement, throttle: throttleDuration = 100 } = options;
4
4
  const lastExecutedRef = useRef(0);
@@ -12,9 +12,9 @@ export const useOnResize = (callback, options = {}) => {
12
12
  };
13
13
  useEffect(() => {
14
14
  const target = outerElement?.current || window;
15
- target.addEventListener("resize", handleResize);
15
+ target.addEventListener('resize', handleResize);
16
16
  return () => {
17
- target.removeEventListener("resize", handleResize);
17
+ target.removeEventListener('resize', handleResize);
18
18
  };
19
19
  }, [throttleDuration, callback]);
20
20
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/useOnResize.tsx"],"names":[],"mappings":"AAGA,OAAO,EAAa,SAAS,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAiBrD,MAAM,CAAC,MAAM,WAAW,GAAG,CACzB,QAAoB,EACpB,UAA8B,EAAE,EAChC,EAAE;IACF,MAAM,EAAE,YAAY,EAAE,QAAQ,EAAE,gBAAgB,GAAG,GAAG,EAAE,GAAG,OAAO,CAAC;IACnE,MAAM,eAAe,GAAG,MAAM,CAAS,CAAC,CAAC,CAAC;IAG1C,MAAM,YAAY,GAAG,GAAG,EAAE;QACxB,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QAGvB,IAAI,GAAG,GAAG,eAAe,CAAC,OAAO,GAAG,gBAAgB,EAAE,CAAC;YACrD,OAAO;QACT,CAAC;QAED,eAAe,CAAC,OAAO,GAAG,GAAG,CAAC;QAC9B,QAAQ,EAAE,CAAC;IACb,CAAC,CAAC;IAEF,SAAS,CAAC,GAAG,EAAE;QAEb,MAAM,MAAM,GAAG,YAAY,EAAE,OAAO,IAAI,MAAM,CAAC;QAG/C,MAAM,CAAC,gBAAgB,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC;QAGhD,OAAO,GAAG,EAAE;YACV,MAAM,CAAC,mBAAmB,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC;QACrD,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,gBAAgB,EAAE,QAAQ,CAAC,CAAC,CAAC;AACnC,CAAC,CAAC","file":"useOnResize.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nimport { RefObject, useEffect, useRef } from \"react\";\n\ninterface UseOnResizeOptions {\n outerElement?: RefObject<HTMLElement | null>;\n throttle?: number;\n}\n\n/**\n * Custom hook that triggers a callback function when the specified element\n * or the window is resized.\n *\n * @param callback - The function to be executed on resize.\n * @param options - Optional parameters to customize the behavior:\n * - outerElement: A ref to an HTMLElement to listen for resize events.\n * - throttle: Optional duration in milliseconds to throttle the callback execution.\n * Default is 100 milliseconds.\n */\nexport const useOnResize = (\n callback: () => void,\n options: UseOnResizeOptions = {}\n) => {\n const { outerElement, throttle: throttleDuration = 100 } = options;\n const lastExecutedRef = useRef<number>(0);\n\n // Throttled resize handler to limit the frequency of callback execution.\n const handleResize = () => {\n const now = Date.now();\n\n // Check if the callback can be executed based on the throttle duration.\n if (now - lastExecutedRef.current < throttleDuration) {\n return;\n }\n\n lastExecutedRef.current = now;\n callback();\n };\n\n useEffect(() => {\n // Determine the target for the resize event listener.\n const target = outerElement?.current || window;\n\n // Add the resize event listener when the component mounts.\n target.addEventListener(\"resize\", handleResize);\n\n // Clean up the event listener when the component unmounts.\n return () => {\n target.removeEventListener(\"resize\", handleResize);\n };\n }, [throttleDuration, callback]);\n};\n"]}
1
+ {"version":3,"sources":["../src/useOnResize.tsx"],"names":[],"mappings":"AAIA,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,OAAO,CAAA;AAiBzC,MAAM,CAAC,MAAM,WAAW,GAAG,CACzB,QAAoB,EACpB,UAA8B,EAAE,EAChC,EAAE;IACF,MAAM,EAAE,YAAY,EAAE,QAAQ,EAAE,gBAAgB,GAAG,GAAG,EAAE,GAAG,OAAO,CAAA;IAClE,MAAM,eAAe,GAAG,MAAM,CAAS,CAAC,CAAC,CAAA;IAGzC,MAAM,YAAY,GAAG,GAAG,EAAE;QACxB,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;QAGtB,IAAI,GAAG,GAAG,eAAe,CAAC,OAAO,GAAG,gBAAgB,EAAE,CAAC;YACrD,OAAM;QACR,CAAC;QAED,eAAe,CAAC,OAAO,GAAG,GAAG,CAAA;QAC7B,QAAQ,EAAE,CAAA;IACZ,CAAC,CAAA;IAED,SAAS,CAAC,GAAG,EAAE;QAEb,MAAM,MAAM,GAAG,YAAY,EAAE,OAAO,IAAI,MAAM,CAAA;QAG9C,MAAM,CAAC,gBAAgB,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAA;QAG/C,OAAO,GAAG,EAAE;YACV,MAAM,CAAC,mBAAmB,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAA;QACpD,CAAC,CAAA;IACH,CAAC,EAAE,CAAC,gBAAgB,EAAE,QAAQ,CAAC,CAAC,CAAA;AAClC,CAAC,CAAA","file":"useOnResize.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nimport type { RefObject } from 'react'\nimport { useEffect, useRef } from 'react'\n\ninterface UseOnResizeOptions {\n outerElement?: RefObject<HTMLElement | null>\n throttle?: number\n}\n\n/**\n * Custom hook that triggers a callback function when the specified element\n * or the window is resized.\n *\n * @param callback - The function to be executed on resize.\n * @param options - Optional parameters to customize the behavior:\n * - outerElement: A ref to an HTMLElement to listen for resize events.\n * - throttle: Optional duration in milliseconds to throttle the callback execution.\n * Default is 100 milliseconds.\n */\nexport const useOnResize = (\n callback: () => void,\n options: UseOnResizeOptions = {}\n) => {\n const { outerElement, throttle: throttleDuration = 100 } = options\n const lastExecutedRef = useRef<number>(0)\n\n // Throttled resize handler to limit the frequency of callback execution.\n const handleResize = () => {\n const now = Date.now()\n\n // Check if the callback can be executed based on the throttle duration.\n if (now - lastExecutedRef.current < throttleDuration) {\n return\n }\n\n lastExecutedRef.current = now\n callback()\n }\n\n useEffect(() => {\n // Determine the target for the resize event listener.\n const target = outerElement?.current || window\n\n // Add the resize event listener when the component mounts.\n target.addEventListener('resize', handleResize)\n\n // Clean up the event listener when the component unmounts.\n return () => {\n target.removeEventListener('resize', handleResize)\n }\n }, [throttleDuration, callback])\n}\n"]}
@@ -1,2 +1,2 @@
1
- import { type RefObject } from "react";
1
+ import { type RefObject } from 'react';
2
2
  export declare const useOutsideAlerter: (ref: RefObject<HTMLElement | null>, callback: () => void, ignore?: string[]) => void;
@@ -1,4 +1,4 @@
1
- import { useEffect } from "react";
1
+ import { useEffect } from 'react';
2
2
  export const useOutsideAlerter = (ref, callback, ignore = []) => {
3
3
  useEffect(() => {
4
4
  const handleClickOutside = (ev) => {
@@ -13,9 +13,9 @@ export const useOutsideAlerter = (ref, callback, ignore = []) => {
13
13
  }
14
14
  }
15
15
  };
16
- document.addEventListener("mousedown", handleClickOutside);
16
+ document.addEventListener('mousedown', handleClickOutside);
17
17
  return () => {
18
- document.removeEventListener("mousedown", handleClickOutside);
18
+ document.removeEventListener('mousedown', handleClickOutside);
19
19
  };
20
20
  }, [ref]);
21
21
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/useOutsideAlerter.tsx"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAkB,MAAM,OAAO,CAAC;AAGlD,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAC/B,GAAkC,EAClC,QAAoB,EACpB,SAAmB,EAAE,EACrB,EAAE;IACF,SAAS,CAAC,GAAG,EAAE;QACb,MAAM,kBAAkB,GAAG,CAAC,EAAc,EAAE,EAAE;YAC5C,IAAI,EAAE,EAAE,CAAC;gBACP,IAAI,GAAG,CAAC,OAAO,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC,MAAc,CAAC,EAAE,CAAC;oBAC5D,MAAM,MAAM,GAAG,EAAE,CAAC,MAAqB,CAAC;oBAGxC,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,WAAW,EAAE,CAAC;oBAC7C,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,CACzB,CAAC,CAAC,EAAE,EAAE,CACJ,CAAC,CAAC,WAAW,EAAE,KAAK,OAAO,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC,KAAK,IAAI,CAClE,CAAC;oBAEF,IAAI,CAAC,OAAO,EAAE,CAAC;wBACb,QAAQ,EAAE,CAAC;oBACb,CAAC;gBACH,CAAC;YACH,CAAC;QACH,CAAC,CAAC;QACF,QAAQ,CAAC,gBAAgB,CAAC,WAAW,EAAE,kBAAkB,CAAC,CAAC;QAC3D,OAAO,GAAG,EAAE;YACV,QAAQ,CAAC,mBAAmB,CAAC,WAAW,EAAE,kBAAkB,CAAC,CAAC;QAChE,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;AACZ,CAAC,CAAC","file":"useOutsideAlerter.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nimport { useEffect, type RefObject } from \"react\";\n\n// A hook that alerts clicks outside of the passed ref.\nexport const useOutsideAlerter = (\n ref: RefObject<HTMLElement | null>,\n callback: () => void,\n ignore: string[] = []\n) => {\n useEffect(() => {\n const handleClickOutside = (ev: MouseEvent) => {\n if (ev) {\n if (ref.current && !ref.current.contains(ev.target as Node)) {\n const target = ev.target as HTMLElement;\n // Ignore tags with a name of `ignore`, or if there is a class of `ignore` in the parent\n // tree.\n const tagName = target.tagName.toLowerCase();\n const ignored = ignore.some(\n (i) =>\n i.toLowerCase() === tagName || target.closest(`.${i}`) !== null\n );\n\n if (!ignored) {\n callback();\n }\n }\n }\n };\n document.addEventListener(\"mousedown\", handleClickOutside);\n return () => {\n document.removeEventListener(\"mousedown\", handleClickOutside);\n };\n }, [ref]);\n};\n"]}
1
+ {"version":3,"sources":["../src/useOutsideAlerter.tsx"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAkB,MAAM,OAAO,CAAA;AAGjD,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAC/B,GAAkC,EAClC,QAAoB,EACpB,SAAmB,EAAE,EACrB,EAAE;IACF,SAAS,CAAC,GAAG,EAAE;QACb,MAAM,kBAAkB,GAAG,CAAC,EAAc,EAAE,EAAE;YAC5C,IAAI,EAAE,EAAE,CAAC;gBACP,IAAI,GAAG,CAAC,OAAO,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC,MAAc,CAAC,EAAE,CAAC;oBAC5D,MAAM,MAAM,GAAG,EAAE,CAAC,MAAqB,CAAA;oBAGvC,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,WAAW,EAAE,CAAA;oBAC5C,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,CACzB,CAAC,CAAC,EAAE,EAAE,CACJ,CAAC,CAAC,WAAW,EAAE,KAAK,OAAO,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC,KAAK,IAAI,CAClE,CAAA;oBAED,IAAI,CAAC,OAAO,EAAE,CAAC;wBACb,QAAQ,EAAE,CAAA;oBACZ,CAAC;gBACH,CAAC;YACH,CAAC;QACH,CAAC,CAAA;QACD,QAAQ,CAAC,gBAAgB,CAAC,WAAW,EAAE,kBAAkB,CAAC,CAAA;QAC1D,OAAO,GAAG,EAAE;YACV,QAAQ,CAAC,mBAAmB,CAAC,WAAW,EAAE,kBAAkB,CAAC,CAAA;QAC/D,CAAC,CAAA;IACH,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAA;AACX,CAAC,CAAA","file":"useOutsideAlerter.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nimport { useEffect, type RefObject } from 'react'\n\n// A hook that alerts clicks outside of the passed ref.\nexport const useOutsideAlerter = (\n ref: RefObject<HTMLElement | null>,\n callback: () => void,\n ignore: string[] = []\n) => {\n useEffect(() => {\n const handleClickOutside = (ev: MouseEvent) => {\n if (ev) {\n if (ref.current && !ref.current.contains(ev.target as Node)) {\n const target = ev.target as HTMLElement\n // Ignore tags with a name of `ignore`, or if there is a class of `ignore` in the parent\n // tree.\n const tagName = target.tagName.toLowerCase()\n const ignored = ignore.some(\n (i) =>\n i.toLowerCase() === tagName || target.closest(`.${i}`) !== null\n )\n\n if (!ignored) {\n callback()\n }\n }\n }\n }\n document.addEventListener('mousedown', handleClickOutside)\n return () => {\n document.removeEventListener('mousedown', handleClickOutside)\n }\n }, [ref])\n}\n"]}
@@ -1,3 +1,3 @@
1
- import type { Context } from "react";
1
+ import type { Context } from 'react';
2
2
  export declare const useSafeContext: <T>(ctx: T | null | undefined) => T;
3
3
  export declare const createSafeContext: <T>() => [Context<T | null>, () => T];
@@ -1,7 +1,7 @@
1
- import { createContext, useContext } from "react";
1
+ import { createContext, useContext } from 'react';
2
2
  export const useSafeContext = (ctx) => {
3
3
  if (ctx === null || ctx === undefined) {
4
- throw new Error("Context value is null or undefined. Please ensure the context Provider is used correctly.");
4
+ throw new Error('Context value is null or undefined. Please ensure the context Provider is used correctly.');
5
5
  }
6
6
  return ctx;
7
7
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/useSafeContext.tsx"],"names":[],"mappings":"AAIA,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AAElD,MAAM,CAAC,MAAM,cAAc,GAAG,CAAK,GAAyB,EAAK,EAAE;IACjE,IAAI,GAAG,KAAK,IAAI,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;QACtC,MAAM,IAAI,KAAK,CACb,2FAA2F,CAC5F,CAAC;IACJ,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,iBAAiB,GAAG,GAAqC,EAAE;IACtE,MAAM,OAAO,GAAG,aAAa,CAAW,IAAI,CAAC,CAAC;IAC9C,MAAM,OAAO,GAAG,GAAG,EAAE,CAAC,cAAc,CAAI,UAAU,CAAC,OAAO,CAAC,CAAC,CAAC;IAC7D,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;AAC5B,CAAC,CAAC","file":"useSafeContext.js","sourcesContent":["// Copyright 2025 @polkadot-cloud/polkadot-staking-dashboard authors & contributors\n// SPDX-License-Identifier: GPL-3.0-only\n\nimport type { Context } from \"react\";\nimport { createContext, useContext } from \"react\";\n\nexport const useSafeContext = <T,>(ctx: T | null | undefined): T => {\n if (ctx === null || ctx === undefined) {\n throw new Error(\n \"Context value is null or undefined. Please ensure the context Provider is used correctly.\"\n );\n }\n return ctx;\n};\n\nexport const createSafeContext = <T,>(): [Context<T | null>, () => T] => {\n const Context = createContext<T | null>(null);\n const useHook = () => useSafeContext<T>(useContext(Context));\n return [Context, useHook];\n};\n"]}
1
+ {"version":3,"sources":["../src/useSafeContext.tsx"],"names":[],"mappings":"AAIA,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,OAAO,CAAA;AAEjD,MAAM,CAAC,MAAM,cAAc,GAAG,CAAK,GAAyB,EAAK,EAAE;IACjE,IAAI,GAAG,KAAK,IAAI,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;QACtC,MAAM,IAAI,KAAK,CACb,2FAA2F,CAC5F,CAAA;IACH,CAAC;IACD,OAAO,GAAG,CAAA;AACZ,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,iBAAiB,GAAG,GAAqC,EAAE;IACtE,MAAM,OAAO,GAAG,aAAa,CAAW,IAAI,CAAC,CAAA;IAC7C,MAAM,OAAO,GAAG,GAAG,EAAE,CAAC,cAAc,CAAI,UAAU,CAAC,OAAO,CAAC,CAAC,CAAA;IAC5D,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,CAAA;AAC3B,CAAC,CAAA","file":"useSafeContext.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nimport type { Context } from 'react'\nimport { createContext, useContext } from 'react'\n\nexport const useSafeContext = <T,>(ctx: T | null | undefined): T => {\n if (ctx === null || ctx === undefined) {\n throw new Error(\n 'Context value is null or undefined. Please ensure the context Provider is used correctly.'\n )\n }\n return ctx\n}\n\nexport const createSafeContext = <T,>(): [Context<T | null>, () => T] => {\n const Context = createContext<T | null>(null)\n const useHook = () => useSafeContext<T>(useContext(Context))\n return [Context, useHook]\n}\n"]}
package/mjs/useSize.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { RefObject } from "react";
1
+ import type { RefObject } from 'react';
2
2
  interface UseSizeOptions {
3
3
  outerElement?: RefObject<HTMLElement | null>;
4
4
  throttle?: number;
package/mjs/useSize.js CHANGED
@@ -1,4 +1,4 @@
1
- import { useEffect, useRef, useState } from "react";
1
+ import { useEffect, useRef, useState } from 'react';
2
2
  export const useSize = (element, options = {}) => {
3
3
  const { outerElement, throttle: throttleDuration = 100 } = options;
4
4
  const getSize = (el = null) => {
@@ -18,9 +18,9 @@ export const useSize = (element, options = {}) => {
18
18
  };
19
19
  useEffect(() => {
20
20
  const listenFor = outerElement?.current || window;
21
- listenFor.addEventListener("resize", handleResize);
21
+ listenFor.addEventListener('resize', handleResize);
22
22
  return () => {
23
- listenFor.removeEventListener("resize", handleResize);
23
+ listenFor.removeEventListener('resize', handleResize);
24
24
  };
25
25
  }, [outerElement?.current]);
26
26
  return size;
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/useSize.tsx"],"names":[],"mappings":"AAIA,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAUpD,MAAM,CAAC,MAAM,OAAO,GAAG,CACrB,OAAsC,EACtC,UAA0B,EAAE,EAC5B,EAAE;IACF,MAAM,EAAE,YAAY,EAAE,QAAQ,EAAE,gBAAgB,GAAG,GAAG,EAAE,GAAG,OAAO,CAAC;IAInE,MAAM,OAAO,GAAG,CAAC,KAAyB,IAAI,EAAE,EAAE;QAChD,MAAM,KAAK,GAAG,EAAE,EAAE,WAAW,IAAI,CAAC,CAAC;QACnC,MAAM,MAAM,GAAG,EAAE,EAAE,YAAY,IAAI,CAAC,CAAC;QACrC,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC;IAC3B,CAAC,CAAC;IAGF,MAAM,eAAe,GAAG,MAAM,CAAS,CAAC,CAAC,CAAC;IAG1C,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,QAAQ,CAC9B,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,CAC1B,CAAC;IAGF,MAAM,YAAY,GAAG,GAAG,EAAE;QACxB,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QACvB,IAAI,GAAG,GAAG,eAAe,CAAC,OAAO,GAAG,gBAAgB,EAAE,CAAC;YACrD,OAAO;QACT,CAAC;QAED,eAAe,CAAC,OAAO,GAAG,GAAG,CAAC;QAE9B,OAAO,CAAC,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC;IACrC,CAAC,CAAC;IAGF,SAAS,CAAC,GAAG,EAAE;QAGb,MAAM,SAAS,GAAG,YAAY,EAAE,OAAO,IAAI,MAAM,CAAC;QAElD,SAAS,CAAC,gBAAgB,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC;QAGnD,OAAO,GAAG,EAAE;YACV,SAAS,CAAC,mBAAmB,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC;QACxD,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC,CAAC;IAG5B,OAAO,IAAI,CAAC;AACd,CAAC,CAAC","file":"useSize.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nimport type { RefObject } from \"react\";\nimport { useEffect, useRef, useState } from \"react\";\n\n// Define the type for the options parameter.\ninterface UseSizeOptions {\n outerElement?: RefObject<HTMLElement | null>;\n throttle?: number;\n}\n\n// Custom hook to get the width and height of a specified element. Updates the `size` state when the\n// specified \"outer element\" (or the window by default) resizes.\nexport const useSize = (\n element: RefObject<HTMLElement | null>,\n options: UseSizeOptions = {}\n) => {\n const { outerElement, throttle: throttleDuration = 100 } = options;\n\n // Helper function to retrieve the width and height of an element\n // If no element is found, default dimensions are set to 0.\n const getSize = (el: HTMLElement | null = null) => {\n const width = el?.offsetWidth || 0;\n const height = el?.offsetHeight || 0;\n return { width, height };\n };\n\n // Ref to store the last execution time of the `resizeThrottle` handler.\n const lastExecutedRef = useRef<number>(0);\n\n // State to store the current width and height of the specified element.\n const [size, setSize] = useState<{ width: number; height: number }>(\n getSize(element?.current)\n );\n\n // Throttle the resize event handler to limit how often size updates occur.\n const handleResize = () => {\n const now = Date.now();\n if (now - lastExecutedRef.current < throttleDuration) {\n return;\n } // Exit if `throttleDuration` has not passed.\n\n lastExecutedRef.current = now; // Update last execution time.\n\n setSize(getSize(element?.current));\n };\n\n // Set up the resize event listener on mount and clean it up on unmount.\n useEffect(() => {\n // Determine the target for the resize event listener.\n // If `outerElement` is provided, listen to its resize events; otherwise, listen to the window's.\n const listenFor = outerElement?.current || window;\n\n listenFor.addEventListener(\"resize\", handleResize);\n\n // Clean up event listener when the component unmounts to avoid memory leaks.\n return () => {\n listenFor.removeEventListener(\"resize\", handleResize);\n };\n }, [outerElement?.current]);\n\n // Return the current size of the element.\n return size;\n};\n"]}
1
+ {"version":3,"sources":["../src/useSize.tsx"],"names":[],"mappings":"AAIA,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;AAUnD,MAAM,CAAC,MAAM,OAAO,GAAG,CACrB,OAAsC,EACtC,UAA0B,EAAE,EAC5B,EAAE;IACF,MAAM,EAAE,YAAY,EAAE,QAAQ,EAAE,gBAAgB,GAAG,GAAG,EAAE,GAAG,OAAO,CAAA;IAIlE,MAAM,OAAO,GAAG,CAAC,KAAyB,IAAI,EAAE,EAAE;QAChD,MAAM,KAAK,GAAG,EAAE,EAAE,WAAW,IAAI,CAAC,CAAA;QAClC,MAAM,MAAM,GAAG,EAAE,EAAE,YAAY,IAAI,CAAC,CAAA;QACpC,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,CAAA;IAC1B,CAAC,CAAA;IAGD,MAAM,eAAe,GAAG,MAAM,CAAS,CAAC,CAAC,CAAA;IAGzC,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,QAAQ,CAC9B,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,CAC1B,CAAA;IAGD,MAAM,YAAY,GAAG,GAAG,EAAE;QACxB,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;QACtB,IAAI,GAAG,GAAG,eAAe,CAAC,OAAO,GAAG,gBAAgB,EAAE,CAAC;YACrD,OAAM;QACR,CAAC;QAED,eAAe,CAAC,OAAO,GAAG,GAAG,CAAA;QAE7B,OAAO,CAAC,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC,CAAA;IACpC,CAAC,CAAA;IAGD,SAAS,CAAC,GAAG,EAAE;QAGb,MAAM,SAAS,GAAG,YAAY,EAAE,OAAO,IAAI,MAAM,CAAA;QAEjD,SAAS,CAAC,gBAAgB,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAA;QAGlD,OAAO,GAAG,EAAE;YACV,SAAS,CAAC,mBAAmB,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAA;QACvD,CAAC,CAAA;IACH,CAAC,EAAE,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC,CAAA;IAG3B,OAAO,IAAI,CAAA;AACb,CAAC,CAAA","file":"useSize.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nimport type { RefObject } from 'react'\nimport { useEffect, useRef, useState } from 'react'\n\n// Define the type for the options parameter.\ninterface UseSizeOptions {\n outerElement?: RefObject<HTMLElement | null>\n throttle?: number\n}\n\n// Custom hook to get the width and height of a specified element. Updates the `size` state when the\n// specified \"outer element\" (or the window by default) resizes.\nexport const useSize = (\n element: RefObject<HTMLElement | null>,\n options: UseSizeOptions = {}\n) => {\n const { outerElement, throttle: throttleDuration = 100 } = options\n\n // Helper function to retrieve the width and height of an element\n // If no element is found, default dimensions are set to 0.\n const getSize = (el: HTMLElement | null = null) => {\n const width = el?.offsetWidth || 0\n const height = el?.offsetHeight || 0\n return { width, height }\n }\n\n // Ref to store the last execution time of the `resizeThrottle` handler.\n const lastExecutedRef = useRef<number>(0)\n\n // State to store the current width and height of the specified element.\n const [size, setSize] = useState<{ width: number; height: number }>(\n getSize(element?.current)\n )\n\n // Throttle the resize event handler to limit how often size updates occur.\n const handleResize = () => {\n const now = Date.now()\n if (now - lastExecutedRef.current < throttleDuration) {\n return\n } // Exit if `throttleDuration` has not passed.\n\n lastExecutedRef.current = now // Update last execution time.\n\n setSize(getSize(element?.current))\n }\n\n // Set up the resize event listener on mount and clean it up on unmount.\n useEffect(() => {\n // Determine the target for the resize event listener.\n // If `outerElement` is provided, listen to its resize events; otherwise, listen to the window's.\n const listenFor = outerElement?.current || window\n\n listenFor.addEventListener('resize', handleResize)\n\n // Clean up event listener when the component unmounts to avoid memory leaks.\n return () => {\n listenFor.removeEventListener('resize', handleResize)\n }\n }, [outerElement?.current])\n\n // Return the current size of the element.\n return size\n}\n"]}
@@ -1,3 +1,3 @@
1
- import type { TimeleftDuration } from "@w3ux/types";
1
+ import type { TimeleftDuration } from '@w3ux/types';
2
2
  export declare const defaultDuration: TimeleftDuration;
3
3
  export declare const defaultRefreshInterval = 60;
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/useTimeLeft/defaults.ts"],"names":[],"mappings":"AAKA,MAAM,CAAC,MAAM,eAAe,GAAqB;IAC/C,IAAI,EAAE,CAAC;IACP,KAAK,EAAE,CAAC;IACR,OAAO,EAAE,CAAC;IACV,OAAO,EAAE,CAAC;IACV,UAAU,EAAE,KAAK;CAClB,CAAC;AAEF,MAAM,CAAC,MAAM,sBAAsB,GAAG,EAAE,CAAC","file":"defaults.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nimport type { TimeleftDuration } from \"@w3ux/types\";\n\nexport const defaultDuration: TimeleftDuration = {\n days: 0,\n hours: 0,\n minutes: 0,\n seconds: 0,\n lastMinute: false,\n};\n\nexport const defaultRefreshInterval = 60;\n"]}
1
+ {"version":3,"sources":["../src/useTimeLeft/defaults.ts"],"names":[],"mappings":"AAKA,MAAM,CAAC,MAAM,eAAe,GAAqB;IAC/C,IAAI,EAAE,CAAC;IACP,KAAK,EAAE,CAAC;IACR,OAAO,EAAE,CAAC;IACV,OAAO,EAAE,CAAC;IACV,UAAU,EAAE,KAAK;CAClB,CAAA;AAED,MAAM,CAAC,MAAM,sBAAsB,GAAG,EAAE,CAAA","file":"defaults.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nimport type { TimeleftDuration } from '@w3ux/types'\n\nexport const defaultDuration: TimeleftDuration = {\n days: 0,\n hours: 0,\n minutes: 0,\n seconds: 0,\n lastMinute: false,\n}\n\nexport const defaultRefreshInterval = 60\n"]}
@@ -1,4 +1,4 @@
1
- import type { TimeLeftAll, UseTimeleftProps } from "@w3ux/types";
1
+ import type { TimeLeftAll, UseTimeleftProps } from '@w3ux/types';
2
2
  export declare const useTimeLeft: (props?: UseTimeleftProps) => {
3
3
  setFromNow: (dateFrom: Date, dateTo: Date) => void;
4
4
  timeleft: TimeLeftAll;
@@ -1,6 +1,6 @@
1
- import { setStateWithRef } from "@w3ux/utils";
2
- import { useEffect, useRef, useState } from "react";
3
- import { getDuration } from "../util";
1
+ import { setStateWithRef } from '@w3ux/utils';
2
+ import { useEffect, useRef, useState } from 'react';
3
+ import { getDuration } from '../util';
4
4
  export const useTimeLeft = (props) => {
5
5
  const depsTimeleft = props?.depsTimeleft || [];
6
6
  const depsFormat = props?.depsFormat || [];
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/useTimeLeft/index.tsx"],"names":[],"mappings":"AAGA,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,EAAE,SAAS,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B,CAAC,EAAE,KAAK,CAAC,CAAC;YACV,eAAe,CAAC,QAAQ,EAAE,cAAc,EAAE,cAAc,CAAC,CAAC;QAC5D,CAAC;IACH,CAAC,EAAE,CAAC,EAAE,EAAE,UAAU,EAAE,EAAE,mBAAmB,EAAE,EAAE,GAAG,YAAY,CAAC,CAAC,CAAC;IAG/D,SAAS,CAAC,GAAG,EAAE;QACb,WAAW,CAAC,WAAW,EAAE,CAAC,CAAC;IAC7B,CAAC,EAAE,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC;IAGpB,SAAS,CACP,GAAG,EAAE,CAAC,GAAG,EAAE;QACT,aAAa,CAAC,WAAW,CAAC,CAAC;QAC3B,aAAa,CAAC,WAAW,CAAC,CAAC;IAC7B,CAAC,EACD,EAAE,CACH,CAAC;IAGF,MAAM,UAAU,GAAG,CAAC,QAAc,EAAE,MAAY,EAAE,EAAE;QAClD,WAAW,CAAC,WAAW,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;QAChD,eAAe,CAAC,MAAM,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC;IACxC,CAAC,CAAC;IAEF,OAAO;QACL,UAAU;QACV,QAAQ;KACT,CAAC;AACJ,CAAC,CAAC","file":"index.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nimport { setStateWithRef } from \"@w3ux/utils\";\nimport { useEffect, useRef, useState } from \"react\";\nimport type {\n TimeLeftAll,\n TimeLeftRaw,\n TimeleftDuration,\n UseTimeleftProps,\n} from \"@w3ux/types\";\nimport { getDuration } from \"../util\";\n\nexport const useTimeLeft = (props?: UseTimeleftProps) => {\n const depsTimeleft = props?.depsTimeleft || [];\n const depsFormat = props?.depsFormat || [];\n\n // check whether timeleft is within a minute of finishing.\n const inLastHour = () => {\n const { days, hours } = getDuration(toRef.current);\n return !days && !hours;\n };\n\n // get the amount of seconds left if timeleft is in the last minute.\n const lastMinuteCountdown = () => {\n const { seconds } = getDuration(toRef.current);\n if (!inLastHour()) {\n return 60;\n }\n return seconds;\n };\n\n // calculate resulting timeleft object from latest duration.\n const getTimeleft = (c?: TimeleftDuration): TimeLeftAll => {\n const { days, hours, minutes, seconds } = c || getDuration(toRef.current);\n const raw: TimeLeftRaw = {\n days,\n hours,\n minutes,\n };\n if (!days && !hours) {\n raw.seconds = seconds;\n }\n return {\n raw,\n };\n };\n\n // the end time as a date.\n const [to, setTo] = useState<Date | null>(null);\n const toRef = useRef(to);\n\n // resulting timeleft object to be returned.\n const [timeleft, setTimeleft] = useState<TimeLeftAll>(getTimeleft());\n\n // timeleft refresh intervals.\n const [minInterval, setMinInterval] = useState<\n ReturnType<typeof setInterval> | undefined\n >(undefined);\n const minIntervalRef = useRef(minInterval);\n\n const [secInterval, setSecInterval] = useState<\n ReturnType<typeof setInterval> | undefined\n >(undefined);\n const secIntervalRef = useRef(secInterval);\n\n // refresh effects.\n useEffect(() => {\n setTimeleft(getTimeleft());\n if (inLastHour()) {\n // refresh timeleft every second.\n if (!secIntervalRef.current) {\n const interval = setInterval(() => {\n if (!inLastHour()) {\n clearInterval(secIntervalRef.current);\n setStateWithRef(undefined, setSecInterval, secIntervalRef);\n }\n setTimeleft(getTimeleft());\n }, 1000);\n\n setStateWithRef(interval, setSecInterval, secIntervalRef);\n }\n }\n // refresh timeleft every minute.\n else if (!minIntervalRef.current) {\n const interval = setInterval(() => {\n if (inLastHour()) {\n clearInterval(minIntervalRef.current);\n setStateWithRef(undefined, setMinInterval, minIntervalRef);\n }\n setTimeleft(getTimeleft());\n }, 60000);\n setStateWithRef(interval, setMinInterval, minIntervalRef);\n }\n }, [to, inLastHour(), lastMinuteCountdown(), ...depsTimeleft]);\n\n // re-render the timeleft upon formatting changes.\n useEffect(() => {\n setTimeleft(getTimeleft());\n }, [...depsFormat]);\n\n // clear intervals on unmount\n useEffect(\n () => () => {\n clearInterval(minInterval);\n clearInterval(secInterval);\n },\n []\n );\n\n // Set the end time and calculate timeleft.\n const setFromNow = (dateFrom: Date, dateTo: Date) => {\n setTimeleft(getTimeleft(getDuration(dateFrom)));\n setStateWithRef(dateTo, setTo, toRef);\n };\n\n return {\n setFromNow,\n timeleft,\n };\n};\n"]}
1
+ {"version":3,"sources":["../src/useTimeLeft/index.tsx"],"names":[],"mappings":"AASA,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAC7C,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;AACnD,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AAErC,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,KAAwB,EAAE,EAAE;IACtD,MAAM,YAAY,GAAG,KAAK,EAAE,YAAY,IAAI,EAAE,CAAA;IAC9C,MAAM,UAAU,GAAG,KAAK,EAAE,UAAU,IAAI,EAAE,CAAA;IAG1C,MAAM,UAAU,GAAG,GAAG,EAAE;QACtB,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,WAAW,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;QAClD,OAAO,CAAC,IAAI,IAAI,CAAC,KAAK,CAAA;IACxB,CAAC,CAAA;IAGD,MAAM,mBAAmB,GAAG,GAAG,EAAE;QAC/B,MAAM,EAAE,OAAO,EAAE,GAAG,WAAW,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;QAC9C,IAAI,CAAC,UAAU,EAAE,EAAE,CAAC;YAClB,OAAO,EAAE,CAAA;QACX,CAAC;QACD,OAAO,OAAO,CAAA;IAChB,CAAC,CAAA;IAGD,MAAM,WAAW,GAAG,CAAC,CAAoB,EAAe,EAAE;QACxD,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,GAAG,CAAC,IAAI,WAAW,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;QACzE,MAAM,GAAG,GAAgB;YACvB,IAAI;YACJ,KAAK;YACL,OAAO;SACR,CAAA;QACD,IAAI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AC;YACjC,MAAM,QAAQ,GAAG,WAAW,CAAC,GAAG,EAAE;gBAChC,IAAI,UAAU,EAAE,EAAE,CAAC;oBACjB,aAAa,CAAC,cAAc,CAAC,OAAO,CAAC,CAAA;oBACrC,eAAe,CAAC,SAAS,EAAE,cAAc,EAAE,cAAc,CAAC,CAAA;gBAC5D,CAAC;gBACD,WAAW,CAAC,WAAW,EAAE,CAAC,CAAA;YAC5B,CAAC,EAAE,KAAK,CAAC,CAAA;YACT,eAAe,CAAC,QAAQ,EAAE,cAAc,EAAE,cAAc,CAAC,CAAA;QAC3D,CAAC;IACH,CAAC,EAAE,CAAC,EAAE,EAAE,UAAU,EAAE,EAAE,mBAAmB,EAAE,EAAE,GAAG,YAAY,CAAC,CAAC,CAAA;IAG9D,SAAS,CAAC,GAAG,EAAE;QACb,WAAW,CAAC,WAAW,EAAE,CAAC,CAAA;IAC5B,CAAC,EAAE,CAAC,GAAG,UAAU,CAAC,CAAC,CAAA;IAGnB,SAAS,CACP,GAAG,EAAE,CAAC,GAAG,EAAE;QACT,aAAa,CAAC,WAAW,CAAC,CAAA;QAC1B,aAAa,CAAC,WAAW,CAAC,CAAA;IAC5B,CAAC,EACD,EAAE,CACH,CAAA;IAGD,MAAM,UAAU,GAAG,CAAC,QAAc,EAAE,MAAY,EAAE,EAAE;QAClD,WAAW,CAAC,WAAW,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAA;QAC/C,eAAe,CAAC,MAAM,EAAE,KAAK,EAAE,KAAK,CAAC,CAAA;IACvC,CAAC,CAAA;IAED,OAAO;QACL,UAAU;QACV,QAAQ;KACT,CAAA;AACH,CAAC,CAAA","file":"index.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nimport type {\n TimeLeftAll,\n TimeLeftRaw,\n TimeleftDuration,\n UseTimeleftProps,\n} from '@w3ux/types'\nimport { setStateWithRef } from '@w3ux/utils'\nimport { useEffect, useRef, useState } from 'react'\nimport { getDuration } from '../util'\n\nexport const useTimeLeft = (props?: UseTimeleftProps) => {\n const depsTimeleft = props?.depsTimeleft || []\n const depsFormat = props?.depsFormat || []\n\n // check whether timeleft is within a minute of finishing.\n const inLastHour = () => {\n const { days, hours } = getDuration(toRef.current)\n return !days && !hours\n }\n\n // get the amount of seconds left if timeleft is in the last minute.\n const lastMinuteCountdown = () => {\n const { seconds } = getDuration(toRef.current)\n if (!inLastHour()) {\n return 60\n }\n return seconds\n }\n\n // calculate resulting timeleft object from latest duration.\n const getTimeleft = (c?: TimeleftDuration): TimeLeftAll => {\n const { days, hours, minutes, seconds } = c || getDuration(toRef.current)\n const raw: TimeLeftRaw = {\n days,\n hours,\n minutes,\n }\n if (!days && !hours) {\n raw.seconds = seconds\n }\n return {\n raw,\n }\n }\n\n // the end time as a date.\n const [to, setTo] = useState<Date | null>(null)\n const toRef = useRef(to)\n\n // resulting timeleft object to be returned.\n const [timeleft, setTimeleft] = useState<TimeLeftAll>(getTimeleft())\n\n // timeleft refresh intervals.\n const [minInterval, setMinInterval] = useState<\n ReturnType<typeof setInterval> | undefined\n >(undefined)\n const minIntervalRef = useRef(minInterval)\n\n const [secInterval, setSecInterval] = useState<\n ReturnType<typeof setInterval> | undefined\n >(undefined)\n const secIntervalRef = useRef(secInterval)\n\n // refresh effects.\n useEffect(() => {\n setTimeleft(getTimeleft())\n if (inLastHour()) {\n // refresh timeleft every second.\n if (!secIntervalRef.current) {\n const interval = setInterval(() => {\n if (!inLastHour()) {\n clearInterval(secIntervalRef.current)\n setStateWithRef(undefined, setSecInterval, secIntervalRef)\n }\n setTimeleft(getTimeleft())\n }, 1000)\n\n setStateWithRef(interval, setSecInterval, secIntervalRef)\n }\n }\n // refresh timeleft every minute.\n else if (!minIntervalRef.current) {\n const interval = setInterval(() => {\n if (inLastHour()) {\n clearInterval(minIntervalRef.current)\n setStateWithRef(undefined, setMinInterval, minIntervalRef)\n }\n setTimeleft(getTimeleft())\n }, 60000)\n setStateWithRef(interval, setMinInterval, minIntervalRef)\n }\n }, [to, inLastHour(), lastMinuteCountdown(), ...depsTimeleft])\n\n // re-render the timeleft upon formatting changes.\n useEffect(() => {\n setTimeleft(getTimeleft())\n }, [...depsFormat])\n\n // clear intervals on unmount\n useEffect(\n () => () => {\n clearInterval(minInterval)\n clearInterval(secInterval)\n },\n []\n )\n\n // Set the end time and calculate timeleft.\n const setFromNow = (dateFrom: Date, dateTo: Date) => {\n setTimeleft(getTimeleft(getDuration(dateFrom)))\n setStateWithRef(dateTo, setTo, toRef)\n }\n\n return {\n setFromNow,\n timeleft,\n }\n}\n"]}
package/mjs/util.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { TimeleftDuration } from "@w3ux/types";
1
+ import type { TimeleftDuration } from '@w3ux/types';
2
2
  export declare const getDuration: (toDate: Date | null) => TimeleftDuration;
3
3
  export declare const secondsFromNow: (seconds: number) => Date;
4
4
  export declare const getDurationFromNow: (toDate: Date | null) => TimeleftDuration;
package/mjs/util.js CHANGED
@@ -1,5 +1,5 @@
1
- import { differenceInDays, getUnixTime, intervalToDuration } from "date-fns";
2
- import { defaultDuration } from "./useTimeLeft/defaults";
1
+ import { differenceInDays, getUnixTime, intervalToDuration } from 'date-fns';
2
+ import { defaultDuration } from './useTimeLeft/defaults';
3
3
  export const getDuration = (toDate) => {
4
4
  if (!toDate) {
5
5
  return defaultDuration;
package/mjs/util.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/util.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAC7E,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAGzD,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,MAAmB,EAAoB,EAAE;IACnE,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,OAAO,eAAe,CAAC;IACzB,CAAC;IACD,IAAI,WAAW,CAAC,MAAM,CAA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E,CAAC;QACnD,OAAO,eAAe,CAAC;IACzB,CAAC;IAED,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,UAAU,EAAE,CAAC,CAAC;IACvC,MAAM,CAAC,GAAG,kBAAkB,CAAC;QAC3B,KAAK,EAAE,IAAI,CAAC,GAAG,EAAE;QACjB,GAAG,EAAE,MAAM;KACZ,CAAC,CAAC;IAEH,MAAM,IAAI,GAAG,gBAAgB,CAAC,MAAM,EAAE,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;IAClD,MAAM,KAAK,GAAG,CAAC,EAAE,KAAK,IAAI,CAAC,CAAC;IAC5B,MAAM,OAAO,GAAG,CAAC,EAAE,OAAO,IAAI,CAAC,CAAC;IAChC,MAAM,OAAO,GAAG,CAAC,EAAE,OAAO,IAAI,CAAC,CAAC;IAChC,MAAM,QAAQ,GAAG,IAAI,KAAK,CAAC,IAAI,KAAK,KAAK,CAAC,CAAC;IAC3C,MAAM,UAAU,GAAG,QAAQ,IAAI,OAAO,KAAK,CAAC,CAAC;IAE7C,OAAO;QACL,IAAI;QACJ,KAAK;QACL,OAAO;QACP,OAAO;QACP,UAAU;KACX,CAAC;AACJ,CAAC,CAAC","file":"util.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nimport { TimeleftDuration } from \"@w3ux/types\";\nimport { differenceInDays, getUnixTime, intervalToDuration } from \"date-fns\";\nimport { defaultDuration } from \"./useTimeLeft/defaults\";\n\n// calculates the current timeleft duration.\nexport const getDuration = (toDate: Date | null): TimeleftDuration => {\n if (!toDate) {\n return defaultDuration;\n }\n if (getUnixTime(toDate) <= getUnixTime(new Date())) {\n return defaultDuration;\n }\n\n toDate.setSeconds(toDate.getSeconds());\n const d = intervalToDuration({\n start: Date.now(),\n end: toDate,\n });\n\n const days = differenceInDays(toDate, Date.now());\n const hours = d?.hours || 0;\n const minutes = d?.minutes || 0;\n const seconds = d?.seconds || 0;\n const lastHour = days === 0 && hours === 0;\n const lastMinute = lastHour && minutes === 0;\n\n return {\n days,\n hours,\n minutes,\n seconds,\n lastMinute,\n };\n};\n\n// Helper: Adds `seconds` to the current time and returns the resulting date.\nexport const secondsFromNow = (seconds: number): Date => {\n const end = new Date();\n end.setSeconds(end.getSeconds() + seconds);\n return end;\n};\n\n// Helper: Calculates the duration between the current time and the provided date.\nexport const getDurationFromNow = (toDate: Date | null): TimeleftDuration => {\n if (!toDate) {\n return defaultDuration;\n }\n if (getUnixTime(toDate) <= getUnixTime(new Date())) {\n return defaultDuration;\n }\n\n toDate.setSeconds(toDate.getSeconds());\n const d = intervalToDuration({\n start: Date.now(),\n end: toDate,\n });\n\n const days = differenceInDays(toDate, Date.now());\n const hours = d?.hours || 0;\n const minutes = d?.minutes || 0;\n const seconds = d?.seconds || 0;\n const lastHour = days === 0 && hours === 0;\n const lastMinute = lastHour && minutes === 0;\n\n return {\n days,\n hours,\n minutes,\n seconds,\n lastMinute,\n };\n};\n"]}
1
+ {"version":3,"sources":["../src/util.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAA;AAC5E,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAA;AAGxD,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,MAAmB,EAAoB,EAAE;IACnE,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,OAAO,eAAe,CAAA;IACxB,CAAC;IACD,IAAI,WAAW,CAAC,MAAM,CAAC,IAAI,WAAW,CAAC,IAAI,IAAI,EAAE,CAAC,EAAE,CAAC;QACnD,OAAO,eAAe,CAAA;IACxB,CAAC;IAED,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,UAAU,EAAE,CAAC,CAAA;IACtC,MAAM,CAAC,GAAG,kBAAkB,CAAC;QAC3B,KAAK,EAAE,IAAI,CAAC,GAAG,EAAE;QACjB,GAAG,EAAE,MAAM;KACZ,CAAC,CAAA;IAEF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IAAI,CAAC,GAAG,EAAE,CAAC,CAAA;IACjD,MAAM,KAAK,GAAG,CAAC,EAAE,KAAK,IAAI,CAAC,CAAA;IAC3B,MAAM,OAAO,GAAG,CAAC,EAAE,OAAO,IAAI,CAAC,CAAA;IAC/B,MAAM,OAAO,GAAG,CAAC,EAAE,OAAO,IAAI,CAAC,CAAA;IAC/B,MAAM,QAAQ,GAAG,IAAI,KAAK,CAAC,IAAI,KAAK,KAAK,CAAC,CAAA;IAC1C,MAAM,UAAU,GAAG,QAAQ,IAAI,OAAO,KAAK,CAAC,CAAA;IAE5C,OAAO;QACL,IAAI;QACJ,KAAK;QACL,OAAO;QACP,OAAO;QACP,UAAU;KACX,CAAA;AACH,CAAC,CAAA","file":"util.js","sourcesContent":["/* @license Copyright 2024 w3ux authors & contributors\nSPDX-License-Identifier: GPL-3.0-only */\n\nimport type { TimeleftDuration } from '@w3ux/types'\nimport { differenceInDays, getUnixTime, intervalToDuration } from 'date-fns'\nimport { defaultDuration } from './useTimeLeft/defaults'\n\n// calculates the current timeleft duration.\nexport const getDuration = (toDate: Date | null): TimeleftDuration => {\n if (!toDate) {\n return defaultDuration\n }\n if (getUnixTime(toDate) <= getUnixTime(new Date())) {\n return defaultDuration\n }\n\n toDate.setSeconds(toDate.getSeconds())\n const d = intervalToDuration({\n start: Date.now(),\n end: toDate,\n })\n\n const days = differenceInDays(toDate, Date.now())\n const hours = d?.hours || 0\n const minutes = d?.minutes || 0\n const seconds = d?.seconds || 0\n const lastHour = days === 0 && hours === 0\n const lastMinute = lastHour && minutes === 0\n\n return {\n days,\n hours,\n minutes,\n seconds,\n lastMinute,\n }\n}\n\n// Helper: Adds `seconds` to the current time and returns the resulting date.\nexport const secondsFromNow = (seconds: number): Date => {\n const end = new Date()\n end.setSeconds(end.getSeconds() + seconds)\n return end\n}\n\n// Helper: Calculates the duration between the current time and the provided date.\nexport const getDurationFromNow = (toDate: Date | null): TimeleftDuration => {\n if (!toDate) {\n return defaultDuration\n }\n if (getUnixTime(toDate) <= getUnixTime(new Date())) {\n return defaultDuration\n }\n\n toDate.setSeconds(toDate.getSeconds())\n const d = intervalToDuration({\n start: Date.now(),\n end: toDate,\n })\n\n const days = differenceInDays(toDate, Date.now())\n const hours = d?.hours || 0\n const minutes = d?.minutes || 0\n const seconds = d?.seconds || 0\n const lastHour = days === 0 && hours === 0\n const lastMinute = lastHour && minutes === 0\n\n return {\n days,\n hours,\n minutes,\n seconds,\n lastMinute,\n }\n}\n"]}
package/package.json CHANGED
@@ -1,13 +1,26 @@
1
1
  {
2
2
  "name": "@w3ux/hooks",
3
- "version": "2.0.4",
3
+ "version": "2.2.0",
4
4
  "license": "GPL-3.0-only",
5
- "dependencies": {
6
- "@w3ux/utils": "^2.0.6",
7
- "date-fns": "^4.1.0"
5
+ "type": "module",
6
+ "description": "A collection of general purpose React hooks",
7
+ "keywords": [
8
+ "w3ux",
9
+ "polkadot",
10
+ "web3",
11
+ "react",
12
+ "hooks",
13
+ "typescript"
14
+ ],
15
+ "homepage": "https://w3ux.org/library/hooks/overview",
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/w3ux/w3ux-library.git",
19
+ "directory": "library/hooks"
20
+ },
21
+ "bugs": {
22
+ "url": "https://github.com/w3ux/w3ux-library/issues"
8
23
  },
9
- "main": "cjs/index.js",
10
- "module": "mjs/index.js",
11
24
  "exports": {
12
25
  ".": {
13
26
  "import": "./mjs/index.js",
@@ -18,6 +31,10 @@
18
31
  "require": "./cjs/util.js"
19
32
  }
20
33
  },
34
+ "dependencies": {
35
+ "@w3ux/utils": "^2.0.10",
36
+ "date-fns": "^4.1.0"
37
+ },
21
38
  "peerDependencies": {
22
39
  "react": "^19"
23
40
  }