-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
145 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,68 @@ | ||
function getScrollBottom(target: HTMLElement) { | ||
const { height: rectHeight } = target.getBoundingClientRect(); | ||
const scrollHeight = target.scrollHeight + (Math.floor(rectHeight) - rectHeight); | ||
return Math.max(0, scrollHeight - (rectHeight + target.scrollTop)); | ||
const { height: containerHeight } = target.getBoundingClientRect(); | ||
const scrollHeight = target.scrollHeight + (Math.floor(containerHeight) - containerHeight); | ||
return Math.max(0, scrollHeight - (containerHeight + target.scrollTop)); | ||
} | ||
|
||
export function bottomScroller(target: HTMLElement | null) { | ||
if (!target) return () => undefined; | ||
|
||
export function bottomScroller(target: HTMLElement) { | ||
let preventScrollHandling = false; | ||
let scrollBottomSaved = getScrollBottom(target); | ||
let preventScrollComputation = false; | ||
let timeoutHandler: NodeJS.Timer; | ||
|
||
function handleWheel() { | ||
scrollBottomSaved = getScrollBottom(target); | ||
} | ||
|
||
function handleScroll() { | ||
if (!target) return; | ||
if (!preventScrollComputation) scrollBottomSaved = getScrollBottom(target); | ||
if (target.scrollTop <= 1) target.scrollTop = 1; | ||
if (preventScrollHandling) { | ||
preventScrollHandling = false; | ||
} else { | ||
scrollBottomSaved = getScrollBottom(target); | ||
} | ||
} | ||
|
||
function handleResize() { | ||
if (!target) return; | ||
preventScrollComputation = true; | ||
const { height: rectHeight } = target.getBoundingClientRect(); | ||
const scrollHeight = target.scrollHeight; | ||
target.scrollTop = scrollHeight - (rectHeight + scrollBottomSaved) + (scrollBottomSaved <= 1 ? 1 : 0); | ||
clearTimeout(timeoutHandler); | ||
timeoutHandler = setTimeout(() => { | ||
preventScrollComputation = false; | ||
}, 100); | ||
preventScrollHandling = true; | ||
const { height: containerHeight } = target.getBoundingClientRect(); | ||
const scrollHeight = target.scrollHeight + (Math.floor(containerHeight) - containerHeight); | ||
target.scrollTop = scrollHeight - (containerHeight + scrollBottomSaved) + (scrollBottomSaved <= 1 ? 1 : 0); // This will trigger a scroll event | ||
} | ||
|
||
target.addEventListener("scroll", handleScroll); | ||
|
||
const ro = new ResizeObserver((entries) => entries.forEach(handleResize)); | ||
ro.observe(target); | ||
|
||
target.addEventListener("scroll", handleScroll); | ||
target.addEventListener("wheel", handleWheel); | ||
|
||
return () => { | ||
target.removeEventListener("scroll", handleScroll); | ||
ro.unobserve(target); | ||
|
||
target.removeEventListener("scroll", handleScroll); | ||
target.removeEventListener("wheel", handleWheel); | ||
}; | ||
} | ||
|
||
export function autoScrollWhenScrollable(target: HTMLElement) { | ||
const targetChild = target.children[0] as HTMLElement; | ||
|
||
let parentHeight = target.offsetHeight; | ||
let childHeight = targetChild.offsetHeight; | ||
let scrollable = childHeight > parentHeight; | ||
|
||
function handleResize() { | ||
parentHeight = target.offsetHeight; | ||
childHeight = targetChild.offsetHeight; | ||
const nextScrollable = childHeight > parentHeight; | ||
if (nextScrollable && !scrollable) { | ||
target.scrollTop = 999999; | ||
} | ||
scrollable = nextScrollable; | ||
} | ||
|
||
const ro = new ResizeObserver((entries) => entries.forEach(handleResize)); | ||
ro.observe(targetChild); | ||
|
||
return () => { | ||
ro.unobserve(targetChild); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export const sleep = (ms: number, callback?: (cancelCallback: () => boolean) => void) => | ||
new Promise<void>((resolve, reject) => { | ||
let timeout: NodeJS.Timeout | undefined = setTimeout(() => { | ||
timeout = undefined; | ||
resolve(); | ||
}, ms); | ||
callback?.(() => { | ||
const timeoutSaved = timeout; | ||
timeout = undefined; | ||
reject(); | ||
if (timeoutSaved) { | ||
clearTimeout(timeoutSaved); | ||
} | ||
return !!timeoutSaved; | ||
}); | ||
}); |
File renamed without changes.
Oops, something went wrong.