clickEventKeyEvents
Reports onClick without keyboard event handlers.
✅ This rule is included in the jsxlogicalandlogicalStrictpresets.
Elements with onClick handlers must also have keyboard event handlers for accessibility.
Users who are not using a mouse, such as those using keyboards or assistive technologies, often rely on those keyboard events to be able to use the page.
If non-interactive elements such as <div>s with an onClick event handler should have at least one of the following corresponding keyboard events registered as well:
onKeyDownonKeyPressonKeyUp
Interactive elements like <button> and <input> are already keyboard accessible and don’t require additional handlers.
This is required for WCAG 2.1.1 compliance.
Examples
Section titled “Examples”<JSX.IntrinsicElements.div: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div DOMAttributes<HTMLDivElement>.onClick?: MouseEventHandler<HTMLDivElement> | undefined
onClick={() => {}} /><JSX.IntrinsicElements.span: DetailedHTMLProps<HTMLAttributes<HTMLSpanElement>, HTMLSpanElement>
span DOMAttributes<HTMLSpanElement>.onClick?: MouseEventHandler<HTMLSpanElement> | undefined
onClick={const handler: any
handler} />;<JSX.IntrinsicElements.div: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div DOMAttributes<HTMLDivElement>.onClick?: MouseEventHandler<HTMLDivElement> | undefined
onClick={() => {}} DOMAttributes<HTMLDivElement>.onKeyDown?: KeyboardEventHandler<HTMLDivElement> | undefined
onKeyDown={const handler: any
handler} />;<JSX.IntrinsicElements.div: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div DOMAttributes<HTMLDivElement>.onClick?: MouseEventHandler<HTMLDivElement> | undefined
onClick={() => {}} DOMAttributes<HTMLDivElement>.onKeyUp?: KeyboardEventHandler<HTMLDivElement> | undefined
onKeyUp={const handler: any
handler} />;<JSX.IntrinsicElements.button: DetailedHTMLProps<ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>
button DOMAttributes<HTMLButtonElement>.onClick?: MouseEventHandler<HTMLButtonElement> | undefined
onClick={() => {}} /><JSX.IntrinsicElements.div: DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div DOMAttributes<HTMLDivElement>.onClick?: MouseEventHandler<HTMLDivElement> | undefined
onClick={() => {}} AriaAttributes["aria-hidden"]?: Booleanish | undefined
Indicates whether the element is exposed to an accessibility API.
aria-hidden="true" /><const CustomElement: any
CustomElement onClick: () => void
onClick={() => {}} aria-hidden: string
aria-hidden="true" />;When Not To Use It
Section titled “When Not To Use It”If you’re using a framework that automatically assigns keyboard events for corresponding mouse events, you can safely disable this rule.