@sohanemon/utils 5.1.2 → 5.1.3
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/dist/hooks/index.d.ts +28 -0
- package/dist/hooks/index.js +56 -0
- package/package.json +1 -1
package/dist/hooks/index.d.ts
CHANGED
|
@@ -160,3 +160,31 @@ export declare const useIsAtTop: ({ offset }?: {
|
|
|
160
160
|
scrollableContainerRef: React.RefObject<HTMLElement>;
|
|
161
161
|
isAtTop: boolean;
|
|
162
162
|
};
|
|
163
|
+
interface UseIntersectionOptions extends IntersectionObserverInit {
|
|
164
|
+
onInteractionStart?: () => void;
|
|
165
|
+
onInteractionEnd?: () => void;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* React hook that tracks when an element enters or leaves the viewport
|
|
169
|
+
* using the Intersection Observer API.
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* ```tsx
|
|
173
|
+
* const { ref, isIntersecting } = useIntersection({
|
|
174
|
+
* threshold: 0.1,
|
|
175
|
+
* onInteractionStart: () => console.log('👀 Element entered viewport'),
|
|
176
|
+
* onInteractionEnd: () => console.log('🙈 Element left viewport'),
|
|
177
|
+
* });
|
|
178
|
+
*
|
|
179
|
+
* return <div ref={ref}>Watch me</div>;
|
|
180
|
+
* ```
|
|
181
|
+
*
|
|
182
|
+
* @param options - Configuration for the intersection observer.
|
|
183
|
+
* @returns Object containing:
|
|
184
|
+
* - `ref`: React ref to attach to the observed element.
|
|
185
|
+
* - `isIntersecting`: Whether the element is currently visible.
|
|
186
|
+
*/
|
|
187
|
+
export declare const useIntersection: ({ threshold, root, rootMargin, onInteractionStart, onInteractionEnd, }?: UseIntersectionOptions) => {
|
|
188
|
+
ref: React.RefObject<any>;
|
|
189
|
+
isIntersecting: boolean;
|
|
190
|
+
};
|
package/dist/hooks/index.js
CHANGED
|
@@ -475,3 +475,59 @@ export const useIsAtTop = ({ offset } = {}) => {
|
|
|
475
475
|
});
|
|
476
476
|
return { scrollableContainerRef, isAtTop };
|
|
477
477
|
};
|
|
478
|
+
/**
|
|
479
|
+
* React hook that tracks when an element enters or leaves the viewport
|
|
480
|
+
* using the Intersection Observer API.
|
|
481
|
+
*
|
|
482
|
+
* @example
|
|
483
|
+
* ```tsx
|
|
484
|
+
* const { ref, isIntersecting } = useIntersection({
|
|
485
|
+
* threshold: 0.1,
|
|
486
|
+
* onInteractionStart: () => console.log('👀 Element entered viewport'),
|
|
487
|
+
* onInteractionEnd: () => console.log('🙈 Element left viewport'),
|
|
488
|
+
* });
|
|
489
|
+
*
|
|
490
|
+
* return <div ref={ref}>Watch me</div>;
|
|
491
|
+
* ```
|
|
492
|
+
*
|
|
493
|
+
* @param options - Configuration for the intersection observer.
|
|
494
|
+
* @returns Object containing:
|
|
495
|
+
* - `ref`: React ref to attach to the observed element.
|
|
496
|
+
* - `isIntersecting`: Whether the element is currently visible.
|
|
497
|
+
*/
|
|
498
|
+
export const useIntersection = ({ threshold = 0.1, root = null, rootMargin, onInteractionStart, onInteractionEnd, } = {}) => {
|
|
499
|
+
const [isIntersecting, setIsIntersecting] = React.useState(false);
|
|
500
|
+
const ref = React.useRef(null);
|
|
501
|
+
React.useEffect(() => {
|
|
502
|
+
if (!ref.current)
|
|
503
|
+
return;
|
|
504
|
+
const observer = new IntersectionObserver((entries) => {
|
|
505
|
+
for (const entry of entries) {
|
|
506
|
+
if (entry.isIntersecting) {
|
|
507
|
+
if (!isIntersecting) {
|
|
508
|
+
onInteractionStart?.();
|
|
509
|
+
setIsIntersecting(true);
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
else {
|
|
513
|
+
if (isIntersecting) {
|
|
514
|
+
onInteractionEnd?.();
|
|
515
|
+
setIsIntersecting(false);
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
}, { threshold, root, rootMargin });
|
|
520
|
+
observer.observe(ref.current);
|
|
521
|
+
return () => {
|
|
522
|
+
observer.disconnect();
|
|
523
|
+
};
|
|
524
|
+
}, [
|
|
525
|
+
threshold,
|
|
526
|
+
root,
|
|
527
|
+
rootMargin,
|
|
528
|
+
onInteractionStart,
|
|
529
|
+
onInteractionEnd,
|
|
530
|
+
isIntersecting,
|
|
531
|
+
]);
|
|
532
|
+
return { ref, isIntersecting };
|
|
533
|
+
};
|