From f49243955f1afd25d71604c40ec01dd90da30904 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ya=C3=ABl=20GUILLOUX?= Date: Mon, 5 Dec 2022 17:59:52 +0100 Subject: [PATCH] feat(scroll-top): fix scroll top on page changes.. same fix as nuxt.com --- app.config.ts | 19 ++++++++++++------- app/router.options.ts | 18 +++++++++++++++++- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/app.config.ts b/app.config.ts index fea8a43c3..209ea8881 100644 --- a/app.config.ts +++ b/app.config.ts @@ -83,6 +83,12 @@ export default defineAppConfig({ } }) +interface IconLink { + href: string + icon: string + label?: string +} + declare module '@nuxt/schema' { interface AppConfigInput { docus?: { @@ -100,6 +106,7 @@ declare module '@nuxt/schema' { aside?: { level: number exclude?: string[] + collapsed?: boolean }, header?: { title?: string, @@ -108,15 +115,13 @@ declare module '@nuxt/schema' { exclude?: string[] }, footer?: { - credits?: boolean + credits?: boolean | { + icon: string + text: string + href: string + } iconLinks?: IconLink[] } } } } - -interface IconLink { - href: string - icon: string - label?: string -} diff --git a/app/router.options.ts b/app/router.options.ts index 626f5b312..9380f053a 100644 --- a/app/router.options.ts +++ b/app/router.options.ts @@ -1,7 +1,16 @@ import type { RouterConfig } from '@nuxt/schema' // https://router.vuejs.org/api/interfaces/routeroptions.html export default { - scrollBehavior (to) { + scrollBehavior (to, _form, savedPosition) { + if (history.state.stop) { return } + + if (history.state.smooth) { + return { + el: history.state.smooth, + behavior: 'smooth' + } + } + if (to.hash) { const el = document.querySelector(to.hash) as any @@ -18,5 +27,12 @@ export default { behavior: 'smooth' } } + + // Scroll to top of window + if (savedPosition) { + return savedPosition + } else { + return { top: 0 } + } } }