Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TextareaAutosize] Improve implementation #40789

Merged

Conversation

ZeeshanTamboli
Copy link
Member

@ZeeshanTamboli ZeeshanTamboli commented Jan 26, 2024

Fixes #40557

I was investigating #40557 and noticed related issues (#33081 and #20106). A React 18 compatibility issue was also addressed in #33253 using ReactDOM.flushSync. However, the problem persists in the latest version, as evident in #40557 and a PR attempt in #38453.


Problem:

The current implementation sets a re-render limit of 20 in order to prevent infinite rendering errors caused by React. However, this limit also disrupts the synchronization of height beyond 20 re-renders, leading to incorrect layout and the warning described in #40557. The primary questions to address are: Why is the infinite re-render occurring, and what is the root cause?

Potential Cause of Too Many Re-renders / infinite loop:

The issue arises from using React state to store height and overflow, which are then passed to the inline style prop of the textarea. The syncHeight function calculates the height, updates the state using setState, triggering a re-render. The new re-render causes another call to syncHeight and a subsequent state update. Even if there are no layout changes or the constraint is not passed, the new reference of the object returned by prevState causes repeated re-renders. Additionally, the absence of a dependency array in the useEffect for syncHeight triggers it on every component re-render, leading to an infinite loop.

Solution:

It's correct that we have to call syncHeight on every render. This PR proposes avoiding the use of React state and instead directly setting the calculated HTML textarea's height and overflow properties on the component DOM using a ref.


Before and After for #40557 can be viewed here: (Make sure to also test by clicking on Show/Hide button)


Closes #38453 (Stale PR)

@ZeeshanTamboli ZeeshanTamboli added bug 🐛 Something doesn't work performance component: TextareaAutosize The React component. labels Jan 26, 2024
@mui-bot
Copy link

mui-bot commented Jan 26, 2024

Netlify deploy preview

https://deploy-preview-40789--material-ui.netlify.app/

@material-ui/core: parsed: -0.06% 😍, gzip: -0.07% 😍
@material-ui/unstyled: parsed: -0.21% 😍, gzip: -0.25% 😍
@mui/material-next: parsed: -0.10% 😍, gzip: -0.11% 😍
@mui/joy: parsed: -0.07% 😍, gzip: -0.09% 😍

Bundle size report

Details of bundle changes (Toolpad)
Details of bundle changes

Generated by 🚫 dangerJS against 8490135

@danilo-leal danilo-leal added the package: base-ui Specific to @mui/base label Jan 26, 2024
Copy link
Member

@michaldudak michaldudak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to do the trick. Impressive!

};
}
if (process.env.NODE_ENV !== 'production') {
if (renders.current === 20) {
Copy link
Member

@oliviertassinari oliviertassinari Jan 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we still need a failsafe for unstable layouts (unrelated to React). This happens when the CSS values are unstable, this warning is also meant to invite developers to create issues so we can debug and fix the logic. Typically you would see the height flicker up and down as you type in the input. Or freeze the app because the resize observer logic run in a loop.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for unstable layouts (unrelated to React). This happens when the CSS values are unstable,

Can you provide reproductions of layout instability not related to React? I'm open to debugging it.

this warning is also meant to invite developers to create issues so we can debug and fix the logic

Sure, but since we're not relying on React state anymore, there's no need for warnings about excessive re-renders caused by state.

Typically you would see the height flicker up and down as you type in the input. Or freeze the app because the resize observer logic run in a loop.

Again, can you provide reproductions using this PR build?

@oliviertassinari
Copy link
Member

We need a regression test for #40557.

@ZeeshanTamboli
Copy link
Member Author

ZeeshanTamboli commented Jan 30, 2024

We need a regression test for #40557.

I don't see #40557 as a regression. Do you think the existing tests not cover it adequately? Also, the warning has been removed. Could you share your thought on what additional test case might be needed?

@ZeeshanTamboli
Copy link
Member Author

ZeeshanTamboli commented Feb 5, 2024

@oliviertassinari Could you review further?

@megatron-hoa-tran
Copy link

I was blocked by this issue
#38453
@oliviertassinari please help to review 🙏

@ZeeshanTamboli
Copy link
Member Author

ZeeshanTamboli commented Feb 12, 2024

Olivier hasn't responded yet (since past two weeks), but since Michał has approved it, I think I can merge it.

@ZeeshanTamboli ZeeshanTamboli merged commit a9026f1 into mui:master Feb 12, 2024
19 checks passed
@ZeeshanTamboli ZeeshanTamboli deleted the refactor-textareaautosize-and-fix-40557 branch February 12, 2024 13:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work component: TextareaAutosize The React component. package: base-ui Specific to @mui/base performance
Projects
None yet
6 participants