diff --git a/packages/editor/src/lib/hooks/useFocusEvents.ts b/packages/editor/src/lib/hooks/useFocusEvents.ts index 593c6ba04..3fad1d755 100644 --- a/packages/editor/src/lib/hooks/useFocusEvents.ts +++ b/packages/editor/src/lib/hooks/useFocusEvents.ts @@ -5,11 +5,25 @@ import { useEditor } from './useEditor' export function useFocusEvents(autoFocus: boolean) { const editor = useEditor() useLayoutEffect(() => { + const container = editor.getContainer() + + function handleFocus() { + if (!editor.instanceState.isFocused) { + editor.updateInstanceState({ isFocused: true }) + } + } + + container.addEventListener('focus', handleFocus) + if (autoFocus && !editor.instanceState.isFocused) { editor.updateInstanceState({ isFocused: true }) editor.getContainer().focus() } else if (editor.instanceState.isFocused) { editor.updateInstanceState({ isFocused: false }) } + + return () => { + container.removeEventListener('focus', handleFocus) + } }, [editor, autoFocus]) }