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.

Selection is null after editor loses focus

See original GitHub issue

Do you want to request a feature or report a bug?

Bug

What’s the current behavior?

Current behavior is that when you click into the toolbar or out of the document the editor’s selection becomes null.

2020-01-08 13 25 51

https://codesandbox.io/s/fervent-bouman-ju71u?fontsize=14&hidenavigation=1&theme=dark

Tested on Firefox, Safari and Chrome although Firefox has different behavior. Firefox sometimes sets the cursor position to the end of the text in the editor depending on how you focus out of the editor.

Slate: 0.57.1 Browser: Chrome / Safari / Firefox OS: Mac

What’s the expected behavior?

Focus out shouldn’t erase editor selection.

Changes to Example Site To Produce Behavior

In order to test this we forked the rich text example and made sure the toolbar style buttons did not disable on focus out. Then we used the ReactEditor.focus method in the MarkdownButton component’s onMouseDown handler in the richtext.js file.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:52
  • Comments:37 (4 by maintainers)

github_iconTop GitHub Comments

42reactions
shvets-sergeycommented, Jul 25, 2020

Here is my solution to this problem:

  1. Put command buttons (like “link” on the gif in the first post) out of the <Slate> tag. This is required, so onBlur event will be fired before your button is clicked.
  2. Add “onBlur” handler to the <Editable> tag. In that handler save selection to some property on the editor. E.g. blurSelection.
  3. Slate sets selection to null after blur, so before executing a command on the editor, you need to set selection to the saved one. Do it with Transforms.select(editor, editor.blurSelection); (blurSelection is the name of a variable from step2.
  4. Run your regular command as if a selection was there. Everything will work exactly the same.
  5. Bonus: now, since we have selection we can use ReactEditor.focus(editor) to return focus, so users can just continue typing.

Works well for all the basic commands my editor has: lists, numbers, formatting, headers, etc.

24reactions
Lalitj03commented, Jan 9, 2020

ezgif com-video-to-gif

Similar thing happened when I tried to use dialog box in link example instead of alert. While image upload as well the editor focus loses and the image gets appended at the last node instead of at the cursor location. Is there a way to control or change focus?

Read more comments on GitHub >

github_iconTop Results From Across the Web

EditorWindow closing when focus lost - Unity Forum
I have a EditorWindow shown with EditorWindow.Show() not ShowUtility(). On windows it works fine, I click away from it and it does not...
Read more >
TokenEdit - Null text is not shown after an editor loses focus
Null text is shown when an edit value equals null or System.DBNull.Value. Please refer to the NullText help topic where we describe when...
Read more >
In React ES6, why does the input field lose focus after typing a ...
it is because you are rendering the form in a function inside render(). Every time your state/prop change, the function returns a new...
Read more >
UI Builder's Inspector text field loses focus after ... - Issue Tracker
1. Open the attached project "case_1397542.zip" · 2. Open "test.uxml" · 3. Click on the expand/collapse dropdown button of "Main. · 4. Try...
Read more >
Active window (program) unexpectedly loses focus in ...
Sometimes focus is lost for good (have to click on window to continue using it), sometimes it is just shifted to some another...
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