-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat($scrollRestoration): add updateScroll calls in relevant places +…
… edit docs
- Loading branch information
1 parent
88120df
commit 5cb3bad
Showing
6 changed files
with
35 additions
and
57 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
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 |
---|---|---|
@@ -1,57 +1,21 @@ | ||
# Scroll Restoration | ||
|
||
We have a companion package, [redux-first-router-restore-scroll](https://github.com/faceyspacey/redux-first-router-restore-scroll), which provides full-on scroll restoration through the call of a single function. It also insures hash changes work as you would expect (e.g. like when you click `#links` to different section of a Github readme it automatically scrolls, and allows you to use the browser back/next buttons to move between sections you've visited). | ||
Complete Scroll restoration and hash `#links` handling is addressed primarily by one of our companion packages: [redux-first-router-restore-scroll](https://github.com/faceyspacey/redux-first-router-restore-scroll) *(we like to save you the bytes sent to clients if you don't need it)*. In most cases all you need to do is: | ||
|
||
Example: | ||
|
||
```js | ||
import restoreScroll from 'redux-first-router-restore-scroll' | ||
|
||
connectRoutes(history, routesMap, { | ||
restoreScroll: restoreScroll((prev, locationState) => { | ||
// disable scroll restoration on history state changes | ||
// note: this is useful if you want to maintain scroll position from previous route | ||
if (prev.type === 'HOME' && locationState.type === 'CATEGORY') { | ||
return false | ||
} | ||
|
||
// scroll into view HTML element with this ID or name attribute value | ||
else if (locationState.load && locationState.type === 'USER') { | ||
return 'profile-box' | ||
} | ||
|
||
|
||
// return an array of xy coordinates to scroll there | ||
else if (locationState.payload.coords) { | ||
return [coords.x, coords.y] | ||
} | ||
|
||
// Accurately emulate the default behavior of scrolling to the top on new history | ||
// entries, and to previous positions on pop state + hash changes. | ||
// This is the default behavior, and this callback is not needed if this is all you want. | ||
return true | ||
}) | ||
}) | ||
connectRoutes(history, routesMap, { restoreScroll: restoreScroll() }) | ||
``` | ||
|
||
## Notes | ||
Modern browsers like Chrome attempt to provide the default behavior, but we have found | ||
it to be flakey in fact. It's pretty good in Chrome, but doesn't always happen. If all you want is the default behavior and nothing more, | ||
simply `return true` as above or even better, just call `restoreScroll()`: | ||
|
||
```js | ||
import restoreScroll from 'redux-first-router-restore-scroll' | ||
connectRoutes(history, routesMap, { restoreScroll: restoreScroll() }) | ||
``` | ||
Visit [redux-first-router-restore-scroll](https://github.com/faceyspacey/redux-first-router-restore-scroll) for more information and advanced usage. | ||
|
||
We try to make everything as modular as possible and not making assumptions on how many bytes/packages you ship to your clients, | ||
hence we try to break out as many such features into independent companion packages as possible. | ||
|
||
## Scroll Restoration for Elements other than `window` | ||
We got you covered. Please checkout [redux-first-router-scroll-container](https://github.com/faceyspacey/redux-first-router-scroll-container). | ||
|
||
## Scroll Restoration for React Native | ||
We got you covered! Please checkout [redux-first-router-scroll-container-native](https://github.com/faceyspacey/redux-first-router-scroll-container-native). | ||
|
||
## Thanks | ||
Our Scroll Restoration package comes thanks to: https://github.com/taion/scroll-behavior, which powered [react-router-scroll](https://github.com/taion/react-router-scroll) in older versions of React Router. See either for more information on how this works. |
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
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