Skip to content

Commit

Permalink
fix: set passive to true for scroll event listener in ProgressiveLoad…
Browse files Browse the repository at this point in the history
…ingContainer [DHIS2-9508] (#1084)

A minor optimization that probably won't have much impact on the performance, but nevertheless doesn't have a negative impact.

Changes include:

* do not add eventListener on the window
* set passive: true on the event listener (see explanation here: https://web.dev/uses-passive-event-listeners/?utm_source=lighthouse&utm_medium=devtools)
  • Loading branch information
jenniferarnesen authored Sep 22, 2020
1 parent b1d7fab commit 763881b
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions src/components/Item/ProgressiveLoadingContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ class ProgressiveLoadingContainer extends Component {
}
containerRef = null
shouldLoadHandler = null
handlerOptions = { passive: true }

checkShouldLoad() {
const bufferPx = this.props.bufferFactor * window.innerHeight

if (!this.containerRef) {
return
}

const bufferPx = this.props.bufferFactor * window.innerHeight
const rect = this.containerRef.getBoundingClientRect()
if (
rect.bottom > -bufferPx &&
Expand All @@ -51,22 +51,23 @@ class ProgressiveLoadingContainer extends Component {
this.props.debounceMs
)

const containers = [
window, // this is probably unnecessary
...document.getElementsByClassName('dashboard-wrapper'),
]
containers.forEach(container => {
container.addEventListener('scroll', this.shouldLoadHandler)
})
document
.getElementsByClassName('dashboard-wrapper')[0]
?.addEventListener(
'scroll',
this.shouldLoadHandler,
this.handlerOptions
)
}

removeHandler() {
const containers = [
window, // this is probably unnecessary
...document.getElementsByClassName('dashboard-wrapper'),
]
containers.forEach(container => {
container.removeEventListener('scroll', this.shouldLoadHandler)
})
document
.getElementsByClassName('dashboard-wrapper')[0]
?.removeEventListener(
'scroll',
this.shouldLoadHandler,
this.handlerOptions
)
}

componentDidMount() {
Expand Down

0 comments on commit 763881b

Please sign in to comment.