• Stars
    star
    2,332
  • Rank 18,973 (Top 0.4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 9 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Identifies accessibility issues in your React.js elements

React A11y

build status

Identifies accessibility issues in your React.js elements

screenshot

⛔️DEPRECATION NOTICE ⛔️

This library is being deprecated in favor of @axe-core/react.
Deque Systems are one of the top authorities on web accessibility. Their auditing tools based on their axe-core engine are unrivaled and have become the gold standard for auditing web accessibility issues. Now that they have developed a runtime React DOM auditing tool, that does what react-a11y attempts to do but better, it seems counterproductive to continue to maintain react-a11y. Let's pool our resources and energies into the best product and I believe any accessibility tool backed by the experts at Deque Systems is bound to be one we can trust and depend on. Please go check out and install @axe-core/react today!

Installation

Run:

npm install react-a11y

Usage

In your main application file, require the module and call it, you'll start getting warnings in the console as your app renders. Note that by default all rules are turned off so you need to turn them on first (by setting them to "warn" or "error").

import React    from 'react';
import ReactDOM from 'react-dom';

if (process.env.NODE_ENV === 'development') {
  const a11y = require('react-a11y').default;
  a11y(React, ReactDOM, {
    rules: {
      'img-uses-alt': 'warn',
      'img-redundant-alt': [ 'warn', [ 'image', 'photo', 'foto', 'bild' ]]
    // ...
    }
  });
}

You probably don't want to call it if you're in production, since it patches the React rendering functions and this might make this slower.

Migrating to v1.1.0

A number of the rule names have changed and the previous rule names have been deprecated. These deprecated rules will be removed in v2.0.0. The following is the mapping of old to new rules:

Old Rule New Rule
avoid-positive-tabindex tabindex-no-positive
button-role-space click-events-have-key-events
label-uses-for label-has-for
mouse-events-map-to-key-events mouse-events-have-key-events
no-unsupported-elements-use-aria aria-unsupported-elements
onclick-uses-tabindex interactive-supports-focus
redundant-alt img-redundant-alt
use-onblur-not-onchange no-onchange
valid-aria-role aria-role

Options

These are the supported configuration options, annotated using flow type annotations

a11y(React : React, ReactDOM : ReactDOM, opts : object? );

React is the React object you want to shim to allow the accessibility tests.

ReactDOM is the ReactDOM object you're using to render the React components. This is only used on the client side, so you can safely omit it when using react-a11y in node.

options:

  • plugins : [string] An array of strings corresponding to names of plugins to be used. Eg. if the array contains 'aria-wai' it would include the rules in a (yet to be written) react-a11y-plugin-aria-wai module. You are responsible for installing this module.

  • rules : object The configuration options for each of the rules. This uses the same format as eslint does:

    const rules = {
      'img-uses-alt': 'off',
      'redundant-alt': [
        'warn',
        // other options to pass to the rule:
        [
          'foto'
        ]
      ]
    };

    Refer to the rule docs to see what options are defined for each rule.

  • reporter : object => undefined Use this to modify how the warnings are displayed. The reporter is a function that accepts an object with the following keys:

    • msg : string - the error message
    • tagName : string - the tagName of the violating element (eg. 'img')
    • severity : string - the severity as configured by the user in the corresponding rule configuration (one of 'off', 'warn', or 'error')
    • props : object - the props as passed to the element
    • displayName : string? - the displayName of the owner, if any
    • DOMNode : object? - the violating DOMNode as rendered to the browser DOM, this is only available on when react-a11y is running in the browser.
    • url : string? - The url to a webpage explaining why this rule is important

    The default reporter displays all the information it can, but listens to the deprecated options includeSrcNode, warningPrefix and throwErro and throwError.

  • filterFn : (string, string, string) => boolean You can filter failures by passing a function to the filterFn option. The filter function will receive three arguments: the name of the Component instance or ReactElement, the id of the violating element, and the failure message.

    Note: If it is a ReactElement, the name will be the node type (eg. div)

    // only show errors on CommentList
    const commentListFailures = function (name, id, msg) {
      return name === "CommentList";
    };
    
    a11y(React, ReactDOM, { filterFn: commentListFailures });

Cleaning Up In Tests

Use the restoreAll() method to clean up mutations made to React. Useful if you are using react-a11y in your test suite:

beforeEach(() => a11y(React));
afterEach(() => a11y.restoreAll());

Writing plugins

The rules in this version of react-a11y are pluggable! You can write your own plugin to add more rules. Have a look at writing plugins in the docs to get started!

Contributing

Interested in contributing? Great! Look here for more info: CONTRIBUTING.md.

More Repositories

1

react.dev

The React documentation website
TypeScript
10,714
star
2

react-transition-group

An easy way to perform animations when a React component enters or leaves the DOM
JavaScript
10,074
star
3

react-router-redux

Ruthlessly simple bindings to keep react-router and redux in sync
JavaScript
7,824
star
4

react-modal

Accessible modal dialog component for React
JavaScript
7,314
star
5

react-rails

Integrate React.js with Rails views and controllers, the asset pipeline, or webpacker.
JavaScript
6,725
star
6

react-router-tutorial

JavaScript
5,532
star
7

rfcs

RFCs for changes to React
5,377
star
8

react-basic

A description of the conceptual model of React without implementation burden.
4,163
star
9

server-components-demo

Demo app of React Server Components.
JavaScript
4,140
star
10

react-codemod

React codemod scripts
JavaScript
4,053
star
11

react-docgen

A CLI and library to extract information from React component files for documentation generation purposes.
TypeScript
3,557
star
12

react-tutorial

Code from the React tutorial.
JavaScript
3,294
star
13

react-tabs

An accessible and easy tab component for ReactJS.
JavaScript
3,048
star
14

react-chartjs

common react charting components using chart.js
JavaScript
2,930
star
15

react-future

Specs & docs for potential future and experimental React APIs and JavaScript syntax.
JavaScript
2,824
star
16

express-react-views

This is an Express view engine which renders React components on server. It renders static markup and *does not* support mounting those views on the client.
JavaScript
2,732
star
17

React.NET

.NET library for JSX compilation and server-side rendering of React components
C#
2,269
star
18

react-autocomplete

WAI-ARIA compliant React autocomplete (combobox) component
JavaScript
2,161
star
19

react-art

React Bridge to the ART Drawing Library
JavaScript
1,987
star
20

react-php-v8js

PHP library that renders React components on the server
PHP
1,325
star
21

react-magic

Automatically AJAXify plain HTML with the power of React. It's magic!
JavaScript
939
star
22

core-notes

Weekly meeting notes from the React core team
899
star
23

zh-hans.react.dev

React documentation website in Simplified Chinese
TypeScript
876
star
24

ru.react.dev

React documentation website in Russian / Официальная русская версия сайта React
TypeScript
673
star
25

ko.react.dev

React documentation website in Korean
TypeScript
660
star
26

pt-br.react.dev

🇧🇷 React documentation website in Portuguese (Brazil)
TypeScript
465
star
27

react-lifecycles-compat

Backwards compatibility polyfill for React class components
JavaScript
459
star
28

react-gradual-upgrade-demo

Demonstration of how to gradually upgrade an app to a new version of React
JavaScript
420
star
29

react-timer-mixin

TimerMixin provides timer functions for executing code in the future that are safely cleaned up when the component unmounts
JavaScript
309
star
30

id.react.dev

(Work in progress) React documentation website in Indonesian
TypeScript
305
star
31

es.react.dev

React documentation website in Spanish / Documentación del sitio web de React en Español
TypeScript
272
star
32

translations.react.dev

Nexus of resources and tools for translating the React docs.
JavaScript
254
star
33

ja.react.dev

React documentation website in Japanese
TypeScript
243
star
34

react-static-container

Renders static content efficiently by allowing React to short-circuit the reconciliation process.
JavaScript
222
star
35

fa.react.dev

(Work in progress) React documentation website in Persian
TypeScript
182
star
36

tr.react.dev

React documentation website in Turkish
TypeScript
161
star
37

uk.react.dev

🇺🇦 React documentation website in Ukrainian / Офіційна українська версія сайту React
TypeScript
130
star
38

ar.react.dev

React documentation website in Arabic 📘⚛️ — وثائق React باللغة العربية
TypeScript
126
star
39

hi.react.dev

(Work in progress) React documentation website in Hindi
TypeScript
110
star
40

zh-hant.react.dev

(Work in progress) React documentation website in Traditional Chinese
TypeScript
105
star
41

bn.react.dev

(Work in progress) React documentation website in Bengali
TypeScript
98
star
42

fr.react.dev

Version française du site de documentation officiel de React
TypeScript
91
star
43

vi.react.dev

(Work in progress) React documentation website in Vietnamese
TypeScript
84
star
44

react-bower

[DISCONTINUED] Bower package for React
JavaScript
69
star
45

legacy.reactjs.org

An archived copy of the legacy React documentation website
JavaScript
60
star
46

bn.reactjs.org

(Work in progress) React documentation website in Bengali
JavaScript
54
star
47

ta.reactjs.org

(Work in progress) React documentation website in Tamil
JavaScript
52
star
48

pl.react.dev

React documentation website in Polish
TypeScript
49
star
49

az.react.dev

🇦🇿 React documentation website in Azerbaijani
TypeScript
43
star
50

rackt-codemod

Codemod scripts for Rackt libraries
JavaScript
40
star
51

th.reactjs.org

(Work in progress) React documentation website in Thai
JavaScript
40
star
52

mn.react.dev

(Work in progress) React documentation website in Mongolian
TypeScript
37
star
53

uz.reactjs.org

(Work in progress) React documentation website in Uzbek
TypeScript
36
star
54

de.react.dev

(Work in progress) React documentation website in German
TypeScript
33
star
55

si.reactjs.org

(Work in progress) React documentation website in Sinhala
JavaScript
32
star
56

ml.react.dev

(Work in progress) React documentation website in Malayalam
TypeScript
31
star
57

it.react.dev

(Work in progress) React documentation website in Italian
TypeScript
30
star
58

ur.reactjs.org

(⚠️ Beta Docs Translation only) React documentation website in Urdu. Check details in https://github.com/reactjs/ur.reactjs.org/issues/1#issuecomment-949791355
TypeScript
29
star
59

he.react.dev

(Work in progress) React documentation website in Hebrew
TypeScript
28
star
60

ku.reactjs.org

(Work in progress) React documentation website in Kurdish
JavaScript
28
star
61

hu.react.dev

Hungarian 🇭🇺 React ⚛ documentation 📚 / React magyar dokumentációja
TypeScript
26
star
62

be.react.dev

(Work in progress) React documentation website in Belarusian
TypeScript
26
star
63

ml.reactjs.org

(Work in progress) React documentation website in Malayalam
JavaScript
25
star
64

ur.react.dev

(Work in progress) React documentation website in Urdu
TypeScript
25
star
65

el.reactjs.org

(Work in progress) React documentation website in Greek
JavaScript
25
star
66

gu.react.dev

(Work in progress) React documentation website in Gujarati
TypeScript
24
star
67

pt-PT.reactjs.org

(Work in progress) React documentation website in Portuguese (Portugal) 🇵🇹
JavaScript
23
star
68

ne.reactjs.org

(Work in progress) React documentation website in Nepali
JavaScript
23
star
69

gu.reactjs.org

(Work in progress) React documentation website in Gujarati
JavaScript
22
star
70

te.reactjs.org

(Work in progress) React documentation website in Telugu
JavaScript
22
star
71

hy.reactjs.org

(Work in progress) React documentation website in Armenian - https://hy.reactjs.org
JavaScript
21
star
72

km.reactjs.org

(Work in progress) React documentation website in Central Khmer
JavaScript
20
star
73

bg.reactjs.org

(Work in progress) React documentation website in Bulgarian
JavaScript
16
star
74

ro.reactjs.org

(Work in progress) React documentation website in Romanian
JavaScript
16
star
75

si.react.dev

(Work in progress) React documentation website in Sinhala
TypeScript
14
star
76

kn.reactjs.org

(Work in progress) React documentation website in Kannada
JavaScript
13
star
77

tl.reactjs.org

(Work in progress) React documentation website in Tagalog
TypeScript
13
star
78

ka.reactjs.org

(Work in progress) React documentation website in Georgian
JavaScript
11
star
79

sv.reactjs.org

(Work in progress) React documentation website in Swedish
JavaScript
9
star
80

nl.reactjs.org

(Work in progress) React documentation website in Dutch
JavaScript
8
star
81

ca.reactjs.org

(Work in progress) React documentation website in Catalan
JavaScript
7
star
82

te.react.dev

(Work in progress) React documentation website in Telugu
TypeScript
7
star
83

sw.react.dev

(Work in progress) React documentation website in Swahili
TypeScript
7
star
84

lt.reactjs.org

(Work in progress) React documentation website in Lithuanian
JavaScript
6
star
85

ta.react.dev

(Work in progress) React documentation website in Tamil
TypeScript
6
star
86

ht.reactjs.org

(Work in progress) React documentation website in Haitian Creole
JavaScript
5
star
87

sr.reactjs.org

(Work in progress) React documentation website in Serbian
TypeScript
5
star
88

is.react.dev

(Work in progress) React documentation website in Icelandic
TypeScript
4
star
89

cs.react.dev

(Work in progress) React documentation website in Czech
TypeScript
4
star
90

kk.react.dev

🇰🇿 React documentation website in Kazakh / React сайтының ресми қазақша нұсқасы
TypeScript
3
star
91

sr.react.dev

(Work in progress) React documentation website in Serbian
TypeScript
3
star
92

sq.reactjs.org

(Work in progress) React documentation website in Albanian
TypeScript
3
star
93

reactjs.github.io

HTML
2
star
94

lo.react.dev

(Work in progress) React documentation website in Lao
TypeScript
2
star
95

fi.react.dev

(Work in progress) React documentation website in Finnish
TypeScript
2
star
96

my.reactjs.org

(Work in progress) React documentation website in Burmese
JavaScript
2
star
97

sw.reactjs.org

(Work in progress) React documentation website in Swahili
JavaScript
2
star
98

mk.react.dev

(Work in progress) React documentation website in Macedonian
TypeScript
1
star
99

tg.reactjs.org

(Work in progress) React documentation website in Tajik
TypeScript
1
star