6.6.1 ‚ÄĘ Published 4 months ago

eslint-plugin-jsx-a11y v6.6.1

Weekly downloads
5,851,498
License
MIT
Repository
github
Last release
4 months ago

Get professional support for eslint-plugin-jsx-a11y on Tidelift

eslint-plugin-jsx-a11y

Static AST checker for accessibility rules on JSX elements.

Read this in other languages.

Mexican Spanishūüá≤ūüáĹ

Why?

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.

Installation

If you are installing this plugin via eslint-config-airbnb, please follow these instructions.

You'll first need to install ESLint:

# npm
npm install eslint --save-dev

# yarn
yarn add eslint --dev

Next, install eslint-plugin-jsx-a11y:

# npm
npm install eslint-plugin-jsx-a11y --save-dev

# yarn
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

Add jsx-a11y to the plugins section of your .eslintrc configuration file. You can omit the eslint-plugin- prefix:

{
  "plugins": ["jsx-a11y"]
}

Then configure the rules you want to use under the rules section.

{
  "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:

{
  "extends": ["plugin:jsx-a11y/recommended"]
}

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.

{
  "settings": {
    "jsx-a11y": {
      "components": {
        "CityInput": "input",
        "CustomButton": "button",
        "MyButton": "button",
        "RoundButton": "button"
      }
    }
  }
}

Supported Rules

Rule strictness in different modes

RuleRecommendedStrict
accessible-emojioffoff
alt-texterrorerror
anchor-has-contenterrorerror
anchor-is-validerrorerror
aria-activedescendant-has-tabindexerrorerror
aria-propserrorerror
aria-proptypeserrorerror
aria-roleerrorerror
aria-unsupported-elementserrorerror
autocomplete-validerrorerror
click-events-have-key-eventserrorerror
control-has-associated-labeloffoff
heading-has-contenterrorerror
html-has-langerrorerror
iframe-has-titleerrorerror
img-redundant-alterrorerror
interactive-supports-focuserrorerror
label-has-associated-controlerrorerror
label-has-foroffoff
langoffoff
media-has-captionerrorerror
mouse-events-have-key-eventserrorerror
no-access-keyerrorerror
no-autofocuserrorerror
no-distracting-elementserrorerror
no-interactive-element-to-noninteractive-roleerror, with optionserror
no-noninteractive-element-interactionserror, with optionserror
no-noninteractive-element-to-interactive-roleerror, with optionserror
no-noninteractive-tabindexerror, with optionserror
no-onchangeoffoff
no-redundant-roleserrorerror
no-static-element-interactionserror, with optionserror
role-has-required-aria-propserrorerror
role-supports-aria-propserrorerror
scopeerror, with optionserror
tabindex-no-positiveerrorerror

The following rules have extra options when in recommended mode:

no-interactive-element-to-noninteractive-role

'jsx-a11y/no-interactive-element-to-noninteractive-role': [
  'error',
  {
    tr: ['none', 'presentation'],
  },
]

no-noninteractive-element-interactions

'jsx-a11y/no-noninteractive-element-interactions': [
  'error',
  {
    handlers: [
      'onClick',
      'onMouseDown',
      'onMouseUp',
      'onKeyPress',
      'onKeyDown',
      'onKeyUp',
    ],
  },
]

no-noninteractive-element-to-interactive-role

'jsx-a11y/no-noninteractive-element-to-interactive-role': [
  'error',
  {
    ul: [
      'listbox',
      'menu',
      'menubar',
      'radiogroup',
      'tablist',
      'tree',
      'treegrid',
    ],
    ol: [
      'listbox',
      'menu',
      'menubar',
      'radiogroup',
      'tablist',
      'tree',
      'treegrid',
    ],
    li: ['menuitem', 'option', 'row', 'tab', 'treeitem'],
    table: ['grid'],
    td: ['gridcell'],
  },
]

no-noninteractive-tabindex

'jsx-a11y/no-noninteractive-tabindex': [
  'error',
  {
    tags: [],
    roles: ['tabpanel'],
  },
]

no-static-element-interactions

'jsx-a11y/no-noninteractive-element-interactions': [
  'error',
  {
    handlers: [
      'onClick',
      'onMouseDown',
      'onMouseUp',
      'onKeyPress',
      'onKeyDown',
      'onKeyUp',
    ],
  },
]

Creating a new rule

If you are developing new rules for this project, you can use the create-rule script to scaffold the new files.

$ ./scripts/create-rule.js my-new-rule

Some background on WAI-ARIA, the AX Tree and Browsers

Accessibility API

An operating system will provide an accessibility API that maps application state and content onto input/output controllers such as a screen reader, braille device, keyboard, etc.

These APIs were developed as computer interfaces shifted from buffers (which are text-based and inherently quite accessible) to graphical user interfaces (GUIs). The first attempts to make GUIs accessible involved raster image parsing to recognize characters, words, etc. This information was stored in a parallel buffer and made accessible to assistive technology (AT) devices.

As GUIs became more complex, the raster parsing approach became untenable. Accessibility APIs were developed to replace them. Check out NSAccessibility (AXAPI) for an example. See Core Accessibility API Mappings 1.1 for more details.

Browsers

Browsers support an Accessibility API on a per operating system basis. For instance, Firefox implements the MSAA accessibility API on Windows, but does not implement the AXAPI on OSX.

The Accessibility (AX) Tree & DOM

From the W3 Core Accessibility API Mappings 1.1

The accessibility tree and the DOM tree are parallel structures. Roughly speaking the accessibility tree is a subset of the DOM tree. It includes the user interface objects of the user agent and the objects of the document. Accessible objects are created in the accessibility tree for every DOM element that should be exposed to assistive technology, either because it may fire an accessibility event or because it has a property, relationship or feature which needs to be exposed. Generally, if something can be trimmed out it will be, for reasons of performance and simplicity. For example, a <span> with just a style change and no semantics may not get its own accessible object, but the style change will be exposed by other means.

Browser vendors are beginning to expose the AX Tree through inspection tools. Chrome has an experiment available to enable their inspection tool.

You can also see a text-based version of the AX Tree in Chrome in the stable release version.

Viewing the AX Tree in Chrome

  1. Navigate to chrome://accessibility/ in Chrome.
  2. Toggle the accessibility off link for any tab that you want to inspect.
  3. A link labeled show accessibility tree will appear; click this link.
  4. Balk at the wall of text that gets displayed, but then regain your conviction.
  5. Use the browser's find command to locate strings and values in the wall of text.

Pulling it all together

A browser constructs an AX Tree as a subset of the DOM. ARIA heavily informs the properties of this AX Tree. This AX Tree is exposed to the system level Accessibility API which mediates assistive technology agents.

We model ARIA in the aria-query project. We model AXObjects (that comprise the AX Tree) in the axobject-query project. The goal of the WAI-ARIA specification is to be a complete declarative interface to the AXObject model. The in-draft 1.2 version is moving towards this goal. But until then, we must consider the semantics constructs afforded by ARIA as well as those afforded by the AXObject model (AXAPI) in order to determine how HTML can be used to express user interface affordances to assistive technology users.

License

eslint-plugin-jsx-a11y is licensed under the MIT License.

eslint-config-react-appverdaccio-okta-auth@dailyswap/eslint-config-tea@ainultech/eslint-config@thaicoding/eslint-configreact-wind-ui@bedrockstreaming/eslint-plugin-reacteslint-config-amanhimselfeslint-config-metamosmart-input-demobyteplayerkstick-react-scriptsdecorators-and-sass-react-scripts@oliveiraswell/eslint-config-oliveiraswell-js@joelcox22/boilerplatelistner-resize-pagekilli8n-react-native-fast-imagewatsize-test-react-scriptsnoodles-normlookly-preset-eslint@mate-academy/eslint-config-frontend-internalglobal-martcomatch-mvp-debuggingmobilelivedesignsystemreact@darapti/darapti-react-scripts@webbranch/eslint-configswitch_mostqroda-sdkqroda-sdk-v1metaliquid-react-scriptsrockywu-uieslint-config-sunrun@tomas2d/react-scriptsmuil-tempjbojcic-react-scriptstext-click-scroll@paperlesspost/react-scripts@tenjo/eslint-config@tetondev/lint-config@jvhellemondt/custom-react-scriptstrippkit@kolesa-group/eslint-config@st4rfox/electron-scriptssv-component-librarysv-component-library-1sv-component-library-2gatsby-starter-emulsify-drupal@thuanpq/airvert-ui@xinyifly/react-scripts@ipavinthan/react-scriptsmio-cli-service@plarin/build-configsandculturecode-react-scriptswebeetle-react-scriptsyadong-react@agney/react-scripts-tailwindzackmoreplay-react-scriptsgooapps-react-opssonos-radio-salithefarm-react-scripts@doordash/eslint-config-doordash@wywk/headquaters-marketing-eventconcord-ui-plugin-react-scriptssebrae-boilerplatesebrae-react-scripts@cloudhuset/zd-app-react-scripts@analogyxbi-ui/build-configidelic-react-scriptseslint-config-wildcattest-app-6test-app-7tm-components-testajsr-eslint-config-react@dormdev/eslint-config-dormdev@jvhellemondt/react-scriptsearthling-interactive-react-scripts@devesharp/typescriptdesign-frameworkopinion-pollkolesa-group-eslint-confighoogo-niuyapingreact-epic-image-slider@frankian/eslint-configeslint-config-dseslint-config-devesharpeslint-config-bigpopakapkg-stylelint-configeslint-config-hubside@rock-kit/ui-eslint-configpejann-scriptsmy-custom-react-jwplayerdne-react-starterinversify-react-scriptselectron-cra-scriptsecom-catalogkolbeinsson88-assignment5kolbeinsson88-stylesjs-advancedreact-scripts-public-urlzula-bravo
6.6.1

4 months ago

6.6.0

5 months ago

6.5.0

1 year ago

6.5.1

1 year ago

6.4.1

2 years ago

6.4.0

2 years ago

6.3.1

2 years ago

6.3.0

2 years ago

6.2.3

3 years ago

6.2.2

3 years ago

6.2.1

4 years ago

6.2.0

4 years ago

6.1.2

4 years ago

6.1.1

4 years ago

6.1.0

4 years ago

6.0.3

5 years ago

5.1.1

5 years ago

6.0.2

5 years ago

6.0.1

5 years ago

6.0.0

5 years ago

5.1.0

5 years ago

5.0.3

6 years ago

5.0.2

6 years ago

5.0.1

6 years ago

5.0.0

6 years ago

4.0.0

6 years ago

3.0.2

6 years ago

3.0.1

6 years ago

3.0.0

6 years ago

2.2.3

6 years ago

2.2.2

6 years ago

2.2.1

6 years ago

2.2.0

6 years ago

2.1.0

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.5.5

6 years ago

1.5.4

6 years ago

1.5.3

6 years ago

1.5.2

6 years ago

1.5.1

6 years ago

1.5.0

6 years ago

1.4.2

6 years ago

1.4.1

6 years ago

1.4.0

6 years ago

1.3.0

6 years ago

1.2.3

7 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.0

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.6.2

7 years ago

0.6.1

7 years ago

0.6.0

7 years ago

0.5.4

7 years ago

0.5.3

7 years ago

0.5.2

7 years ago

0.5.1

7 years ago

0.5.0

7 years ago

0.4.3

7 years ago

0.4.2

7 years ago

0.4.1

7 years ago

0.4.0

7 years ago

0.3.1

7 years ago

0.3.0

7 years ago

0.2.2

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago