@@ -161,6 +161,7 @@ export function autosize(textarea: HTMLTextAreaElement, {viewportMarginBottom =
161
161
function resizeToFit ( ) {
162
162
if ( isUserResized ) return ;
163
163
if ( textarea . offsetWidth <= 0 && textarea . offsetHeight <= 0 ) return ;
164
+ const previousMargin = textarea . style . marginBottom ;
164
165
165
166
try {
166
167
const { top, bottom} = overflowOffset ( ) ;
@@ -176,6 +177,9 @@ export function autosize(textarea: HTMLTextAreaElement, {viewportMarginBottom =
176
177
const curHeight = parseFloat ( computedStyle . height ) ;
177
178
const maxHeight = curHeight + bottom - adjustedViewportMarginBottom ;
178
179
180
+ // In Firefox, setting auto height momentarily may cause the page to scroll up
181
+ // unexpectedly, prevent this by setting a temporary margin.
182
+ textarea . style . marginBottom = `${ textarea . clientHeight } px` ;
179
183
textarea . style . height = 'auto' ;
180
184
let newHeight = textarea . scrollHeight + borderAddOn ;
181
185
@@ -196,6 +200,12 @@ export function autosize(textarea: HTMLTextAreaElement, {viewportMarginBottom =
196
200
textarea . style . height = `${ newHeight } px` ;
197
201
lastStyleHeight = textarea . style . height ;
198
202
} finally {
203
+ // restore previous margin
204
+ if ( previousMargin ) {
205
+ textarea . style . marginBottom = previousMargin ;
206
+ } else {
207
+ textarea . style . removeProperty ( 'margin-bottom' ) ;
208
+ }
199
209
// ensure that the textarea is fully scrolled to the end, when the cursor
200
210
// is at the end during an input event
201
211
if ( textarea . selectionStart === textarea . selectionEnd &&
0 commit comments