Automatically moving keyboard focus to an element when it is rendered or shown, before users have had a chance to get their bearings, can disorient or actively harm them:
Autofocusing a dialog element, an element with a popover attribute, an element with an ARIA dialog or
alertdialog role, or any element inside one of these, does not raise this issue: moving focus into a freshly opened modal or popover is
expected, and the dialog/popover element itself is a valid autofocus target when it should receive focus as soon as it opens.
Remove the attribute that autofocuses the element, and let the browser’s default focus behavior apply. If an element genuinely needs to receive
focus in response to a user action, for example a form opened on demand, move it into a dialog element or a popover
container instead of autofocusing it directly when it is rendered or shown.
<input autofocus />
<input />
Autofocusing an element inside a dialog or a popover is compliant:
<dialog> <input autofocus /> </dialog>
Autofocusing the dialog or popover element itself is also compliant:
<dialog autofocus></dialog>
Autofocusing an element with an ARIA dialog or alertdialog role, or an element inside one, is compliant too:
<div role="dialog" aria-modal="true"> <input autofocus /> </div>
In Vue templates, an autofocus attribute on a PascalCase or kebab-case tag is not flagged, since such tags are component references
whose rendering the analyzer cannot resolve. Vue passes undeclared attributes through to the component’s root element by default, so such code can
still autofocus an element:
<CustomInput autofocus /> <custom-input autofocus />
dialog role