Use options parameters with types and default on smoothScroll function
This commit is contained in:
@@ -158,7 +158,7 @@ export const scrollToTop = (delay?: number) => {
|
||||
/**
|
||||
* Smooth Scroll to an element
|
||||
* @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 position = target.getBoundingClientRect().top + 1
|
||||
@@ -190,13 +190,19 @@ const smoothScrollPromise = (target: HTMLElement, duration: number = 1600): Prom
|
||||
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)
|
||||
|
||||
smoothScrollPromise(target).then(() => {
|
||||
if (changeHash) {
|
||||
location.hash = hash
|
||||
}
|
||||
if (changeHash) location.hash = hash
|
||||
callback && callback()
|
||||
})
|
||||
}
|
||||
type smoothScrollOptions = {
|
||||
hash: string
|
||||
changeHash?: boolean
|
||||
event?: MouseEvent
|
||||
callback?: Function
|
||||
}
|
||||
Reference in New Issue
Block a user