Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support container with overflow #28

Merged
merged 1 commit into from
Jan 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 56 additions & 20 deletions src/hoc/trackWindowScroll.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import React from 'react';
import ReactDom from 'react-dom';
import { PropTypes } from 'prop-types';
import debounce from 'lodash.debounce';
import throttle from 'lodash.throttle';
import isIntersectionObserverAvailable from '../utils/intersection-observer';
import getScrollElement from '../utils/get-scroll-element';

const getScrollX = () => typeof window === 'undefined' ?
0 : (window.scrollX || window.pageXOffset);
const getScrollY = () => typeof window === 'undefined' ?
0 : (window.scrollY || window.pageYOffset);

const trackWindowScroll = (BaseComponent) => {
class ScrollAwareComponent extends React.Component {
Expand All @@ -23,48 +30,76 @@ const trackWindowScroll = (BaseComponent) => {

this.state = {
scrollPosition: {
x: (typeof window === 'undefined' ?
0 :
(window.scrollX || window.pageXOffset)
),
y: (typeof window === 'undefined' ?
0 :
(window.scrollY || window.pageYOffset)
),
x: getScrollX(),
y: getScrollY(),
},
};

this.baseComponentRef = React.createRef();
}

componentDidMount() {
if (typeof window == 'undefined' || isIntersectionObserverAvailable()) {
this.addListeners();
}

componentWillUnmount() {
this.removeListeners();
}

componentDidUpdate() {
if (typeof window === 'undefined' || isIntersectionObserverAvailable()) {
return;
}

const scrollElement = getScrollElement(
ReactDom.findDOMNode(this.baseComponentRef.current)
);

if (scrollElement !== this.scrollElement) {
this.removeListeners();
this.addListeners();
}
}

addListeners() {
if (typeof window === 'undefined' || isIntersectionObserverAvailable()) {
return;
}
window.addEventListener('scroll', this.delayedScroll);

this.scrollElement = getScrollElement(
ReactDom.findDOMNode(this.baseComponentRef.current)
);

this.scrollElement.addEventListener('scroll', this.delayedScroll);
window.addEventListener('resize', this.delayedScroll);

if (this.scrollElement !== window) {
window.addEventListener('scroll', this.delayedScroll);
}
}

componentWillUnmount() {
removeListeners() {
if (typeof window == 'undefined' || isIntersectionObserverAvailable()) {
return;
}
window.removeEventListener('scroll', this.delayedScroll);

this.scrollElement.removeEventListener('scroll', this.delayedScroll);
window.removeEventListener('resize', this.delayedScroll);

if (this.scrollElement !== window) {
window.removeEventListener('scroll', this.delayedScroll);
}
}

onChangeScroll() {
if (isIntersectionObserverAvailable()) {
return;
}

this.setState({
scrollPosition: {
x: (typeof window == 'undefined' ?
0 :
(window.scrollX || window.pageXOffset)
),
y: (typeof window === 'undefined' ?
0 :
(window.scrollY || window.pageYOffset)
),
x: getScrollX(),
y: getScrollY(),
},
});
}
Expand All @@ -76,6 +111,7 @@ const trackWindowScroll = (BaseComponent) => {

return (
<BaseComponent
ref={this.baseComponentRef}
scrollPosition={scrollPosition}
{...props} />
);
Expand Down
36 changes: 36 additions & 0 deletions src/utils/get-scroll-element.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// copyright https://github.com/loktar00/react-lazy-load/blob/master/src/utils/parentScroll.js

const style = (element, prop) =>
typeof getComputedStyle === 'undefined' ? element.style[prop] :
getComputedStyle(element, null).getPropertyValue(prop);

const overflow = (element) =>
style(element, 'overflow') +
style(element, 'overflow-y') +
style(element, 'overflow-x');

const scrollParent = (element) => {
if (!(element instanceof HTMLElement)) {
return window;
}

let parent = element;

while (parent) {
if (parent === document.body ||
parent === document.documentElement ||
!parent.parentNode) {
break;
}

if (/(scroll|auto)/.test(overflow(parent))) {
return parent;
}

parent = parent.parentNode;
}

return window;
};

export default scrollParent;