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

enable initial scroll #42

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
56 changes: 40 additions & 16 deletions src/ScrollSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import PropTypes from 'prop-types'
*/

export default class ScrollSync extends Component {

static propTypes = {
/**
* Callback to be invoked any time synchronization happens
Expand All @@ -19,29 +18,39 @@ export default class ScrollSync extends Component {
proportional: PropTypes.bool,
vertical: PropTypes.bool,
horizontal: PropTypes.bool,
enabled: PropTypes.bool
enabled: PropTypes.bool,
initialScrollLeft: PropTypes.number,
initialScrollTop: PropTypes.number
};

static defaultProps = {
proportional: true,
vertical: true,
horizontal: true,
enabled: true
enabled: true,
initialScrollLeft: 0,
initialScrollTop: 0
};

static childContextTypes = {
registerPane: PropTypes.func,
unregisterPane: PropTypes.func
}

constructor(props) {
super(props)
this.state = {
initialized: false
}
}
getChildContext() {
return {
registerPane: this.registerPane,
unregisterPane: this.unregisterPane
}
}

panes = {}
panes = {};
henrysha marked this conversation as resolved.
Show resolved Hide resolved

registerPane = (node, groups) => {
groups.forEach((group) => {
Expand All @@ -57,7 +66,7 @@ export default class ScrollSync extends Component {
}
})
this.addEvents(node, groups)
}
};
henrysha marked this conversation as resolved.
Show resolved Hide resolved

unregisterPane = (node, groups) => {
groups.forEach((group) => {
Expand All @@ -66,25 +75,25 @@ export default class ScrollSync extends Component {
this.panes[group].splice(this.panes[group].indexOf(node), 1)
}
})
}
};
henrysha marked this conversation as resolved.
Show resolved Hide resolved

addEvents = (node, groups) => {
/* For some reason element.addEventListener doesnt work with document.body */
node.onscroll = this.handlePaneScroll.bind(this, node, groups) // eslint-disable-line
}
node.onscroll = this.handlePaneScroll.bind(this, node, groups); // eslint-disable-line
henrysha marked this conversation as resolved.
Show resolved Hide resolved
};
henrysha marked this conversation as resolved.
Show resolved Hide resolved

removeEvents = (node) => {
/* For some reason element.removeEventListener doesnt work with document.body */
node.onscroll = null // eslint-disable-line
}
node.onscroll = null; // eslint-disable-line
henrysha marked this conversation as resolved.
Show resolved Hide resolved
};
henrysha marked this conversation as resolved.
Show resolved Hide resolved

findPane = (node, group) => {
if (!this.panes[group]) {
return false
}

return this.panes[group].find(pane => pane === node)
}
};
henrysha marked this conversation as resolved.
Show resolved Hide resolved

handlePaneScroll = (node, groups) => {
if (!this.props.enabled) {
Expand All @@ -94,7 +103,7 @@ export default class ScrollSync extends Component {
window.requestAnimationFrame(() => {
this.syncScrollPositions(node, groups)
})
}
};
henrysha marked this conversation as resolved.
Show resolved Hide resolved

syncScrollPosition(scrolledPane, pane) {
const {
Expand All @@ -109,18 +118,33 @@ export default class ScrollSync extends Component {
const scrollTopOffset = scrollHeight - clientHeight
const scrollLeftOffset = scrollWidth - clientWidth

const { proportional, vertical, horizontal } = this.props
const {
proportional,
vertical,
horizontal,
initialScrollTop,
initialScrollLeft
} = this.props

/* Calculate the actual pane height */
const paneHeight = pane.scrollHeight - clientHeight
const paneWidth = pane.scrollWidth - clientWidth
/* Adjust the scrollTop position of it accordingly */
if (vertical && scrollTopOffset > 0) {
pane.scrollTop = proportional ? (paneHeight * scrollTop) / scrollTopOffset : scrollTop // eslint-disable-line
if (!this.state.initialized) pane.scrollTop = initialScrollTop
// eslint-disable-next-line curly
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not disable this rule please and instead do what ESLint suggests.

else pane.scrollTop = proportional
? (paneHeight * scrollTop) / scrollTopOffset
: scrollTop; // eslint-disable-line
henrysha marked this conversation as resolved.
Show resolved Hide resolved
}
if (horizontal && scrollLeftOffset > 0) {
pane.scrollLeft = proportional ? (paneWidth * scrollLeft) / scrollLeftOffset : scrollLeft // eslint-disable-line
if (!this.state.initialized) pane.scrollLeft = initialScrollLeft
// eslint-disable-next-line curly
else pane.scrollLeft = proportional
? (paneWidth * scrollLeft) / scrollLeftOffset
: scrollLeft; // eslint-disable-line
henrysha marked this conversation as resolved.
Show resolved Hide resolved
}
if (!this.state.initialized) this.state.initialized = true
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think React state should not be mutated like this. Either use setState or don't use state for it. Maybe that's there the ESLint error comes from?

}

syncScrollPositions = (scrolledPane, groups) => {
Expand All @@ -139,7 +163,7 @@ export default class ScrollSync extends Component {
})
})
if (this.props.onSync) this.props.onSync(scrolledPane)
}
};
henrysha marked this conversation as resolved.
Show resolved Hide resolved

render() {
return React.Children.only(this.props.children)
Expand Down