Skip to content

Conversation

@Hukumchand-Narwre
Copy link

This is my first Open source contribution. This PR fixes an issue in TextField height when the multiline prop is passed.

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);
}

Fixes

Fixes #45293

@mui-bot
Copy link

mui-bot commented Dec 7, 2025

Netlify deploy preview

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

Bundle size report

Bundle Parsed size Gzip size
@mui/material 🔺+224B(+0.04%) 🔺+96B(+0.06%)
@mui/lab 0B(0.00%) 0B(0.00%)
@mui/system 0B(0.00%) 0B(0.00%)
@mui/utils 0B(0.00%) 0B(0.00%)

Details of bundle changes

Generated by 🚫 dangerJS against 737074e

@ZeeshanTamboli ZeeshanTamboli added scope: text field Changes related to the text field. browser: Safari Affects or fixes behavior in Apple Safari. labels Dec 9, 2025
@zannager zannager requested a review from mj12albert December 10, 2025 06:53
const tempValue = textarea.value;
textarea.value = '';
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
textarea.offsetHeight;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
textarea.offsetHeight;
void textarea.offsetHeight;

Does this work instead of eslint-disable?

Copy link
Author

Choose a reason for hiding this comment

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

Yes, it worked without eslint-disable


// This is a workaround for Safari/WebKit not reflowing text when the textarea height changes
// Force Safari to reflow the text by manipulating the textarea value
const containerDocument = ownerDocument(textarea);
Copy link
Member

Choose a reason for hiding this comment

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

If it's just webkit we could wrap this change with a check:

const isWebKit = typeof CSS === 'undefined' || !CSS.supports
  ? false
  : CSS.supports('-webkit-backdrop-filter:none');

Copy link
Author

Choose a reason for hiding this comment

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

Wrapped the changes to work only in WebKit

@Hukumchand-Narwre
Copy link
Author

Thank you, @mj12albert, for reviewing the PR. Required changes are done, please review again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

browser: Safari Affects or fixes behavior in Apple Safari. scope: text field Changes related to the text field.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

4 participants