Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: focus fighting priority #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

WindRunnerMax
Copy link

This issue originated from the Focus Fighting.

The simplest demo is shown below, and you can reproduce this issue in react-focus-lock stories/FocusFighting.js as well.

function App() {
  const ref = useRef<HTMLInputElement>(null);

  return (
    <Fragment>
      <div style={{ background: "#eee", marginTop: 10, padding: 10 }}>
        <span>WorkSpace</span>
        <FocusLock autoFocus>
          <input type="text" onFocus={() => console.log("Lock input 1")} />
          <input type="text" onFocus={() => console.log("Lock input 2")} />
        </FocusLock>
      </div>
      <input
        autoFocus
        ref={ref}
        onBlur={() => ref.current?.focus()}
        onFocus={() => console.log("Lock input 3")}
      />
    </Fragment>
  );
}

When focus-stealing occurs, the conflict is detected but not actually prevented. You can see from the console output that the focus is still being taken over continuously. It seems like the async operation successfully steals the focus, which can affect behaviors like IME input.

I believe this is caused by the priority of asynchronous tasks. Here, the deferAction is setTimeout(x, 1), which doesn't wait for all callbacks to complete. This releases the lock, leading to constant contention.

So here I changed the setTimeout time to 2ms. Our goal has never been to actually delay but to adjust the priority. Calling element.focus() directly is a synchronous task, so the asynchronous lock has to wait for all previous asynchronous tasks to complete.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant