Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b02d1e1

Browse files
committed
Optimize and refactor getScrollElement
1 parent 7559469 commit b02d1e1

File tree

4 files changed

+42
-43
lines changed

4 files changed

+42
-43
lines changed

‎src/hoc/trackWindowScroll.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { PropTypes } from 'prop-types';
44
import debounce from 'lodash.debounce';
55
import throttle from 'lodash.throttle';
66
import isIntersectionObserverAvailable from '../utils/intersection-observer';
7-
import getScrollElement from '../utils/get-scroll-element';
7+
import getScrollAncestor from '../utils/get-scroll-ancestor';
88

99
const getScrollX = () =>
1010
typeof window === 'undefined' ? 0 : window.scrollX || window.pageXOffset;
@@ -54,7 +54,7 @@ const trackWindowScroll = BaseComponent => {
5454
return;
5555
}
5656

57-
const scrollElement = getScrollElement(
57+
const scrollElement = getScrollAncestor(
5858
ReactDom.findDOMNode(this.baseComponentRef.current)
5959
);
6060

@@ -69,7 +69,7 @@ const trackWindowScroll = BaseComponent => {
6969
return;
7070
}
7171

72-
this.scrollElement = getScrollElement(
72+
this.scrollElement = getScrollAncestor(
7373
ReactDom.findDOMNode(this.baseComponentRef.current)
7474
);
7575

‎src/utils/get-scroll-ancestor.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Adapted from https://github.com/loktar00/react-lazy-load/blob/master/src/utils/parentScroll.js
2+
3+
const getElementOverflowValues = (element: HTMLElement) : string => {
4+
const computedStyle = getComputedStyle(element, null);
5+
6+
return (
7+
computedStyle.getPropertyValue('overflow') +
8+
computedStyle.getPropertyValue('overflow-y') +
9+
computedStyle.getPropertyValue('overflow-x')
10+
);
11+
};
12+
13+
const getScrollAncestor = (element: Node) : HTMLElement | Window => {
14+
if (!(element instanceof HTMLElement)) {
15+
return window;
16+
}
17+
18+
let parent : Node = element;
19+
20+
while (parent) {
21+
if (!(parent instanceof HTMLElement)) {
22+
break;
23+
}
24+
25+
if (/(scroll|auto)/.test(getElementOverflowValues(parent))) {
26+
return parent;
27+
}
28+
29+
parent = parent.parentNode;
30+
}
31+
32+
return window;
33+
};
34+
35+
export default getScrollAncestor;

‎src/utils/get-scroll-element.js‎

Lines changed: 0 additions & 39 deletions
This file was deleted.

‎webpack.config.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = {
1212
module: {
1313
rules: [
1414
{
15-
test: /\.jsx?$/,
15+
test: /\.(j|t)sx?$/,
1616
include: path.resolve(__dirname, 'src'),
1717
exclude: /(node_modules|bower_components|build)/,
1818
use: {
@@ -46,4 +46,7 @@ module.exports = {
4646
extensions: ['js', 'jsx', 'ts', 'tsx'],
4747
}),
4848
],
49+
resolve: {
50+
extensions: ['.ts', '.tsx', '.js', '.jsx'],
51+
},
4952
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /