Static AST checker for accessibility rules on JSX elements.
npm install eslint-plugin-jsx-a11y
alt="CI status" />
alt="npm version">
alt="license">
alt='Total npm downloads' />
Get professional support for eslint-plugin-jsx-a11y on Tidelift
Static AST checker for accessibility rules on JSX elements.
#### _Read this in other languages._
This plugin does aΒ static evaluation of the JSX to spot accessibility issues in React apps. Because it only catches errors in static code, use it in combination with @axe-core/react to test the accessibility of the rendered DOM. Consider theseΒ toolsΒ just as one step of a larger a11y testing process andΒ always test your apps with assistive technology.
If you are installing this plugin via eslint-config-airbnb, please follow these instructions.
You'll first need to install ESLint:
``shnpm
npm install eslint --save-dev
Next, install
eslint-plugin-jsx-a11y:`sh
npm
npm install eslint-plugin-jsx-a11y --save-devyarn
yarn add eslint-plugin-jsx-a11y --dev
`Note: If you installed ESLint globally (using the
-g flag in npm, or the global prefix in yarn) then you must also install eslint-plugin-jsx-a11y globally.Usage - Legacy Config (
.eslintrc)Add
jsx-a11y to the plugins section of your .eslintrc configuration file. You can omit the eslint-plugin- prefix:`json
{
"plugins": ["jsx-a11y"]
}
`Then configure the rules you want to use under the rules section.
`json
{
"rules": {
"jsx-a11y/rule-name": 2
}
}
`You can also enable all the recommended or strict rules at once.
Add
plugin:jsx-a11y/recommended or plugin:jsx-a11y/strict in extends:`json
{
"extends": ["plugin:jsx-a11y/recommended"]
}
`$3
> As you are extending our configuration, you can omit
"plugins": ["jsx-a11y"] from your .eslintrc configuration file.`json
{
"settings": {
"jsx-a11y": {
"polymorphicPropName": "as",
"components": {
"CityInput": "input",
"CustomButton": "button",
"MyButton": "button",
"RoundButton": "button"
},
"attributes": {
"for": ["htmlFor", "for"]
}
}
}
}
`Usage - Flat Config (
eslint.config.js)The default export of
eslint-plugin-jsx-a11y is a plugin object.`js
const jsxA11y = require('eslint-plugin-jsx-a11y');module.exports = [
β¦
{
files: ['*/.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}'],
plugins: {
'jsx-a11y': jsxA11y,
},
languageOptions: {
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
},
rules: {
// ... any rules you want
'jsx-a11y/alt-text': 'error',
},
// ... others are omitted for brevity
},
β¦
];
`$3
There are two shareable configs, provided by the plugin.
-
flatConfigs.recommended
- flatConfigs.strict#### CJS
`js
const jsxA11y = require('eslint-plugin-jsx-a11y');export default [
jsxA11y.flatConfigs.recommended,
{
// Your additional configs and overrides
},
];
`#### ESM
`js
import jsxA11y from 'eslint-plugin-jsx-a11y';export default [
jsxA11y.flatConfigs.recommended,
{
// Your additional configs and overrides
},
];
`Note: Our shareable configs do NOT configure
files or languageOptions.globals.
For most of the cases, you probably want to configure some of these properties yourself.`js
const jsxA11y = require('eslint-plugin-jsx-a11y');
const globals = require('globals');module.exports = [
β¦
{
files: ['*/.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}'],
...jsxA11y.flatConfigs.recommended,
languageOptions: {
...jsxA11y.flatConfigs.recommended.languageOptions,
globals: {
...globals.serviceworker,
...globals.browser,
},
},
},
β¦
];
`#### Component Mapping
To enable your custom components to be checked as DOM elements, you can set global settings in your configuration file by mapping each custom component name to a DOM element type.
#### Attribute Mapping
To configure the JSX property to use for attribute checking, you can set global settings in your configuration file by mapping each DOM attribute to the JSX property you want to check.
For example, you may want to allow the
for attribute in addition to the htmlFor attribute for checking label associations.#### Polymorphic Components
You can optionally use the
polymorphicPropName setting to define the prop your code uses to create polymorphic components.
This setting will be used determine the element type in rules that require semantic context.For example, if you set the
polymorphicPropName setting to as then this element:will be evaluated as an
h3. If no polymorphicPropName is set, then the component will be evaluated as Box.To restrict polymorphic linting to specified components, additionally set
polymorphicAllowList to an array of component names.β οΈ Polymorphic components can make code harder to maintain; please use this feature with caution.
Supported Rules
πΌ Configurations enabled in.\
π« Configurations disabled in.\
βοΈ Set in the
recommended configuration.\
π Set in the strict configuration.\
β Deprecated.| NameΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β | Description | πΌ | π« | β |
| :----------------------------------------------------------------------------------------------------------- | :---------------------------------------------------------------------------------------------------------------------------------- | :---- | :---- | :- |
| accessible-emoji | Enforce emojis are wrapped in
and provide screen reader access. | | | β |
| alt-text | Enforce all elements that require alternative text have meaningful information to relay back to end user. | βοΈ π | | |
| anchor-ambiguous-text | Enforce text to not exactly match "click here", "here", "link", or "a link". | | βοΈ | |
| anchor-has-content | Enforce all anchors to contain accessible content. | βοΈ π | | |
| anchor-is-valid | Enforce all anchors are valid, navigable elements. | βοΈ π | | |
| aria-activedescendant-has-tabindex | Enforce elements with aria-activedescendant are tabbable. | βοΈ π | | |
| aria-props | Enforce all aria-* props are valid. | βοΈ π | | |
| aria-proptypes | Enforce ARIA state and property values are valid. | βοΈ π | | |
| aria-role | Enforce that elements with ARIA roles must use a valid, non-abstract ARIA role. | βοΈ π | | |
| aria-unsupported-elements | Enforce that elements that do not support ARIA roles, states, and properties do not have those attributes. | βοΈ π | | |
| autocomplete-valid | Enforce that autocomplete attributes are used correctly. | βοΈ π | | |
| click-events-have-key-events | Enforce a clickable non-interactive element has at least one keyboard event listener. | βοΈ π | | |
| control-has-associated-label | Enforce that a control (an interactive element) has a text label. | | βοΈ π | |
| heading-has-content | Enforce heading (h1, h2, etc) elements contain accessible content. | βοΈ π | | |
| html-has-lang | Enforce element has lang prop. | βοΈ π | | |
| iframe-has-title | Enforce iframe elements have a title attribute. | βοΈ π | | |
| img-redundant-alt | Enforce alt prop does not contain the word "image", "picture", or "photo". | βοΈ π | | |
| interactive-supports-focus | Enforce that elements with interactive handlers like onClick must be focusable. | βοΈ π | | |
| label-has-associated-control | Enforce that a label tag has a text label and an associated control. | βοΈ π | | |
| label-has-for | Enforce that