Unverified Commit b49717f8 authored by Wouter Raateland's avatar Wouter Raateland Committed by GitHub

Allow scroll prevention on hash change (#31921)

* Allow scroll prevention on hash change

Currently, `scrollToHash` is performed on every hash change, even when this change is caused by `<Link scroll={false} {...props} />`.
This change prevents scrolling in this case and allows users to specify the desired scrolling behavior in the router's `hashChangeComplete` event.

* Add test case and apply fixes
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent 41706d6f
...@@ -96,11 +96,6 @@ function linkClicked( ...@@ -96,11 +96,6 @@ function linkClicked(
e.preventDefault() e.preventDefault()
// avoid scroll for urls with anchor refs
if (scroll == null && as.indexOf('#') >= 0) {
scroll = false
}
// replace state instead of push if prop is present // replace state instead of push if prop is present
router[replace ? 'replace' : 'push'](href, as, { router[replace ? 'replace' : 'push'](href, as, {
shallow, shallow,
......
...@@ -1009,7 +1009,7 @@ export default class Router implements BaseRouter { ...@@ -1009,7 +1009,7 @@ export default class Router implements BaseRouter {
performance.mark('routeChange') performance.mark('routeChange')
} }
const { shallow = false } = options const { shallow = false, scroll = true } = options
const routeProps = { shallow } const routeProps = { shallow }
if (this._inFlightRoute) { if (this._inFlightRoute) {
...@@ -1045,8 +1045,13 @@ export default class Router implements BaseRouter { ...@@ -1045,8 +1045,13 @@ export default class Router implements BaseRouter {
this.asPath = cleanedAs this.asPath = cleanedAs
Router.events.emit('hashChangeStart', as, routeProps) Router.events.emit('hashChangeStart', as, routeProps)
// TODO: do we need the resolved href when only a hash change? // TODO: do we need the resolved href when only a hash change?
this.changeState(method, url, as, options) this.changeState(method, url, as, {
this.scrollToHash(cleanedAs) ...options,
scroll: false,
})
if (scroll) {
this.scrollToHash(cleanedAs)
}
this.notify(this.components[this.route], null) this.notify(this.components[this.route], null)
Router.events.emit('hashChangeComplete', as, routeProps) Router.events.emit('hashChangeComplete', as, routeProps)
return true return true
......
...@@ -27,6 +27,11 @@ const HashChanges = ({ count }) => { ...@@ -27,6 +27,11 @@ const HashChanges = ({ count }) => {
<Link href="#name-item-400"> <Link href="#name-item-400">
<a id="scroll-to-name-item-400">Go to name item 400</a> <a id="scroll-to-name-item-400">Go to name item 400</a>
</Link> </Link>
<Link href="#name-item-400" scroll={false}>
<a id="scroll-to-name-item-400-no-scroll">
Go to name item 400 (no scroll)
</a>
</Link>
<p>COUNT: {count}</p> <p>COUNT: {count}</p>
{Array.from({ length: 500 }, (x, i) => i + 1).map((i) => { {Array.from({ length: 500 }, (x, i) => i + 1).map((i) => {
return ( return (
......
...@@ -625,6 +625,17 @@ describe('Client Navigation', () => { ...@@ -625,6 +625,17 @@ describe('Client Navigation', () => {
} }
}) })
it('should not scroll to hash when scroll={false} is set', async () => {
const browser = await webdriver(context.appPort, '/nav/hash-changes')
const curScroll = await browser.eval(
'document.documentElement.scrollTop'
)
await browser.elementByCss('#scroll-to-name-item-400-no-scroll').click()
expect(curScroll).toBe(
await browser.eval('document.documentElement.scrollTop')
)
})
it('should scroll to the specified position on the same page with a name property', async () => { it('should scroll to the specified position on the same page with a name property', async () => {
let browser let browser
try { try {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment