Skip to content

Commit 7b1e791

Browse files
autarcokonet
authored andcommitted
fix: Ensure valid non-zero scroll offset values on multi-scroll panes (#8)
* Ensure valid offset for multi-scroll * Allow usage with previous react version
1 parent 32646e8 commit 7b1e791

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@
7070
"webpack": "^1.13.2"
7171
},
7272
"peerDependencies": {
73-
"react": "15.x",
74-
"react-dom": "15.x",
73+
"react": "0.14.x || 15.x",
74+
"react-dom": "0.14.x || 15.x",
7575
"prop-types": "^15.x"
7676
},
7777
"dependencies": {}

src/ScrollSync.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,17 @@ export default class ScrollSync extends Component {
6868
}
6969

7070
syncScrollPositions = (scrolledPane) => {
71-
const { scrollTop, scrollHeight, clientHeight,
72-
scrollLeft, scrollWidth, clientWidth } = scrolledPane
71+
const {
72+
scrollTop,
73+
scrollHeight,
74+
clientHeight,
75+
scrollLeft,
76+
scrollWidth,
77+
clientWidth
78+
} = scrolledPane
79+
80+
const scrollTopOffset = scrollHeight - clientHeight
81+
const scrollLeftOffset = scrollWidth - clientWidth
7382

7483
const { proportional, vertical, horizontal } = this.props
7584

@@ -82,11 +91,11 @@ export default class ScrollSync extends Component {
8291
const paneHeight = pane.scrollHeight - clientHeight
8392
const paneWidth = pane.scrollWidth - clientWidth
8493
/* Adjust the scrollTop position of it accordingly */
85-
if (vertical) {
86-
pane.scrollTop = proportional ? (paneHeight * scrollTop) / (scrollHeight - clientHeight) : scrollTop // eslint-disable-line
94+
if (vertical && scrollTopOffset > 0) {
95+
pane.scrollTop = proportional ? (paneHeight * scrollTop) / scrollTopOffset : scrollTop // eslint-disable-line
8796
}
88-
if (horizontal) {
89-
pane.scrollLeft = proportional ? (paneWidth * scrollLeft) / (scrollWidth - clientWidth) : scrollLeft // eslint-disable-line
97+
if (horizontal && scrollLeftOffset > 0) {
98+
pane.scrollLeft = proportional ? (paneWidth * scrollLeft) / scrollLeftOffset : scrollLeft // eslint-disable-line
9099
}
91100
/* Re-attach event listeners after we're done scrolling */
92101
window.requestAnimationFrame(() => {

0 commit comments

Comments
 (0)