Skip to content

Commit cde9030

Browse files
committed
test(e2e): add smooth scrolling for demo
1 parent 12e9209 commit cde9030

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

e2e/scroll-behavior/index.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createRouter, createWebHistory, ScrollBehavior } from '../../src'
22
import { RouteComponent } from '../../src/types'
3-
import { createApp } from 'vue'
3+
import { createApp, ref } from 'vue'
44
import { scrollWaiter } from './scrollWaiter'
55

66
const Home: RouteComponent = { template: '<div class="home">home</div>' }
@@ -28,19 +28,23 @@ const scrollBehavior: ScrollBehavior = async function (
2828
) {
2929
await scrollWaiter.promise
3030

31+
const behavior: ScrollOptions['behavior'] = smoothScroll.value
32+
? 'smooth'
33+
: 'auto'
34+
3135
if (savedPosition) {
3236
// savedPosition is only available for popstate navigations.
33-
return savedPosition
37+
return { ...savedPosition, behavior }
3438
} else {
3539
let position: ReturnType<ScrollBehavior>
3640

3741
// scroll to anchor by returning the selector
3842
if (to.hash) {
39-
position = { selector: decodeURI(to.hash) }
43+
position = { selector: decodeURI(to.hash), offset: { behavior } }
4044

4145
// specify offset of the element
4246
if (to.hash === '#anchor2') {
43-
position.offset = { top: 100 }
47+
position.offset = { top: 100, behavior }
4448
}
4549

4650
if (document.querySelector(position.selector)) {
@@ -56,7 +60,7 @@ const scrollBehavior: ScrollBehavior = async function (
5660
if (to.matched.some(m => m.meta.scrollToTop)) {
5761
// coords will be used if no selector is provided,
5862
// or if the selector didn't match any element.
59-
return { left: 0, top: 0 }
63+
return { left: 0, top: 0, behavior }
6064
}
6165

6266
return false
@@ -76,9 +80,12 @@ const router = createRouter({
7680

7781
scrollWaiter.add()
7882

83+
const smoothScroll = ref(false)
84+
7985
const app = createApp({
8086
setup() {
8187
return {
88+
smoothScroll,
8289
hashWithNumber: { path: '/bar', hash: '#\\31 number' },
8390
flushWaiter: scrollWaiter.flush,
8491
setupWaiter: scrollWaiter.add,
@@ -101,6 +108,9 @@ const app = createApp({
101108
<li><router-link to="/bar#anchor2">/bar#anchor2</router-link></li>
102109
<li><router-link :to="hashWithNumber">/bar#1number</router-link></li>
103110
</ul>
111+
<label>
112+
<input type="checkbox" v-model="smoothScroll"> Use smooth scroll
113+
</label>
104114
<router-view class="view" v-slot="{ Component, props }">
105115
<transition
106116
name="fade"

0 commit comments

Comments
 (0)