From 6076d44ab26d5d71005688fb55fa400de9f5045e Mon Sep 17 00:00:00 2001 From: Joe Anderson Date: Mon, 22 Jan 2024 16:42:15 +0000 Subject: [PATCH] Fix `useFocused` not updated when calling `ReactEditor.focus` --- .changeset/shiny-numbers-matter.md | 5 +++++ packages/slate-react/src/plugin/react-editor.ts | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 .changeset/shiny-numbers-matter.md diff --git a/.changeset/shiny-numbers-matter.md b/.changeset/shiny-numbers-matter.md new file mode 100644 index 0000000000..858cadb432 --- /dev/null +++ b/.changeset/shiny-numbers-matter.md @@ -0,0 +1,5 @@ +--- +'slate-react': patch +--- + +Fix: Calling `ReactEditor.focus` doesn't update `useFocused` when running in @testing-library/react diff --git a/packages/slate-react/src/plugin/react-editor.ts b/packages/slate-react/src/plugin/react-editor.ts index 422b1e816d..9485e9df21 100644 --- a/packages/slate-react/src/plugin/react-editor.ts +++ b/packages/slate-react/src/plugin/react-editor.ts @@ -447,8 +447,10 @@ export const ReactEditor: ReactEditorInterface = { Transforms.select(editor, Editor.start(editor, [])) editor.onChange() } - el.focus({ preventScroll: true }) + // IS_FOCUSED should be set before calling el.focus() to ensure that + // FocusedContext is updated to the correct value IS_FOCUSED.set(editor, true) + el.focus({ preventScroll: true }) } },