perf: Move to passive scroll listeners where supported

This commit is contained in:
Tom Moor
2022-02-10 19:29:25 -08:00
parent af6c5a1f45
commit 25fd8466e0
4 changed files with 36 additions and 20 deletions

View File

@@ -2,22 +2,7 @@
// maintained.
import { throttle } from "lodash";
import { useState, useEffect } from "react";
let supportsPassive = false;
try {
const opts = Object.defineProperty({}, "passive", {
get: function () {
supportsPassive = true;
},
});
// @ts-expect-error ts-migrate(2769) FIXME: No overload matches this call.
window.addEventListener("testPassive", null, opts);
// @ts-expect-error ts-migrate(2769) FIXME: No overload matches this call.
window.removeEventListener("testPassive", null, opts);
} catch (e) {
// No-op
}
import { supportsPassiveListener } from "~/utils/browser";
const getPosition = () => ({
x: window.pageXOffset,
@@ -44,7 +29,7 @@ export default function useWindowScrollPosition(options: {
window.addEventListener(
"scroll",
handleScroll,
supportsPassive
supportsPassiveListener
? {
passive: true,
}