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.

Focus not working

See original GitHub issue

Describe the bug

It looks like calling editor.focus() does not work.

To Reproduce

Here is a minimal example (in TypeScript). The code tries to give the editor focus in the onEditorDidMount callback. The goal is that the user can start typing in the editor immediately.

import React from 'react';
import ReactDOM from 'react-dom';

import Editor from "@monaco-editor/react";
import * as monacoEditor from 'monaco-editor/esm/vs/editor/editor.api';

type IStandaloneCodeEditor = monacoEditor.editor.IStandaloneCodeEditor

const EditorWrapper = () => {

  const onEditorDidMount = (_: () => string, editor: IStandaloneCodeEditor) => {
    editor.setPosition({lineNumber: 1, column: 6})
    editor.focus()
  }

  return (
    <Editor
      height="90vh"
      theme="dark"
      language="markdown"
      value={"Hello world"}
      editorDidMount={onEditorDidMount}
    />
  );
}

ReactDOM.render(
  <React.StrictMode>
    <EditorWrapper/>
  </React.StrictMode>,
  document.getElementById('root')
);

Expected behavior

The editor should get focused.

Desktop (please complete the following information):

  • OS: Ubuntu 18.04
  • Browser: Firefox 83, Chrome 80.0.3987.162
  • monaco-editor 0.21.2
  • @monaco-editor/react: 3.7.0

Additional context

I was using Monaco editor before in a non-React context, and there the editor focusing was working fine. Therefore I would assume this is not an upstream Monaco editor issue, but rather an issue in the React integration?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
bluenote10commented, Dec 16, 2020

Great, thanks for the fix!

1reaction
suren-atoyancommented, Nov 29, 2020

@bluenote10 Here is the problem. In this line. After we trigger editorDidMount we call setIsEditorReady(true), which cause a re-rendering the MonacoContainer component. The last one re-renders the root of the editor, which leads to losing the focus.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Focus Mode Not Working? Try These 8 Troubleshooting Tips
Focus Mode Isn't On When It's Supposed to Be · Open Settings and select Focus. Tap Focus · Select the Focus mode you...
Read more >
jquery - $.focus() not working - Stack Overflow
The reason that's not working is simply because it's not stealing focus from the dev console. If you run the following code in...
Read more >
Focus mode not working on iOS 16? 8 Quick fixes - iGeeksBlog
Focus mode not working on iPhone running iOS 16? Try these fixes: 1. Turn Off Focus Sharing Across Devices, 2. Check for Whitelisted...
Read more >
Focus Not Working on your iPhone? 10 Ways to Fix the Issue
10 Ways to fix Focus not working issue · #1: Make sure you have not whitelisted the app · #2: Make sure you...
Read more >
HTMLElement.focus() - Web APIs | MDN
The HTMLElement.focus() method sets focus on the specified element, if it can be focused. The focused element is the element that will ...
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