Skip to content

Commit

Permalink
run focus event listener after existing onFocus handlers (#4755)
Browse files Browse the repository at this point in the history
* run focus event listener after existing onFocus handlers

* change window.setTimeout to setTimeout
  • Loading branch information
jhurwitz authored Jan 10, 2022
1 parent 51e02de commit 8daa77e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/stale-poems-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'slate-react': patch
---

fix useFocused hook
12 changes: 10 additions & 2 deletions packages/slate-react/src/components/slate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,21 @@ export const Slate = (props: {
})

useIsomorphicLayoutEffect(() => {
const fn = () => setIsFocused(ReactEditor.isFocused(editor))
const fn = () => {
setTimeout(() => {
setIsFocused(ReactEditor.isFocused(editor))
}, 0)
}
document.addEventListener('focus', fn, true)
return () => document.removeEventListener('focus', fn, true)
}, [])

useIsomorphicLayoutEffect(() => {
const fn = () => setIsFocused(ReactEditor.isFocused(editor))
const fn = () => {
setTimeout(() => {
setIsFocused(ReactEditor.isFocused(editor))
}, 0)
}
document.addEventListener('blur', fn, true)
return () => document.removeEventListener('blur', fn, true)
}, [])
Expand Down

0 comments on commit 8daa77e

Please sign in to comment.