Skip to content

Commit 9aafc88

Browse files
committed
fix: fix scroll to hash on new tab during dev
close #653
1 parent 470ce3d commit 9aafc88

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/client/app/index.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from 'vue'
1010
import RawTheme from '@theme/index'
1111
import { inBrowser, pathToFile } from './utils.js'
12-
import { type Router, RouterSymbol, createRouter } from './router.js'
12+
import { type Router, RouterSymbol, createRouter, scrollTo } from './router.js'
1313
import { siteDataRef, useData } from './data.js'
1414
import { useUpdateHead } from './composables/head.js'
1515
import { usePrefetch } from './composables/preFetch.js'
@@ -149,6 +149,14 @@ if (inBrowser) {
149149
// dynamically update head tags
150150
useUpdateHead(router.route, data.site)
151151
app.mount('#app')
152+
153+
// scroll to hash on new tab during dev
154+
if (import.meta.env.DEV && location.hash) {
155+
const target = document.querySelector(location.hash)
156+
if (target) {
157+
scrollTo(target, location.hash)
158+
}
159+
}
152160
})
153161
})
154162
}

src/client/app/router.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,8 @@ export function useRoute(): Route {
229229
return useRouter().route
230230
}
231231

232-
function scrollTo(el: HTMLElement | SVGElement, hash: string, smooth = false) {
233-
let target: HTMLElement | SVGElement | null = null
232+
export function scrollTo(el: Element, hash: string, smooth = false) {
233+
let target: Element | null = null
234234

235235
try {
236236
target = el.classList.contains('header-anchor')

0 commit comments

Comments
 (0)