Fix: Calling ReactEditor.focus
doesn't update useFocused
when running in @testing-library/react
#5593
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Sometimes, when calling
ReactEditor.focus
in @testing-library/react, the value ofuseFocused
doesn't get updated. This PR addresses this issue by settingIS_FOCUSED
to true prior to dispatching a DOM focus event.Context
The
isFocused
state variable controllinguseFocused
(viaFocusedContext
) is updated inslate.tsx
in response to focus and blur events, but the value ofisFocused
is always pulled fromIS_FOCUSED
, regardless of which event triggered the update. This stopped working in #5527 when theReactEditor.focus
line settingIS_FOCUSED
was moved to afterel.focus({ preventScroll: true })
.When running in a real browser,
IS_FOCUSED
is already set to true whendomSelection?.addRange(domRange)
is called higher up inReactEditor.focus
, viaonDOMSelectionChange
.Checks
yarn test
.yarn lint
. (Fix errors withyarn fix
.)yarn start
.)yarn changeset add
.)