-
Notifications
You must be signed in to change notification settings - Fork 289
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
Fix scroll doesn't reset to top #115
Fix scroll doesn't reset to top #115
Conversation
src/FixedDataTable.js
Outdated
var maxScrollTop = scrollContentHeight - bodyHeight; | ||
// Handle the case where the scrollTop is beyond the maxScrollTop, such as when the user is completely scrolled | ||
// down and resizes the viewport to be smaller vertically | ||
if (scrollTop > maxScrollTop || scrollTop === 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately I couldn't come up with a way to do the scrollTop === 0 once. If the 0 logic is removed, scrolling slowly back up can cause the scrollTop to never update all the way back to 0. There may be a better way, but for now this seems to work well enough.
Another big bug is when I resize the page, I end up jumping to the bottom of a large table. I expect to not change my first index |
src/FixedDataTable.js
Outdated
// This calculation is synonymous to Element.scrollTop | ||
var scrollTop = firstRowIndex && Math.abs(firstRowOffset - this._scrollHelper.getRowPosition(firstRowIndex)); | ||
if (scrollTop >= 0) { | ||
var maxScrollTop = maxScrollY ? scrollContentHeight - bodyHeight : 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maxScrollY is already this value. We don't need to recalculate maxScrollTop
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah true, removed maxScrollTop
src/FixedDataTable.js
Outdated
@@ -1056,6 +1056,21 @@ var FixedDataTable = React.createClass({ | |||
|
|||
this._scrollHelper.setViewportHeight(bodyHeight); | |||
|
|||
// This calculation is synonymous to Element.scrollTop | |||
var scrollTop = firstRowIndex && Math.abs(firstRowOffset - this._scrollHelper.getRowPosition(firstRowIndex)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't use && as an if statement
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to
var scrollTop = firstRowIndex ? Math.abs(firstRowOffset - this._scrollHelper.getRowPosition(firstRowIndex)) : 0;
Was also able to get rid of the following if statement (line 1061) because of this
src/FixedDataTable.js
Outdated
var maxScrollTop = maxScrollY ? scrollContentHeight - bodyHeight : 0; | ||
// Handle the case where the scrollTop is beyond the maxScrollTop, such as when the user is completely scrolled | ||
// down and resizes the viewport to be smaller vertically | ||
if (scrollTop > maxScrollTop || maxScrollTop === 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What case does maxScrollTop === zero catch?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it! Originally I couldn't come up with a way to do the maxScrollTop === 0 once. Before, if the 0 logic was removed, scrolling slowly back up can cause the scrollTop to never update all the way back to 0. At the time I couldn't figure out how to do the check so that we only set back to 0 once.
But I found out how to do it. I have the previous scrollY, so:
if (scrollTop > maxScrollY || (oldState && oldState.scrollY !== 0 && maxScrollY === 0)) {
See in my newest commit.
src/FixedDataTable.js
Outdated
@@ -1056,6 +1056,19 @@ var FixedDataTable = React.createClass({ | |||
|
|||
this._scrollHelper.setViewportHeight(bodyHeight); | |||
|
|||
// This calculation is synonymous to Element.scrollTop | |||
var scrollTop = firstRowIndex ? Math.abs(firstRowOffset - this._scrollHelper.getRowPosition(firstRowIndex)) : 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
firstRowIndex should always exist. you can get rid of the if statement here.
Also, is the absolute value necessary? When would we get a negative value here
src/FixedDataTable.js
Outdated
@@ -1056,6 +1056,19 @@ var FixedDataTable = React.createClass({ | |||
|
|||
this._scrollHelper.setViewportHeight(bodyHeight); | |||
|
|||
// This calculation is synonymous to Element.scrollTop | |||
var scrollTop = firstRowIndex != null ? Math.abs(firstRowOffset - this._scrollHelper.getRowPosition(firstRowIndex)) : 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
firstRowIndex is always defined, you don't need the null check
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, thanks. Removed
src/FixedDataTable.js
Outdated
// Handle the case where the scrollTop is beyond the maxScrollY, such as when the user is completely scrolled | ||
// down and resizes the viewport to be smaller vertically. The other case is when resizing the viewport large enough | ||
// so that a scrollbar is not necessary anymore, to make sure we set the scrollTop back to 0. | ||
if (scrollTop > maxScrollY || (oldState && oldState.scrollY !== 0 && maxScrollY === 0)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use scrollY instead of oldState.scrollY. We might have modified it by the time we reach this line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not necessary anymore because of your above comment
@@ -1056,6 +1056,19 @@ var FixedDataTable = React.createClass({ | |||
|
|||
this._scrollHelper.setViewportHeight(bodyHeight); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After some investigation, it seems the real bug is this line. We update the viewport height after having calculated the rows. This makes our clamping operation invalid.
I think all we really need to do is check if scrollTop === scrollY.
src/FixedDataTable.js
Outdated
var scrollTop = firstRowIndex != null ? Math.abs(firstRowOffset - this._scrollHelper.getRowPosition(firstRowIndex)) : 0; | ||
// Handle the case where the scrollTop is beyond the maxScrollY, such as when the user is completely scrolled | ||
// down and resizes the viewport to be smaller vertically. The other case is when resizing the viewport large enough | ||
// so that a scrollbar is not necessary anymore, to make sure we set the scrollTop back to 0. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is really only one case here. Your second comment about resizing the viewport large enough that the scrollbar isn't necessary necessitates that we hit the first case, of having scrollTop being beyond maxScrollY
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, true
Description
This fixes the bug where if you were to scroll to the bottom (or anywhere below the top), then resize the screen bigger until there is no scrollbar, the content used to not scroll up as the 'clientHeight' was increasing.
Motivation and Context
Fixes #87
How Has This Been Tested?
Tested manually by sizing the screen smaller to create a scrollbar, scrolling to bottom, then increasing the size until there is no scrollbar. Did this several times and used logging to ensure that bad state was not happening.
Screenshots (if appropriate):
Types of changes
Checklist: