diff --git a/modules/useScrollToTop.js b/modules/useScrollToTop.js index 2af348d..c9703c3 100644 --- a/modules/useScrollToTop.js +++ b/modules/useScrollToTop.js @@ -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' /** @@ -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() { @@ -38,5 +38,5 @@ export default function useScrollToTop(createHistory) { } } - return createUseScroll(updateScroll, start, stop)(createHistory) + return createUseScroll(getScrollPosition, start, stop)(createHistory) } diff --git a/modules/useSimpleScroll.js b/modules/useSimpleScroll.js index a33db11..0d12904 100644 --- a/modules/useSimpleScroll.js +++ b/modules/useSimpleScroll.js @@ -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) } diff --git a/modules/useStandardScroll.js b/modules/useStandardScroll.js index e71628b..1d3faaa 100644 --- a/modules/useStandardScroll.js +++ b/modules/useStandardScroll.js @@ -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 @@ -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) { @@ -92,6 +88,6 @@ export default function useStandardScroll(createHistory) { } return createUseScroll( - updateScroll, start, stop, updateLocation + getScrollPosition, start, stop, updateLocation )(createHistory) } diff --git a/modules/utils/createUseScroll.js b/modules/utils/createUseScroll.js index be37325..8e0cd89 100644 --- a/modules/utils/createUseScroll.js +++ b/modules/utils/createUseScroll.js @@ -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 = {}) { @@ -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) } } diff --git a/modules/utils/scrollTo.js b/modules/utils/scrollTo.js new file mode 100644 index 0000000..d1a1937 --- /dev/null +++ b/modules/utils/scrollTo.js @@ -0,0 +1,3 @@ +export default function scrollTo([ x, y ]) { + window.scrollTo(x, y) +}