ianstormtaylor / slate

A completely customizable framework for building rich text editors. (Currently in beta.)
http://slatejs.org
MIT License
30.02k stars 3.26k forks source link

in Firefox caret position is lost if render happens right after onFocus #4560

Open Torvin opened 3 years ago

Torvin commented 3 years ago

Description In Firefox caret position is lost if render happens right after onFocus.

Imagine you do something like <Editable onFocus={() => setState(...)} - this causes the caret to jump back to the last position Slate remembers instead of the position I just clicked on (which resulted in this focus event to happen)

This seems to work fine in latest Chrome. Didn't test other browsers.

Recording 1

Sandbox https://codesandbox.io/s/slate-0-5x-forked-0lxy7?file=/src/App.tsx

Steps To reproduce the behavior:

  1. Click anywhere in the editor showing "Testing"
  2. The caret jumps to the start of the editor in Firefox (or to the last position Slate remembers if it's not the first time you move the caret around)

Expectation Slate shouldn't reset caret position if re-render happens right after focus event

Environment

Torvin commented 3 years ago

Update: was able to break it in Chrome as well by adding setTimeout: https://codesandbox.io/s/slate-0-5x-forked-mq5sd?file=/src/App.tsx

Now when I click, the focus just gets lost until I click again. Sounds like related to https://github.com/ianstormtaylor/slate/issues/3634?