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 2 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
40 changes: 32 additions & 8 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,21 +18,31 @@ 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,
Expand Down Expand Up @@ -70,7 +79,7 @@ export default class ScrollSync extends Component {

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
}

removeEvents = (node) => {
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
}
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
}
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 Down