question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error when calling `focus()` on an empty editor

See original GitHub issue

Do 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/

kapture 2017-09-21 at 14 47 08

What’s the expected behavior?

The editor should be focused.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
ms88privatcommented, Apr 18, 2018

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:

if (typeof value === 'undefined' || value === null) {
        return Value.fromJSON({
            document: {
                nodes: [{
                    kind: 'block',
                    type: 'paragraph',
                    nodes: [
                        {
                            kind: 'text',
                            text: '',
                        },
                    ],
                }],
            },
        });
    }

instead of:

return Value.fromJSON({
            document: {
                nodes: [{
                    kind: 'block',
                    type: 'paragraph',
                }],
            },
        });
2reactions
anewusrcommented, Sep 25, 2017

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:

   state = {
      state: html.deserialize('<p></p>')
   };

    focus() {
        const { state } = this.state;
        const change = state.change().focus();
        this.onChange(change);
    }

    onChange = (change) => {
        let {state} = change;
        this.setState({ state })
    };

And a screenshot of the error: screen shot 2017-09-25 at 1 20 06 am

Any idea why we’re seeing this regression?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found