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?
I found out I can subscribe to
FOCUS_COMMANDandBLUR_COMMANDand update a local state when they change:This seems sufficient, but I'm still wondering if it is possible to get the information directly from the source of truth (
EditorState), instead of tracking it via a side-effect.