microsoft / microsoft/monaco-editor

[Bug] Font size changes after I destroy and create editor instance

Open
#4,746 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug confirmed editor-rendering
Dominant language
JavaScript
Stars
46.8k
Forks
4.1k
Avg merge
17h 58m
Merged PRs (30d)
1

Description

Reproducible in vscode.dev or in VS Code Desktop?
  • Not reproducible in vscode.dev or VS Code Desktop
Reproducible in the monaco editor playground?
Monaco Editor Playground Link

https://microsoft.github.io/monaco-editor/playground.html?source=v0.52.0#XQAAAAJqBAAAAAAAAABBqQkHQ5NjdVKIfkDi6SE6eJIbQyteFotciG7lKwh8wfIZ36OGD47nBRTK6zV7toIBs2O5GCJoRJ17GWu3dzLMrVeQg8lDNo5C5IsGbN5X3yVU_goa8kylb35AgX74t8fhMsaN38UxRlwliqsgRHk8DKKEdEFxy49_9oP8-yYptg-YyxPV3HkgXmYD4GCxG7xOT0Fl2GSHFKfVgCika6sWhJNLchhy2FKthrpxv8q-oGs9RoVe-fzST8bhsxCMZIaYN3sU_eOkQ0uVAM4K4K5JEZDqfe00scutsdHdg67Y8znXuXetLU7CoWmy10Gc-W5jv0UzVvrAjD_VY_0nBdRJRiL6l8AsBhBSg2OsPLgzgAxyYopcdu5GNtobz7hopLs2tCZL7IlxPujK_pv_NNrKE1KngzKT3FzpuHrf5zUkS5WlH9MoJ2l0bSMSBvN7GT_psjGJjO9p3kE3lkVsRMJyBRrB8mmEHsY_LKVTTOatXVoPokJI7y_qYaGRBQR7BHVfpcVkHF-lkomo0Ujz6O8ZLHQUl3sOMK4n09YJszoHq4lqLbftFmTrXdcN6M-NnSfjxfD5-dDIUS13e7FIIYnHidXqRcRwv1vVDjnviRFLn3F1beuOBhjHsxy5GOTGVNhoa7K99Zu6RRkbbFKFjffhz8DsNJsxqTg1PbbLe0P8DG6iVxTaVSf6pdqYWAHMhy_-_3T7wjg

Monaco Editor Playground Code
let localStorage_fontSize = '';

function createEditor() {
  const options = {
    value: '<div>Some text goes here</div>',
    language: "html",
    fontSize: 12,
  };

  const fontSize = localStorage_fontSize;
  if (fontSize && /\d+(\.\d+)?/.test(fontSize)) {
    options.fontSize = parseFloat(fontSize);
  }

  const editor = monaco.editor.create(document.getElementById("editor"), options);

  // listen font size change and save new value in local storage
  editor.onDidChangeConfiguration((e) => {
    if (!e.hasChanged(monaco.editor.EditorOption.fontSize)) {
      return;
    }

    localStorage_fontSize = editor.getOption(monaco.editor.EditorOption.fontSize).toString();
  });

  return editor;
}

let editor = createEditor();

document.getElementById('create').addEventListener('click', function () {
  editor = createEditor();
});

document.getElementById('destroy').addEventListener('click', function () {
  editor.dispose();
});
Reproduction Steps
  1. Open playground
  2. Increate/decrease font size once
    Image
  3. Click destroy button
  4. Click create button
Actual (Problematic) Behavior

Font size changes twice. Instead of 13.2px (lets assume we are increasing font size and default value was 12px) it becomes 14.52px.
Image

Expected Behavior

The font size does not change after instance re-creation.

Additional Context

It's easy to reproduce when you use any client router and monaco editor at the same time. In my case it's react-router. When I change font size, navigate away from the page and come back - font size changes one more time.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked Monaco Editor Playground reproduction and run the destroy/create sequence after changing the font size. Trace the editor lifecycle and configuration-change listener shown in the playground code, especially how the saved font size is read and updated. Done means recreating the editor preserves the selected font size without applying the change twice, including after navigation-like disposal and recreation.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.