Skip to content

Native EditContext: focusing the editor scrolls natively-scrolling ancestors (missing the textarea path's scroll-preservation guard) #324758

Description

@nstrayer

Type: Bug

When the editor uses the native EditContext input path (editor.editContext: true, the default on Chromium), focusing the editor scrolls every natively-scrolling ancestor of the hidden edit context node. The textarea input path has guarded against exactly this for years; the native EditContext path is missing the same guard.

Root cause

FocusTracker.focus() in src/vs/editor/browser/controller/editContext/native/nativeEditContextUtils.ts calls this._domNode.focus() bare:

public focus(): void {
	this._domNode.focus();
	this.refreshFocusState();
}

The hidden .native-edit-context node is absolutely positioned at the editor's last cursor position. When the editor gains focus (e.g. on mouse down), the browser scrolls every scrollable ancestor to reveal that node. If any ancestor scrolls natively (overflow: auto), its scroll position jumps, shifting layout between Monaco's mouse-down hit test and the click resolution -- so the click can land the cursor on the wrong line, and the container visibly jumps.

The textarea path already handles this in writeNativeTextAreaContent (src/vs/editor/browser/controller/editContext/textArea/textAreaEditContextInput.ts):

// If the focus is outside the textarea, browsers will try really hard to reveal the textarea.
// Here, we try to undo the browser's desperate reveal.
try {
	const scrollState = dom.saveParentsScrollTop(textArea);
	this.setIgnoreSelectionChangeTime('setSelectionRange');
	textArea.focus();
	textArea.setSelectionRange(selectionStart, selectionEnd);
	dom.restoreParentsScrollTop(textArea, scrollState);
} catch (e) {

Why VS Code itself doesn't show it

VS Code's own surfaces cannot reproduce this: workbench scrolling is synthetic, and ListView actively neutralizes browser-initiated scrolls (src/vs/base/browser/ui/list/listView.ts, scroll listener resetting element.scrollTop = 0). The bug only manifests for embedders of the Monaco editor whose ancestors scroll natively.

Downstream data point: Positron (Monaco embedder) hit this as posit-dev/positron#14085 (click places the cursor on the wrong line in an editor taller than its scroll container) and posit-dev/positron#14656 (click yanks the container scroll to the top), and carries this exact fix with a Playwright regression test in posit-dev/positron#14696.

Repro (Chromium, monaco-editor 0.55.1)

Editor inside a natively-scrolling container, editor taller than the container:

repro.html
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8" />
	<style>
		#scroller { height: 300px; width: 700px; overflow: auto; border: 2px solid #c00; }
		#editor { height: 900px; width: 100%; }
	</style>
</head>
<body>
	<div id="scroller"><div id="editor"></div></div>
	<script src="https://cdn.jsdelivr.net/npm/[email protected]/min/vs/loader.js"></script>
	<script>
		const useEditContext = new URLSearchParams(location.search).get('editContext') !== 'false';
		require.config({ paths: { vs: 'https://cdn.jsdelivr.net/npm/[email protected]/min/vs' } });
		require(['vs/editor/editor.main'], function () {
			monaco.editor.create(document.getElementById('editor'), {
				value: Array.from({ length: 60 }, (_, i) => '// line ' + (i + 1)).join('\n'),
				language: 'javascript',
				editContext: useEditContext,
				scrollBeyondLastLine: false,
				minimap: { enabled: false }
			});
		});
	</script>
</body>
</html>
  1. Open the page in Chromium (EditContext is Chromium-only). Do not focus the editor.
  2. Scroll the red container down (e.g. scroller.scrollTop = 400).
  3. Click a visible line.

Actual: the container scroll jumps back to the top (scrollTop 400 -> 0) to reveal the hidden edit context node parked at the previous cursor position; with the layout shift the cursor can land on a different line than the one clicked.

Expected: cursor lands on the clicked line and the container does not scroll -- which is exactly what happens when you open the same page with ?editContext=false (textarea path): scrollTop stays at 400.

Measured with the harness above (same synthetic click at the container center, line 31 under the pointer):

input path scrollTop before click scrollTop after click
EditContext (default) 400 0
textarea (editContext: false) 400 400

Proposed fix

Mirror the textarea guard in FocusTracker.focus() using the existing saveParentsScrollTop / restoreParentsScrollTop helpers from base/browser/dom.ts:

public focus(): void {
	// If the focus is outside the dom node, browsers will try really hard to reveal it.
	// Here, we try to undo the browser's desperate reveal.
	const scrollState = saveParentsScrollTop(this._domNode);
	this._domNode.focus();
	restoreParentsScrollTop(this._domNode, scrollState);
	this.refreshFocusState();
}

Verified: rebuilding monaco-editor-core from main (e8abd58) with this guard and pointing the same harness at it, the same click keeps scrollTop at 400, lands the cursor on the clicked line, and the editor is focused. PR to follow.

VS Code version: main @ e8abd58

Activity

  1. vs-code-engineering commented on Jul 7, 2026

    @vs-code-engineering
    Contributor

    Thanks for creating this issue! It looks like you may be using an old version of VS Code; the latest stable release is 1.127.0. Please try upgrading to the latest version and checking whether this issue remains.

    Happy Coding!

  2. added
    bug
    on Jul 7, 2026
  3. karstendick commented on Sep 16, 2026

    @karstendick

    I'm able to reproduce this bug in the stock VS Code workbench. The trigger is clicking into a visible but unfocused editor in a side-by-side group layout.

    VS Code: 1.137.0 (645f29c, arm64)
    OS: macOS 15.4.1 (24E263)

    Repro:

    1. Split into two editor groups side by side. Put a file taller than the viewport in the left group (mine was a 372-line Markdown doc); leave both groups visible.
    2. In the left editor, scroll somewhere and click to place the cursor.
    3. Click a tab in the right group to move focus out of the left editor.
    4. Click back into the left editor's text.

    Step 4 scrolls the left editor away from where I clicked, and the cursor lands on a different line than the one under the pointer — exactly the "click can land the cursor on the wrong line, and the container visibly jumps" failure described above.

    I found a workaround: Setting "editor.editContext": false and reloading the window fixes it completely; I can no longer reproduce. Switching back to the default reintroduces it. That's consistent with the missing saveParentsScrollTop / restoreParentsScrollTop guard being the cause, since the textarea path is the one that has it.

  4. aiday-mar commented on Nov 11, 2026

    @aiday-mar
    Collaborator

    I am not able to reproduce the issue you mentioned in the comment? I see the following. Am I perhaps missing some steps?

    Screen.Recording.2026-11-11.at.15.39.17.mov
  5. added
    info-needed
    and removed
    bug
    on Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

editor-edit-contextinfo-needed

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions