@@ -321,18 +321,9 @@ export class PrompterViewContent extends React.Component<Translated<IProps & ITr
321321 const target = document . querySelector < HTMLElement > ( `[data-part-instance-id="${ partInstanceId } "]` )
322322
323323 if ( ! target ) return
324- const offsetTop = window . scrollY + target . offsetTop
325- this . _lastAnimation ?. stop ( )
326- this . _lastAnimation = animate (
327- window ,
328- {
329- scrollY : offsetTop + - 1 * scrollMargin ,
330- } ,
331- {
332- duration : 0.4 ,
333- ease : 'easeOut' ,
334- }
335- )
324+
325+ const targetOffsetTop = target . getBoundingClientRect ( ) . top + window . scrollY
326+ this . animateScrollTo ( targetOffsetTop - scrollMargin )
336327 }
337328 scrollToLive ( ) : void {
338329 const scrollMargin = this . calculateScrollPosition ( )
@@ -341,37 +332,17 @@ export class PrompterViewContent extends React.Component<Translated<IProps & ITr
341332
342333 if ( ! current ) return
343334
344- const offsetTop = window . scrollY + current . offsetTop
345- this . _lastAnimation ?. stop ( )
346- this . _lastAnimation = animate (
347- window ,
348- {
349- scrollY : offsetTop + - 1 * scrollMargin ,
350- } ,
351- {
352- duration : 0.4 ,
353- ease : 'easeOut' ,
354- }
355- )
335+ const targetOffsetTop = current . getBoundingClientRect ( ) . top + window . scrollY
336+ this . animateScrollTo ( targetOffsetTop - scrollMargin )
356337 }
357338 scrollToNext ( ) : void {
358339 const scrollMargin = this . calculateScrollPosition ( )
359340 const next = document . querySelector < HTMLElement > ( '.prompter .next' )
360341
361342 if ( ! next ) return
362343
363- const offsetTop = window . scrollY + next . offsetTop
364- this . _lastAnimation ?. stop ( )
365- this . _lastAnimation = animate (
366- window ,
367- {
368- scrollY : offsetTop + - 1 * scrollMargin ,
369- } ,
370- {
371- duration : 0.4 ,
372- ease : 'easeOut' ,
373- }
374- )
344+ const targetOffsetTop = next . getBoundingClientRect ( ) . top + window . scrollY
345+ this . animateScrollTo ( targetOffsetTop - scrollMargin )
375346 }
376347 scrollToPrevious ( ) : void {
377348 const scrollMargin = this . calculateScrollPosition ( )
@@ -380,18 +351,8 @@ export class PrompterViewContent extends React.Component<Translated<IProps & ITr
380351 const target = anchors [ anchors . length - 2 ] || anchors [ 0 ]
381352 if ( ! target ) return
382353
383- const offsetTop = window . scrollY + target [ 0 ]
384- this . _lastAnimation ?. stop ( )
385- this . _lastAnimation = animate (
386- window ,
387- {
388- scrollY : offsetTop + - 1 * scrollMargin ,
389- } ,
390- {
391- duration : 0.4 ,
392- ease : 'easeOut' ,
393- }
394- )
354+ const targetOffsetTop = target [ 0 ] + window . scrollY
355+ this . animateScrollTo ( targetOffsetTop - scrollMargin )
395356 }
396357 scrollToFollowing ( ) : void {
397358 const scrollMargin = this . calculateScrollPosition ( )
@@ -400,18 +361,16 @@ export class PrompterViewContent extends React.Component<Translated<IProps & ITr
400361 const target = anchors [ 0 ]
401362 if ( ! target ) return
402363
403- const offsetTop = window . scrollY + target [ 0 ]
364+ const targetOffsetTop = target [ 0 ] + window . scrollY
365+ this . animateScrollTo ( targetOffsetTop - scrollMargin )
366+ }
367+ private animateScrollTo ( scrollToPosition : number ) {
404368 this . _lastAnimation ?. stop ( )
405- this . _lastAnimation = animate (
406- window ,
407- {
408- scrollY : offsetTop + - 1 * scrollMargin ,
409- } ,
410- {
411- duration : 0.4 ,
412- ease : 'easeOut' ,
413- }
414- )
369+ this . _lastAnimation = animate ( window . scrollY , scrollToPosition , {
370+ duration : 0.4 ,
371+ ease : 'easeOut' ,
372+ onUpdate : ( latest : number ) => window . scrollTo ( 0 , latest ) ,
373+ } )
415374 }
416375 listAnchorPositions ( startY : number , endY : number , sortDirection = 1 ) : [ number , Element ] [ ] {
417376 let foundPositions : [ number , Element ] [ ] = [ ]
0 commit comments