elk/plugins/scroll-to-top.ts

17 lines
507 B
TypeScript
Raw Normal View History

2023-01-07 17:29:36 +00:00
export default defineNuxtPlugin((nuxt) => {
2022-11-29 20:15:53 +00:00
return {
provide: {
2023-01-07 17:29:36 +00:00
scrollToTop: (evt?: MouseEvent | KeyboardEvent) => {
const path = evt?.composedPath?.() as HTMLElement[]
const el = path?.find(el => el.tagName?.toUpperCase() === 'A') as HTMLAnchorElement
2023-01-07 18:11:10 +00:00
if (el?.href) {
2023-01-07 17:29:36 +00:00
if (nuxt.$preventScrollToTop(new URL(el.href, import.meta.url).pathname))
return
}
window.scrollTo({ top: 0, left: 0, behavior: 'smooth' })
2022-11-29 20:15:53 +00:00
},
},
}
})