Focus not working
See original GitHub issueDescribe 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:
- Created 3 years ago
- Comments:6 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Great, thanks for the fix!
@bluenote10 Here is the problem. In this line. After we trigger
editorDidMount
we callsetIsEditorReady(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.