Skip to content

Commit

Permalink
Update useScrollRequired logic (#20889)
Browse files Browse the repository at this point in the history
* Update `useScrollRequired` to only require to scroll to bottom one time

* remove console.log

* simplify logic

* add comment about magic number
  • Loading branch information
GuillaumeRx authored Sep 15, 2023
1 parent 4d5c2be commit f315a2b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
23 changes: 18 additions & 5 deletions ui/hooks/useScrollRequired.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,32 @@ export const useScrollRequired = (dependencies = []) => {
const [isScrolledToBottomState, setIsScrolledToBottom] = useState(false);

const update = () => {
if (!ref.current) {
return;
}

const isScrollable =
ref.current && ref.current.scrollHeight > ref.current.clientHeight;
const isScrolledToBottom = isScrollable
? Math.round(ref.current.scrollTop) + ref.current.offsetHeight >=
ref.current.scrollHeight
: true;

const isScrolledToBottom =
isScrollable &&
// Add 16px to the actual scroll position to trigger setIsScrolledToBottom sooner.
// This avoids the problem where a user has scrolled down to the bottom and it's not detected.
Math.round(ref.current.scrollTop) + ref.current.offsetHeight + 16 >=
ref.current.scrollHeight;

setIsScrollable(isScrollable);
setIsScrolledToBottom(isScrolledToBottom);

if (!isScrollable || isScrolledToBottom) {
setIsScrolledToBottom(true);
}
};

useEffect(update, [ref, ...dependencies]);

const scrollToBottom = () => {
setIsScrolledToBottom(true);

if (ref.current) {
ref.current.scrollTo(0, ref.current.scrollHeight);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export default function SnapInstall({
<SnapAuthorshipHeader snapId={targetSubjectMetadata.origin} />
)}
<Box
ref={ref}
ref={!isLoading && !hasError ? ref : undefined}
onScroll={onScroll}
className="snap-install__content"
style={{
Expand Down

0 comments on commit f315a2b

Please sign in to comment.