Error when calling `focus()` on an empty editor
See original GitHub issueDo you want to request a feature or report a bug?
bug
What’s the current behavior?
Calling focus()
on an empty editor after it’s mounted causes an exception:
Uncaught Error: Invalid `key` argument! It must be either a block, an inline, a text, or a string. You passed: null
at normalizeKey (https://npmcdn.com/slate@0.25.1/dist/slate.js:14384:9)
at Document.getFurthestAncestor (https://npmcdn.com/slate@0.25.1/dist/slate.js:12810:13)
at Document.object.(anonymous function) [as getFurthestAncestor] (https://npmcdn.com/slate@0.25.1/dist/slate.js:19296:28)
at Document.getSelectionIndexes (https://npmcdn.com/slate@0.25.1/dist/slate.js:13493:26)
at Content.render (https://npmcdn.com/slate-react@0.1.8/dist/slate-react.js:8409:30)
[ ... ]
This was working as recently as 0.24.5, so the breakage must be pretty new.
https://jsfiddle.net/rgrove/sjcc7p0e/2/
What’s the expected behavior?
The editor should be focused.
Issue Analytics
- State:
- Created 6 years ago
- Comments:10 (4 by maintainers)
Top Results From Across the Web
Javascript Focus() does not work on empty textarea
It works as expected when the textarea is filled, but nothing happens when the textarea is empty.
Read more >Problems with callback "focus" - Forums
Case B: The user clicks on an empty part of the editor where there is no text (eg. in middle of text area.)...
Read more >Focusing: focus/blur
Losing the focus generally means: “the data has been entered”, so we can run the code to check it or even to save...
Read more >focus - CSS: Cascading Style Sheets - MDN Web Docs - Mozilla
The :focus CSS pseudo-class represents an element (such as a form input) that has received focus. It is generally triggered when the user ......
Read more >Handling Hover, Focus, and Other States
Using utilities to style elements on hover, focus, and more. ... Call</span> <svg class="group-hover/edit:translate-x-0.5 group-hover/edit:text-slate-500 .
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Why was this closed? We are also running into this.
The solution above doesn’t work for me. It will not focus the editor content.
Edit: Ok, it helped to initialise the empty document like this:
instead of:
I don’t think this is a red herring.
After upgrading to 0.25.1, I get the same error as the OP whenever I invoke the “focus” function below via the parent element’s ref, i.e.
this.refs.myEditor.focus()
For some reason, focusing works without error when clicking into the editor or if
<p></p>
is replaced with<p>a</p>
.Here is the relevant code:
And a screenshot of the error:
Any idea why we’re seeing this regression?