Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1e0d857

Browse files
Merge pull request #446 from github/kendallg/sr-only
Create rule: github/a11y-no-visually-hidden-interactive-element
2 parents dad26dc + c916e88 commit 1e0d857

File tree

6 files changed

+289
-23
lines changed

6 files changed

+289
-23
lines changed

README.md

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -82,28 +82,29 @@ This config will be interpreted in the following way:
8282
🔧 Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).\
8383
❌ Deprecated.
8484

85-
| Name                              | Description | 💼 | 🔧 ||
86-
| :----------------------------------------------------------------------------------- | :----------------------------------------------------------------------------------------------------------------------- | :- | :- | :- |
87-
| [a11y-aria-label-is-well-formatted](docs/rules/a11y-aria-label-is-well-formatted.md) | [aria-label] text should be formatted as you would visual text. | ⚛️ | | |
88-
| [a11y-no-generic-link-text](docs/rules/a11y-no-generic-link-text.md) | disallow generic link text | | ||
89-
| [array-foreach](docs/rules/array-foreach.md) | enforce `for..of` loops over `Array.forEach` || | |
90-
| [async-currenttarget](docs/rules/async-currenttarget.md) | disallow `event.currentTarget` calls inside of async functions | 🔍 | | |
91-
| [async-preventdefault](docs/rules/async-preventdefault.md) | disallow `event.preventDefault` calls inside of async functions | 🔍 | | |
92-
| [authenticity-token](docs/rules/authenticity-token.md) | disallow usage of CSRF tokens in JavaScript | 🔐 | | |
93-
| [get-attribute](docs/rules/get-attribute.md) | disallow wrong usage of attribute names | 🔍 | 🔧 | |
94-
| [js-class-name](docs/rules/js-class-name.md) | enforce a naming convention for js- prefixed classes | 🔐 | | |
95-
| [no-blur](docs/rules/no-blur.md) | disallow usage of `Element.prototype.blur()` | 🔍 | | |
96-
| [no-d-none](docs/rules/no-d-none.md) | disallow usage the `d-none` CSS class | 🔐 | | |
97-
| [no-dataset](docs/rules/no-dataset.md) | enforce usage of `Element.prototype.getAttribute` instead of `Element.prototype.datalist` | 🔍 | | |
98-
| [no-dynamic-script-tag](docs/rules/no-dynamic-script-tag.md) | disallow creating dynamic script tags || | |
99-
| [no-implicit-buggy-globals](docs/rules/no-implicit-buggy-globals.md) | disallow implicit global variables || | |
100-
| [no-inner-html](docs/rules/no-inner-html.md) | disallow `Element.prototype.innerHTML` in favor of `Element.prototype.textContent` | 🔍 | | |
101-
| [no-innerText](docs/rules/no-innerText.md) | disallow `Element.prototype.innerText` in favor of `Element.prototype.textContent` | 🔍 | 🔧 | |
102-
| [no-then](docs/rules/no-then.md) | enforce using `async/await` syntax over Promises || | |
103-
| [no-useless-passive](docs/rules/no-useless-passive.md) | disallow marking a event handler as passive when it has no effect | 🔍 | 🔧 | |
104-
| [prefer-observers](docs/rules/prefer-observers.md) | disallow poorly performing event listeners | 🔍 | | |
105-
| [require-passive-events](docs/rules/require-passive-events.md) | enforce marking high frequency event handlers as passive | 🔍 | | |
106-
| [role-supports-aria-props](docs/rules/role-supports-aria-props.md) | Enforce that elements with explicit or implicit roles defined contain only `aria-*` properties supported by that `role`. | ⚛️ | | |
107-
| [unescaped-html-literal](docs/rules/unescaped-html-literal.md) | disallow unescaped HTML literals | 🔍 | | |
85+
| Name                                        | Description | 💼 | 🔧 ||
86+
| :------------------------------------------------------------------------------------------------------- | :----------------------------------------------------------------------------------------------------------------------- | :- | :- | :- |
87+
| [a11y-aria-label-is-well-formatted](docs/rules/a11y-aria-label-is-well-formatted.md) | [aria-label] text should be formatted as you would visual text. | ⚛️ | | |
88+
| [a11y-no-generic-link-text](docs/rules/a11y-no-generic-link-text.md) | disallow generic link text | | ||
89+
| [a11y-no-visually-hidden-interactive-element](docs/rules/a11y-no-visually-hidden-interactive-element.md) | Ensures that interactive elements are not visually hidden | ⚛️ | | |
90+
| [array-foreach](docs/rules/array-foreach.md) | enforce `for..of` loops over `Array.forEach` || | |
91+
| [async-currenttarget](docs/rules/async-currenttarget.md) | disallow `event.currentTarget` calls inside of async functions | 🔍 | | |
92+
| [async-preventdefault](docs/rules/async-preventdefault.md) | disallow `event.preventDefault` calls inside of async functions | 🔍 | | |
93+
| [authenticity-token](docs/rules/authenticity-token.md) | disallow usage of CSRF tokens in JavaScript | 🔐 | | |
94+
| [get-attribute](docs/rules/get-attribute.md) | disallow wrong usage of attribute names | 🔍 | 🔧 | |
95+
| [js-class-name](docs/rules/js-class-name.md) | enforce a naming convention for js- prefixed classes | 🔐 | | |
96+
| [no-blur](docs/rules/no-blur.md) | disallow usage of `Element.prototype.blur()` | 🔍 | | |
97+
| [no-d-none](docs/rules/no-d-none.md) | disallow usage the `d-none` CSS class | 🔐 | | |
98+
| [no-dataset](docs/rules/no-dataset.md) | enforce usage of `Element.prototype.getAttribute` instead of `Element.prototype.datalist` | 🔍 | | |
99+
| [no-dynamic-script-tag](docs/rules/no-dynamic-script-tag.md) | disallow creating dynamic script tags || | |
100+
| [no-implicit-buggy-globals](docs/rules/no-implicit-buggy-globals.md) | disallow implicit global variables || | |
101+
| [no-inner-html](docs/rules/no-inner-html.md) | disallow `Element.prototype.innerHTML` in favor of `Element.prototype.textContent` | 🔍 | | |
102+
| [no-innerText](docs/rules/no-innerText.md) | disallow `Element.prototype.innerText` in favor of `Element.prototype.textContent` | 🔍 | 🔧 | |
103+
| [no-then](docs/rules/no-then.md) | enforce using `async/await` syntax over Promises || | |
104+
| [no-useless-passive](docs/rules/no-useless-passive.md) | disallow marking a event handler as passive when it has no effect | 🔍 | 🔧 | |
105+
| [prefer-observers](docs/rules/prefer-observers.md) | disallow poorly performing event listeners | 🔍 | | |
106+
| [require-passive-events](docs/rules/require-passive-events.md) | enforce marking high frequency event handlers as passive | 🔍 | | |
107+
| [role-supports-aria-props](docs/rules/role-supports-aria-props.md) | Enforce that elements with explicit or implicit roles defined contain only `aria-*` properties supported by that `role`. | ⚛️ | | |
108+
| [unescaped-html-literal](docs/rules/unescaped-html-literal.md) | disallow unescaped HTML literals | 🔍 | | |
108109

109110
<!-- end auto-generated rules list -->
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Ensures that interactive elements are not visually hidden (`github/a11y-no-visually-hidden-interactive-element`)
2+
3+
💼 This rule is enabled in the ⚛️ `react` config.
4+
5+
<!-- end auto-generated rule header -->
6+
7+
## Rule Details
8+
9+
This rule guards against visually hiding interactive elements. If a sighted keyboard user navigates to an interactive element that is visually hidden they might become confused and assume that keyboard focus has been lost.
10+
11+
Note: we are not guarding against visually hidden `input` elements at this time. Some visually hidden inputs might cause a false positive (e.g. some file inputs).
12+
13+
### Why do we visually hide content?
14+
15+
Visually hiding content can be useful when you want to provide information specifically to screen reader users or other assitive technology users while keeping content hidden from sighted users.
16+
17+
Applying the following css will visually hide content while still making it accessible to screen reader users.
18+
19+
```css
20+
clip-path: inset(50%);
21+
height: 1px;
22+
overflow: hidden;
23+
position: absolute;
24+
white-space: nowrap;
25+
width: 1px;
26+
```
27+
28+
👎 Examples of **incorrect** code for this rule:
29+
30+
```jsx
31+
<button className="visually-hidden">Submit</button>
32+
```
33+
34+
```jsx
35+
<VisuallyHidden>
36+
<button>Submit</button>
37+
</VisuallyHidden>
38+
```
39+
40+
```jsx
41+
<VisuallyHidden as="button">Submit</VisuallyHidden>
42+
```
43+
44+
👍 Examples of **correct** code for this rule:
45+
46+
```jsx
47+
<h2 className="visually-hidden">Welcome to GitHub</h2>
48+
```
49+
50+
```jsx
51+
<VisuallyHidden>
52+
<h2>Welcome to GitHub</h2>
53+
</VisuallyHidden>
54+
```
55+
56+
```jsx
57+
<VisuallyHidden as="h2">Welcome to GitHub</VisuallyHidden>
58+
```
59+
60+
## Options
61+
62+
- className - A css className that visually hides content. Defaults to `sr-only`.
63+
- componentName - A react component name that visually hides content. Defaults to `VisuallyHidden`.
64+
- htmlPropName - A prop name used to replace the semantic element that is rendered. Defaults to `as`.
65+
66+
```json
67+
{
68+
"a11y-no-visually-hidden-interactive-element": [
69+
"error",
70+
{
71+
"className": "visually-hidden",
72+
"componentName": "VisuallyHidden",
73+
"htmlPropName": "as"
74+
}
75+
]
76+
}
77+
```
78+
79+
## Version

lib/configs/react.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module.exports = {
1010
rules: {
1111
'jsx-a11y/role-supports-aria-props': 'off', // Override with github/role-supports-aria-props until https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/issues/910 is resolved
1212
'github/a11y-aria-label-is-well-formatted': 'error',
13+
'github/a11y-no-visually-hidden-interactive-element': 'error',
1314
'github/role-supports-aria-props': 'error',
1415
'jsx-a11y/no-aria-hidden-on-focusable': 'error',
1516
'jsx-a11y/no-autofocus': 'off',

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module.exports = {
22
rules: {
3+
'a11y-no-visually-hidden-interactive-element': require('./rules/a11y-no-visually-hidden-interactive-element'),
34
'a11y-no-generic-link-text': require('./rules/a11y-no-generic-link-text'),
45
'a11y-aria-label-is-well-formatted': require('./rules/a11y-aria-label-is-well-formatted'),
56
'array-foreach': require('./rules/array-foreach'),
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
const {getProp, getPropValue} = require('jsx-ast-utils')
2+
const {getElementType} = require('../utils/get-element-type')
3+
const {generateObjSchema} = require('eslint-plugin-jsx-a11y/lib/util/schemas')
4+
5+
const defaultClassName = 'sr-only'
6+
const defaultcomponentName = 'VisuallyHidden'
7+
const defaultHtmlPropName = 'as'
8+
9+
const schema = generateObjSchema({
10+
className: {type: 'string'},
11+
componentName: {type: 'string'},
12+
htmlPropName: {type: 'string'},
13+
})
14+
15+
/** Note: we are not including input elements at this time
16+
* because a visually hidden input field might cause a false positive.
17+
* (e.g. fileUpload https://github.com/primer/react/pull/3492)
18+
*/
19+
const INTERACTIVELEMENTS = ['a', 'button', 'summary', 'select', 'option', 'textarea']
20+
21+
const checkIfInteractiveElement = (context, htmlPropName, node) => {
22+
const elementType = getElementType(context, node.openingElement)
23+
const asProp = getPropValue(getProp(node.openingElement.attributes, htmlPropName))
24+
25+
for (const interactiveElement of INTERACTIVELEMENTS) {
26+
if ((asProp ?? elementType) === interactiveElement) {
27+
return true
28+
}
29+
}
30+
return false
31+
}
32+
33+
// if the node is visually hidden recursively check if it has interactive children
34+
const checkIfVisuallyHiddenAndInteractive = (context, options, node, isParentVisuallyHidden) => {
35+
const {className, componentName, htmlPropName} = options
36+
if (node.type === 'JSXElement') {
37+
const classes = getPropValue(getProp(node.openingElement.attributes, 'className'))
38+
const isVisuallyHiddenElement = node.openingElement.name.name === componentName
39+
const hasSROnlyClass = typeof classes !== 'undefined' && classes.includes(className)
40+
let isHidden = false
41+
if (hasSROnlyClass || isVisuallyHiddenElement || !!isParentVisuallyHidden) {
42+
if (checkIfInteractiveElement(context, htmlPropName, node)) {
43+
return true
44+
}
45+
isHidden = true
46+
}
47+
if (node.children && node.children.length > 0) {
48+
return (
49+
typeof node.children?.find(child =>
50+
checkIfVisuallyHiddenAndInteractive(context, options, child, !!isParentVisuallyHidden || isHidden),
51+
) !== 'undefined'
52+
)
53+
}
54+
}
55+
return false
56+
}
57+
58+
module.exports = {
59+
meta: {
60+
docs: {
61+
description: 'Ensures that interactive elements are not visually hidden',
62+
url: require('../url')(module),
63+
},
64+
schema: [schema],
65+
},
66+
67+
create(context) {
68+
const {options} = context
69+
const config = options[0] || {}
70+
const className = config.className || defaultClassName
71+
const componentName = config.componentName || defaultcomponentName
72+
const htmlPropName = config.htmlPropName || defaultHtmlPropName
73+
74+
return {
75+
JSXElement: node => {
76+
if (checkIfVisuallyHiddenAndInteractive(context, {className, componentName, htmlPropName}, node, false)) {
77+
context.report({
78+
node,
79+
message:
80+
'Avoid visually hidding interactive elements. Visually hiding interactive elements can be confusing to sighted keyboard users as it appears their focus has been lost when they navigate to the hidden element.',
81+
})
82+
return
83+
}
84+
},
85+
}
86+
},
87+
}

0 commit comments

Comments
 (0)