Use options parameters with types and default on smoothScroll function

This commit is contained in:
2022-07-19 13:43:25 +02:00
parent 3ba5a100c8
commit 6a66cdaf22
5 changed files with 15 additions and 9 deletions

View File

@@ -36,7 +36,7 @@
size="xsmall" size="xsmall"
url="/shop/poster-{location.slug}" url="/shop/poster-{location.slug}"
text="View" text="View"
on:click={() => setTimeout(() => smoothScroll('poster', false), 1000)} on:click={() => setTimeout(() => smoothScroll({ hash: 'poster', changeHash: false }), 1000)}
/> />
<Button <Button
tag="button" tag="button"

View File

@@ -24,7 +24,7 @@
const newPath = `/shop/poster-${value}` const newPath = `/shop/poster-${value}`
goto(newPath, { replaceState: true, noscroll: true, keepfocus: true }) goto(newPath, { replaceState: true, noscroll: true, keepfocus: true })
// Scroll to anchor // Scroll to anchor
setTimeout(() => smoothScroll('poster'), 1000) setTimeout(() => smoothScroll({ hash: 'poster' }), 1000)
} }
</script> </script>

View File

@@ -107,7 +107,7 @@
<ul> <ul>
{#each shopLocations as { name, slug }} {#each shopLocations as { name, slug }}
<li class:is-active={product && slug === product.location.slug}> <li class:is-active={product && slug === product.location.slug}>
<a href="/shop/poster-{slug}" on:click={() => smoothScroll('poster')} sveltekit:prefetch sveltekit:noscroll> <a href="/shop/poster-{slug}" on:click={() => smoothScroll({ hash: 'poster' })} sveltekit:prefetch sveltekit:noscroll>
{name} {name}
</a> </a>
</li> </li>

View File

@@ -95,7 +95,7 @@
{settings.description} {settings.description}
</p> </p>
<Button url="#locations" text="Explore locations" on:click={() => smoothScroll('locations')}> <Button url="#locations" text="Explore locations" on:click={event => smoothScroll({ hash: 'locations', event })}>
<IconEarth animate={true} /> <IconEarth animate={true} />
</Button> </Button>
</div> </div>

View File

@@ -158,7 +158,7 @@ export const scrollToTop = (delay?: number) => {
/** /**
* Smooth Scroll to an element * Smooth Scroll to an element
* @description Promised based * @description Promised based
* @url https://www.youtube.com/watch?v=oUSvlrDTLi4 * @url Based on: https://www.youtube.com/watch?v=oUSvlrDTLi4
*/ */
const smoothScrollPromise = (target: HTMLElement, duration: number = 1600): Promise<void> => { const smoothScrollPromise = (target: HTMLElement, duration: number = 1600): Promise<void> => {
const position = target.getBoundingClientRect().top + 1 const position = target.getBoundingClientRect().top + 1
@@ -190,13 +190,19 @@ const smoothScrollPromise = (target: HTMLElement, duration: number = 1600): Prom
requestAnimationFrame(animation) requestAnimationFrame(animation)
}) })
} }
export const smoothScroll = async (hash: string, changeHash: boolean = true, callback?: Function) => { export const smoothScroll = async ({ hash, callback, changeHash = true, event }: smoothScrollOptions) => {
if (event) event.preventDefault()
const target = document.getElementById(hash) const target = document.getElementById(hash)
smoothScrollPromise(target).then(() => { smoothScrollPromise(target).then(() => {
if (changeHash) { if (changeHash) location.hash = hash
location.hash = hash
}
callback && callback() callback && callback()
}) })
}
type smoothScrollOptions = {
hash: string
changeHash?: boolean
event?: MouseEvent
callback?: Function
} }