Skip to content

Commit b5fd3e7

Browse files
Fix comment textarea scroll issue in Firefox (#34438)
In the comment editor, there is a bug in Firefox where the scroll position unexpectedly moves up, which is annoying. This is not reproducible in Chrome and Safari. To reproduce here are some steps: - Go into an editable issue - Scroll page to bottom - Focus the textarea and press Return many times, causing the textarea to get a scrollbar - Scroll page to bottom again - Press Return once more - Page should not scroll up. This fixes the bug by adding a temporary margin, and I verified it works in all browsers. Co-authored-by: wxiaoguang <[email protected]>
1 parent 4011e22 commit b5fd3e7

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

web_src/js/utils/dom.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ export function autosize(textarea: HTMLTextAreaElement, {viewportMarginBottom =
161161
function resizeToFit() {
162162
if (isUserResized) return;
163163
if (textarea.offsetWidth <= 0 && textarea.offsetHeight <= 0) return;
164+
const previousMargin = textarea.style.marginBottom;
164165

165166
try {
166167
const {top, bottom} = overflowOffset();
@@ -176,6 +177,9 @@ export function autosize(textarea: HTMLTextAreaElement, {viewportMarginBottom =
176177
const curHeight = parseFloat(computedStyle.height);
177178
const maxHeight = curHeight + bottom - adjustedViewportMarginBottom;
178179

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`;
179183
textarea.style.height = 'auto';
180184
let newHeight = textarea.scrollHeight + borderAddOn;
181185

@@ -196,6 +200,12 @@ export function autosize(textarea: HTMLTextAreaElement, {viewportMarginBottom =
196200
textarea.style.height = `${newHeight}px`;
197201
lastStyleHeight = textarea.style.height;
198202
} finally {
203+
// restore previous margin
204+
if (previousMargin) {
205+
textarea.style.marginBottom = previousMargin;
206+
} else {
207+
textarea.style.removeProperty('margin-bottom');
208+
}
199209
// ensure that the textarea is fully scrolled to the end, when the cursor
200210
// is at the end during an input event
201211
if (textarea.selectionStart === textarea.selectionEnd &&

0 commit comments

Comments
 (0)