1
- import { reactive , inject , markRaw , nextTick , readonly } from 'vue'
2
1
import type { Component , InjectionKey } from 'vue'
2
+ import { inject , markRaw , nextTick , reactive , readonly } from 'vue'
3
+ import type { Awaitable , PageData , PageDataPayload } from '../shared'
3
4
import { notFoundPageData , treatAsHtml } from '../shared'
4
- import type { PageData , PageDataPayload , Awaitable } from '../shared'
5
- import { inBrowser , withBase } from './utils'
6
5
import { siteDataRef } from './data'
6
+ import { getScrollOffset , inBrowser , withBase } from './utils'
7
7
8
8
export interface Route {
9
9
path : string
@@ -261,34 +261,14 @@ export function scrollTo(el: Element, hash: string, smooth = false) {
261
261
}
262
262
263
263
if ( target ) {
264
- let scrollOffset = siteDataRef . value . scrollOffset
265
- let offset = 0
266
- let padding = 24
267
- if ( typeof scrollOffset === 'object' && 'padding' in scrollOffset ) {
268
- padding = scrollOffset . padding
269
- scrollOffset = scrollOffset . selector
270
- }
271
- if ( typeof scrollOffset === 'number' ) {
272
- offset = scrollOffset
273
- } else if ( typeof scrollOffset === 'string' ) {
274
- offset = tryOffsetSelector ( scrollOffset , padding )
275
- } else if ( Array . isArray ( scrollOffset ) ) {
276
- for ( const selector of scrollOffset ) {
277
- const res = tryOffsetSelector ( selector , padding )
278
- if ( res ) {
279
- offset = res
280
- break
281
- }
282
- }
283
- }
284
264
const targetPadding = parseInt (
285
265
window . getComputedStyle ( target ) . paddingTop ,
286
266
10
287
267
)
288
268
const targetTop =
289
269
window . scrollY +
290
270
target . getBoundingClientRect ( ) . top -
291
- offset +
271
+ getScrollOffset ( ) +
292
272
targetPadding
293
273
function scrollToTarget ( ) {
294
274
// only smooth scroll if distance is smaller than screen height.
@@ -300,14 +280,6 @@ export function scrollTo(el: Element, hash: string, smooth = false) {
300
280
}
301
281
}
302
282
303
- function tryOffsetSelector ( selector : string , padding : number ) : number {
304
- const el = document . querySelector ( selector )
305
- if ( ! el ) return 0
306
- const bot = el . getBoundingClientRect ( ) . bottom
307
- if ( bot < 0 ) return 0
308
- return bot + padding
309
- }
310
-
311
283
function handleHMR ( route : Route ) : void {
312
284
// update route.data on HMR updates of active page
313
285
if ( import . meta. hot ) {
0 commit comments