Skip to content

Commit

Permalink
fix: fix scroll to hash on new tab during dev
Browse files Browse the repository at this point in the history
close #653
  • Loading branch information
yyx990803 committed Mar 13, 2023
1 parent 470ce3d commit 9aafc88
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/client/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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)
}
}
})
})
}
4 changes: 2 additions & 2 deletions src/client/app/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 9aafc88

Please sign in to comment.