Last active
June 27, 2021 22:24
-
-
Save Izhaki/ea32f4d3813a27bd4e9d137aff7a71c1 to your computer and use it in GitHub Desktop.
Revisions
-
Izhaki revised this gist
Jun 27, 2021 . 1 changed file with 20 additions and 17 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -4,25 +4,28 @@ export default function useAutoScrollToBottom(deps) { const paused = React.useRef(false); const element = React.useRef<HTMLDivElement | null>(null); const onWheel = React.useCallback(() => { const { scrollTop, scrollHeight, offsetHeight } = element.current || { scrollTop: 0, scrollHeight: 0, offsetHeight: 0, }; paused.current = scrollHeight - (scrollTop + offsetHeight) > 4; }, []); const ref = React.useCallback( (el) => { if (el) { element.current = el; el.addEventListener('wheel', onWheel, { passive: true }); } else if (element.current !== null) { element.current.removeEventListener('wheel', onWheel); element.current = null; } }, [onWheel] ); React.useLayoutEffect(() => { if (element.current && !paused.current) { element.current.scrollTop = element.current.scrollHeight; -
Izhaki revised this gist
Jun 25, 2021 . 1 changed file with 4 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ import React from 'react'; export default function useAutoScrollToBottom(deps) { const paused = React.useRef(false); const element = React.useRef<HTMLDivElement | null>(null); const ref = React.useCallback((el) => { @@ -11,7 +11,7 @@ export default function useAutoScrollToBottom(deps) { scrollHeight: 0, offsetHeight: 0, }; paused.current = scrollHeight - (scrollTop + offsetHeight) > 4; } if (el) { @@ -24,9 +24,10 @@ export default function useAutoScrollToBottom(deps) { }, []); React.useLayoutEffect(() => { if (element.current && !paused.current) { element.current.scrollTop = element.current.scrollHeight; } // eslint-disable-next-line react-hooks/exhaustive-deps }, deps); return { ref }; -
Izhaki created this gist
Jun 25, 2021 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,33 @@ import React from 'react'; export default function useAutoScrollToBottom(deps) { const pause = React.useRef(false); const element = React.useRef<HTMLDivElement | null>(null); const ref = React.useCallback((el) => { function onWheel() { const { scrollTop, scrollHeight, offsetHeight } = element.current || { scrollTop: 0, scrollHeight: 0, offsetHeight: 0, }; pause.current = scrollHeight - (scrollTop + offsetHeight) > 4; } if (el) { element.current = el; el.addEventListener('wheel', onWheel, { passive: true }); } else if (element.current !== null) { element.current.removeEventListener('wheel', onWheel); element.current = null; } }, []); React.useLayoutEffect(() => { if (element.current && !pause.current) { element.current.scrollTop = element.current.scrollHeight; } }, deps); return { ref }; }