From 0141f683659025c7e851c11274cf200da05fd31e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laufey=20Rut=20Gu=C3=B0mundsd=C3=B3ttir?= Date: Mon, 19 Dec 2022 17:51:13 +0000 Subject: [PATCH] Check if getBoundingClientRect exist before trying to call bind (#5226) * Stop trying to call bind on undifined when getBoundingClientRect is not defined. This is happening when testing slate with react testing library * Add changeset --- .changeset/curly-news-cover.md | 5 +++++ packages/slate-react/src/components/editable.tsx | 5 +++-- 2 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 .changeset/curly-news-cover.md diff --git a/.changeset/curly-news-cover.md b/.changeset/curly-news-cover.md new file mode 100644 index 000000000..6bda897e0 --- /dev/null +++ b/.changeset/curly-news-cover.md @@ -0,0 +1,5 @@ +--- +'slate-react': minor +--- + +Check if getBoundingClientRect exist before trying to call bind on it. Makes unit testing experience agains Editable nicer diff --git a/packages/slate-react/src/components/editable.tsx b/packages/slate-react/src/components/editable.tsx index 83522aa94..533845c36 100644 --- a/packages/slate-react/src/components/editable.tsx +++ b/packages/slate-react/src/components/editable.tsx @@ -1691,8 +1691,9 @@ const defaultScrollSelectionIntoView = ( // This was affecting the selection of multiple blocks and dragging behavior, // so enabled only if the selection has been collapsed. if ( - !editor.selection || - (editor.selection && Range.isCollapsed(editor.selection)) + domRange.getBoundingClientRect && + (!editor.selection || + (editor.selection && Range.isCollapsed(editor.selection))) ) { const leafEl = domRange.startContainer.parentElement! leafEl.getBoundingClientRect = domRange.getBoundingClientRect.bind(domRange)