question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug] Mouse click incorrectly sets the caret position

See original GitHub issue

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 Code

const customElement = document.createElement('div');
customElement.style.height = '100%';
customElement.attachShadow({mode: 'open'});

const customContainer = document.createElement('div');
customContainer.style.height = '100%';
customElement.shadowRoot.append(customContainer);

document.getElementById('container').append(customElement);

const editorRef = monaco.editor.create(customContainer, {
	value: `let wrapper = document.createElement('span');
wrapper.setAttribute('class', 'wrapper');
let icon = document.createElement('span');
icon.setAttribute('class', 'icon');
icon.setAttribute('tabindex', 0);
let info = document.createElement('span');
info.setAttribute('class', 'info');`,
      language: 'javascript',
});


Reproduction Steps

No response

Actual (Problematic) Behavior

I created a MonacoEditor which has issues with highlighting text and setting the caret in the desired position when clicking with the mouse, and I can’t figure out what’s going on.

I added a few console logs as an example, and for instance, I get:

// log1
mouseX: 376
mouseColumn: 16

// log2
mouseX: 223
mouseColumn: 23

What could be interfering with the correct position of the caret in the editor? How could it be that a click further to the right return a lower column number? The deviation is not consistent or proportional either. If I click in the same place, I do get it in the same position. But clicking in one place can have +2 columns wrong, in another to the right have about +6 columns wrong, and further to the right the caret can be even positioned -7 columns in the other direction (like in the example above).

Expected Behavior

Clicking in the editor correctly positions the caret

Additional Context

No response

UPDATE: I found out the issue is when using the editor inside a shadowDOM

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
janarvaezcommented, Sep 21, 2022

@janarvaez Did you find a fix or workaround? I’m facing the same issue and using monaco inside a shadowDOM is a requirement for me

I have 2 workarounds.

  1. have the container outside the shadowRoot passed as children through a slot. This works fine since the slotted container is actually outside, but wrapped and styled inside the shadow.
  2. Wrap the whole editor inside an iframe, but depending on your setup, this could be harder to achieve (although isolating the editor in an iframe prevents other issues, like having multiple editors with different languages rendered at the same time).
0reactions
lucasMariozacommented, Sep 22, 2022

The workaround 1 worked but I figured it out my actual problem. I had one external style from an ancestor component(outside the shadowDOM) that was setting font-variant: tabular-nums. For some reason if monaco was rendered outside the shadowDOM this style was not an issue, and if it was inside the shadowDOM not even remeasureFonts fixed the issue.

I suppose that somehow it is not considering styles coming from outside the shadowDOM during the font measurements, or something like that.

In my case removing this style was not a problem, so I sticked with that solution.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cursor location after mouse click is incorrect.
If you now type something, the text is placed at the cursor location. NOT after the last visible character on the line. It...
Read more >
Mouse cursor position incorrect within thumbnail pane
To fix it, I choose another View (e.g. Slide Sorter) and then go back to Normal, or adjust the width of the thumbnail...
Read more >
Incorrect caret position when nesting elements in a content ...
This is a bug in google chrome and safari. In firefox it's working properly. I spend a while figuring out how to fix...
Read more >
Wrong caret position in multiline text components on Windows ...
The caret should always be inserted where the mouse is clicked. ... The caret is not in the same position as where the...
Read more >
Bug #1306550 “Wrong mouse-coordinates in the window after ...
The actual position of the mouse pointer in the window is lower the the position of the cursor on the screen (about the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found