-
-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
117566f
commit ca41e31
Showing
3 changed files
with
103 additions
and
175 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
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,4 +1,5 @@ | ||
import * as React from "react"; | ||
import { useDidMount } from "shared/useDidMount"; | ||
|
||
const initialState = { | ||
intersectionObj: {}, | ||
|
@@ -11,7 +12,6 @@ interface Iaction { | |
data: any; | ||
} | ||
|
||
|
||
function IntersectionObserverReducer(state: any, action: Iaction) { | ||
switch (action.type) { | ||
case "SETINTERSECTIONOBJ": { | ||
|
@@ -46,16 +46,16 @@ const checkFeasibility = () => { | |
return true; | ||
}; | ||
interface IOptions { | ||
elementRef: React.MutableRefObject<HTMLElement>; | ||
rootRef?: React.MutableRefObject<HTMLElement>; | ||
root: HTMLElement; | ||
rootMargin?: string; | ||
threshold?: string; | ||
threshold?: number[]; | ||
when?: boolean; | ||
callback?: Function; | ||
visibilityCondition?: (entry: IntersectionObserverEntry) => boolean; | ||
} | ||
|
||
type useIntersectionObserverReturn = [ | ||
(node: HTMLElement) => void, | ||
boolean, | ||
IntersectionObserverEntry, | ||
IntersectionObserver | ||
|
@@ -89,125 +89,111 @@ function useIntersectionObserver( | |
): useIntersectionObserverReturn { | ||
const defaultOptions = { | ||
rootMargin: "0px 0px 0px 0px", | ||
threshold: "0, 1", | ||
threshold: [0, 1], | ||
when: true, | ||
visibilityCondition: defaultVisibilityCondition, | ||
rootRef: React.useRef(null) | ||
visibilityCondition: defaultVisibilityCondition | ||
}; | ||
|
||
const { | ||
elementRef, | ||
rootRef, | ||
root = null, | ||
rootMargin, | ||
threshold, | ||
when, | ||
callback, | ||
visibilityCondition | ||
} = { ...defaultOptions, ...options }; | ||
|
||
const [element, setElement] = React.useState<HTMLElement>(null); | ||
|
||
const [state, dispatch] = React.useReducer( | ||
IntersectionObserverReducer, | ||
initialState | ||
); | ||
const { intersectionObj, observerInState, isVisible } = state; | ||
const observerRef = React.useRef(null); | ||
const callbackRef = React.useRef(null); | ||
const savedCallbackRef = React.useRef(callback); | ||
const visibilityConditionRef = React.useRef(visibilityCondition); | ||
|
||
React.useEffect(() => { | ||
visibilityConditionRef.current = visibilityCondition; | ||
}); | ||
React.useEffect(() => { | ||
savedCallbackRef.current = callback; | ||
}); | ||
|
||
useDidMount(() => { | ||
checkFeasibility(); | ||
}); | ||
This comment has been minimized.
Sorry, something went wrong.
simbathesailor
Contributor
|
||
|
||
/** | ||
* Setting callback Ref | ||
*/ | ||
|
||
React.useEffect(() => { | ||
if (!checkFeasibility()) { | ||
return; | ||
} | ||
if (!callback) { | ||
let callbackDefault = function( | ||
entries: IntersectionObserverEntry[], | ||
observer: IntersectionObserver | ||
) { | ||
entries.forEach((entry: IntersectionObserverEntry) => { | ||
// setIntersectionObj(entry); | ||
dispatch({ | ||
type: "SETINTERSECTIONOBJ", | ||
data: entry | ||
}); | ||
if (!observerInState) { | ||
dispatch({ | ||
type: "SETOBSERVERHANDLE", | ||
data: observer | ||
}); | ||
// setObserver(observer); | ||
} | ||
let finalVisibilityFunction = defaultVisibilityCondition; | ||
if (visibilityCondition) { | ||
finalVisibilityFunction = visibilityCondition; | ||
} | ||
const handleIntersectionObserve = React.useCallback( | ||
(entries: IntersectionObserverEntry[], observer: IntersectionObserver) => { | ||
entries.forEach((entry: IntersectionObserverEntry) => { | ||
// setIntersectionObj(entry); | ||
dispatch({ | ||
type: "SETINTERSECTIONOBJ", | ||
data: entry | ||
}); | ||
if (!observerInState) { | ||
dispatch({ | ||
type: "SET_VISIBILITY", | ||
data: finalVisibilityFunction(entry) | ||
type: "SETOBSERVERHANDLE", | ||
data: observer | ||
}); | ||
|
||
// Each entry describes an intersection change for one observed | ||
// target element: | ||
// entry.boundingClientRect | ||
// entry.intersectionRatio | ||
// entry.intersectionRect | ||
// entry.isIntersecting | ||
// entry.rootBounds | ||
// entry.target | ||
// entry.time | ||
// setObserver(observer); | ||
} | ||
const _visibilityFn = | ||
visibilityConditionRef.current || defaultVisibilityCondition; | ||
dispatch({ | ||
type: "SET_VISIBILITY", | ||
data: _visibilityFn(entry) | ||
}); | ||
}; | ||
callbackRef.current = callbackDefault; | ||
} else { | ||
callbackRef.current = callback; | ||
} | ||
}, [callback, observerInState, visibilityCondition]); | ||
|
||
/** | ||
* unobserving intersectionobserver when "when" key is false | ||
*/ | ||
React.useEffect(() => { | ||
if (!checkFeasibility()) { | ||
return; | ||
} | ||
if (!when) { | ||
if (observerRef.current) { | ||
observerRef.current.unobserve(elementRef.current); | ||
} | ||
} | ||
}, [observerRef, elementRef, rootRef, rootMargin, when, callback]); | ||
// Each entry describes an intersection change for one observed | ||
// target element: | ||
// entry.boundingClientRect | ||
// entry.intersectionRatio | ||
// entry.intersectionRect | ||
// entry.isIntersecting | ||
// entry.rootBounds | ||
// entry.target | ||
// entry.time | ||
}); | ||
savedCallbackRef.current && savedCallbackRef.current(); | ||
}, | ||
[observerInState] | ||
); | ||
|
||
/** | ||
* Effect responsible for creating intersection observer and | ||
* registering the observer for specific element | ||
*/ | ||
React.useEffect(() => { | ||
if (!checkFeasibility()) { | ||
return; | ||
} | ||
const currentELem = elementRef.current; | ||
const currentRootElem = rootRef.current; | ||
if (when) { | ||
let observer = new IntersectionObserver(callbackRef.current, { | ||
root: currentRootElem, | ||
threshold: threshold.split(",").map(elem => parseFloat(elem)), | ||
const observer = new IntersectionObserver(handleIntersectionObserve, { | ||
root, | ||
threshold, | ||
rootMargin | ||
}); | ||
observerRef.current = observer; | ||
|
||
if (currentELem && observerRef.current) { | ||
observerRef.current.observe(currentELem); | ||
if (element && observerRef.current) { | ||
observerRef.current.observe(element); | ||
return () => { | ||
if (element && observerRef.current) { | ||
observerRef.current.unobserve(element); | ||
} | ||
}; | ||
} | ||
} | ||
return () => { | ||
if (currentELem) { | ||
observerRef.current.unobserve(currentELem); | ||
} | ||
}; | ||
}, [elementRef, rootRef, rootMargin, when, callbackRef, threshold]); | ||
}, [rootMargin, when, savedCallbackRef, threshold]); | ||
|
||
const callbackRef = React.useCallback((node: HTMLElement) => { | ||
setElement(node); | ||
}, []); | ||
|
||
return [isVisible, intersectionObj, observerInState]; | ||
return [callbackRef, isVisible, intersectionObj, observerInState]; | ||
} | ||
|
||
export { 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
Cool, I was thinking to discuss this. But any ways, now the API is similar to native IntersectionObserver