@@ -5,18 +5,18 @@ import { useEffect, useRef, useCallback } from 'react';
55 */
66type ScrollOptions = {
77 /**
8- * Only scroll if the user is already near the top of the pane (avoids yanking them
8+ * Only scroll if the user is already near the bottom of the pane (avoids yanking them
99 * away from where they were reading).
1010 */
11- onlyIfNearTop ?: boolean ;
11+ onlyIfNearBottom ?: boolean ;
1212} ;
1313
1414export function useAutoScrollOnTaskSwitch ( isActive : boolean , taskId : string | null ) {
1515 const previousTaskIdRef = useRef < string | null > ( null ) ;
1616 const scrollTimeoutRef = useRef < NodeJS . Timeout | null > ( null ) ;
1717
1818 const scrollToBottom = useCallback ( ( options : ScrollOptions = { } ) => {
19- const { onlyIfNearTop = true } = options ;
19+ const { onlyIfNearBottom = true } = options ;
2020
2121 // Restrict to terminal panes so we don't accidentally scroll unrelated panels.
2222 const selectors = [
@@ -35,10 +35,12 @@ export function useAutoScrollOnTaskSwitch(isActive: boolean, taskId: string | nu
3535 container . getClientRects ( ) . length > 0 &&
3636 container . clientHeight > 0 ;
3737 const hasScrollableContent = container . scrollHeight > container . clientHeight ;
38- const nearTop = container . scrollTop <= 32 ;
38+ const distanceFromBottom =
39+ container . scrollHeight - ( container . scrollTop + container . clientHeight ) ;
40+ const nearBottom = distanceFromBottom <= 32 ;
3941
4042 if ( ! isVisible || ! hasScrollableContent ) return ;
41- if ( onlyIfNearTop && ! nearTop ) return ;
43+ if ( onlyIfNearBottom && ! nearBottom ) return ;
4244
4345 container . scrollTo ( {
4446 top : container . scrollHeight ,
@@ -69,7 +71,7 @@ export function useAutoScrollOnTaskSwitch(isActive: boolean, taskId: string | nu
6971
7072 // Delay scroll to allow content to render
7173 scrollTimeoutRef . current = setTimeout ( ( ) => {
72- scrollToBottom ( { onlyIfNearTop : false } ) ;
74+ scrollToBottom ( { onlyIfNearBottom : false } ) ;
7375 } , 200 ) ;
7476 }
7577
0 commit comments