Skip to content

Commit 89f1df0

Browse files
GiteaBotsilverwindwxiaoguang
authored
Fix comment textarea scroll issue in Firefox (#34438) (#34446)
Backport #34438 by @silverwind 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: silverwind <[email protected]> Co-authored-by: wxiaoguang <[email protected]>
1 parent 94b67f1 commit 89f1df0

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
@@ -168,6 +168,7 @@ export function autosize(textarea: HTMLTextAreaElement, {viewportMarginBottom =
168168
function resizeToFit() {
169169
if (isUserResized) return;
170170
if (textarea.offsetWidth <= 0 && textarea.offsetHeight <= 0) return;
171+
const previousMargin = textarea.style.marginBottom;
171172

172173
try {
173174
const {top, bottom} = overflowOffset();
@@ -183,6 +184,9 @@ export function autosize(textarea: HTMLTextAreaElement, {viewportMarginBottom =
183184
const curHeight = parseFloat(computedStyle.height);
184185
const maxHeight = curHeight + bottom - adjustedViewportMarginBottom;
185186

187+
// In Firefox, setting auto height momentarily may cause the page to scroll up
188+
// unexpectedly, prevent this by setting a temporary margin.
189+
textarea.style.marginBottom = `${textarea.clientHeight}px`;
186190
textarea.style.height = 'auto';
187191
let newHeight = textarea.scrollHeight + borderAddOn;
188192

@@ -203,6 +207,12 @@ export function autosize(textarea: HTMLTextAreaElement, {viewportMarginBottom =
203207
textarea.style.height = `${newHeight}px`;
204208
lastStyleHeight = textarea.style.height;
205209
} finally {
210+
// restore previous margin
211+
if (previousMargin) {
212+
textarea.style.marginBottom = previousMargin;
213+
} else {
214+
textarea.style.removeProperty('margin-bottom');
215+
}
206216
// ensure that the textarea is fully scrolled to the end, when the cursor
207217
// is at the end during an input event
208218
if (textarea.selectionStart === textarea.selectionEnd &&

0 commit comments

Comments
 (0)