Use of "autofocus" in popup.html not consistent

I wanted to support a full keyboard experience with my new addon and started by getting the keyboard shortcut working. I then wanted popup.html to make the search field selected and active when it it started so the user can just start typing their search.

To do this, I found using “autofocus” on the search input HTML should work. However, only using this case, I am seeing the search input is only selected and active sometimes. I also tried putting a repeating loop to check for the search input is ready and calling input.focus() and input.select().

Even with all this in place, I am not seeing the search input field being consistently selected and active when popup.html is loaded and waiting for input.

Finally, I even added added listeners for focusin/focusout and logged those events thinking maybe something was taking focus from the “autofocus” element.

2025-05-06_08-47-03

Here are the logs by just opening/closing and opening/closing the search. The first section “worked” and the input was properly selected and the second did not. The observation I have is that in the cases it works I do see focusin and focusout events being generated, but the failure case did not. HOWEVER, in both cases, I do check if “Search input === document.activeElement” and that evaluates true in both cases.

Not selected

Selected

Logs

As a double check, I took a different addon I had and just added the “autofocus” tag on the “Search Term” and I am seeing the exact same behavior.

Not selected

Selected

Video
2025-05-06_09-03-38

1 Like

This has been reported and fixed here, but there was either a regression or the fix had a race condition, because it’s still there.
Currently tracked by:

4 Likes