Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jsx-a11y/no-autofocus should not warn for false #1014

Open
khinshankhan opened this issue Oct 10, 2024 · 5 comments
Open

jsx-a11y/no-autofocus should not warn for false #1014

khinshankhan opened this issue Oct 10, 2024 · 5 comments

Comments

@khinshankhan
Copy link

khinshankhan commented Oct 10, 2024

I know the docs https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/docs/rules/no-autofocus.md show fail as

<div autoFocus />
<div autoFocus="true" />
<div autoFocus="false" />
<div autoFocus={undefined} />

but I feel like this is bad linting for components outside of developer control. We use the prop on chakra ui components to force it to be false --which is proper a11y.

Ideally a framework would have set it up properly but they didn't. The workaround to set no-autofocus to warning or to throw in comments to ignore each case. However, I think a better way forward would be if the plugin allowed for falsy values (maybe as an option) so it would not fail on <Component autoFocus="false" /> or <Component autoFocus={false} />.

Comments work fine so completely understandable if this isn't the right mentality.

@ljharb
Copy link
Member

ljharb commented Oct 10, 2024

Can you explain why you think it's proper a11y to set autofocus of a div, ever?

@khinshankhan
Copy link
Author

Components implementations outside of developer control may implement it with autofocus true out of the box (which is wrong), so to make it proper, setting autofocus explicitly to false gives back expected behavior (which is right).

Some examples would be chakra ui's drawer component or bootstrap's modal component.

@ljharb
Copy link
Member

ljharb commented Oct 12, 2024

I'm not sure I understand - the linter only warns on a literal <div>, which in that scenario would appear in the third party code, not your own (and you never ever lint third party code).

@khinshankhan
Copy link
Author

That's with ignoreNonDOM enabled. I want it to keep checking developer created components.

This should be fine:

// from third party, unlinted
function ImagineFromPackage(props) {
  return <div autoFocus="true" {...props} />;
}

// my code, linted
function MyComponent() {
  return <ImagineFromPackage autoFocus={false} />;
}

And this should not:

// from third party, unlinted
function ImagineFromPackage(props) {
  return <div {...props} />;
}

// my code, linted
function MyComponent() {
  return <ImagineFromPackage autoFocus />;
}

@ljharb
Copy link
Member

ljharb commented Oct 16, 2024

OK, I agree that should be fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants