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

✨ Router scroll behaviour, fix #1071 #1075

Merged
merged 4 commits into from
Dec 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/@vuepress/core/lib/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,15 @@ export function createApp (isServer) {
base: siteData.base,
mode: 'history',
fallback: false,
routes
routes,
scrollBehavior (to, from, savedPosition) {
Copy link
Contributor

Choose a reason for hiding this comment

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

What about cases when to.hash is set?

Copy link
Contributor Author

@dcastil dcastil Dec 7, 2018

Choose a reason for hiding this comment

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

That interferes with plugin-active-header-links unfortunately. It causes the website to jump around when the anchor is being pushed.

But jumping to an anchor still works with the native browser behaviour.

Copy link
Contributor

Choose a reason for hiding this comment

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

Alright perfect, then i guess in the old implementation it was just to disable scroll behavior via some flag.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah that would make sense here, too! Have to check that out later.

if (savedPosition) {
return savedPosition
}
if (to.path !== from.path) {
return { x: 0, y: 0 }
}
}
})

// redirect /foo to /foo/
Expand Down
30 changes: 0 additions & 30 deletions packages/@vuepress/core/lib/app/root-mixins/updateLoadingState.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,13 @@
import SmoothScroll from 'smooth-scroll/dist/smooth-scroll.js'

export default {
created () {
this.$vuepress.$on('AsyncMarkdownContentMounted', () => {
this.$vuepress.$set('contentMounted', true)

this.$smoothScroll = new SmoothScroll('a[href*="#"]', {
speed: 1,
speedAsDuration: true,
easing: 'easeInOutCubic'
})

if (this.$route.hash) {
const hash = decodeURIComponent(this.$route.hash)
try {
const anchor = document.getElementById(hash.slice(1))
const anchorLink = anchor.querySelector('a.header-anchor')
setTimeout(() => {
window.scroll({
top: anchorLink.offsetTop - 70,
left: 0,
behavior: 'auto'
})
})
} catch (e) {
console.error(e)
}
}
})
},

watch: {
'$route.path' () {
this.$vuepress.$set('contentMounted', false)
this.$smoothScroll.destroy()
}
},

beforeDestroy () {
this.$smoothScroll.destroy()
}
}
1 change: 0 additions & 1 deletion packages/@vuepress/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
"optimize-css-assets-webpack-plugin": "^4.0.0",
"portfinder": "^1.0.13",
"postcss-loader": "^2.1.5",
"smooth-scroll": "^15.0.0",
"toml": "^2.3.3",
"url-loader": "^1.0.1",
"vue": "^2.5.16",
Expand Down
4 changes: 0 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7606,10 +7606,6 @@ [email protected]:
dependencies:
is-fullwidth-code-point "^2.0.0"

smooth-scroll@^15.0.0:
version "15.0.0"
resolved "https://registry.yarnpkg.com/smooth-scroll/-/smooth-scroll-15.0.0.tgz#be4f9cb2cc4952d80db6736e5656ed5849305272"

snapdragon-node@^2.0.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/snapdragon-node/-/snapdragon-node-2.1.1.tgz#6c175f86ff14bdb0724563e8f3c1b021a286853b"
Expand Down