Fix Page transitions and scroll back to top
Might fix all the issues so far! - start page loading on entering page transition start - scroll back to top on leaving page transition end
This commit is contained in:
@@ -2,25 +2,32 @@
|
|||||||
import { page } from '$app/stores'
|
import { page } from '$app/stores'
|
||||||
import { afterUpdate } from 'svelte'
|
import { afterUpdate } from 'svelte'
|
||||||
import { fade } from 'svelte/transition'
|
import { fade } from 'svelte/transition'
|
||||||
import { pageLoading } from '$utils/stores'
|
|
||||||
import { scrollToTop } from '$utils/functions'
|
import { scrollToTop } from '$utils/functions'
|
||||||
import { DURATION } from '$utils/contants'
|
import { pageLoading } from '$utils/stores'
|
||||||
|
import { DELAY, DURATION } from '$utils/constants'
|
||||||
|
|
||||||
let loadingTimeout: ReturnType<typeof setTimeout> | number = null
|
let loadingTimeout: ReturnType<typeof setTimeout> | number = null
|
||||||
|
|
||||||
$: doNotScroll = !$page.url.searchParams.get('country') && !$page.url.pathname.includes('/shop/')
|
$: doNotScroll = !$page.url.searchParams.get('country') && !$page.url.pathname.includes('/shop/')
|
||||||
|
|
||||||
|
// Hide page loading indicator on page update
|
||||||
afterUpdate(() => {
|
afterUpdate(() => {
|
||||||
// Turn page loading on page mount
|
|
||||||
clearTimeout(loadingTimeout)
|
clearTimeout(loadingTimeout)
|
||||||
loadingTimeout = setTimeout(() => $pageLoading = false, DURATION.PAGE_IN)
|
loadingTimeout = setTimeout(() => $pageLoading = false, DURATION.PAGE_IN)
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="page"
|
<div class="page"
|
||||||
in:fade={{ duration: DURATION.PAGE_IN, delay: DURATION.PAGE_DELAY }}
|
in:fade={{ duration: DURATION.PAGE_IN, delay: DELAY.PAGE_LOADING }}
|
||||||
out:fade={{ duration: DURATION.PAGE_OUT }}
|
out:fade={{ duration: DURATION.PAGE_OUT }}
|
||||||
on:outroend={() => doNotScroll && scrollToTop()}
|
on:outrostart={() => {
|
||||||
|
// Show page loading indicator
|
||||||
|
$pageLoading = true
|
||||||
|
}}
|
||||||
|
on:outroend={() => {
|
||||||
|
// Scroll back to top
|
||||||
|
doNotScroll && requestAnimationFrame(() => scrollToTop())
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
<slot />
|
<slot />
|
||||||
</div>
|
</div>
|
||||||
@@ -2,13 +2,13 @@
|
|||||||
import '../style/global.scss'
|
import '../style/global.scss'
|
||||||
|
|
||||||
import { browser } from '$app/environment'
|
import { browser } from '$app/environment'
|
||||||
import { navigating, page } from '$app/stores'
|
import { page } from '$app/stores'
|
||||||
import { beforeNavigate } from '$app/navigation'
|
import { beforeNavigate } from '$app/navigation'
|
||||||
|
import { PUBLIC_ANALYTICS_KEY, PUBLIC_ANALYTICS_URL } from '$env/static/public'
|
||||||
import type { PageData } from './$types'
|
import type { PageData } from './$types'
|
||||||
import { onMount, setContext } from 'svelte'
|
import { onMount, setContext } from 'svelte'
|
||||||
import { pageLoading, previousPage } from '$utils/stores'
|
import { pageLoading, previousPage } from '$utils/stores'
|
||||||
import '$utils/polyfills'
|
import '$utils/polyfills'
|
||||||
import { PUBLIC_ANALYTICS_KEY, PUBLIC_ANALYTICS_URL } from '$env/static/public'
|
|
||||||
// Components
|
// Components
|
||||||
import SVGSprite from '$components/SVGSprite.svelte'
|
import SVGSprite from '$components/SVGSprite.svelte'
|
||||||
import SmoothScroll from '$components/SmoothScroll.svelte'
|
import SmoothScroll from '$components/SmoothScroll.svelte'
|
||||||
@@ -43,10 +43,6 @@
|
|||||||
$previousPage = from.url.pathname
|
$previousPage = from.url.pathname
|
||||||
})
|
})
|
||||||
|
|
||||||
// Define page loading from navigating store
|
|
||||||
navigating.subscribe((store: any) => {
|
|
||||||
store && ($pageLoading = true)
|
|
||||||
})
|
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
// Avoid FOUC
|
// Avoid FOUC
|
||||||
|
|||||||
@@ -131,7 +131,7 @@ export const scrollToTop = (delay?: number) => {
|
|||||||
if (delay && delay > 0) {
|
if (delay && delay > 0) {
|
||||||
setTimeout(scroll, delay)
|
setTimeout(scroll, delay)
|
||||||
} else {
|
} else {
|
||||||
scroll()
|
return scroll()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user