### Legacy Config Setup (.eslintrc) for ESLint Plugin JSX A11y
Source: https://context7.com/jsx-eslint/eslint-plugin-jsx-a11y/llms.txt
Configuration example for ESLint Plugin JSX A11y using the legacy .eslintrc JSON format, applying the recommended preset and custom rule settings.
```json
{
"plugins": ["jsx-a11y"],
"extends": ["plugin:jsx-a11y/recommended"],
"rules": {
"jsx-a11y/anchor-is-valid": "error",
"jsx-a11y/alt-text": "error"
}
}
```
--------------------------------
### Flat Config Setup (CommonJS) for ESLint Plugin JSX A11y
Source: https://context7.com/jsx-eslint/eslint-plugin-jsx-a11y/llms.txt
Configuration example for ESLint Plugin JSX A11y using the flat config format for CommonJS projects. It includes recommended rules and custom rule overrides.
```javascript
// eslint.config.cjs
const globals = require('globals');
const js = require('@eslint/js');
const jsxA11y = require('eslint-plugin-jsx-a11y');
module.exports = [
js.configs.recommended,
jsxA11y.flatConfigs.recommended,
{
files: ['**/*.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}'],
languageOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
globals: globals.browser,
},
rules: {
'jsx-a11y/no-autofocus': 'off',
},
},
];
```
--------------------------------
### Install ESLint and Plugin
Source: https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/README.md
Installs ESLint and the eslint-plugin-jsx-a11y package as development dependencies using npm or yarn.
```sh
# npm
npm install eslint --save-dev
npm install eslint-plugin-jsx-a11y --save-dev
# yarn
yarn add eslint --dev
yarn add eslint-plugin-jsx-a11y --dev
```
--------------------------------
### Flat Config Setup (ESM) for ESLint Plugin JSX A11y
Source: https://context7.com/jsx-eslint/eslint-plugin-jsx-a11y/llms.txt
Configuration example for ESLint Plugin JSX A11y using the modern flat config format for ES modules projects. It includes recommended rules and custom rule overrides.
```javascript
// eslint.config.js
import globals from 'globals';
import js from '@eslint/js';
import jsxA11y from 'eslint-plugin-jsx-a11y';
export default [
js.configs.recommended,
jsxA11y.flatConfigs.recommended,
{
files: ['**/*.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}'],
languageOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
globals: globals.browser,
},
rules: {
// Override specific rules as needed
'jsx-a11y/anchor-ambiguous-text': 'warn',
'jsx-a11y/anchor-is-valid': 'warn',
},
},
];
```
--------------------------------
### Install ESLint Plugin JSX A11y (npm/yarn)
Source: https://context7.com/jsx-eslint/eslint-plugin-jsx-a11y/llms.txt
Commands to install the eslint-plugin-jsx-a11y package along with ESLint as development dependencies using npm or yarn.
```bash
# Install with npm
npm install eslint eslint-plugin-jsx-a11y --save-dev
# Install with yarn
yarn add eslint eslint-plugin-jsx-a11y --dev
```
--------------------------------
### Configure interactive-supports-focus rule
Source: https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/docs/rules/interactive-supports-focus.md
Example of how to configure the rule in an ESLint configuration file, specifying which roles should be treated as tabbable.
```javascript
{
'jsx-a11y/interactive-supports-focus': [
'error',
{
tabbable: [
'button',
'checkbox',
'link',
'searchbox',
'spinbutton',
'switch',
'textbox',
],
},
]
}
```
--------------------------------
### Configure anchor-ambiguous-text rule
Source: https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/docs/rules/anchor-ambiguous-text.md
Example configuration for the ESLint rule, demonstrating how to provide a custom list of ambiguous words.
```json
{
"rules": {
"jsx-a11y/anchor-ambiguous-text": [2, {
"words": ["click this"]
}]
}
}
```
--------------------------------
### Configure img-redundant-alt Rule
Source: https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/docs/rules/img-redundant-alt.md
Example configuration for the ESLint rule, demonstrating how to define custom components and additional forbidden words in the rule options.
```json
{
"rules": {
"jsx-a11y/img-redundant-alt": [ 2, {
"components": [ "Image" ],
"words": [ "Bild", "Foto" ]
}]
}
}
```
--------------------------------
### Configure mouse-events-have-key-events rule
Source: https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/docs/rules/mouse-events-have-key-events.md
Example configuration for the ESLint rule in a JSON format. It demonstrates how to customize hoverInHandlers and hoverOutHandlers to include additional pointer events.
```json
{
"rules": {
"jsx-a11y/mouse-events-have-key-events": [
"error",
{
"hoverInHandlers": [
"onMouseOver",
"onMouseEnter",
"onPointerOver",
"onPointerEnter"
],
"hoverOutHandlers": [
"onMouseOut",
"onMouseLeave",
"onPointerOut",
"onPointerLeave"
]
}
]
}
}
```
--------------------------------
### JSX Media Element Caption Examples
Source: https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/docs/rules/media-has-caption.md
These examples demonstrate correct and incorrect usage of media elements with captions in JSX, according to the jsx-a11y/media-has-caption rule. The 'Succeed' examples show valid implementations, while 'Fail' examples highlight common mistakes.
```jsx
```
```jsx
```
--------------------------------
### alt-text Rule Examples for ESLint Plugin JSX A11y
Source: https://context7.com/jsx-eslint/eslint-plugin-jsx-a11y/llms.txt
Examples demonstrating the correct and incorrect usage of the 'alt-text' accessibility rule in JSX. It covers images, input elements, and objects, including configuration for custom components.
```jsx
// Valid - Images with proper alt text
{/* Empty alt for decorative images */}
// Valid - Input images and areas
// Valid - Object with accessible label
// Invalid - Missing or improper alt
{/* Missing alt */}
{/* Spread without explicit alt */}
{/* Use alt="" instead of role */}
// Rule configuration with custom components
// .eslintrc.json
{
"rules": {
"jsx-a11y/alt-text": ["error", {
"elements": ["img", "object", "area", "input[type=\"image\"]"],
"img": ["Image", "Avatar"],
"object": ["Object"],
"area": ["Area"],
"input[type=\"image\"]": ["InputImage"]
}]
}
}
```
--------------------------------
### JSX Examples for no-redundant-roles
Source: https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/docs/rules/no-redundant-roles.md
These JSX code snippets illustrate the 'no-redundant-roles' rule. The 'Succeed' examples show valid usage where no redundant roles are present, while the 'Fail' examples demonstrate incorrect usage with redundant ARIA roles.
```jsx
```
```jsx
```
--------------------------------
### Validating recommended rule usage
Source: https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/docs/rules/no-noninteractive-element-to-interactive-role.md
An example of an element that is valid under the recommended configuration but would be flagged as invalid under strict rules.
```jsx
```
--------------------------------
### Accessible anchor tag examples
Source: https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/docs/rules/anchor-ambiguous-text.md
Examples of anchor tags that pass the rule because they provide descriptive text or use aria-labels correctly.
```jsx
read this tutorial${here}click here
```
--------------------------------
### Configure jsx-a11y/alt-text rule
Source: https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/docs/rules/alt-text.md
Example of how to configure the rule in your ESLint configuration file, including custom component mapping.
```json
{
"rules": {
"jsx-a11y/alt-text": [ 2, {
"elements": [ "img", "object", "area", "input[type=\"image\"]" ],
"img": ["Image"],
"object": ["Object"],
"area": ["Area"],
"input[type=\"image\"]": ["InputImage"]
}],
}
}
```
--------------------------------
### Examples of Heading Elements with Content (jsx-a11y)
Source: https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/docs/rules/heading-has-content.md
These JSX examples illustrate valid heading elements that satisfy the jsx-a11y/heading-has-content rule. They include headings with direct text content, headings with child components, and headings using dangerouslySetInnerHTML.
```jsx
Heading Content!
```
--------------------------------
### Examples of passing and failing code
Source: https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/docs/rules/alt-text.md
A collection of valid and invalid JSX patterns for elements requiring alternative text.
```jsx
// Succeed
// Fail
```
--------------------------------
### Example of a Label Without Content (jsx-a11y)
Source: https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/docs/rules/heading-has-content.md
This JSX example demonstrates a label element without any content, which would be flagged by accessibility rules if it were intended to be a heading or if the rule was misconfigured. It serves as a contrast to valid heading content.
```jsx
function Foo(props) {
return
}
```
--------------------------------
### Implement valid anchor navigation
Source: https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/docs/rules/anchor-is-valid.md
Provides examples of valid href attribute usage for anchor tags to ensure proper hyperlink functionality.
```jsx
Navigate to pageNavigate to page and locationNavigate to internal page location
```
--------------------------------
### Define custom anchor component
Source: https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/docs/rules/anchor-has-content.md
Example of a wrapper component that renders an anchor, which can be configured in the ESLint rule to be checked for content.
```javascript
const Anchor = props => {
return (
{ props.children }
);
}
```
--------------------------------
### Handling Multiple Labels for One Input
Source: https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/docs/rules/label-has-associated-control.md
Provides an example of how to manage scenarios with multiple labels for a single input, often requiring an eslint override comment for the second label.
```jsx
{/* eslint jsx-a11y/label-has-associated-control: ["error", { assert: "either" } ] */}
...
```
--------------------------------
### JSX Label Nesting Example
Source: https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/docs/rules/label-has-associated-control.md
Demonstrates associating a label with an input by wrapping the input within the label element. This is a simple and effective method for accessibility.
```jsx
```
--------------------------------
### Valid and Invalid JSX usage for mouse events
Source: https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/docs/rules/mouse-events-have-key-events.md
Examples of JSX elements that satisfy or violate the mouse-events-have-key-events rule. Valid examples include both mouse and keyboard handlers, while invalid examples lack the required keyboard event counterparts.
```jsx
// Valid examples
void 0 } />
```
--------------------------------
### Using Expression Values for Roles
Source: https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/docs/rules/no-noninteractive-tabindex.md
Examples of using dynamic expressions for the role attribute when allowExpressionValues is enabled.
```jsx
{}} tabIndex="0" />;
{}} tabIndex="0" />;
```
--------------------------------
### Inaccessible anchor tag examples
Source: https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/docs/rules/anchor-ambiguous-text.md
Examples of anchor tags that fail the rule due to ambiguous text, including cases with nested elements or aria-labels.
```jsx
herelinkclick heresomething
```
--------------------------------
### Custom Link Component Example
Source: https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/docs/rules/anchor-is-valid.md
This JavaScript code demonstrates a custom React Link component that wraps a standard `` element. This is useful for configuring the `anchor-is-valid` rule to recognize custom link components.
```javascript
// Link.js
const Link = props => A link;
...
// NavBar.js (for example)
...
return (
);
```
--------------------------------
### JSX: Failing example - Button without Accessible Label
Source: https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/docs/rules/control-has-associated-label.md
Demonstrates a button that violates the control-has-associated-label rule because it lacks any form of accessible text label.
```jsx
```
--------------------------------
### Invalid html-has-lang usage
Source: https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/docs/rules/html-has-lang.md
Example of an element that fails the rule due to the missing lang attribute.
```jsx
```
--------------------------------
### Validate accessible prop passing
Source: https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/docs/rules/alt-text.md
Examples of correct and incorrect ways to pass props to components to ensure accessibility requirements are met.
```jsx
// Good: Explicitly destructuring props
function Foo({ alt, ...props}) {
return
}
// Bad: Spreading props without explicit accessibility handling
function Foo(props) {
return
}
```
--------------------------------
### ESM Configuration for eslint-plugin-jsx-a11y
Source: https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/README.md
This snippet shows how to import and use the recommended configuration for eslint-plugin-jsx-a11y in an ESM environment. It demonstrates the basic setup for integrating the plugin's recommended rules into your ESLint configuration.
```javascript
import jsxA11y from 'eslint-plugin-jsx-a11y';
export default [
jsxA11y.flatConfigs.recommended,
{
// Your additional configs and overrides
},
];
```
--------------------------------
### Resolving non-interactive element errors
Source: https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/docs/rules/no-noninteractive-element-to-interactive-role.md
Examples showing how to correctly nest interactive controls within non-interactive containers or wrap content inside interactive elements to satisfy accessibility requirements.
```jsx
{}}
onKeyPress={() => {}}>
Save
```
```jsx
{}}
onKeyPress={() => {}}
tabIndex="0">
```
--------------------------------
### ESLint Configuration for jsx-a11y/control-has-associated-label
Source: https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/docs/rules/control-has-associated-label.md
Provides an example of how to configure the jsx-a11y/control-has-associated-label rule in an ESLint configuration file, including options for label attributes, custom components, ignored elements, and roles.
```json
{
"rules": {
"jsx-a11y/control-has-associated-label": [ 2, {
"labelAttributes": ["label"],
"controlComponents": ["CustomComponent"],
"ignoreElements": [
"audio",
"canvas",
"embed",
"input",
"textarea",
"tr",
"video",
],
"ignoreRoles": [
"grid",
"listbox",
"menu",
"menubar",
"radiogroup",
"row",
"tablist",
"toolbar",
"tree",
"treegrid",
],
"depth": 3,
}],
}
}
```
--------------------------------
### Valid html-has-lang usage
Source: https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/docs/rules/html-has-lang.md
Examples of elements that correctly include a lang attribute, either as a static string or a dynamic expression.
```jsx
```
--------------------------------
### Configure jsx-a11y/no-autofocus rule
Source: https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/docs/rules/no-autofocus.md
Example configuration for the ESLint rule in a JSON configuration file. The ignoreNonDOM option allows developers to skip checking custom components.
```json
{
"rules": {
"jsx-a11y/no-autofocus": [ 2, {
"ignoreNonDOM": true
}],
}
}
```
--------------------------------
### Resolving interactive element role conflicts
Source: https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/docs/rules/no-interactive-element-to-noninteractive-role.md
Examples of how to correctly structure HTML elements by wrapping interactive controls or nesting content to maintain proper ARIA semantics.
```jsx
```
```jsx
{}}
onKeyPress={() => {}}
tabIndex="0">
```
--------------------------------
### Valid and Invalid JSX Usage
Source: https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/docs/rules/img-redundant-alt.md
Examples showing valid JSX implementations, including hidden images and dynamic variables, alongside invalid implementations that trigger the rule.
```jsx
// Valid examples
// Invalid examples
```
--------------------------------
### Valid JSX click event implementations
Source: https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/docs/rules/no-noninteractive-element-interactions.md
Examples of JSX elements that satisfy the click-events-have-key-events rule. These include interactive elements like button and input, or elements with appropriate roles and aria attributes.
```jsx