@w3ux/hooks 2.0.3 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/index.d.ts +6 -6
- package/cjs/index.js.map +1 -1
- package/cjs/useEffectIgnoreInitial.js +1 -1
- package/cjs/useEffectIgnoreInitial.js.map +1 -1
- package/cjs/useOnResize.d.ts +1 -1
- package/cjs/useOnResize.js +2 -2
- package/cjs/useOnResize.js.map +1 -1
- package/cjs/useOutsideAlerter.d.ts +1 -1
- package/cjs/useOutsideAlerter.js +2 -2
- package/cjs/useOutsideAlerter.js.map +1 -1
- package/cjs/useSafeContext.d.ts +1 -1
- package/cjs/useSafeContext.js +1 -1
- package/cjs/useSafeContext.js.map +1 -1
- package/cjs/useSize.d.ts +1 -1
- package/cjs/useSize.js +2 -2
- package/cjs/useSize.js.map +1 -1
- package/cjs/useTimeLeft/defaults.d.ts +1 -1
- package/cjs/useTimeLeft/defaults.js.map +1 -1
- package/cjs/useTimeLeft/index.d.ts +1 -1
- package/cjs/useTimeLeft/index.js.map +1 -1
- package/cjs/util.d.ts +1 -1
- package/cjs/util.js.map +1 -1
- package/mjs/index.d.ts +6 -6
- package/mjs/index.js +6 -6
- package/mjs/index.js.map +1 -1
- package/mjs/useEffectIgnoreInitial.js +2 -2
- package/mjs/useEffectIgnoreInitial.js.map +1 -1
- package/mjs/useOnResize.d.ts +1 -1
- package/mjs/useOnResize.js +3 -3
- package/mjs/useOnResize.js.map +1 -1
- package/mjs/useOutsideAlerter.d.ts +1 -1
- package/mjs/useOutsideAlerter.js +3 -3
- package/mjs/useOutsideAlerter.js.map +1 -1
- package/mjs/useSafeContext.d.ts +1 -1
- package/mjs/useSafeContext.js +2 -2
- package/mjs/useSafeContext.js.map +1 -1
- package/mjs/useSize.d.ts +1 -1
- package/mjs/useSize.js +3 -3
- package/mjs/useSize.js.map +1 -1
- package/mjs/useTimeLeft/defaults.d.ts +1 -1
- package/mjs/useTimeLeft/defaults.js.map +1 -1
- package/mjs/useTimeLeft/index.d.ts +1 -1
- package/mjs/useTimeLeft/index.js +3 -3
- package/mjs/useTimeLeft/index.js.map +1 -1
- package/mjs/util.d.ts +1 -1
- package/mjs/util.js +2 -2
- package/mjs/util.js.map +1 -1
- package/package.json +2 -2
package/cjs/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
5
|
-
export * from
|
|
6
|
-
export * from
|
|
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,
|
|
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"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/useEffectIgnoreInitial.tsx"],"names":[],"mappings":";;;AAGA,
|
|
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"]}
|
package/cjs/useOnResize.d.ts
CHANGED
package/cjs/useOnResize.js
CHANGED
|
@@ -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(
|
|
18
|
+
target.addEventListener('resize', handleResize);
|
|
19
19
|
return () => {
|
|
20
|
-
target.removeEventListener(
|
|
20
|
+
target.removeEventListener('resize', handleResize);
|
|
21
21
|
};
|
|
22
22
|
}, [throttleDuration, callback]);
|
|
23
23
|
};
|
package/cjs/useOnResize.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/useOnResize.tsx"],"names":[],"mappings":";;;
|
|
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
|
|
1
|
+
import { type RefObject } from 'react';
|
|
2
2
|
export declare const useOutsideAlerter: (ref: RefObject<HTMLElement | null>, callback: () => void, ignore?: string[]) => void;
|
package/cjs/useOutsideAlerter.js
CHANGED
|
@@ -16,9 +16,9 @@ const useOutsideAlerter = (ref, callback, ignore = []) => {
|
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
18
|
};
|
|
19
|
-
document.addEventListener(
|
|
19
|
+
document.addEventListener('mousedown', handleClickOutside);
|
|
20
20
|
return () => {
|
|
21
|
-
document.removeEventListener(
|
|
21
|
+
document.removeEventListener('mousedown', handleClickOutside);
|
|
22
22
|
};
|
|
23
23
|
}, [ref]);
|
|
24
24
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/useOutsideAlerter.tsx"],"names":[],"mappings":";;;AAGA,
|
|
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"]}
|
package/cjs/useSafeContext.d.ts
CHANGED
package/cjs/useSafeContext.js
CHANGED
|
@@ -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(
|
|
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,
|
|
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":["// 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"]}
|
package/cjs/useSize.d.ts
CHANGED
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(
|
|
24
|
+
listenFor.addEventListener('resize', handleResize);
|
|
25
25
|
return () => {
|
|
26
|
-
listenFor.removeEventListener(
|
|
26
|
+
listenFor.removeEventListener('resize', handleResize);
|
|
27
27
|
};
|
|
28
28
|
}, [outerElement === null || outerElement === void 0 ? void 0 : outerElement.current]);
|
|
29
29
|
return size;
|
package/cjs/useSize.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/useSize.tsx"],"names":[],"mappings":";;;AAIA,
|
|
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,EA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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 +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,
|
|
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
|
|
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":";;;
|
|
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,G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IACH,CAAC,EAAE,CAAC,EAAE,EAAE,UAAU,EAAE,EAAE,mBAAmB,EAAE,EAAE,GAAG,YAAY,CAAC,CAAC,CAAA;IAG9D,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,WAAW,CAAC,WAAW,EAAE,CAAC,CAAA;IAC5B,CAAC,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
|
|
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,
|
|
1
|
+
{"version":3,"sources":["../src/util.ts"],"names":[],"mappings":";;;AAIA,uCAA4E;AAC5E,qDAAwD;AAGjD,MAAM,WAAW,GAAG,CAAC,MAAmB,EAAoB,EAAE;IACnE,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,OAAO,0BAAe,CAAA;IACxB,CAAC;IACD,IAAI,IAAA,sBAAW,EAAC,MAAM,CAAC,IAAI,IAAA,sBAAW,EAAC,IAAI,IAAI,EAAE,CAAC,EAAE,CAAC;QACnD,OAAO,0BAAe,CAAA;IACxB,CAAC;IAED,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,UAAU,EAAE,CAAC,CAAA;IACtC,MAAM,CAAC,GAAG,IAAA,6BAAkB,EAAC;QAC3B,KAAK,EAAE,IAAI,CAAC,GAAG,EAAE;QACjB,GAAG,EAAE,MAAM;KACZ,CAAC,CAAA;IAEF,MAAM,IAAI,GAAG,IAAA,2BAAgB,EAAC,MAAM,EAAE,IAAI,CAAC,GAAG,EAAE,CAAC,CAAA;IACjD,MAAM,KAAK,GAAG,CAAA,CAAC,aAAD,CAAC,uBAAD,CAAC,CAAE,KAAK,KAAI,CAAC,CAAA;IAC3B,MAAM,OAAO,GAAG,CAAA,CAAC,aAAD,CAAC,uBAAD,CAAC,CAAE,OAAO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AC,CAAE,KAAK,KAAI,CAAC,CAAA;IAC3B,MAAM,OAAO,GAAG,CAAA,CAAC,aAAD,CAAC,uBAAD,CAAC,CAAE,OAAO,KAAI,CAAC,CAAA;IAC/B,MAAM,OAAO,GAAG,CAAA,CAAC,aAAD,CAAC,uBAAD,CAAC,CAAE,OAAO,KAAI,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;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
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
5
|
-
export * from
|
|
6
|
-
export * from
|
|
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
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
5
|
-
export * from
|
|
6
|
-
export * from
|
|
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,
|
|
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
|
|
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 ===
|
|
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,
|
|
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"]}
|
package/mjs/useOnResize.d.ts
CHANGED
package/mjs/useOnResize.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useEffect, useRef } from
|
|
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(
|
|
15
|
+
target.addEventListener('resize', handleResize);
|
|
16
16
|
return () => {
|
|
17
|
-
target.removeEventListener(
|
|
17
|
+
target.removeEventListener('resize', handleResize);
|
|
18
18
|
};
|
|
19
19
|
}, [throttleDuration, callback]);
|
|
20
20
|
};
|
package/mjs/useOnResize.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/useOnResize.tsx"],"names":[],"mappings":"
|
|
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
|
|
1
|
+
import { type RefObject } from 'react';
|
|
2
2
|
export declare const useOutsideAlerter: (ref: RefObject<HTMLElement | null>, callback: () => void, ignore?: string[]) => void;
|
package/mjs/useOutsideAlerter.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useEffect } from
|
|
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(
|
|
16
|
+
document.addEventListener('mousedown', handleClickOutside);
|
|
17
17
|
return () => {
|
|
18
|
-
document.removeEventListener(
|
|
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,
|
|
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"]}
|
package/mjs/useSafeContext.d.ts
CHANGED
package/mjs/useSafeContext.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { createContext, useContext } from
|
|
1
|
+
import { createContext, useContext } from 'react';
|
|
2
2
|
export const useSafeContext = (ctx) => {
|
|
3
3
|
if (ctx === null || ctx === undefined) {
|
|
4
|
-
throw new Error(
|
|
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,
|
|
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":["// 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"]}
|
package/mjs/useSize.d.ts
CHANGED
package/mjs/useSize.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useEffect, useRef, useState } from
|
|
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(
|
|
21
|
+
listenFor.addEventListener('resize', handleResize);
|
|
22
22
|
return () => {
|
|
23
|
-
listenFor.removeEventListener(
|
|
23
|
+
listenFor.removeEventListener('resize', handleResize);
|
|
24
24
|
};
|
|
25
25
|
}, [outerElement?.current]);
|
|
26
26
|
return size;
|
package/mjs/useSize.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/useSize.tsx"],"names":[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AY,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 +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,
|
|
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
|
|
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;
|
package/mjs/useTimeLeft/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { setStateWithRef } from
|
|
2
|
-
import { useEffect, useRef, useState } from
|
|
3
|
-
import { getDuration } from
|
|
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":"
|
|
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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,cAAc,EAAE,cAAc,CAAC,CAAA;YAC3D,CAAC;QACH,CAAC;aAEI,IAAI,CAAC,cAAc,CAAC,OAAO,EAAE,CAAC;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
|
|
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
|
|
2
|
-
import { defaultDuration } from
|
|
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,
|
|
1
|
+
{"version":3,"sources":["../src/util.ts"],"names":[],"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,CAAC,UAAU,EAAE,GAAG,OAAO,CAAC,CAAA;IAC1C,OAAO,GAAG,CAAA;AACZ,CAAC,CAAA;AAGD,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,MAAmB,EAAoB,EAAE;IAC1E,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,MAAM,IAAI,GAAG,gBAAgB,CAAC,MAAM,EAAE,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"]}
|