Skip to content
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

Topics/custom should update scroll #663

Merged
merged 3 commits into from
Feb 7, 2017
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,8 @@ files which start with an underscore:
* (optional) `gatsby-browser.js` - a way to hook into key application events.
* Export `onRouteUpdate` of type `function()` to be notified whenever React-Router navigates.
* Export `modifyRoutes` of type `function(routes: Object) => Object` to modify the react-router routes.
* Export `shouldUpdateScroll` of type `function(prevRouterProps: Object, nextRouterProps: Object) => boolean`
to determine if a given route change should scroll.
* (optional) `gatsby-node.js` - a way to hook into events during build
and development.

Expand Down
10 changes: 7 additions & 3 deletions lib/utils/web-entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ReactDOM from 'react-dom'
import { applyRouterMiddleware, browserHistory, Router } from 'react-router'
import useScroll from 'react-router-scroll/lib/useScroll'
import createRoutes from 'create-routes'
import { onRouteChange, onRouteUpdate, modifyRoutes } from 'gatsby-browser'
import { onRouteChange, onRouteUpdate, modifyRoutes, shouldUpdateScroll } from 'gatsby-browser'

const loadContext = require('.gatsby-context')

Expand Down Expand Up @@ -33,9 +33,13 @@ function onUpdate () {
}
}

function shouldUpdateScroll (prevRouterProps, { location: { pathname } }) {
function defaultShouldUpdateScroll (prevRouterProps, nextRouterProps) {
if (shouldUpdateScroll) {
return shouldUpdateScroll(prevRouterProps, nextRouterProps)
}
if (prevRouterProps) {
const { location: { pathname: oldPathname } } = prevRouterProps
const { location: { pathname } } = nextRouterProps
if (oldPathname === pathname) {
return false
}
Expand All @@ -62,7 +66,7 @@ loadConfig(() =>
<Router
history={browserHistory}
routes={routes}
render={applyRouterMiddleware(useScroll(shouldUpdateScroll))}
render={applyRouterMiddleware(useScroll(defaultShouldUpdateScroll))}
onUpdate={onUpdate}
/>,
typeof window !== 'undefined' ? document.getElementById('react-mount') : undefined)
Expand Down