Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run <Slate />'s focus event listeners after <Editable />'s focus handlers in React >= 17 #4920

Merged
merged 4 commits into from
Apr 3, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/five-emus-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'slate-react': patch
---

fix useFocused hook in react >= 17
25 changes: 15 additions & 10 deletions packages/slate-react/src/components/slate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
SlateSelectorContext,
} from '../hooks/use-slate-selector'
import { EDITOR_TO_ON_CHANGE } from '../utils/weak-maps'
import { IS_REACT_VERSION_17_OR_ABOVE } from '../utils/environment'
import { useIsomorphicLayoutEffect } from '../hooks/use-isomorphic-layout-effect'

/**
Expand Down Expand Up @@ -70,16 +71,20 @@ export const Slate = (props: {

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

useIsomorphicLayoutEffect(() => {
const fn = () => setIsFocused(ReactEditor.isFocused(editor))
document.addEventListener('blur', fn, true)
return () => {
document.removeEventListener('focus', fn, true)
document.removeEventListener('blur', fn, true)
if (IS_REACT_VERSION_17_OR_ABOVE) {
Copy link
Contributor

@bryanph bryanph Apr 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My only remark would be to add a little summary of your explanation in the PR here just to make it easier to figure out why this code is here in the future

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have added a summary of my explanation!

document.addEventListener('focusin', fn)
document.addEventListener('focusout', fn)
return () => {
document.removeEventListener('focusin', fn)
document.removeEventListener('focusout', fn)
}
} else {
document.addEventListener('focus', fn, true)
document.addEventListener('blur', fn, true)
return () => {
document.removeEventListener('focus', fn, true)
document.removeEventListener('blur', fn, true)
}
}
}, [])

Expand Down
5 changes: 5 additions & 0 deletions packages/slate-react/src/utils/environment.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import React from 'react'

export const IS_REACT_VERSION_17_OR_ABOVE =
parseInt(React.version.split('.')[0], 10) >= 17

export const IS_IOS =
typeof navigator !== 'undefined' &&
typeof window !== 'undefined' &&
Expand Down