I want to create a function that can determine if my editor has focus:
function hasFocus(editor: LexicalEditor) {
const hasFocus = editor.getEditorState().read(() => {
// return $...
})
return hasFocus
}
I dag through source code and docs, but found no method that could detect this directly. In my testing, Selection
object doesn't seem to reliably determine whether the Editor is focused in DOM or not.
So, how can I detect editor focus?
For some reason, the focus command didn't work fine for me, so I decided to use the global editor listener instead for reading keyboard and mouse updates
It could have some improvements (useRef, ...) but worked for me