Skip to content

Commit

Permalink
[material][tab] Show/hide scroll buttons for dynamically added childr…
Browse files Browse the repository at this point in the history
…en (#39415)
  • Loading branch information
brijeshb42 authored Oct 17, 2023
1 parent b978924 commit 8937597
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions packages/mui-material/src/Tabs/Tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -575,10 +575,29 @@ const Tabs = React.forwardRef(function Tabs(inProps, ref) {
updateIndicatorState();
}
});

let resizeObserver;

/**
* @type {MutationCallback}
*/
const handleMutation = (records) => {
records.forEach((record) => {
record.removedNodes.forEach((item) => {
resizeObserver?.unobserve(item);
});
record.addedNodes.forEach((item) => {
resizeObserver?.observe(item);
});
});
handleResize();
updateScrollButtonState();
};

const win = ownerWindow(tabsRef.current);
win.addEventListener('resize', handleResize);

let resizeObserver;
let mutationObserver;

if (typeof ResizeObserver !== 'undefined') {
resizeObserver = new ResizeObserver(handleResize);
Expand All @@ -587,14 +606,20 @@ const Tabs = React.forwardRef(function Tabs(inProps, ref) {
});
}

if (typeof MutationObserver !== 'undefined') {
mutationObserver = new MutationObserver(handleMutation);
mutationObserver.observe(tabListRef.current, {
childList: true,
});
}

return () => {
handleResize.clear();
win.removeEventListener('resize', handleResize);
if (resizeObserver) {
resizeObserver.disconnect();
}
mutationObserver?.disconnect();
resizeObserver?.disconnect();
};
}, [updateIndicatorState]);
}, [updateIndicatorState, updateScrollButtonState]);

/**
* Toggle visibility of start and end scroll buttons
Expand Down

0 comments on commit 8937597

Please sign in to comment.