@@ -536,31 +536,7 @@ class Grid extends PureComponent<GridProps, GridState> {
536536
537537 this . requestUpdateCanvas ( ) ;
538538
539- if ( ! this . metrics || ! this . prevMetrics ) {
540- return ;
541- }
542-
543- const { rowCount, columnCount, height, width } = this . metrics ;
544- const {
545- rowCount : prevRowCount ,
546- columnCount : prevColumnCount ,
547- height : prevHeight ,
548- width : prevWidth ,
549- } = this . prevMetrics ;
550-
551- if ( prevRowCount !== rowCount || height !== prevHeight ) {
552- const { isStuckToBottom } = this . state ;
553- if ( isStuckToBottom ) {
554- this . scrollToBottom ( ) ;
555- }
556- }
557-
558- if ( prevColumnCount !== columnCount || width !== prevWidth ) {
559- const { isStuckToRight } = this . state ;
560- if ( isStuckToRight ) {
561- this . scrollToRight ( ) ;
562- }
563- }
539+ this . checkStickyScroll ( ) ;
564540
565541 if ( this . validateSelection ( ) ) {
566542 this . checkSelectionChange ( prevState ) ;
@@ -799,17 +775,23 @@ class Grid extends PureComponent<GridProps, GridState> {
799775 this . animationFrame = requestAnimationFrame ( ( ) => {
800776 this . animationFrame = null ;
801777
802- if ( ! this . metrics ) throw new Error ( 'Metrics not set' ) ;
803-
804- this . updateCanvas ( this . metrics ) ;
778+ this . updateCanvas ( ) ;
805779 } ) ;
806780 }
807781
808- updateCanvas ( metrics = this . updateMetrics ( ) ) : void {
782+ updateCanvas ( ) : void {
809783 this . updateCanvasScale ( ) ;
784+
785+ this . updateMetrics ( ) ;
786+
810787 this . updateRenderState ( ) ;
811- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
812- this . renderer . configureContext ( this . canvasContext ! , this . renderState ) ;
788+
789+ const { canvasContext, metrics, renderState } = this ;
790+ assertNotNull ( canvasContext ) ;
791+ assertNotNull ( metrics ) ;
792+ assertNotNull ( renderState ) ;
793+
794+ this . renderer . configureContext ( canvasContext , renderState ) ;
813795
814796 const { onViewChanged } = this . props ;
815797 onViewChanged ( metrics ) ;
@@ -850,6 +832,40 @@ class Grid extends PureComponent<GridProps, GridState> {
850832 }
851833 }
852834
835+ /**
836+ * Compares the current metrics with the previous metrics to see if we need to scroll when it is stuck to the bottom or the right
837+ */
838+ checkStickyScroll ( ) {
839+ if ( ! this . metrics ) {
840+ return ;
841+ }
842+
843+ if ( this . prevMetrics ) {
844+ const { rowCount, columnCount, height, width } = this . metrics ;
845+ const {
846+ rowCount : prevRowCount ,
847+ columnCount : prevColumnCount ,
848+ height : prevHeight ,
849+ width : prevWidth ,
850+ } = this . prevMetrics ;
851+
852+ if ( prevRowCount !== rowCount || height !== prevHeight ) {
853+ const { isStuckToBottom } = this . state ;
854+ if ( isStuckToBottom ) {
855+ this . scrollToBottom ( ) ;
856+ }
857+ }
858+
859+ if ( prevColumnCount !== columnCount || width !== prevWidth ) {
860+ const { isStuckToRight } = this . state ;
861+ if ( isStuckToRight ) {
862+ this . scrollToRight ( ) ;
863+ }
864+ }
865+ }
866+ this . prevMetrics = this . metrics ;
867+ }
868+
853869 updateMetrics ( state = this . state ) : GridMetrics {
854870 this . prevMetrics = this . metrics ;
855871
0 commit comments