material-ui icon indicating copy to clipboard operation
material-ui copied to clipboard

[TextField] Backspacing after line break in multiline causes height issues on Safari

Open xiaoxiaofan123 opened this issue 10 months ago • 7 comments

Steps to reproduce

Steps:

  1. Open this link to live example: https://codesandbox.io/embed/frpzyp?module=/src/Demo.tsx&fontsize=12(required) 2.Enter a line of English characters with space in the middle. 3.Enter one more character and the line will wrap. Deleting a character will cause height problems.

Current behavior

On safari

https://github.com/user-attachments/assets/203e552e-c4b4-4bff-8114-8ee3a15358e2

Expected behavior

On Chrome

https://github.com/user-attachments/assets/b6a8ce13-6033-4cb7-86fc-62b1ee2f4212

Context

No response

Your environment

npx @mui/envinfo
  On safari

Search keywords: Textfield multiple height

xiaoxiaofan123 avatar Feb 12 '25 09:02 xiaoxiaofan123

I just reproduced it in Safari 18.3, marking this as a bug.

aarongarciah avatar Feb 12 '25 11:02 aarongarciah

It reproduces here too: https://codesandbox.io/p/devbox/focused-bessie-vy64pw

So at least this is an independent issue and not another problem with ResizeObserver 🤞

mj12albert avatar Feb 19 '25 10:02 mj12albert

Hi @xiaoxiaofan123 , I'm interested in contributing to this issue. Could you please assign it to me? Looking forward to working on it!

himani-mehra avatar Mar 03 '25 20:03 himani-mehra

Hi @himani-mehra, you can fork the repo and work on the issue directly. Submit a PR, and we'll review it. If everything looks good, we'll merge it. Looking forward to your contribution! 🚀

0210shivam avatar Mar 04 '25 05:03 0210shivam

Apologies @0210shivam, I just noticed the issue is specific to Safari, and I primarily use Windows. @xiaoxiaofan123, feel free to take it up if you'd like. Thanks!

himani-mehra avatar Mar 04 '25 05:03 himani-mehra

Hi @mj12albert! I'd like to work on this issue. This will be my first open source contribution!

Root Cause

In safari when textarea height is reduced programmatically, Safari updates the height but does not recalculate text line wrapping & caret position. Chrome handles this correctly

Proposed Fix

We can force textline wrapping recalculation in Safari by changing the textArea width.

const selectionStart = textarea.selectionStart;
const selectionEnd = textarea.selectionEnd;
const tempValue = textarea.value;

textarea.value = '';
textarea.offsetHeight; // Force reflow
textarea.value = tempValue;

// Restore selection
if (document.activeElement === textarea) {
  textarea.setSelectionRange(selectionStart, selectionEnd);
}

Should this be Safari-specific or applied to all browsers? Happy to adjust based on your feedback!

Hukumchand-Narwre avatar Dec 04 '25 12:12 Hukumchand-Narwre

@Hukumchand-Narwre You are welcome to open a PR, be sure to link it to this issue

mj12albert avatar Dec 05 '25 09:12 mj12albert