Skip to content

Commit

Permalink
Clean up scroll utility
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniGuardiola committed Oct 4, 2024
1 parent 84fda4b commit d174e38
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions packages/components/src/tabs/tablist.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,17 @@ import { useTrackElementOffsetRect } from '../utils/element-rect';
import { useTrackOverflow } from './use-track-overflow';
import { useAnimatedOffsetRect } from '../utils/hooks/use-animated-offset-rect';

const SCROLL_MARGIN = 24;
const DEFAULT_SCROLL_MARGIN = 24;

function useScrollSubelementIntoView(
/**
* Scrolls a given parent element so that a given rect is visible.
*
* The scroll is updated initially and whenever the rect changes.
*/
function useScrollRectIntoView(
parent: HTMLElement | null | undefined,
rect: ElementOffsetRect
rect: ElementOffsetRect,
{ margin = DEFAULT_SCROLL_MARGIN } = {}
) {
useLayoutEffect( () => {
if ( ! parent || ! rect ) {
Expand All @@ -41,14 +47,14 @@ function useScrollSubelementIntoView(

const parentRightEdge = parentScroll + parentWidth;
const childRightEdge = childLeft + childWidth;
const rightOverflow = childRightEdge + SCROLL_MARGIN - parentRightEdge;
const leftOverflow = parentScroll - ( childLeft - SCROLL_MARGIN );
const rightOverflow = childRightEdge + margin - parentRightEdge;
const leftOverflow = parentScroll - ( childLeft - margin );
if ( leftOverflow > 0 ) {
parent.scrollLeft = parentScroll - leftOverflow;
} else if ( rightOverflow > 0 ) {
parent.scrollLeft = parentScroll + rightOverflow;
}
}, [ parent, rect ] );
}, [ margin, parent, rect ] );
}

export const TabList = forwardRef<
Expand Down Expand Up @@ -81,7 +87,7 @@ export const TabList = forwardRef<
} );

// Make sure selected tab is scrolled into view.
useScrollSubelementIntoView( parent, selectedRect );
useScrollRectIntoView( parent, selectedRect );

const onBlur = () => {
if ( ! selectOnMove ) {
Expand Down

0 comments on commit d174e38

Please sign in to comment.