Skip to content

Commit

Permalink
Enabling intersection observer options.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheodorTomas committed Nov 1, 2018
1 parent 212f210 commit cc79d34
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/createLoadableVisibilityComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ import { IntersectionObserver } from './capacities'
let intersectionObserver
const trackedElements = new Map()

if (IntersectionObserver) {
intersectionObserver = new window.IntersectionObserver((entries, observer) => {
entries.forEach((entry) => {
const trackedElement = trackedElements.get(entry.target)

if (trackedElement && entry.intersectionRatio > 0) {
trackedElement.visibilityHandler()
}
})
})
const setIntersectionObserver = (options) => {
if (IntersectionObserver) {
intersectionObserver = new window.IntersectionObserver((entries, observer) => {
entries.forEach((entry) => {
const trackedElement = trackedElements.get(entry.target)

if (trackedElement && entry.intersectionRatio > 0) {
trackedElement.visibilityHandler()
}
})
}, options)
}
}

function createLoadableVisibilityComponent (args, {
Expand Down Expand Up @@ -52,6 +54,7 @@ function createLoadableVisibilityComponent (args, {
if (!preloaded) {
const element = this.loadingRef
trackedElements.set(element, this)
setIntersectionObserver(args[0].observerOptions)
intersectionObserver.observe(element)
}
}
Expand Down

0 comments on commit cc79d34

Please sign in to comment.