Merged in refactor/LOY-495-hook-for-sidepeek-scrolling (pull request #3297)
refactor(LOY-495): create useSidePeekScrollToTop hook * refactor(LOY-495): create useSidePeekScrollToTop hook * fix(LOY-495): Read ref fresh each time Approved-by: Emma Zettervall
This commit is contained in:
@@ -29,11 +29,12 @@ export function useScrollToTop({
|
||||
const scrollTarget = refScrollable && targetElement ? targetElement : window
|
||||
|
||||
function handleScroll() {
|
||||
const currentElement = element ?? elementRef?.current
|
||||
let position = window.scrollY
|
||||
if (targetElement) {
|
||||
if (currentElement) {
|
||||
position = refScrollable
|
||||
? targetElement.scrollTop
|
||||
: targetElement.getBoundingClientRect().top * -1
|
||||
? currentElement.scrollTop
|
||||
: currentElement.getBoundingClientRect().top * -1
|
||||
}
|
||||
setShowBackToTop(position > threshold)
|
||||
}
|
||||
@@ -47,8 +48,9 @@ export function useScrollToTop({
|
||||
if (targetElement) {
|
||||
if (refScrollable) {
|
||||
targetElement.scrollTo({ top: 0, behavior: "smooth" })
|
||||
} else {
|
||||
window.scrollTo({ top: targetElement.offsetTop, behavior: "smooth" })
|
||||
}
|
||||
window.scrollTo({ top: targetElement.offsetTop, behavior: "smooth" })
|
||||
} else {
|
||||
window.scrollTo({ top: 0, behavior: "smooth" })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user