Skip to content

Commit 669fecc

Browse files
galvezhomerjam
authored andcommitted
feat: allow scrollToTop to be explicitly disabled (nuxt#4564)
Co-authored-by: James Homer <jameshomer85@gmail.com>
1 parent a32947c commit 669fecc

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

packages/vue-app/template/router.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,11 @@ const scrollBehavior = function (to, from, savedPosition) {
5959
// will retain current scroll position.
6060
let position = false
6161

62-
// if no children detected
63-
if (to.matched.length < 2) {
62+
// if no children detected and scrollToTop is not explicitly disabled
63+
if (
64+
to.matched.length < 2 &&
65+
to.matched.every(r => r.components.default.options.scrollToTop !== false)
66+
) {
6467
// scroll to the top of the page
6568
position = { x: 0, y: 0 }
6669
} else if (to.matched.some(r => r.components.default.options.scrollToTop)) {

test/e2e/basic.browser.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,15 @@ describe('basic browser', () => {
119119
expect(await page.$text('h1')).toBe('User: 1')
120120
})
121121

122+
test('/scroll-to-top', async () => {
123+
const page = await browser.page(url('/scroll-to-top'))
124+
await page.evaluate(() => window.scrollBy(0, window.innerHeight))
125+
await page.nuxt.navigate('/scroll-to-top/other')
126+
const pageYOffset = await page.evaluate(() => window.pageYOffset)
127+
expect(pageYOffset).toBeGreaterThan(0)
128+
page.close()
129+
})
130+
122131
test('/validate should display a 404', async () => {
123132
await page.nuxt.navigate('/validate')
124133

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<template>
2+
<div>
3+
<NuxtLink to="/scroll-to-top/other">
4+
go to other
5+
</NuxtLink>
6+
</div>
7+
</template>
8+
9+
<script>
10+
export default {
11+
}
12+
</script>
13+
14+
<style>
15+
div {
16+
margin-top: 1500px;
17+
}
18+
</style>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<template>
2+
<div>
3+
<NuxtLink to="/scroll-to-top">
4+
go to index
5+
</NuxtLink>
6+
</div>
7+
</template>
8+
9+
<script>
10+
export default {
11+
scrollToTop: false
12+
}
13+
</script>

0 commit comments

Comments
 (0)