[fix] focus events (#2013)

Somehow the previous focus PR snuck through with broken tests.

### Change Type

- [x] `patch` — Bug fix
This commit is contained in:
Steve Ruiz 2023-10-03 18:55:51 +01:00 committed by GitHub
parent 6ce1f8e66f
commit 1f21f11886
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,12 +1,12 @@
import { useLayoutEffect } from 'react'
import { useContainer } from './useContainer'
import { useEditor } from './useEditor'
/** @internal */
export function useFocusEvents(autoFocus: boolean) {
const editor = useEditor()
const container = useContainer()
useLayoutEffect(() => {
const container = editor.getContainer()
function handleFocus() {
if (!editor.instanceState.isFocused) {
editor.updateInstanceState({ isFocused: true })
@ -14,16 +14,18 @@ export function useFocusEvents(autoFocus: boolean) {
}
container.addEventListener('focus', handleFocus)
container.addEventListener('pointerdown', handleFocus)
if (autoFocus && !editor.instanceState.isFocused) {
editor.updateInstanceState({ isFocused: true })
editor.getContainer().focus()
container.focus()
} else if (editor.instanceState.isFocused) {
editor.updateInstanceState({ isFocused: false })
}
return () => {
container.removeEventListener('focus', handleFocus)
container.removeEventListener('pointerdown', handleFocus)
}
}, [editor, autoFocus])
}, [editor, container, autoFocus])
}