diff --git a/packages/slate-react/src/components/editable.tsx b/packages/slate-react/src/components/editable.tsx index d5a19b4b7..a553b19d6 100644 --- a/packages/slate-react/src/components/editable.tsx +++ b/packages/slate-react/src/components/editable.tsx @@ -14,7 +14,12 @@ import scrollIntoView from 'scroll-into-view-if-needed' import Children from './children' import Hotkeys from '../utils/hotkeys' -import { IS_FIREFOX, IS_SAFARI, IS_EDGE_LEGACY } from '../utils/environment' +import { + IS_FIREFOX, + IS_SAFARI, + IS_EDGE_LEGACY, + IS_CHROME_LEGACY, +} from '../utils/environment' import { ReactEditor } from '..' import { ReadOnlyContext } from '../hooks/use-read-only' import { useSlate } from '../hooks/use-slate' @@ -39,7 +44,12 @@ import { } from '../utils/weak-maps' // COMPAT: Firefox/Edge Legacy don't support the `beforeinput` event -const HAS_BEFORE_INPUT_SUPPORT = !(IS_FIREFOX || IS_EDGE_LEGACY) +// Chrome Legacy doesn't support `beforeinput` correctly +const HAS_BEFORE_INPUT_SUPPORT = !( + IS_FIREFOX || + IS_EDGE_LEGACY || + IS_CHROME_LEGACY +) /** * `RenderElementProps` are passed to the `renderElement` handler. @@ -347,13 +357,13 @@ export const Editable = (props: EditableProps) => { // real `beforeinput` events sadly... (2019/11/04) // https://github.com/facebook/react/issues/11211 useIsomorphicLayoutEffect(() => { - if (ref.current) { + if (ref.current && HAS_BEFORE_INPUT_SUPPORT) { // @ts-ignore The `beforeinput` event isn't recognized. ref.current.addEventListener('beforeinput', onDOMBeforeInput) } return () => { - if (ref.current) { + if (ref.current && HAS_BEFORE_INPUT_SUPPORT) { // @ts-ignore The `beforeinput` event isn't recognized. ref.current.removeEventListener('beforeinput', onDOMBeforeInput) } diff --git a/packages/slate-react/src/utils/environment.ts b/packages/slate-react/src/utils/environment.ts index 1d67593c8..a12cdbcaa 100644 --- a/packages/slate-react/src/utils/environment.ts +++ b/packages/slate-react/src/utils/environment.ts @@ -19,3 +19,8 @@ export const IS_SAFARI = export const IS_EDGE_LEGACY = typeof navigator !== 'undefined' && /Edge?\/(?:[0-6][0-9]|[0-7][0-8])/i.test(navigator.userAgent) + +// Native beforeInput events don't work well with react on Chrome 75 and older, Chrome 76+ can use beforeInput +export const IS_CHROME_LEGACY = + typeof navigator !== 'undefined' && + /Chrome?\/(?:[0-7][0-5]|[0-6][0-9])/i.test(navigator.userAgent)