forked from remix-run/react-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix scroll position history enhancers
- Loading branch information
Showing
2 changed files
with
66 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { readState, saveState } from 'history/lib/DOMStateStorage' | ||
import { addEventListener, getWindowScrollPosition, setWindowScrollPosition } | ||
from './DOMUtils' | ||
|
||
export default function useStandardScrollBehavior(createHistory) { | ||
return function (options) { | ||
const history = createHistory(options) | ||
|
||
let currentLocation | ||
let savePositionHandle = null | ||
|
||
addEventListener(window, 'scroll', () => { | ||
if (savePositionHandle !== null) { | ||
clearTimeout(savePositionHandle) | ||
} | ||
|
||
savePositionHandle = setTimeout(() => { | ||
savePositionHandle = null | ||
|
||
if (!currentLocation) { | ||
return | ||
} | ||
const { key } = currentLocation | ||
|
||
const state = readState(key) | ||
saveState(key, { | ||
...state, scrollPosition: getWindowScrollPosition() | ||
}) | ||
}) | ||
}) | ||
|
||
history.listenBefore(() => { | ||
if (savePositionHandle !== null) { | ||
clearTimeout(savePositionHandle) | ||
savePositionHandle = null | ||
} | ||
}) | ||
|
||
function getScrollPosition() { | ||
const state = readState(currentLocation.key) | ||
if (!state) { | ||
return null | ||
} | ||
|
||
return state.scrollPosition | ||
} | ||
|
||
history.listen(location => { | ||
currentLocation = location | ||
|
||
const scrollPosition = getScrollPosition() || {} | ||
const { x = 0, y = 0 } = scrollPosition | ||
|
||
// Need to defer the scroll operation because this listener fires before | ||
// e.g. the router updates its state, and this might need to scroll past | ||
// the end of the page pre-transition if the popped page was longer. | ||
setTimeout(() => setWindowScrollPosition(x, y)) | ||
}) | ||
|
||
return history | ||
} | ||
} |