Skip to content

Commit

Permalink
Components: Fix React Compiler error for 'useScrollRectIntoView' (#66498
Browse files Browse the repository at this point in the history
)

* Components: Fix React Compiler error for 'useScrollRectIntoView'
* Use optional chaining and add inline comment
* Use single scroll call

Co-authored-by: Mamaduka <[email protected]>
Co-authored-by: tyxla <[email protected]>
Co-authored-by: ciampo <[email protected]>
  • Loading branch information
4 people authored Nov 4, 2024
1 parent 5445afc commit b992649
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions packages/components/src/tabs/tablist.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,21 @@ function useScrollRectIntoView(
const childRightEdge = childLeft + childWidth;
const rightOverflow = childRightEdge + margin - parentRightEdge;
const leftOverflow = parentScroll - ( childLeft - margin );

let scrollLeft = null;
if ( leftOverflow > 0 ) {
parent.scrollLeft = parentScroll - leftOverflow;
scrollLeft = parentScroll - leftOverflow;
} else if ( rightOverflow > 0 ) {
parent.scrollLeft = parentScroll + rightOverflow;
scrollLeft = parentScroll + rightOverflow;
}

if ( scrollLeft !== null ) {
/**
* The optional chaining is used here to avoid unit test failures.
* It can be removed when JSDOM supports `Element` scroll methods.
* See: https://github.com/WordPress/gutenberg/pull/66498#issuecomment-2441146096
*/
parent.scroll?.( { left: scrollLeft } );
}
}, [ margin, parent, rect ] );
}
Expand Down

0 comments on commit b992649

Please sign in to comment.