Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

eslint-plugin-react-native-a11y

FormidableLabs914.3kMIT3.5.1

React Native specific accessibility linting rules.

eslint, eslintplugin, eslint-plugin, a11y, accessibility, react-native

readme

Eslint-plugin-react-native-a11y — Formidable, We build the modern web

Maintenance Status

Eslint-plugin-react-native-a11y is a collection of React Native specific ESLint rules for identifying accessibility issues. Building upon the foundation set down by eslint-plugin-jsx-a11y, eslint-plugin-react-native-a11y detects a few of the most commonly made accessibility issues found in react native apps. These rules make it easier for your apps to be navigable by users with screen readers.

Setup

Pre-Requisites

Before starting, check you already have ESLint as a devDependency of your project.

Projects created using react-native init will already have this, but for Expo depending on your template you may need to follow ESLint's installation instructions.

Installation

Next, install eslint-plugin-react-native-a11y:

npm install eslint-plugin-react-native-a11y --save-dev

# or

yarn add eslint-plugin-react-native-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-react-native-a11y globally.

Configuration

This plugin exposes four recommended configs.

Name Description
basic Only use basic validation rules common to both iOS & Android
ios Use all rules from "basic", plus iOS-specific extras
android Use all rules from "basic", plus Android-specific extras
all Use all rules from "basic", plus iOS-specific extras, plus Android-specific extras

If your project only supports a single platform, you may get the best experience using a platform-specific config. This will both avoid reporting issues which do not affect your platform and also results in slightly faster linting for larger projects.

If you are unsure which one to use, in most cases all can be safely used.

Add the config you want to use to the extends section of your ESLint config using the pattern plugin:react-native-a11y/ followed by your config name, as shown below:

// .eslintrc.js

module.exports = {
  root: true,
  extends: ['@react-native-community', 'plugin:react-native-a11y/ios'],
};

Alternatively if you do not want to use one of the pre-defined configs — or want to override the behaviour of a specific rule — you can always include a list rules and configurations in the rules section of your ESLint config.

// .eslintrc.js

module.exports = {
  root: true,
  extends: ['@react-native-community'],
  rules: {
    'react-native-a11y/rule-name': 2,
  },
};

For more information on configuring behaviour of an individual rule, please refer to the ESLint docs

Supported Rules

Basic

iOS

Android

Rule Options

The following options are available to customize the recommended rule set.

Custom Touchables

react-native-a11y/has-accessibility-props and react-native-a11y/no-nested-touchables allow you to define an array of names for custom components that you may have that conform to the same accessibility interfaces as Touchables.

"react-native-a11y/has-accessibility-props": [
  "error",
  {
    "touchables": ["TouchableCustom"]
  }
]

Custom Invertable Components (iOS)

react-native-a11y/has-valid-accessibility-ignores-invert-colors allows you to optionally define an Array of component names to check in addition to <Image />.

For more information, see the rule docs.

"react-native-a11y/has-valid-accessibility-ignores-invert-colors": [
  "error",
  {
    "invertableComponents": [
      "FastImage",
    ]
  }
]

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

Attribution

This project started as a fork of eslint-plugin-jsx-a11y and a lot of the work was carried out by its contributors, to whom we owe a lot!

License

eslint-plugin-react-native-a11y is licensed under the MIT License.

Maintenance Status

Active: Nearform is actively working on this project, and we expect to continue for work for the foreseeable future. Bug reports, feature requests and pull requests are welcome.

changelog

Changes

3.5.1

Patch Changes

  • Fix for has-valid-accessibility-ignores-invert-colors with eslint 9 (#162)

3.5.0

Minor Changes

  • Allow aliasing Images (#93)

3.4.1

Patch Changes

  • Remove changeset from deps (#156)

3.4.0

Minor Changes

  • 0f2e813: Additional support for role and accessibility props

V3.3.0

✨ New Features ✨

  • Allow Eslint 8 as a peer dependency (#145)

V3.2.1

🐛 Bugfixes 🐛

  • update fixer for has-valid-accessibility-descriptors (#136)

V3.2.0

✨ New Features ✨

  • make has-valid-accessibility-descriptors fixable (#131)

🐛 Bugfixes 🐛

  • allow Identifiers in accessibilityState (#129)
  • support spread props in accessibilityState (#132)
  • allow Touchables with accessible={false} (#130)
  • assume MemberExpressions are valid (#133)
  • update has-accessibility-value to only typecheck Literals (#134)
  • update has-accessibility-role typechecking (#135)

V3.1.0

✨ New Features ✨

  • Checks Touchable* components have accessibility props (#128)

V3.0.0

🚨 Breaking 🚨

  • This release removes support for Node 10 (#126)

✨ New Features ✨

  • Allow Eslint 7 as a peer dependency (#111)

🐛 Bugfixes 🐛

  • only validate Literals in accessibilityState (#112)
  • allow Identifiers in accessibilityActions (#113)

V2.0.4

  • include Pressable when checking no-nested-touchables (#103
  • Dependency upgrades (#106)

V2.0.3

  • allow CallExpressions in accessibilityActions (#101)
  • Dependency upgrades (#102)

V2.0.2

  • Update accessibilityState to allow Identifiers for checked value (#98)
  • Dev Dependency upgrades (#99)

V2.0.1

  • Allow Expressions in prop validators (#96)
  • Dependency upgrades (#95)

V2.0.0

  • Minor doc improvements (#78)
  • Dev Dependency upgrades (#89)

V2.0.0-rc2

  • Dev Dependency upgrades (#88)

V2.0.0-rc1

  • Ignore Identifier expressions in bool typechecks (#85)
  • Enabled no-unused-vars (#86)

V2.0.0-rc0

🚨 Breaking 🚨

  • This release removes support for Node 8 (#80)
  • The has-valid-accessibility-state rule has been re-written to cover the new accessibilityState implementation (#60)
  • Deprecates the recommended config and introduces new platform-specific configs (#83)

✨ New Features ✨

  • Adds has-valid-accessibility-value rule for accessibilityValue prop (#68)
  • Adds has-valid-accessibility-actions rule for accessibilityActions and onAccessibilityAction props (#69)
  • Adds has-valid-accessibility-ignores-invert-colors rule for accessibilityIgnoresInvertColors (#73)
  • Adds has-accessibility-hint for accessibilityHint (#74)

🐛 Bugfixes 🐛

  • Removes Touchable~ as a requirement for custom Touchable names (#70)
  • Allows Touchables without either accessibilityRole or both accessibilityTraits and accessibilityComponentType (#81)
  • Removes has-accessibility-label rule (#82)

V1.3.1

  • Migrate to Babel v7 (to fix security issue) (#67)

V1.3.0

  • Adds support for modern accessibilityRoles (#54)
  • Allow empty accessibilityState prop for View (#48)
  • Allow empty accessibilityState prop for Touchable* (#44)
  • accessibilityRole no longer required on Components with accessible={false} (#43)
  • Support for ESLint version ^6 (#57)
  • Adopted Prettier (#51)
  • Dev Dependency upgrades (#58)

V1.2.0

  • Updated accessibilityState to accessibilityStates

V1.1.0

  • Added support for accessibilityRole and accessibilityState
  • Added support for validating an array being passed by a prop.