@@ -2568,15 +2568,25 @@ function useThreadFeedPresentation(
25682568}
25692569
25702570type DisclosureLayout = {
2571+ /** Each row's top relative to the anchor row's top, before the toggle. */
25712572 positions : ReadonlyMap < string , number > ;
25722573 anchorIndex : number ;
2574+ /** Distance from the anchor's top to the end of the content, before the toggle. */
2575+ anchorToEnd : number ;
25732576 list : LegendListRef ;
25742577 playing : boolean ;
25752578} ;
25762579const DISCLOSURE_DETAIL_DELAY_MS = 135 ;
25772580const DISCLOSURE_DETAIL_FADE_MS = 70 ;
25782581const unsubscribeNothing = ( ) => { } ;
25792582
2583+ function feedContentEnd ( state : ReturnType < LegendListRef [ "getState" ] > ) : number | undefined {
2584+ const last = state . data . length - 1 ;
2585+ if ( last < 0 ) return undefined ;
2586+ const position = state . positionAtIndex ( last ) ;
2587+ return position === undefined ? undefined : position + state . sizeAtIndex ( last ) ;
2588+ }
2589+
25802590function DisclosureRow ( props : {
25812591 itemKey : string ;
25822592 index : number ;
@@ -2587,18 +2597,35 @@ function DisclosureRow(props: {
25872597 const previous = layout ?. positions . get ( itemKey ) ;
25882598 const subscribe = useCallback (
25892599 ( notify : ( ) => void ) => {
2590- if ( ! layout || layout . playing || previous === undefined || previous <= 0 )
2591- return unsubscribeNothing ;
2592- return layout . list . getState ( ) . listenToPosition ( itemKey , notify ) ;
2600+ if ( ! layout || layout . playing || previous === undefined ) return unsubscribeNothing ;
2601+ const state = layout . list . getState ( ) ;
2602+ const stops = [
2603+ state . listenToPosition ( itemKey , notify ) ,
2604+ state . listen ( "totalSize" , notify ) ,
2605+ state . listen ( "isAtEnd" , notify ) ,
2606+ state . listen ( "isFollowingEnd" , notify ) ,
2607+ ] ;
2608+ return ( ) => {
2609+ for ( const stop of stops ) stop ( ) ;
2610+ } ;
25932611 } ,
25942612 [ itemKey , layout , previous ] ,
25952613 ) ;
2614+ // Rows hold their old screen spot relative to whatever stays still: the content end
2615+ // while the feed sits at the bottom (end follow, or a collapse that clamps the scroll),
2616+ // otherwise the anchored disclosure row.
25962617 const getOffset = ( ) => {
2597- if ( ! layout || layout . playing || previous === undefined || previous <= 0 ) return 0 ;
2618+ if ( ! layout || layout . playing || previous === undefined ) return 0 ;
25982619 const state = layout . list . getState ( ) ;
25992620 const position = state . positionAtIndex ( props . index ) ;
2621+ if ( position === undefined ) return 0 ;
2622+ if ( state . isFollowingEnd || state . isAtEnd ) {
2623+ const end = feedContentEnd ( state ) ;
2624+ return end === undefined ? 0 : previous - layout . anchorToEnd - ( position - end ) ;
2625+ }
2626+ if ( previous <= 0 ) return 0 ;
26002627 const anchor = state . positionAtIndex ( layout . anchorIndex ) ;
2601- return position === undefined || anchor === undefined ? 0 : previous - ( position - anchor ) ;
2628+ return anchor === undefined ? 0 : previous - ( position - anchor ) ;
26022629 } ;
26032630 const offset = useSyncExternalStore ( subscribe , getOffset , getOffset ) ;
26042631 const entering = ! ! layout && previous === undefined ;
@@ -3386,6 +3413,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
33863413 const motion = {
33873414 positions,
33883415 anchorIndex,
3416+ anchorToEnd : ( feedContentEnd ( state ) ?? anchor ) - anchor ,
33893417 list,
33903418 playing : false ,
33913419 } ;
0 commit comments