-
Notifications
You must be signed in to change notification settings - Fork 292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: view port observer on lost visibility #17417
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fba5fe5
refactor: improve types
PatrykBuniX 83d0d02
feat: add optional isIntersecting param
PatrykBuniX 5526f3c
feat: add not visible handler
PatrykBuniX dfb1530
refactor: improve naming
PatrykBuniX f0c8eb8
runfix: visibility lost on unmount
PatrykBuniX b6aa3d2
docs: add unmount comment
PatrykBuniX File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -17,12 +17,21 @@ | |
* | ||
*/ | ||
|
||
const observedElements = new Map(); | ||
const observedElements = new Map< | ||
Element, | ||
{ | ||
allowBiggerThanViewport?: boolean; | ||
requireFullyInView?: boolean; | ||
onVisible?: Function; | ||
onVisibilityChange?: (isVisible: boolean, isPartiallyVisible: boolean) => void; | ||
Comment on lines
+21
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just added types here and renamed |
||
} | ||
>(); | ||
const tolerance = 0.8; | ||
|
||
const onIntersect: IntersectionObserverCallback = entries => { | ||
entries.forEach(({intersectionRatio, isIntersecting, target: element, rootBounds}) => { | ||
const {onVisible, onChange, requireFullyInView, allowBiggerThanViewport} = observedElements.get(element) || {}; | ||
const {onVisible, onVisibilityChange, requireFullyInView, allowBiggerThanViewport} = | ||
observedElements.get(element) || {}; | ||
const isFullyInView = intersectionRatio >= tolerance; | ||
|
||
const isBiggerThanRoot = () => { | ||
|
@@ -35,11 +44,11 @@ const onIntersect: IntersectionObserverCallback = entries => { | |
|
||
const isVisible = isIntersecting && (!requireFullyInView || isFullyInView || isBiggerThanRoot()); | ||
|
||
if (onChange) { | ||
onChange(isVisible); | ||
if (onVisibilityChange) { | ||
onVisibilityChange(!!isVisible, isIntersecting); | ||
} else if (isVisible) { | ||
removeElement(element); | ||
return onVisible && onVisible(); | ||
return onVisible?.(); | ||
} | ||
}); | ||
}; | ||
|
@@ -72,18 +81,22 @@ const onElementInViewport = ( | |
* Will track an element and trigger the callback whenever the intersecting state changes | ||
* | ||
* @param element the element to observe | ||
* @param onChange the callback to call when the element intersects or not | ||
* @param onVisibilityChange the callback to call when the element intersects or not | ||
* @param requireFullyInView should the element be fully in view | ||
* @param allowBiggerThanViewport should fire when element is bigger than viewport | ||
*/ | ||
const trackElement = ( | ||
element: HTMLElement, | ||
onChange: Function, | ||
onVisibilityChange: (isVisible: boolean, isPartiallyVisible: boolean) => void, | ||
requireFullyInView = false, | ||
allowBiggerThanViewport = false, | ||
): void => { | ||
if (element) { | ||
observedElements.set(element, {allowBiggerThanViewport, onChange, requireFullyInView}); | ||
observedElements.set(element, { | ||
allowBiggerThanViewport, | ||
onVisibilityChange, | ||
requireFullyInView, | ||
}); | ||
return observer.observe(element); | ||
} | ||
}; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't want to release the trackers after detecting element's visibility (we want to detect its visibility lost later).