diff --git a/.changeset/selfish-frogs-press.md b/.changeset/selfish-frogs-press.md new file mode 100644 index 0000000000..48c48727b7 --- /dev/null +++ b/.changeset/selfish-frogs-press.md @@ -0,0 +1,5 @@ +--- +'slate-react': minor +--- + +Retain editor selection when using ReactEditor.focus() diff --git a/packages/slate-react/src/plugin/react-editor.ts b/packages/slate-react/src/plugin/react-editor.ts index 3653c99c04..1d77d16a7f 100644 --- a/packages/slate-react/src/plugin/react-editor.ts +++ b/packages/slate-react/src/plugin/react-editor.ts @@ -417,6 +417,12 @@ export const ReactEditor: ReactEditorInterface = { IS_FOCUSED.set(editor, true) if (root.activeElement !== el) { + if (editor.selection && root instanceof Document) { + const domSelection = root.getSelection() + const domRange = ReactEditor.toDOMRange(editor, editor.selection) + domSelection?.removeAllRanges() + domSelection?.addRange(domRange) + } el.focus({ preventScroll: true }) } },