jsx-a11y/no-autofocus

Disallow the autoFocus attribute

has suggestions

Also called jsx_a11y/no-autofocus

autoFocus moves focus as soon as the element renders. A screen reader starts reading at that element, so the user misses everything before it. On a phone, the keyboard opens and the page scrolls by itself.

autoFocus={false} isn't reported. Neither is autoFocus inside a <dialog>, an element with role="dialog", or an element with a popover attribute, because opening one of those is supposed to move focus.

--fix-suggestions removes the attribute.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"jsx-a11y/no-autofocus" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"jsx-a11y/no-autofocus" = ["error", {
  ignoreNonDOM = false,
}]

Examples#

example.jsx1 problem
const search = <input type="search" autoFocus />;
example.jsxNo problems
const search = <input type="search" />;

const rename = (
  <dialog open>
    <input type="text" autoFocus />
  </dialog>
);

Options#

OptionDefaultWhat it does
ignoreNonDOMfalseOnly check HTML elements, not components.
example.jsx1 problem
const search = <SearchBox autoFocus />;
example.jsxwith { "ignoreNonDOM": true }No problems
const search = <SearchBox autoFocus />;