diff --git a/.changeset/new-trainers-peel.md b/.changeset/new-trainers-peel.md new file mode 100644 index 000000000..20eec3abe --- /dev/null +++ b/.changeset/new-trainers-peel.md @@ -0,0 +1,5 @@ +--- +'slate-react': patch +--- + +Fix crash when unmounting an editor rendered within a React portal. The issue was arising at unmount time, because `getRootNode` returned the dettached portal node and it is not an instance of `Document` or `ShadowRoot`. As a fix, `getDocumentOrShadowRoot` has been refactored to return a root node instead of throwing. In sum, this patch fixes a regression bug introduced by https://github.com/ianstormtaylor/slate/pull/3749/ diff --git a/packages/slate-react/src/plugin/react-editor.ts b/packages/slate-react/src/plugin/react-editor.ts index fe271c90b..b4b3fd383 100644 --- a/packages/slate-react/src/plugin/react-editor.ts +++ b/packages/slate-react/src/plugin/react-editor.ts @@ -106,23 +106,14 @@ export const ReactEditor = { const el = ReactEditor.toDOMNode(editor, editor) const root = el.getRootNode() - // The below exception will always be thrown for iframes because the document inside an iframe - // does not inherit it's prototype from the parent document, therefore we return early - if (el.ownerDocument !== document) return el.ownerDocument + if ( + (root instanceof Document || root instanceof ShadowRoot) && + root.getSelection != null + ) { + return root + } - if (!(root instanceof Document || root instanceof ShadowRoot)) - throw new Error( - `Unable to find DocumentOrShadowRoot for editor element: ${el}` - ) - - // COMPAT: Only Chrome implements the DocumentOrShadowRoot mixin for - // ShadowRoot; other browsers still implement it on the Document - // interface. (2020/08/08) - // https://developer.mozilla.org/en-US/docs/Web/API/ShadowRoot#Properties - if (root.getSelection === undefined && el.ownerDocument !== null) - return el.ownerDocument - - return root + return el.ownerDocument }, /**