fix: fix scroll to hash on new tab during dev

close #653
pull/2040/head
Evan You 1 year ago
parent 470ce3d3f3
commit 9aafc88d59

@ -9,7 +9,7 @@ import {
} from 'vue'
import RawTheme from '@theme/index'
import { inBrowser, pathToFile } from './utils.js'
import { type Router, RouterSymbol, createRouter } from './router.js'
import { type Router, RouterSymbol, createRouter, scrollTo } from './router.js'
import { siteDataRef, useData } from './data.js'
import { useUpdateHead } from './composables/head.js'
import { usePrefetch } from './composables/preFetch.js'
@ -149,6 +149,14 @@ if (inBrowser) {
// dynamically update head tags
useUpdateHead(router.route, data.site)
app.mount('#app')
// scroll to hash on new tab during dev
if (import.meta.env.DEV && location.hash) {
const target = document.querySelector(location.hash)
if (target) {
scrollTo(target, location.hash)
}
}
})
})
}

@ -229,8 +229,8 @@ export function useRoute(): Route {
return useRouter().route
}
function scrollTo(el: HTMLElement | SVGElement, hash: string, smooth = false) {
let target: HTMLElement | SVGElement | null = null
export function scrollTo(el: Element, hash: string, smooth = false) {
let target: Element | null = null
try {
target = el.classList.contains('header-anchor')

Loading…
Cancel
Save