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

Doesn't update if made to change width without re-rendering #389

Open
Macil opened this issue Sep 26, 2023 · 2 comments
Open

Doesn't update if made to change width without re-rendering #389

Macil opened this issue Sep 26, 2023 · 2 comments

Comments

@Macil
Copy link

Macil commented Sep 26, 2023

Here's a codesandbox demo:

https://codesandbox.io/s/react-textarea-autosize-bug-2hc6yv

If you press the "Toggle sidebar" button, then the textarea gets squished by the expanding sidebar and fails to become taller to account for the further-wrapped text.

A fix would be to use ResizeObserver specifically to track changes to the element's width. ResizeObservers have had support on all popular browsers since 2020.

The window resize listener at

useWindowResizeListener(resizeTextarea);
would be unnecessary if a useLayoutEffect call like this was present there instead which set up the ResizeObserver:

React.useLayoutEffect(() => {
  // monitor for changes to width
  if (globalThis.ResizeObserver) {
    let knownWidth = libRef.current.clientWidth;
    const ro = new ResizeObserver((entries) => {
      const newWidth = entries[entries.length-1].contentRect.width;
      if (newWidth !== knownWidth) {
        knownWidth = newWidth;
        resizeTextarea();
      }
    });
    ro.observe(libRef.current);
    return () => {
      ro.disconnect();
    };
  }
}, []);

Additionally, even though issue #337 isn't really this library's fault, using a ResizeObserver would work around that issue as it would get triggered as soon as the textarea is inserted into the document.

@shakti97
Copy link

@Andarist I am also facing a similar issue; once its width changes, it doesn't adjust the rows accordingly. When can we expect this to be fixed, or would you like me to raise a pull request to address it?

@Andarist
Copy link
Owner

A PR is always appreciated but I can't promise that I will actually have time to review this. The fact that we don't quite have automated tests here doesn't help 😢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants