In the general sense of autofocus, I believe it should not be allowed, just like we don't support arbitrary tabindex because:
But in a recent discussion (https://github.com/whatwg/html/issues/833) new information arise about autofocus, and how that might work in the context of the shadow. I'm not 100% sure I have a full understanding of the implications, but that's why I'm opening this issue, to investigate.
From what I can tell, it sounds like the autofocus global attribute would only grab the focus if (1) the component is focused and (2) the component is delegating focus. It seems like these conditions would make it trivial to implement, but focus-related features usually end up being more difficult than we thought.
I vote "no" to implementing this because I think authors can easily simulate this behavior using focusin and focus().
I just saw this code in core:
export default class SearchInput extends LightningElement {
@api value;
@api autofocus;
@api placeholder;
renderedCallback() {
if (this.autofocus) {
const inputBox = this.template.querySelector('lightning-input');
inputBox.focus();
}
}
That code is faulty because it will attempt to set the focus after every rerendering, disrupting the user that might want to set the focus somewhere else. It is probably OK, because it will probably only rerender the component if the user is focused on the input and typing, but nevertheless, it is problematic.