Skip to content

Commit

Permalink
fix(tablistvertical): add scrollintoview
Browse files Browse the repository at this point in the history
  • Loading branch information
ariellalgilmore committed Jun 6, 2024
1 parent f0e9267 commit dbda7c7
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions packages/react/src/components/Tabs/Tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,12 @@ export interface TabListVerticalProps extends DivAttributes {
* Specify an optional className to be added to the container node
*/
className?: string;

/**
* Choose whether to automatically scroll to newly selected tabs
* on component rerender
*/
scrollIntoView?: boolean;
}
// type TabElement = HTMLElement & { disabled?: boolean };

Expand All @@ -745,6 +751,7 @@ function TabListVertical({
'aria-label': label,
children,
className: customClassName,
scrollIntoView,
...rest
}: TabListVerticalProps) {
const { activeIndex, selectedIndex, setSelectedIndex, setActiveIndex } =
Expand Down Expand Up @@ -804,6 +811,25 @@ function TabListVertical({
}
});

useEffect(() => {
function handler() {
const tab = tabs.current[selectedIndex];
if (scrollIntoView && tab) {
tab.scrollIntoView({
block: 'nearest',
inline: 'nearest',
});
}
}
window.addEventListener('resize', handler);

handler();

return () => {
window.removeEventListener('resize', handler);
};
}, [selectedIndex, scrollIntoView]);

useEffect(() => {
const element = ref.current;
if (!element) {
Expand Down

0 comments on commit dbda7c7

Please sign in to comment.