Skip to content

Commit

Permalink
Add "scroll into view" behavior to selected tabs.
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniGuardiola committed Aug 8, 2024
1 parent 2dda47d commit 9979d58
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions packages/components/src/tabs/tablist.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@ export const TabList = forwardRef<
const context = useTabsContext();

const selectedId = context?.store.useState( 'selectedId' );
const indicatorPosition = useTrackElementOffsetRect(
context?.store.item( selectedId )?.element
);
const selectedElement = context?.store.item( selectedId )?.element;
const indicatorPosition = useTrackElementOffsetRect( selectedElement );

const [ animationEnabled, setAnimationEnabled ] = useState( false );
useOnValueUpdate(
selectedId,
( { previousValue } ) => previousValue && setAnimationEnabled( true )
);
useOnValueUpdate( selectedId, ( { previousValue } ) => {
selectedElement?.scrollIntoView( { behavior: 'instant' } );
if ( previousValue ) {
setAnimationEnabled( true );
}
} );

if ( ! context ) {
warning( '`Tabs.TabList` must be wrapped in a `Tabs` component.' );
Expand Down

0 comments on commit 9979d58

Please sign in to comment.