@sohanemon/utils 4.1.12 → 4.1.13

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.
@@ -191,3 +191,12 @@ export declare function throttle<F extends (...args: any[]) => any>(function_: F
191
191
  * ```
192
192
  */
193
193
  export declare function printf(format: string, ...args: unknown[]): string;
194
+ /**
195
+ * Merges multiple refs into a single ref callback.
196
+ *
197
+ * @param refs - An array of refs to merge.
198
+ *
199
+ * @returns - A function that updates the merged ref with the provided value.
200
+ */
201
+ export type MergeRefs = <T>(...refs: Array<React.Ref<T> | undefined>) => React.RefCallback<T>;
202
+ export declare const mergeRefs: MergeRefs;
@@ -347,3 +347,17 @@ export function printf(format, ...args) {
347
347
  return arg === undefined ? '' : String(arg);
348
348
  });
349
349
  }
350
+ export const mergeRefs = (...refs) => {
351
+ return (value) => {
352
+ for (const ref of refs) {
353
+ if (!ref)
354
+ continue;
355
+ if (typeof ref === 'function') {
356
+ ref(value);
357
+ }
358
+ else {
359
+ ref.current = value;
360
+ }
361
+ }
362
+ };
363
+ };
@@ -142,3 +142,21 @@ export declare const useDomCalculation: ({ blockIds, margin, substract, dynamic,
142
142
  height: number;
143
143
  width: number;
144
144
  };
145
+ /**
146
+ * Hook to detect if the user is scrolling.
147
+ * @returns An object containing the isScrolling state and a ref to the scrollable container.
148
+ */
149
+ export declare const useIsScrolling: () => {
150
+ isScrolling: boolean;
151
+ scrollableContainerRef: React.RefObject<HTMLElement>;
152
+ };
153
+ /**
154
+ * Hook to detect if the user is at the top of the page.
155
+ * @returns An object containing the isAtTop state and a ref to the scrollable container.
156
+ */
157
+ export declare const useIsAtTop: ({ offset }?: {
158
+ offset?: number;
159
+ }) => {
160
+ scrollableContainerRef: React.RefObject<HTMLElement>;
161
+ isAtTop: boolean;
162
+ };
@@ -416,3 +416,62 @@ export const useDomCalculation = ({ blockIds = [], margin = 0, substract = true,
416
416
  }, [handleCalculation, dynamic, blockIds.join(',')]);
417
417
  return dimensions;
418
418
  };
419
+ /**
420
+ * Hook to detect if the user is scrolling.
421
+ * @returns An object containing the isScrolling state and a ref to the scrollable container.
422
+ */
423
+ export const useIsScrolling = () => {
424
+ const [isScrolling, setIsScrolling] = React.useState(false);
425
+ const scrollTimerRef = React.useRef(null);
426
+ const scrollableContainerRef = React.useRef(null);
427
+ useEffectOnce(() => {
428
+ const mainElement = scrollableContainerRef.current;
429
+ if (!mainElement)
430
+ return;
431
+ const handleScroll = () => {
432
+ setIsScrolling(true);
433
+ if (scrollTimerRef.current) {
434
+ clearTimeout(scrollTimerRef.current);
435
+ }
436
+ // Set a timeout to mark scrolling as finished after delay
437
+ scrollTimerRef.current = setTimeout(() => {
438
+ setIsScrolling(false);
439
+ }, 150);
440
+ };
441
+ handleScroll();
442
+ mainElement.addEventListener('scroll', handleScroll);
443
+ return () => {
444
+ mainElement.removeEventListener('scroll', handleScroll);
445
+ if (scrollTimerRef.current) {
446
+ clearTimeout(scrollTimerRef.current);
447
+ }
448
+ };
449
+ });
450
+ return {
451
+ isScrolling,
452
+ scrollableContainerRef,
453
+ };
454
+ };
455
+ /**
456
+ * Hook to detect if the user is at the top of the page.
457
+ * @returns An object containing the isAtTop state and a ref to the scrollable container.
458
+ */
459
+ export const useIsAtTop = ({ offset } = {}) => {
460
+ const [isAtTop, setIsAtTop] = React.useState(true);
461
+ const scrollableContainerRef = React.useRef(null);
462
+ useEffectOnce(() => {
463
+ const mainElement = scrollableContainerRef.current;
464
+ if (!mainElement)
465
+ return;
466
+ const handleScroll = () => {
467
+ const scrolled = mainElement.scrollTop > (offset ?? 10);
468
+ setIsAtTop(!scrolled);
469
+ };
470
+ handleScroll();
471
+ mainElement.addEventListener('scroll', handleScroll);
472
+ return () => {
473
+ mainElement.removeEventListener('scroll', handleScroll);
474
+ };
475
+ });
476
+ return { scrollableContainerRef, isAtTop };
477
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sohanemon/utils",
3
- "version": "4.1.12",
3
+ "version": "4.1.13",
4
4
  "author": "Sohan Emon <sohanemon@outlook.com>",
5
5
  "description": "",
6
6
  "type": "module",
@@ -15,30 +15,44 @@
15
15
  },
16
16
  "typesVersions": {
17
17
  "*": {
18
- "core": ["dist/index.d.ts"],
19
- "types": ["dist/types/index.d.ts"],
20
- "hooks": ["dist/hooks/index.d.ts"],
21
- "components": ["dist/components/index.d.ts"]
18
+ "core": [
19
+ "dist/index.d.ts"
20
+ ],
21
+ "types": [
22
+ "dist/types/index.d.ts"
23
+ ],
24
+ "hooks": [
25
+ "dist/hooks/index.d.ts"
26
+ ],
27
+ "components": [
28
+ "dist/components/index.d.ts"
29
+ ]
22
30
  }
23
31
  },
24
- "files": ["dist", "README.md"],
32
+ "files": [
33
+ "dist",
34
+ "README.md"
35
+ ],
25
36
  "scripts": {
26
37
  "build": "tsc",
27
38
  "build:watch": "tsc --watch",
28
39
  "export": "tsc && npm publish"
29
40
  },
30
- "keywords": ["utils", "cn"],
41
+ "keywords": [
42
+ "utils",
43
+ "cn"
44
+ ],
31
45
  "license": "ISC",
32
46
  "devDependencies": {
33
47
  "@types/node": "^22.4.0",
34
- "@types/react": "^18.3.3",
48
+ "@types/react": "^19.1.4",
35
49
  "typescript": "^5.5.4"
36
50
  },
37
51
  "dependencies": {
38
52
  "@fluid-tailwind/tailwind-merge": "^0.0.2",
39
53
  "@iconify/react": "^4.1.1",
40
54
  "clsx": "^2.1.1",
41
- "react": "^18.3.1",
55
+ "react": "^19.1.0",
42
56
  "tailwind-merge": "^1.14.0"
43
57
  }
44
58
  }