diff --git a/.changeset/tall-adults-attack.md b/.changeset/tall-adults-attack.md new file mode 100644 index 000000000..9803b5ae3 --- /dev/null +++ b/.changeset/tall-adults-attack.md @@ -0,0 +1,5 @@ +--- +'slate-react': patch +--- + +Some code clean-up in Editable. diff --git a/packages/slate-react/src/components/editable.tsx b/packages/slate-react/src/components/editable.tsx index cb1954c9a..31375dc71 100644 --- a/packages/slate-react/src/components/editable.tsx +++ b/packages/slate-react/src/components/editable.tsx @@ -704,10 +704,19 @@ export const Editable = (props: EditableProps) => { EDITOR_TO_ELEMENT.delete(editor) NODE_TO_ELEMENT.delete(editor) - if (HAS_BEFORE_INPUT_SUPPORT) { + if (ref.current && HAS_BEFORE_INPUT_SUPPORT) { // @ts-ignore The `beforeinput` event isn't recognized. ref.current.removeEventListener('beforeinput', onDOMBeforeInput) } + } else { + // Attach a native DOM event handler for `beforeinput` events, because React's + // built-in `onBeforeInput` is actually a leaky polyfill that doesn't expose + // real `beforeinput` events sadly... (2019/11/04) + // https://github.com/facebook/react/issues/11211 + if (HAS_BEFORE_INPUT_SUPPORT) { + // @ts-ignore The `beforeinput` event isn't recognized. + node.addEventListener('beforeinput', onDOMBeforeInput) + } } ref.current = node @@ -715,24 +724,6 @@ export const Editable = (props: EditableProps) => { [ref, onDOMBeforeInput] ) - // Attach a native DOM event handler for `beforeinput` events, because React's - // built-in `onBeforeInput` is actually a leaky polyfill that doesn't expose - // real `beforeinput` events sadly... (2019/11/04) - // https://github.com/facebook/react/issues/11211 - useIsomorphicLayoutEffect(() => { - if (ref.current && HAS_BEFORE_INPUT_SUPPORT) { - // @ts-ignore The `beforeinput` event isn't recognized. - ref.current.addEventListener('beforeinput', onDOMBeforeInput) - } - - return () => { - if (ref.current && HAS_BEFORE_INPUT_SUPPORT) { - // @ts-ignore The `beforeinput` event isn't recognized. - ref.current.removeEventListener('beforeinput', onDOMBeforeInput) - } - } - }, [onDOMBeforeInput]) - // Attach a native DOM event handler for `selectionchange`, because React's // built-in `onSelect` handler doesn't fire for all selection changes. It's a // leaky polyfill that only fires on keypresses or clicks. Instead, we want to