-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: useIntersectionObserver 기능 개선 (#57)
- Loading branch information
Showing
6 changed files
with
46 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@modern-kit/react': patch | ||
--- | ||
|
||
fix: useIntersectionObserver 기능 개선 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 26 additions & 38 deletions
64
packages/react/src/hooks/useIntersectionObserver/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,54 @@ | ||
import { useCallback, useEffect, useRef } from 'react'; | ||
import { useRef } from 'react'; | ||
import { usePreservedCallback } from '../usePreservedCallback'; | ||
import { noop } from '@modern-kit/utils'; | ||
|
||
export interface UseIntersectionObserverProps { | ||
export interface UseIntersectionObserverProps extends IntersectionObserverInit { | ||
action: (entry: IntersectionObserverEntry) => void; | ||
calledOnce?: boolean; | ||
threshold?: number | number[]; | ||
root?: Document | Element | null; | ||
rootMargin?: string; | ||
} | ||
|
||
export const useIntersectionObserver = <T extends HTMLElement>({ | ||
action, | ||
calledOnce = false, | ||
root = null, | ||
threshold = 0, | ||
threshold = [0], | ||
rootMargin = '0px 0px 0px 0px', | ||
}: UseIntersectionObserverProps) => { | ||
const ref = useRef<T>(null); | ||
const callbackAction = usePreservedCallback(action); | ||
const intersectionObserverRef = useRef<IntersectionObserver | null>(null); | ||
|
||
const observerCallback = useCallback( | ||
const callbackAction = usePreservedCallback(action ?? noop); | ||
|
||
const observerAction = usePreservedCallback( | ||
([entry]: IntersectionObserverEntry[], observer: IntersectionObserver) => { | ||
if (entry) { | ||
if (entry.isIntersecting) { | ||
const targetElement = entry.target as HTMLElement; | ||
if (entry && entry.isIntersecting) { | ||
const targetElement = entry.target as T; | ||
|
||
if (callbackAction) { | ||
callbackAction(entry); | ||
} | ||
if (callbackAction) { | ||
callbackAction(entry); | ||
} | ||
|
||
if (calledOnce) { | ||
observer.unobserve(targetElement); | ||
} | ||
if (calledOnce) { | ||
observer.unobserve(targetElement); | ||
} | ||
} | ||
}, | ||
[callbackAction, calledOnce] | ||
); | ||
|
||
useEffect(() => { | ||
const targetElement = ref.current; | ||
|
||
if (typeof IntersectionObserver === 'undefined') { | ||
return; | ||
} | ||
); | ||
|
||
if (!targetElement) { | ||
return; | ||
const targetRef = usePreservedCallback((node: T) => { | ||
if (intersectionObserverRef.current) { | ||
intersectionObserverRef.current.disconnect(); | ||
} | ||
|
||
const observer = new IntersectionObserver(observerCallback, { | ||
intersectionObserverRef.current = new IntersectionObserver(observerAction, { | ||
root, | ||
rootMargin, | ||
threshold, | ||
rootMargin, | ||
}); | ||
|
||
observer.observe(targetElement); | ||
|
||
return () => { | ||
observer.unobserve(targetElement); | ||
}; | ||
}, [root, threshold, rootMargin, observerCallback]); | ||
if (node) { | ||
intersectionObserverRef.current.observe(node); | ||
} | ||
}); | ||
|
||
return ref; | ||
return targetRef; | ||
}; |