Skip to content

Commit

Permalink
[TextField] Fix unstable height of memoized multiline TextField compo…
Browse files Browse the repository at this point in the history
…nent (mui#37135)
  • Loading branch information
amal-qb authored and mnajdova committed Sep 8, 2023
1 parent 9b39a55 commit 9e5ad94
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions packages/mui-base/src/TextareaAutosize/TextareaAutosize.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,18 @@ const TextareaAutosize = React.forwardRef(function TextareaAutosize(
};

React.useEffect(() => {
const handleResize = debounce(() => {
const handleResize = () => {
renders.current = 0;

// If the TextareaAutosize component is replaced by Suspense with a fallback, the last
// ResizeObserver's handler that runs because of the change in the layout is trying to
// access a dom node that is no longer there (as the fallback component is being shown instead).
// See https://github.com/mui/material-ui/issues/32640
if (inputRef.current) {
syncHeightWithFlushSync();
}
};
const handleResizeWindow = debounce(() => {
renders.current = 0;

// If the TextareaAutosize component is replaced by Suspense with a fallback, the last
Expand All @@ -202,16 +213,16 @@ const TextareaAutosize = React.forwardRef(function TextareaAutosize(
const input = inputRef.current!;
const containerWindow = ownerWindow(input);

containerWindow.addEventListener('resize', handleResize);
containerWindow.addEventListener('resize', handleResizeWindow);

if (typeof ResizeObserver !== 'undefined') {
resizeObserver = new ResizeObserver(handleResize);
resizeObserver.observe(input);
}

return () => {
handleResize.clear();
containerWindow.removeEventListener('resize', handleResize);
handleResizeWindow.clear();
containerWindow.removeEventListener('resize', handleResizeWindow);
if (resizeObserver) {
resizeObserver.disconnect();
}
Expand Down

0 comments on commit 9e5ad94

Please sign in to comment.