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

Trix breaks autofocus on <script defer> in Chrome / Safari #1187

Open
fschwahn opened this issue Sep 24, 2024 · 0 comments
Open

Trix breaks autofocus on <script defer> in Chrome / Safari #1187

fschwahn opened this issue Sep 24, 2024 · 0 comments

Comments

@fschwahn
Copy link

When there is another <input> with autofocus-attribute on the page, trix will take away the focus from the field. I tracked this down to this method (added in 9c433d5)

clearSelection() {
return getDOMSelection()?.removeAllRanges()
}

which is called when initializing the editor. This only happens when the JS code is loaded using defer, and only on Chrome / Safari (thus probably Edge too). I found the following info on MDN:

Safari and Chrome (unlike Firefox) currently focus the element containing selection when modifying the selection programmatically; it's possible that this may change in the future (see W3C bug 14383 and Webkit bug 38696).

I think this may be the same issue as described in #1166.

Steps to Reproduce
  1. Open the following in Chrome (or Safari)
<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm//trix/dist/trix.min.css" crossorigin="anonymous">
    <script src="https://cdn.jsdelivr.net/npm//trix/dist/trix.umd.js" crossorigin="anonymous" defer="defer"></script>
  </head>
  <body>
    <input type="text" autofocus>

    <input type="hidden" id="content" value="&lt;h1&gt;Trix&lt;/h1&gt;">
    <trix-editor input="content" class="trix-content"></trix-editor>
  </body>
</html>

(I was not able to reproduce this on codepen due to their iframe-structure, but it reproduces when hosted normally)

  1. Note that the focus is not on the initial <input>-element (the outline is still rendered, but when typing in characters nothing happens). After focusing the element manually a hard-reload is necessary to reproduce the problem.
  2. Removing defer or the <trix-editor>-element restores normal autofocus-behavior.
Details
  • Trix version: 2.1.5
  • Browser name and version: Latest Chrome & Safari - works on Firefox
  • Operating system: macOS
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

No branches or pull requests

1 participant