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

feat(scroll): allow behavior in scrollBehavior #3210

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 31 additions & 19 deletions src/util/scroll.js
@@ -1,7 +1,7 @@
/* @flow */

import type Router from '../index'
import { assert } from './warn'
import { assert, warn } from './warn'
import { getStateKey, setStateKey } from './state-key'
import { extend } from './misc'

Expand Down Expand Up @@ -82,8 +82,8 @@ export function saveScrollPosition () {
const key = getStateKey()
if (key) {
positionStore[key] = {
x: window.pageXOffset,
y: window.pageYOffset
left: window.pageXOffset,
top: window.pageYOffset
}
}
}
Expand All @@ -107,26 +107,33 @@ function getElementPosition (el: Element, offset: Object): Object {
const docRect = docEl.getBoundingClientRect()
const elRect = el.getBoundingClientRect()
return {
x: elRect.left - docRect.left - offset.x,
y: elRect.top - docRect.top - offset.y
behavior: offset.behavior,
left: elRect.left - docRect.left - offset.left,
top: elRect.top - docRect.top - offset.top
}
}

function isValidPosition (obj: Object): boolean {
return isNumber(obj.x) || isNumber(obj.y)
return isNumber(obj.left) || isNumber(obj.top)
}

function normalizePosition (obj: Object): Object {
return {
x: isNumber(obj.x) ? obj.x : window.pageXOffset,
y: isNumber(obj.y) ? obj.y : window.pageYOffset
function normalizePosition (obj: Object, defaultLeft?: number, defaultTop?: number): Object {
const left = isNumber(obj.left) ? obj.left : obj.x
const top = isNumber(obj.top) ? obj.top : obj.y
// defaultLeft = isNumber(defaultLeft) ? defaultLeft : window.pageXOffset
// defaultTop = isNumber(defaultTop) ? defaultTop : window.pageYOffset
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove comment code

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to add it back 👀


if (process.env.NODE_ENV !== 'production') {
if (isNumber(obj.x) || isNumber(obj.y)) {
const original = JSON.stringify(obj, null, 2)
warn(false, 'Position objects returned by "scrollBehavior" with "x" and "y" positions are deprecated, replace it with "left" and "top" respectively. Replace\n\n' + original + '\n\nwith\n\n' + original.replace('"x"', '"left"').replace('"y"', '"top"'))
}
}
}

function normalizeOffset (obj: Object): Object {
return {
x: isNumber(obj.x) ? obj.x : 0,
y: isNumber(obj.y) ? obj.y : 0
behavior: obj.behavior,
left: isNumber(left) ? left : defaultLeft,
top: isNumber(top) ? top : defaultTop
}
}

Expand All @@ -150,16 +157,21 @@ function scrollToPosition (shouldScroll, position) {
shouldScroll.offset && typeof shouldScroll.offset === 'object'
? shouldScroll.offset
: {}
offset = normalizeOffset(offset)
offset = normalizePosition(offset, 0, 0)
position = getElementPosition(el, offset)
} else if (isValidPosition(shouldScroll)) {
} else {
position = normalizePosition(shouldScroll)
}
} else if (isObject && isValidPosition(shouldScroll)) {
} else if (isObject) {
position = normalizePosition(shouldScroll)
}

if (position) {
window.scrollTo(position.x, position.y)
if (position && isValidPosition(position)) {
const docEl: any = document.documentElement
if ('scrollBehavior' in docEl.style) {
window.scrollTo(position)
} else {
window.scrollTo(position.left, position.top)
}
}
}
2 changes: 1 addition & 1 deletion src/util/warn.js
Expand Up @@ -16,6 +16,6 @@ export function isError (err: any): boolean {
return Object.prototype.toString.call(err).indexOf('Error') > -1
}

export function isRouterError (err: any, errorType: ?string): boolean {
export function isRouterError (err: any, errorType: ?number): boolean {
return isError(err) && err._isRouter && (errorType == null || err.type === errorType)
}