Skip to content

Commit 53024af

Browse files
authored
fix: optimize observers code (#385)
1 parent 0252048 commit 53024af

File tree

3 files changed

+31
-36
lines changed

3 files changed

+31
-36
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@
146146
"@storybook/react": "^6.0.21",
147147
"@storybook/theming": "^6.0.21",
148148
"@testing-library/jest-dom": "^5.5.0",
149-
"@testing-library/react": "^10.0.2",
149+
"@testing-library/react": "^11.0.2",
150150
"@types/jest": "^26.0.12",
151151
"@types/react": "^16.9.49",
152152
"@types/react-dom": "^16.9.6",
@@ -165,7 +165,7 @@
165165
"eslint-plugin-jsx-a11y": "^6.2.3",
166166
"eslint-plugin-react": "^7.19.0",
167167
"eslint-plugin-react-hooks": "^4.1.0",
168-
"framer-motion": "^2.6.5",
168+
"framer-motion": "^2.6.10",
169169
"husky": "^4.2.5",
170170
"intersection-observer": "^0.11.0",
171171
"jest": "^26.4.2",

src/observers.ts

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,14 @@ function getRootId(root?: Element | null) {
3131
* @param options
3232
*/
3333
export function optionsToId(options: IntersectionObserverInit) {
34-
const values = Object.keys(options)
34+
return Object.keys(options)
3535
.sort()
3636
.map((key) => {
37-
let value = options[key];
38-
if (key === 'root') {
39-
value = getRootId(options.root);
40-
}
41-
return `${key}_${value}`;
42-
});
43-
44-
return values.join('|');
37+
return `${key}_${
38+
key === 'root' ? getRootId(options.root) : options[key]
39+
}`;
40+
})
41+
.join('|');
4542
}
4643

4744
function createObserver(options: IntersectionObserverInit) {
@@ -57,13 +54,11 @@ function createObserver(options: IntersectionObserverInit) {
5754
entries.forEach((entry) => {
5855
// While it would be nice if you could just look at isIntersecting to determine if the component is inside the viewport, browsers can't agree on how to use it.
5956
// -Firefox ignores `threshold` when considering `isIntersecting`, so it will never be false again if `threshold` is > 0
60-
const inView = observer.thresholds.some((threshold) => {
61-
return !entry.isIntersecting
62-
? // The intersectionRatio should be more than the threshold to be considered inside the viewport
63-
entry.intersectionRatio > threshold
64-
: // If we're not intersecting, make sure we accept `intersectionRatio` 0 as not inside the viewport
65-
entry.intersectionRatio >= threshold;
66-
});
57+
const inView =
58+
entry.isIntersecting &&
59+
observer.thresholds.some(
60+
(threshold) => entry.intersectionRatio > threshold,
61+
);
6762

6863
// @ts-ignore support IntersectionObserver v2
6964
if (options.trackVisibility && typeof entry.isVisible === 'undefined') {
@@ -73,7 +68,7 @@ function createObserver(options: IntersectionObserverInit) {
7368
}
7469

7570
elements.get(entry.target)?.forEach((callback) => {
76-
callback(inView && entry.isIntersecting, entry);
71+
callback(inView, entry);
7772
});
7873
});
7974
}, options);

yarn.lock

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,7 @@
10341034
core-js-pure "^3.0.0"
10351035
regenerator-runtime "^0.13.4"
10361036

1037-
"@babel/runtime@^7.1.2", "@babel/runtime@^7.10.2", "@babel/runtime@^7.10.3", "@babel/runtime@^7.3.1", "@babel/runtime@^7.5.0", "@babel/runtime@^7.5.5", "@babel/runtime@^7.7.2", "@babel/runtime@^7.7.6", "@babel/runtime@^7.8.4", "@babel/runtime@^7.8.7", "@babel/runtime@^7.9.2":
1037+
"@babel/runtime@^7.1.2", "@babel/runtime@^7.10.2", "@babel/runtime@^7.10.3", "@babel/runtime@^7.11.2", "@babel/runtime@^7.3.1", "@babel/runtime@^7.5.0", "@babel/runtime@^7.5.5", "@babel/runtime@^7.7.2", "@babel/runtime@^7.7.6", "@babel/runtime@^7.8.4", "@babel/runtime@^7.8.7", "@babel/runtime@^7.9.2":
10381038
version "7.11.2"
10391039
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.11.2.tgz#f549c13c754cc40b87644b9fa9f09a6a95fe0736"
10401040
integrity sha512-TeWkU52so0mPtDcaCTxNBI/IHiz0pZgr8VEFqXFtZWpYD08ZB6FaSwVAS8MKRQAP3bYKiVjwysOJgMFY28o6Tw==
@@ -2067,7 +2067,7 @@
20672067
"@svgr/plugin-svgo" "^5.4.0"
20682068
loader-utils "^2.0.0"
20692069

2070-
"@testing-library/dom@^7.22.3":
2070+
"@testing-library/dom@^7.23.0":
20712071
version "7.24.1"
20722072
resolved "https://registry.yarnpkg.com/@testing-library/dom/-/dom-7.24.1.tgz#0e8acd042070f2c1b183fbfe5c0d38b3194ad3c0"
20732073
integrity sha512-TemHWY59gvzcScGiE5eooZpzYk9GaED0TuuK4WefbIc/DQg0L5wOpnj7MIEeAGF3B7Ekf1kvmVnQ97vwz4Lmhg==
@@ -2094,13 +2094,13 @@
20942094
lodash "^4.17.15"
20952095
redent "^3.0.0"
20962096

2097-
"@testing-library/react@^10.0.2":
2098-
version "10.4.9"
2099-
resolved "https://registry.yarnpkg.com/@testing-library/react/-/react-10.4.9.tgz#9faa29c6a1a217bf8bbb96a28bd29d7a847ca150"
2100-
integrity sha512-pHZKkqUy0tmiD81afs8xfiuseXfU/N7rAX3iKjeZYje86t9VaB0LrxYVa+OOsvkrveX5jCK3IjajVn2MbePvqA==
2097+
"@testing-library/react@^11.0.2":
2098+
version "11.0.2"
2099+
resolved "https://registry.yarnpkg.com/@testing-library/react/-/react-11.0.2.tgz#4588cc537085907bd1d98b531eb247dbbf57b1cc"
2100+
integrity sha512-iOuNNHt4ZgMH5trSKC4kaWDcKzUOf7e7KQIcu7xvGCd68/w1EegbW89F9T5sZ4IjS0gAXdvOfZbG9ESZ7YjOug==
21012101
dependencies:
2102-
"@babel/runtime" "^7.10.3"
2103-
"@testing-library/dom" "^7.22.3"
2102+
"@babel/runtime" "^7.11.2"
2103+
"@testing-library/dom" "^7.23.0"
21042104

21052105
"@types/anymatch@*":
21062106
version "1.3.1"
@@ -6132,14 +6132,14 @@ fragment-cache@^0.2.1:
61326132
dependencies:
61336133
map-cache "^0.2.2"
61346134

6135-
framer-motion@^2.6.5:
6136-
version "2.6.9"
6137-
resolved "https://registry.yarnpkg.com/framer-motion/-/framer-motion-2.6.9.tgz#6eee59a70e3715d2ec621bb41efd426736096c6e"
6138-
integrity sha512-BGKqzF7dt1i7myy6GtxfhLVoKvDwjVPk/YpxGiAKkR6WpsoIzQB5NdbwgAxe+soDHXtDEaukh53lt+dLXFg5Wg==
6135+
framer-motion@^2.6.10:
6136+
version "2.6.10"
6137+
resolved "https://registry.yarnpkg.com/framer-motion/-/framer-motion-2.6.10.tgz#fe34f1961320874ddb9fc9e8fc7b7812d400e721"
6138+
integrity sha512-FXJCjntZoxl9wpCVJOsqZ8JvmiqvMZ/tgZrgI0IHfJ88RFnDH+rMjVwkYBILRZREJPjIz2MKPDSs43rabBdxcA==
61396139
dependencies:
61406140
framesync "^4.1.0"
61416141
hey-listen "^1.0.8"
6142-
popmotion "9.0.0-rc.13"
6142+
popmotion "9.0.0-rc.14"
61436143
style-value-types "^3.1.9"
61446144
tslib "^1.10.0"
61456145
optionalDependencies:
@@ -9527,10 +9527,10 @@ polished@^3.4.4:
95279527
dependencies:
95289528
"@babel/runtime" "^7.9.2"
95299529

9530-
9531-
version "9.0.0-rc.13"
9532-
resolved "https://registry.yarnpkg.com/popmotion/-/popmotion-9.0.0-rc.13.tgz#0280968fb0dc3bd273cc3dbd4da24b01b6cfbcce"
9533-
integrity sha512-M8Ksx2THYrAUrptE5Ydd3jazQAMoXxqpClR/K8xK87v4UrJUTCb39kNar/ucJ4Rs7qPqPdVu+iqPKOJhpMylXQ==
9530+
9531+
version "9.0.0-rc.14"
9532+
resolved "https://registry.yarnpkg.com/popmotion/-/popmotion-9.0.0-rc.14.tgz#e57351b7b85a3e42b7a16affbbd440138797c11f"
9533+
integrity sha512-zdMw1OSKjFBH+KKpZx7P+cGSUb3QCqg5QD12f6llucUeEFT+SDZYxvTY09JI23ZcJyzxgKFT1anbLq0eZ9bj3g==
95349534
dependencies:
95359535
framesync "^4.1.0"
95369536
hey-listen "^1.0.8"

0 commit comments

Comments
 (0)