useKeyWithClickEvents
Diagnostic Category: lint/a11y/useKeyWithClickEvents
Since: v1.0.0
Sources:
Description
Section titled DescriptionEnforce onClick is accompanied by at least one of the following: onKeyUp
, onKeyDown
, onKeyPress
.
Coding for the keyboard is important for users with physical disabilities who cannot use a mouse, AT compatibility, and screenreader users. This does not apply for interactive or hidden elements.
Examples
Section titled ExamplesInvalid
Section titled Invalid<div onClick={() => {}} />
code-block.jsx:1:1 lint/a11y/useKeyWithClickEvents ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Enforce to have the onClick mouse event with the onKeyUp, the onKeyDown, or the onKeyPress keyboard event.
> 1 │ <div onClick={() => {}} />
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Actions triggered using mouse events should have corresponding keyboard events to account for keyboard-only navigation.
Valid
Section titled Valid<div onClick={() => {}} onKeyDown={handleKeyDown} />
<div onClick={() => {}} onKeyUp={handleKeyUp} />
<div onClick={() => {}} onKeyPress={handleKeyPress} />
// this rule doesn't apply to user created component<MyComponent onClick={() => {}} />
<div onClick={() => {}} {...spread}></div>
<div {...spread} onClick={() => {}} ></div>
<button onClick={() => console.log("test")}>Submit</button>
Accessibility guidelines
Section titled Accessibility guidelinesHow to configure
Section titled How to configure{ "linter": { "rules": { "a11y": { "useKeyWithClickEvents": "error" } } }}