Skip to content

Commit

Permalink
Clean up custom scroll position implementation (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
taion committed Apr 14, 2016
1 parent 550c052 commit 0430e95
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 24 deletions.
12 changes: 6 additions & 6 deletions modules/useScrollToTop.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { POP } from 'history/lib/Actions'

import createUseScroll from './utils/createUseScroll'
import scrollTo from './utils/scrollTo'
import setScrollRestoration from './utils/setScrollRestoration'

/**
Expand All @@ -11,18 +12,17 @@ import setScrollRestoration from './utils/setScrollRestoration'
export default function useScrollToTop(createHistory) {
let unsetScrollRestoration

function updateScroll({ action }, customPosition) {
const [ x, y ] = customPosition || [ 0, 0 ]
function getScrollPosition({ action }) {
// If we didn't manage to disable the default scroll restoration, and it's
// a pop transition for which the browser might restore scroll position,
// then let the browser update to its remembered scroll position first,
// before we set the actual correct scroll position.
if (action === POP && !unsetScrollRestoration) {
setTimeout(() => window.scrollTo(x, y))
return
setTimeout(() => scrollTo([ 0, 0 ]))
return null
}

window.scrollTo(x, y)
return [ 0, 0 ]
}

function start() {
Expand All @@ -38,5 +38,5 @@ export default function useScrollToTop(createHistory) {
}
}

return createUseScroll(updateScroll, start, stop)(createHistory)
return createUseScroll(getScrollPosition, start, stop)(createHistory)
}
9 changes: 4 additions & 5 deletions modules/useSimpleScroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ export default function useSimpleScroll(createHistory) {
// Don't override the browser's scroll behavior here - we actively want the
// the browser to take care of scrolling on `POP` transitions.

function updateScroll({ action }, customPosition) {
function getScrollPosition({ action }) {
if (action === POP) {
return
return null
}

const [ x, y ] = customPosition || [ 0, 0 ]
window.scrollTo(x, y)
return [ 0, 0 ]
}

return createUseScroll(updateScroll)(createHistory)
return createUseScroll(getScrollPosition)(createHistory)
}
12 changes: 4 additions & 8 deletions modules/useStandardScroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ export default function useStandardScroll(createHistory) {

function getScrollPosition() {
const state = readState(currentKey)
if (!state) {
return null

if (!state || !state.scrollPosition) {
return [ 0, 0 ]
}

return state.scrollPosition
Expand All @@ -31,11 +32,6 @@ export default function useStandardScroll(createHistory) {
currentKey = key
}

function updateScroll(location, customPosition) {
const [ x, y ] = customPosition || getScrollPosition() || [ 0, 0 ]
window.scrollTo(x, y)
}

let unsetScrollRestoration, unlistenScroll, unlistenBefore

function start(history) {
Expand Down Expand Up @@ -92,6 +88,6 @@ export default function useStandardScroll(createHistory) {
}

return createUseScroll(
updateScroll, start, stop, updateLocation
getScrollPosition, start, stop, updateLocation
)(createHistory)
}
20 changes: 15 additions & 5 deletions modules/utils/createUseScroll.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import scrollTo from './scrollTo'

export default function createUseScroll(
updateScroll, start, stop, updateLocation
getScrollPosition, start, stop, updateLocation
) {
return function (createHistory) {
return function (options = {}) {
Expand Down Expand Up @@ -46,11 +48,19 @@ export default function createUseScroll(
updateLocation(location)
}

const updateDecision = shouldUpdateScroll && shouldUpdateScroll(oldLocation, currentLocation)
let scrollPosition
if (shouldUpdateScroll) {
scrollPosition = shouldUpdateScroll(oldLocation, currentLocation)
} else {
scrollPosition = true
}

if (scrollPosition && !Array.isArray(scrollPosition)) {
scrollPosition = getScrollPosition(currentLocation)
}

if (!shouldUpdateScroll || updateDecision) {
const customPosition = Array.isArray(updateDecision) ? updateDecision : null
updateScroll(location, customPosition)
if (scrollPosition) {
scrollTo(scrollPosition)
}
}

Expand Down
3 changes: 3 additions & 0 deletions modules/utils/scrollTo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function scrollTo([ x, y ]) {
window.scrollTo(x, y)
}

0 comments on commit 0430e95

Please sign in to comment.