-
-
Notifications
You must be signed in to change notification settings - Fork 249
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
TextArea is wrong size initially (off by a hair), then it adjusts as you type? #337
Comments
Experiencing the same thing in our app, started happening recently. We haven't changed any styles or markup on that page nor updated the library, so it's either a browser change (happens in Chrome/FF/Safari though) or something new in React. |
Please always try to share a repro case in a runnable form - either by providing a git repository to clone or a codesandbox. OSS maintainers usually can't afford the time to set up the repro, even if exact steps are given. |
Just browsing by, not having a repro at hand either (sorry). But I can confirm a similar behavior: Randomly the initial height seems ~double of what it should be. When the textarea gets the focus, it immediately "snaps back" to the expected height. It also occured out of the blue a couple of weeks/months ago. |
I can confirm this behavior as well and I just installed the package without any sort of customization. |
The bug can be reproduced when I set the font size with classnames and in rem. |
In my case, the font size alone doesn't change the behavior. It's a combination of multiple styles applied to the element (cannot figure out a pattern yet, though). |
@Andarist could not reproduce it for simple scenarios, but was able to reproduce it with |
@raduflp that is... somewhat helpful but I also believe that this might be a different issue than the issue reported originally here. In this codesandbox, our My bet is that this code is related: It looks like doing exactly that - mounting to a different container and then attaching/detaching it manually from within an effect. The problem is that... effects are called from the bottom to the top so our effect gets called first and we can't measure what the height should be when the textarea isn't mounted in the DOM. |
Thanks @Andarist for the explanation. Makes sense, but I'm not sure what's the best way to go about fixing this long term, not too familiar with the internals of As a workaround now I have a thin wrapper that triggers a re-render on mount. That's less than ideal, but works for my use-cases.
|
Me neither 😉 In a way - this is a clash between the internals of those two libs. I don't plan to make any changes here to accommodate this use case - unless those would be really small changes. For instance, this could be solved with a MutationObserver but I don't plan on using it because it would complicate the codebase and add more code than I'm comfortable with. If anyone figures out how to improve this - I'm open to discussion but I won't certainly be looking for an answer myself. |
Have the same issue with thx @raduflp for the workaround 👍🏻 using it like this
|
The workaround works for me as well. In my setup the logic was as follows: <div className="advanced-search">
<AdvancedSearch onClick={onShowAdvanced} expanded={showAdvanced} />
{showAdvanced && (
<CustomTextarea label="Text" maxRows={5} />
)}
</div> where advanced search is a simple button toggling the "advanced section". Removing the conditional The re-render trick made it render the correct height. |
Not sure if it's the same bug, but I was having issues with the default height being off by 3px, but only on Chrome. I debugged it and it turns out that in Chrome (I couldn't test Safari rn) on the hidden text area the line height can differ from the line height of the actual text area. I can't figure out why, but explicitly setting a line height like |
IMO This is due to the internal |
17.02.2023 |
This is a good explanation. Here's some notes of mine about solving an equivalent issue before, that are more relevant to outside projects than this project: I've run into and fixed a similar issue before with a library I wrote (react-float-anchor) that functioned similar to that portal component. The issue was that an input with an This can be solved by using useInsertionEffect instead of useLayoutEffect in the portal component for attaching the portal element to the document. React runs the useInsertionEffect callbacks for parents before children have their mount/useLayoutEffects callbacks run, so it seems perfect for the use-case of attaching a portal element to the document before the children mount. (The React documentation does say it's mainly intended for CSS-in-JS systems, which have similar requirements to this use-case: they need to be able to inject styles before any of the children have done mount callbacks and potentially measured themselves, and they don't need to interact with state or component refs.) Older alternative solutionAn alternative solution, which I used in the current release version of react-float-anchor because it's still a class-based React component, is to make the portal-handling component render a dummy child component (before all props-provided children) which attaches the portal element to the document during its own mount callback. I do this here with the "LifecycleHelper" component: floatPortal = (
<FloatAnchorContext.Provider value={this._childContext}>
<LifecycleHelper onMount={this._mountPortalEl} />
{createPortal(float, portalEl)}
</FloatAnchorContext.Provider>
); where export default class LifecycleHelper extends React.Component {
componentDidMount() {
this.props.onMount();
}
render() {
return null;
}
} So now the portal being attached to the DOM happens during a child's mount callbacks instead of during the parent's mount callbacks, and it runs before the other children's mount callbacks because it's an earlier child than them. However, all that being said, I just discovered the not directly related bug #389, and it turns out the fix for it would happen to work around this issue in the separate portal library. |
I have a
<TextareaAutosize>
used with react-hook-form, and when it starts out, the textarea is slightly smaller than it should be. When I type, it fixes. What should I do to fix this? Is it the font that is throwing it off, or my CSS or something?Here is a gif.
Here is a snippet of my code from a larger project:
And usage like this:
The CSS for the file is here:
Any ideas?
The text was updated successfully, but these errors were encountered: