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" autoFocusThe autoFocus attribute is found here, which can cause usability issues for sighted and non-sighted users. />;example.jsxNo problems
const search = <input type="search" />;
const rename = (
<dialog open>
<input type="text" autoFocus />
</dialog>
);Options#
| Option | Default | What it does |
|---|---|---|
ignoreNonDOM | false | Only check HTML elements, not components. |
example.jsx1 problem
const search = <SearchBox autoFocusThe autoFocus attribute is found here, which can cause usability issues for sighted and non-sighted users. />;example.jsxwith
{ "ignoreNonDOM": true }No problemsconst search = <SearchBox autoFocus />;