@@ -11,24 +11,23 @@ const SETTLE_MAX_FRAMES = 60;
1111/**
1212 * Index and end scrolling start from estimated row sizes, so they can stop
1313 * short of a tall row. Corrects against the DOM until the viewport holds the
14- * target for two layout frames, or the frame budget runs out; resolves false
15- * when cancelled or the target disappears.
14+ * target for two layout frames, the frame budget runs out, the caller cancels,
15+ * or the target disappears.
1616 */
1717export function settleListScroll (
1818 list : Pick < LegendListRef , "getScrollableNode" | "scrollToOffset" > ,
1919 target : ( element : HTMLElement ) => number | undefined ,
2020 isCancelled : ( ) => boolean ,
21- ) : Promise < boolean > {
21+ ) : Promise < void > {
2222 return new Promise ( ( resolve ) => {
2323 let stableFrames = 0 ;
2424 let frames = 0 ;
2525 const reconcile = ( ) => {
26- if ( isCancelled ( ) ) return resolve ( false ) ;
26+ if ( isCancelled ( ) ) return resolve ( ) ;
2727 const element = list . getScrollableNode ( ) ;
2828 const offset = element ? target ( element ) : undefined ;
29- if ( ! element || offset === undefined ) return resolve ( false ) ;
3029 frames += 1 ;
31- if ( frames > SETTLE_MAX_FRAMES ) return resolve ( true ) ;
30+ if ( ! element || offset === undefined || frames > SETTLE_MAX_FRAMES ) return resolve ( ) ;
3231 if ( Math . abs ( element . scrollTop - offset ) > 1 ) {
3332 stableFrames = 0 ;
3433 void list . scrollToOffset ( { offset, animated : false } ) . then ( ( ) => {
@@ -37,7 +36,7 @@ export function settleListScroll(
3736 return ;
3837 }
3938 stableFrames += 1 ;
40- if ( stableFrames >= 2 ) resolve ( true ) ;
39+ if ( stableFrames >= 2 ) resolve ( ) ;
4140 else requestAnimationFrame ( reconcile ) ;
4241 } ;
4342 requestAnimationFrame ( reconcile ) ;
@@ -144,37 +143,31 @@ export function useTimelinePositionRestore({
144143 void Promise . resolve ( scrolling ) . then ( async ( ) => {
145144 if ( cancelled ) return ;
146145 if ( settlesAtEnd ) {
147- if ( await settleListScroll ( list , measuredEndOffset , ( ) => cancelled ) ) {
148- onPositioned ( listIdentityKey ) ;
149- }
150- return ;
151- }
152- if ( position ?. atEnd !== false || index < 0 ) {
153- onPositioned ( listIdentityKey ) ;
154- return ;
146+ await settleListScroll ( list , measuredEndOffset , ( ) => cancelled ) ;
147+ } else if ( position ?. atEnd === false && index >= 0 ) {
148+ // Keep the saved row mounted until its measured position and the DOM agree.
149+ await settleListScroll (
150+ list ,
151+ ( element ) => {
152+ const state = list . getState ( ) ;
153+ const rowIndex = state . indexByKey ( position . rowId ) ;
154+ const row = rowIndex === undefined ? undefined : state . elementAtIndex ( rowIndex ) ;
155+ if ( ! row ) return undefined ;
156+ return Math . max (
157+ 0 ,
158+ Math . min (
159+ element . scrollTop +
160+ row . getBoundingClientRect ( ) . top -
161+ element . getBoundingClientRect ( ) . top +
162+ position . offsetWithinRow ,
163+ measuredEndOffset ( element ) ,
164+ ) ,
165+ ) ;
166+ } ,
167+ ( ) => cancelled ,
168+ ) ;
155169 }
156- // Keep the saved row mounted until its measured position and the DOM agree.
157- const settled = await settleListScroll (
158- list ,
159- ( element ) => {
160- const state = list . getState ( ) ;
161- const rowIndex = state . indexByKey ( position . rowId ) ;
162- const row = rowIndex === undefined ? undefined : state . elementAtIndex ( rowIndex ) ;
163- if ( ! row ) return undefined ;
164- return Math . max (
165- 0 ,
166- Math . min (
167- element . scrollTop +
168- row . getBoundingClientRect ( ) . top -
169- element . getBoundingClientRect ( ) . top +
170- position . offsetWithinRow ,
171- measuredEndOffset ( element ) ,
172- ) ,
173- ) ;
174- } ,
175- ( ) => cancelled ,
176- ) ;
177- if ( settled ) onPositioned ( listIdentityKey ) ;
170+ if ( ! cancelled ) onPositioned ( listIdentityKey ) ;
178171 } ) ;
179172 return ( ) => {
180173 cancelled = true ;
0 commit comments