Skip to content

Fix Firefox scroll position may be locked occasionally #12

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

Merged
merged 5 commits into from
Feb 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
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Changed
- `Composer`: fix #13, user scrolling in Firefox may have the scroll position locked occasionally, in PR [#12](https://github.com/compulim/react-scroll-to-bottom/issues/12)

## [1.3.0] - 2019-01-21
### Changed
- Playground: bumped to `react@16.6.0`, `react-dom@16.6.0`, and `react-scripts@2.1.6`
- Update algorithm, instead of using `componentDidUpdate`, we now use `setInterval` to check if the panel is sticky or not, this help to track content update that happen outside of React lifecycle, for example, `HTMLImageElement.onload` event
- `scrollTo()` now accepts `"100%"` instead of `"bottom"`
- `*`: Update algorithm, instead of using `componentDidUpdate`, we now use `setInterval` to check if the panel is sticky or not, this help to track content update that happen outside of React lifecycle, for example, `HTMLImageElement.onload` event
- `Composer`: `scrollTo()` now accepts `"100%"` instead of `"bottom"`

### Removed
- Removed `threshold` props because the algorithm is now more robust
Expand Down
31 changes: 25 additions & 6 deletions packages/component/src/ScrollToBottom/Composer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import InternalContext from './InternalContext';
import SpineTo from '../SpineTo';
import StateContext from './StateContext';

const MIN_CHECK_INTERVAL = 17;
const MIN_CHECK_INTERVAL = 17; // 1 frame
const SCROLL_DECISION_DURATION = 34; // 2 frames

function setImmediateInterval(fn, ms) {
fn();
Expand Down Expand Up @@ -85,15 +86,33 @@ export default class Composer extends React.Component {
enableWorker() {
clearInterval(this._stickyCheckTimeout);

let stickyButNotAtEndSince = false;

this._stickyCheckTimeout = setImmediateInterval(
() => {
const { state } = this;
const { stateContext: { sticky }, target } = state;

if (sticky && target) {
const { atEnd } = computeViewState(state);

!atEnd && state.functionContext.scrollToEnd();
if (
sticky
&& target
&& !computeViewState(state).atEnd
) {
if (!stickyButNotAtEndSince) {
stickyButNotAtEndSince = Date.now();
} else if (Date.now() - stickyButNotAtEndSince > SCROLL_DECISION_DURATION) {
// Quirks: In Firefox, after user scroll down, Firefox do two things:
// 1. Set to a new "scrollTop"
// 2. Fire "scroll" event
// For what we observed, #1 is fired about 20ms before #2. There is a chance that this stickyCheckTimeout is being scheduled between 1 and 2.
// That means, if we just look at #1 to decide if we should scroll, we will always scroll, in oppose to the user's intention.
// Repro: Open Firefox, set checkInterval to a lower number, and try to scroll by dragging the scroll handler. It will jump back.

state.functionContext.scrollToEnd();
stickyButNotAtEndSince = false;
}
} else {
stickyButNotAtEndSince = false;
}
},
Math.max(MIN_CHECK_INTERVAL, this.props.checkInterval) || MIN_CHECK_INTERVAL
Expand Down Expand Up @@ -207,7 +226,7 @@ export default class Composer extends React.Component {
}

Composer.defaultProps = {
checkInterval: 150,
checkInterval: 100,
debounce: 17
};

Expand Down