-
-
Notifications
You must be signed in to change notification settings - Fork 638
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
[New] no-disabled rule #830
base: main
Are you sure you want to change the base?
Changes from 7 commits
c95e07b
aa41427
1d89958
b2cfb4b
b95ebd2
167c235
a556263
23d6ef9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* eslint-env jest */ | ||
/** | ||
* @fileoverview Enforce disabled prop is not used. | ||
* @author Courtney Nguyen <@courtyenn> | ||
*/ | ||
|
||
// ----------------------------------------------------------------------------- | ||
// Requirements | ||
// ----------------------------------------------------------------------------- | ||
|
||
import { RuleTester } from 'eslint'; | ||
import rule from '../../../src/rules/no-disabled'; | ||
import parserOptionsMapper from '../../__util__/parserOptionsMapper'; | ||
|
||
// ----------------------------------------------------------------------------- | ||
// Tests | ||
// ----------------------------------------------------------------------------- | ||
|
||
const ruleTester = new RuleTester(); | ||
|
||
const expectedWarning = { | ||
message: 'The disabled prop removes the element from being detected by screen readers.', | ||
type: 'JSXAttribute', | ||
}; | ||
|
||
ruleTester.run('no-disabled', rule, { | ||
valid: [ | ||
{ code: '<div />' }, | ||
{ code: '<div disabled />' }, | ||
{ code: '<input />' }, | ||
{ code: '<button />' }, | ||
{ code: '<select />' }, | ||
{ code: '<textarea />' }, | ||
{ code: '<option />' }, | ||
{ code: '<command />' }, | ||
{ code: '<fieldset />' }, | ||
{ code: '<keygen />' }, | ||
{ code: '<optgroup />' }, | ||
].map(parserOptionsMapper), | ||
invalid: [ | ||
{ code: '<input disabled />', errors: [expectedWarning] }, | ||
{ code: '<input disabled="true" />', errors: [expectedWarning] }, | ||
{ code: '<input disabled="false" />', errors: [expectedWarning] }, | ||
{ code: '<button disabled />', errors: [expectedWarning] }, | ||
{ code: '<select disabled />', errors: [expectedWarning] }, | ||
{ code: '<textarea disabled />', errors: [expectedWarning] }, | ||
{ code: '<option disabled />', errors: [expectedWarning] }, | ||
{ code: '<command disabled />', errors: [expectedWarning] }, | ||
{ code: '<fieldset disabled />', errors: [expectedWarning] }, | ||
{ code: '<keygen disabled />', errors: [expectedWarning] }, | ||
{ code: '<optgroup disabled />', errors: [expectedWarning] }, | ||
].map(parserOptionsMapper), | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# no-disabled | ||
|
||
Rule that `disabled` prop should be cautioned on elements. Disabling interactive elements removes the element from the accessibility tree. Consider using `aria-disabled`. | ||
|
||
## Rule details | ||
The general consensus is that `disabled` should be used with specific intent. It goes against intuition since `disabled` is a native HTML attribute, which disables. However, from a usability stand-point it is not a good UX for screen readers: It removes the element from the a11y tree, and may omit critical information. It is best to use `aria-disabled` and add the additional JS logic to "disable" the element. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm going to be a bit more prescriptive here with this text. Apologies for the heavy-handed editing: "The For this reason, we recommend that authors not use the |
||
|
||
### Succeed | ||
```jsx | ||
<div /> | ||
<div disabled /> | ||
<input /> | ||
<select /> | ||
<textarea /> | ||
<button /> | ||
``` | ||
|
||
### Fail | ||
```jsx | ||
<input disabled /> | ||
<input disabled="true" /> | ||
<input disabled="false" /> | ||
<input disabled={undefined} /> | ||
``` | ||
|
||
### Resources | ||
- [MDN aria-disabled](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-disabled) | ||
- [W3 KBD Disabled Controls](https://www.w3.org/TR/wai-aria-practices/#kbd_disabled_controls) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* @fileoverview Rule to flag 'disabled' prop | ||
* @author Courtney Nguyen <@courtyenn> | ||
*/ | ||
|
||
// ---------------------------------------------------------------------------- | ||
// Rule Definition | ||
// ---------------------------------------------------------------------------- | ||
|
||
import { propName, elementType } from 'jsx-ast-utils'; | ||
import { | ||
enumArraySchema, | ||
generateObjSchema, | ||
} from '../util/schemas'; | ||
|
||
const warningMessage = 'The disabled prop removes the element from being detected by screen readers.'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "The disabled attribute makes the element imperceivable to assistive technology agents, like screen readers." Just a small nit that an Also, we should avoid being screen-reader-centric. There are many assistive technology agents. Screen readers are common and probably what a developer is familiar with, so it helps to mention them. We just want to be sure we leave room for the full spectrum of agents that we need to support. |
||
|
||
const DEFAULT_ELEMENTS = [ | ||
jessebeach marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'button', | ||
'command', | ||
'fieldset', | ||
'keygen', | ||
'optgroup', | ||
'option', | ||
'select', | ||
'textarea', | ||
'input', | ||
]; | ||
|
||
const schema = generateObjSchema({ | ||
disabable: enumArraySchema(DEFAULT_ELEMENTS) | ||
.filter((name) => DEFAULT_ELEMENTS.includes(name)), | ||
}); | ||
|
||
export default { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
recommended: false, | ||
url: 'https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/tree/HEAD/docs/rules/no-disabled.md', | ||
}, | ||
schema: [schema], | ||
}, | ||
|
||
create: (context) => ({ | ||
JSXAttribute: (attribute) => { | ||
const disabable = ( | ||
context.options && context.options[0] && context.options[0].disabable | ||
) || DEFAULT_ELEMENTS; | ||
// Only monitor eligible elements for "disabled". | ||
const type = elementType(attribute.parent); | ||
if (!disabable.includes(type)) { | ||
return; | ||
} | ||
|
||
if (propName(attribute) === 'disabled') { | ||
context.report({ | ||
node: attribute, | ||
message: warningMessage, | ||
}); | ||
} | ||
}, | ||
}), | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.